计算机问题求解-算法在计算机科学中的地位
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
For your reference: L’Hôpital’s rule f ( n) f ' ( n) lim lim n g ( n) n g ' ( n) with some constraints
对数函数与幂函数
Which grows faster?
log2 n or
n?
So, log2nO(n)
关键是“增长率”
Algorithm Time function(ms) Input size(n) 10 100 1,000 10,000 100,000 0.00033 sec. 0.0033 sec. 0.033 sec. 0.33 sec. 3.3 sec. 0.0015 sec. 0.03 sec. 0.45 sec. 6.1 sec. 1.3 min. 1 33n 2 46n lg n 3 13n2 Solution time 0.0013 sec. 0.13 sec. 13 sec. 22 min. 1.5 days 0.0034 sec. 3.4 sec. 0.94 hr. 39 days 108 yr. 0.001 sec. 41016 yr. 4 3.4n3 2n
f ( n) A function fΟ(g) if lim c n g ( n )
– Note: c may be zero. In that case, f(g), “little Oh”
一个例子 Using L’Hopital’s Rule
Let f(n)=n2, g(n)=nlgn, then:
2 n n n 1 – fΟ(g), since lim lim lim lim n n lg n n lg n n ln n n 1 ln 2 n ln 2
– gΟ(f), since lim n
n log n log n ln n 1 lim lim lim 0 2 n n n ln 2 n n ln 2 n n
TSP: (也许是)世界上最 具挑战性的(算法) 问题。
Rules that give a number of trials below the number of permutations of the given points are not known!
这年头,我们当然使用计算机…
假设我们使用…
Байду номын сангаас
For any integer k1, if m>n1 and n<Fk+1, then the call Euclid(m,n) makes fewer than k recursive calls. (to be proved)
– Since Fk is approximately k / 5 , the number of recursive calls in Euclid is O(lgn).
考你一下: Let A be an array of integers and S a target integer. Design an efficient algorithm for determining if there exist a pair of indices i,j such that A[i]+A[j]=S. 如果这里“efficient”是指“线性 的”,你的答案满足要求吗?
A
a1 a2
……
ai
……
aj
Hash(ai) i
If ai = S-aj
Hash(S-aj)
Sleeping Tigers
Considered currently
课外作业
DH: pp.153– 6.1 – 6.3 – 6.10 , 6.11 – 6.15, 6.16
log2 e log2 n n n lim lim (2 log2 e) lim 0 n n n n 1 n 2 n
一般性结论
The log function grows more slowly than any positive power of n lgn o(n) for any >0
假设要在n个元素的序列中搜索K;
– 假设K确实在该序列中的概率是q,则不在其中 的概率是1-q。 – 如果在其中,假设K出现在每一个位置上的概 率是一样的。 – 对于特定的位置i (0in-1), 比较次数为i+1。
因此,平均代价是:
q n 1 (i 1) n(1 q) n i 0
– – – – IBM Roadrunner Cluster (美国能源部) 129,600个处理器,每秒执行1457万亿次算数运算 价值1亿3千3百万美元 2009年高性能计算机500强第一名 耐心等待…请勿关机…
We would then need roughly 28 trillion years to solve the 33-city TSP on the Roadrunner, an uncomfortable amount of time, given that the universe is estimated to be only 14 billion years old.
计算机问题求解 – 论题1-14
-
算法的效率
2018年12月18日
需要移动多少次圆盘?
相应的递归方程是: T(n) = 2T(n-1)+1 即:T(n) = 2n-1 当n=64 (据说原始问题是这样的):
– 其数量级大约是 1019,即1000亿亿! – 如果每秒移动1亿个盘子,需要大约3200年!
g
Θ (g): 这里的函数与 g 具有“相 同”的增长率。
Ο (g): 该集合中任一函数的增长 率不高于g的增长率(最多与g增 长得一样快!)
集合 “Big Oh”
Definition
– Giving g:N→R+, then Ο(g) is the set of f:N→R+, such that for some cR+ and some n0N, f(n)cg(n) for all nn0.
By the way: The power of n grows more slowly than any exponential function with base greater than 1
nk o(cn) for any c>1
顺便问一下:书上讲的优化方法如何实现?
现在说说“平均”
解决33city-TSP…
问题 实例
TSP – 挑战计算思维能力的极限
一个例子 – 找序列中的最大元素
如果我们关心A的值: 显然:与实际输入有关; 最小值:0; 最大值:n-1; 平均值:???
“最坏情况”也不一定容易看出来
Euclid(int m,n) measured by the number of recursive calls if n=0 then return m else return Euclid(n, m mod n)
Time allowed
1 second 1 minute 30,000 1,800,000
Maximum solvable input size (approx.)
2,000 82,000 280 2,200 67 260 20 26
函数增长率的比较
Ω (g): 该集合中任一函数的增 长率不低于g的增长率(至少与 g增长得一样快!)