算法导论-第一章-导论

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
(c5 / 4 c6 / 4 c7 / 4)n2 (c1 c2 c4 3c5 / 4 3c 6 /4 3c7 / 4 c8)n (c2 c4 c5 c6 / 2 c7 / 2 c8)
The running time is a quadratic function of n二次
Usually describe the running time of a program as a function of the input size通常 将运行时间表示为输入的函数
Main fact impacting on running time (影响运行时间的主要因素)
The running time of an algorithm may depending on how the algorithm is implemented as well as what kind of data structure is used有 时候运行时间和算法采用的数据结构有关系
函数
n
n
t j ( j 1) / 2 (n2 3n 4) / 4
j2
j2
n
n
(t j 1) ( j / 2 1) (n2 3n 2) / 4
j2
j2
Order of growth增长率
在算法分析过程中,通过抽象来简化分析过程,忽 略每个语句的实际开销,代之以抽象的尝试Ci
The average-case:与输入的概率分布有关,假设
对所有的数据出现的概率相等,tj=(j+1)/2
T (n) c1n c2 (n 1) c4 (n 1) c5 (n2 3n 2) / 4 c6 (n2 3n 2) / 4 c7 (n2 3n 2) / 4 c8 (n 1)
But most often we want to measure the computational time of an algorithm计算时间
Before we can analyze an algorithm, we must have a model of the implementation technology that will be used, including a model for the resources of that technology and their costs计算模型
Input: sequence <a1,a2,…,an> of n natural numbers
Output: permutation <a’1,a’2,…,a’n> such that a’1≤a’2≤…≤a’n (重新排列)
• Example --Input: <8, 2, 4, 9, 3, 6> --Output: <2, 3, 4, 6, 8, 9>
Analysis of algorithm 算法分析---估 计算法所需要的资源
We shall assume a generic one-processor, random-access machine (RAM) model of computation随机存储器作为计算模型
指令一条接一条执行,没有并发操作 指令作为一个原子操作被执行,指令包括算术操 作、逻辑操作、数据移动和控制操作 指令需要一个定量的执行时间 RAM容量足够大 ……
(c1 c2 c4 c5 c8 )n (c2 c4 c5 c8 )
--The running time is a linear function of n线性函数
Analysis of insertion sort
The worst-case results if the array is in reverse sorted order- that is, in decreasing order最坏情况是
The running time is a quadratic function of n二次
函数
n
n
t j j n(n 1) / 2 1
j2
j2
n
n
(t j 1) ( j 1) n(n 1) / 2
j2
j2
Analysis of insertion sort
Average-case: (sometimes) --T(n) = expected time of algorithm over all inputs of size n. --Need assumption of statistics distribution of inputs.
平均情况下的运行时间,指的是size给定,不同的实例分布 的平均所需的运行时间
Main fact impacting on running time (影响运行时间的主要因素)
Numbers of inputs输入规模
The distribution of input, some (not all) algorithms can take different amounts of time to sort two different input sequence of the same size输入构成
元素个数n即为输入规模(Input size)
对另外一些问题,例如两个整数相乘,其输入规 模是输入在二进制表示下的位数
有时候用二个数来表示输入,例如输入是一个图
因此需要确定每个问题的输入规模
Running time(运行时间)
运行时间是指执行的基本操作数(步数), 基本操作独立于具体机器
数组是逆序tj=j
T (n) c1n c2(n 1) c4(n 1) c5(n(n 1) / 2 1) c6(n(n 1) / 2)
c7 (n(n 1) / 2) c8(n 1)
(c5 / 2 c6 / 2 c7 / 2)n2
(c1 c2 c4 c5 / 2 c6/ 2 c7 / 2 c8 )n (c2 c4 c5 c8 )
假设每行代码花费的时间是常量Ci ,但每一 行代码花费的时间可能不同
Running time(运行时间)
Worst-case: (usually) --T(n) = maximum time of algorithm on any input of size n.
最坏情况下的运行时间,指的是size给定,任何输入实例的 最长运行时间
Under RAM model: count fundamental operations计算基本操作数目
RAM model (RAM 模型)
为了在RAM模型上分析算法,我需要:
Combinatorics组合
Probability theory概率
Algebra代数
The ability to identify the most significant terms in a formula找出最重要项的能力
Analysis of insertion sort
To compute T(n), the running time of Insertion-sort, we
sum the products of the cost and times columns, obtaining
n
nBiblioteka Baidu
T (n) c1n c2 (n 1) c4 (n 1) c5 t j c6 (t j 1)
Ignore not only the actual statement costs, but also the
Analysis of insertion sort
cost c1 c2 0 c4 c5
c6
times
n
n-1
n-1 n-1
t n j2 j
n j2
(t
j
1)
c7
n j2
(t
j
1)
c8
n-1
tj : the number of times the while loop test in line 5 is executed for the j value第5行中while循环所做的测试次数
Generally, we seek upper bounds on the running time, because everybody likes a guarantee
Input size(输入规模)
输入规模依赖所研究的问题
对许多计算问题,其输入规模就是输入项的个数, 例如排序和计算傅里叶变换(DFT),输入数组的
For some algorithms, worst-case occur fairly often最 坏情况经常发生
比如在数据库中寻找一条信息,若该信息不在数据 库中,则搜索算法的最坏情况发生!
The worst case running time is an upper bound on the running time for any input, it is usually fairly easy to analyze and often close to the average or real
j2
j2
n
c7 (t j 1) c8 (n 1) j2
The best-case if the array is already sorted最好情况是数组
已经有序tj=1
T (n) c1n c2 (n 1) c4 (n 1) c5(n 1) c8(n 1)
Ch1 导论
Alogrithm算法
A well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output. (良定义的计算过程,良定义指的是 满足五个要素:有穷性、确定性、可行性、有输入和输出,计 算过程指的是一系列的计算步骤)
Best-case: (bogus) --Cheat with a slow algorithm that works fast on some
input. 最好情况下的运行时间,指的是size给定,输入实例的最短 运行时间
Running time(运行时间)
The average running time is usually very hard to compute, we usually strive to analyze the worst case running time. The average case is often roughly as bad as the worst case平均情况难以分析,经常分析 最坏情况
Example of insertion sort
Example of insertion sort
Insertion sort
Analysis of algorithm 算法分析---估 计算法所需要的资源
Analysis an algorithm: predicting the resources, including memory, communication bandwidth, or computer hardware and so on, that the algorithm requires
Input
Algorithm
Output
Note :实例-------计算一个解(输出)所有的输入
Application of algorithm
人类基因组计划
互联网
路由问题:寻找数据由源节点到达目的节点的路径 搜索问题
制造业
电子商务
The problem of sorting
相关文档
最新文档