计算思维导论-2014-10-07

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

21/2 15/2
144/15 D
a/b: a是千次模拟访问数, b是百万次模拟访问的百分比值.
我们用复杂(严格意义上说,不知道复杂不复杂)的人的思维,找出简单, 但却能让计算机将它的特性发挥到“极至”的方法,使得计 算机能帮我们找出问题的解。这种“人的思维”也许就是我 们希望明确的“计算思维”。而计算机的特性就是…
只能做很“弱智”的动作,但做得很快; 只能“死记硬背”,但能记得很多; “愚忠”,只要人能等待(通常不能),要它算多久它也不会“造反”。
THINKING RECURSIVELY: PROBLEM 1
Towers of Hanoi How many moves are need to move all the disks to the third peg by moving only one at a time and never placing a disk on top of a smaller one.
•Each node has a key, belonging to a linear ordered set •An inorder traversal produces a sorted list of the keys
40 50
排序问题以及基于比较的解法
问题:按照递增顺序排序
J(1) =1 J(2n) = 2J(n)-1 J(2n+1) = 2J(n)+1
1
3
1
2n+1 1
10
23
11
25
2n-1
9
35 10
37
8
4
7
5
6
9
4
8
5
76
SOLVING THE RECURSION
By trying the smaller n, we can make a guess:
J(2m+l) =2l+1
If we write n in binary form, the result is very interesting:
数学归纳法 与 递归方法
数学归纳法历史悠久 数学归纳法与用递归方式解题本质上并没有差别 在没有计算机之前, 递归地考虑问题也可以为解题提
供一定的思路
计算机如何执行递归程序?
加入“位置”信息Βιβλιοθήκη Baidu这个想法不简单
The cat sat on the mat
The dog stood on the mat
The cat stood while a dog sat
a cat dog mat on sat stood the while
3-5 1-2, 3-2 2-2, 3-6 1-6, 2-6 1-4, 2-4 1-3, 3-7 2-3, 3-3 1-1, 1-5, 2-1, 2-5, 3-1 3-4
T(1) = 1 T(n) = 2T(n-1) +1
Hanoi(n, A, C) Hanoi(n-1, A, B); Move(A, C); Hanoi(n-1, B, C);
end
THINKING RECURSIVELY: PROBLEM 2
Josephus’s problem
Find the position to live, or die!
从MATCHING到RELEVANT
By far the most common cause of malaria is being bitten by an infected mosquito, but there are also other ways to contract the disease.
假设只有3个网页
想想:如果要找”cat sat”会怎样?
进一步,不仅可以发现“紧挨着”,还可以发现“隔得不远”。
有“结构”的网页
<titleStart> my cat <titleEnd> <bodyStart> The cat sat on the mat <bodyEnd>
<titleStart> my dog <titleEnd> <bodyStart> The dog stood on the mat <bodyEnd>
查询dog IN TITLE:
Dog
2-3, 2-7, 3-11
<titleStart> 1-1, 2-1, 3-1
<titleEnd> 1-4, 2-4, 3-4
AltaVista 拥有的专利“Constraint Searching of an Index”帮助它利用 metaword indexing 方法实现了高效率的 全web搜索,取得了竞争优势,但终于输 给了拥有PageRank算法的Google.
sat
1-8, 3-12
stood 2-8, 3-8
the
1-6, 1-10, 2-6, 2-10, 3-6
while
3-9
<bodyEnd> 1-12, 2-12, 3-13
<bodyStart> 1-5, 2-5, 3-5
<titleEnd> 1-4, 2-4, 3-4
<titleStart> 1-1, 2-1, 3-1
这一招实在不怎么样。
招数2:ranking by authority of hyperlink
想法不赖,但“authority”怎么确定?
二”招”合一”招”
关于疟疾病产生原因的
网页1
……
2
关于疟疾病产生原因的
网页2
……
100
通过hyperlink连接到”关
于疟疾并产生原因的网
页1” ……
100 1,000 10,000 100,000 Time allowed 1 second 1 minute
1
2
3
4
33n
46n lg n
13n2
3.4n3
Solution time
0.00033 sec. 0.0015 0.0013 sec. 0.0034 sec. sec.
0.0033 sec. 0.03 sec. 0.13 sec.
搜索引擎完成的最重要的事是:matching 和 ranking。
搜索的第一步:匹配
如果你查询“南京到上海的火车时刻表”,搜索引 擎首先必须回答:哪些网页与你的查询相匹配。
伟大的思想往往很简单:索引
不仅简单,而且:“原始”,据说巴比伦人5000年前就 使用索引了。
网络搜索引擎采用索引算法开始于1994-95年,20世 纪90年代中期的“搜索之王”AltaVista(不是Google!) 实现了全部网页的索引,在此基础上实现了高效率 的匹配。
Our cause was not helped by the troops, many of whom were suffering from malaria and other tropical diseases.
如果查询的关键词是“malaria”和“cause”, 显然,右边的“网页”是“不相关”的。 问题是:计算机如何根据“相关度”排序?
在公司成立仅仅 3 个月后, Google被PC Magazine评为当年 (1998年) Top 100 网站, 理由是它的“uncanny knack for returning extremely relevant results”。
“复杂而又难以解释的‘招数’”。
其实也不“稀奇”
招数1:ranking by hyperlink number(hyperlink本身算得上 是个“伟大”的想法,其实早在互联网没出现之前就有 了。)
这个问题难吗?
Well, it depends. 方法其实很容易:枚举+累加 但是…
这其实是“背包问题”的一个特例,那问题可是计算机科 学中一个赫赫有名的“难”问题!
HOW FUNCTIONS GROW
Algorithm Time function(ms)
Input size(n) 10
2
通过hyperlink连接到”关
于疟疾并产生原因的网
页1” ……
100
1
1
1
1
… (100个)
1
有”大麻烦” – 回路
A
B
C
D
E
“随机冲浪者” – 真的很聪明
B
109/10
16/2
74/7
55/4
55/4 32/4
A 118/1 3
126/1 4
58/5 46/4
C 13/2
17/2
101/1 0
3.4 sec.
0.033 sec. 0.45 sec. 13 sec.
0.94 hr.
0.33 sec. 6.1 sec. 22 min.
39 days
3.3 sec. 1.3 min. 1.5 days
108 yr.
Maximum solvable input size (approx.)
30,000
计算思维导论 – 第3讲 – 算法方法
2014年10月07日
算法
计算机科学思想在很多情况下是以“算法”的形式呈现的。
什么是“伟大”的算法
普通的计算机用户每天都在使用这些算法; 它们能解决客观世界中的具体问题; 它们具有科学原理的支撑。
例如: 与搜索相关的算法; 与密码相关的算法; 与数据压缩相关的算法; 与数据一致性相关的算法 …… (还有一些注定”伟大”的算法还在我们的期待之中: 例如: 精确的自 然语言翻译、自动驾驶汽车、帮我改学生作业)
2,000
280
67
1,800,000 82,000
2,200
260
2n 0.001 sec. 41016 yr.
20 26
SORTING A ARRAY OF 1 MILLION NUMBERS
Using merge sort, taking time 50nlogn:
100 seconds!
但是,保持平衡也是有代价的
BINARY SEARCH TREE
Good balancing 40 (logn)
20
60
30 50
80
Poor balancing
30
(n)
20
80
60
In a properly drawn tree, pushing forward to get the ordered list.
Computer B 10 Mips
Computer A 1000 Mips
Using insertion sort, taking time 2n2:
2000 seconds!
关于搜索
最简单的方式:顺序查找
最坏情况下必须比较n次。
二分搜索树
如果能够保持树的“平衡”,最坏情况的搜索代价也只 不过是lgn
简单索引 – 这个真简单
The cat sat on the mat
The dog stood on the mat
The cat stood while a dog sat
假设只有3个网页
a cat dog mat on sat stood the while
3 1, 3 2, 3 1, 2 1, 2 1, 3 2, 3 1, 2, 3 3
也是你并没有意识到, 但 这个“问题”相当于: 在世界上最大的一堆干 草中寻找绣花针!
GOOGLE 的背后有什么?
我们每个人的生活都在很大程度上受到了“搜索引擎”的 影响。
这么大量的信息,这么快的速度。因为“习以为常”,我 们甚至忘记了这其实“不简单”!
Google在全世界拥有数量庞大的服务器,能提供的处理能 力极为惊人。但即便如此,这些“硬件”并不能提供我们 习以为常的搜索服务。
<titleStart> my pets <titleEnd> <bodyStart> The cat stood while a dog sat <bodyEnd>
a
3-10
cat
1-3, 1-7, 3-7
dog
2-3, 2-7, 3-11
mat
1-11, 2-11
on
1-9, 2-9
pets
3-3
递归只是人的思考方式, 不是计算机实际解题的路径 计算机必须先求出“base case”的结果,在逐步用每
一个较小的结果去算更大的结果,直至你要的结果
这个过程可能很冗长,但计算机不在乎,而人在乎!
考考你: 用FILLER填充指定空间
空缺
一个与美国总统选举有关的例子
问题:美国总统选举是否可能“平局”?
×1
1×0
×2
×9
×3
×8 ×7 ×6

5
1 11 10
9 8 76
For small n: 1, 1, 3, 1, 3, 5, 7, …
2 3
4 5
THINKING RECURSIVELY: PROBLEM 2
Josephus’s problem
Find the position to live, or die!
相关文档
最新文档