Algorithm Design and Analysis
University Computer Science Topic 10 5:37 English narration · English and 中文 subtitles you can switch
Chapters
Transcript
This course teaches you to price an algorithm's design before you trust it with real data.
本课程教你在把真实数据交给算法处理之前,先评估其设计的计算代价。
The first two units cover analysis tools and divide and conquer.
前两个单元介绍分析工具和分治法。
The next two are the choice patterns: greedy algorithms and dynamic programming.
接下来两个单元介绍两种选择模式:贪心算法和动态规划。
The last two reach the frontier: graph optimisation, then reductions and NP-completeness.
最后两个单元探索前沿内容:先是图优化,然后是归约和 NP 完全性。
Every claim is checked on concrete inputs.
每个论断都用具体输入加以检验。
Draw the recursion tree, sum each level, and count the depth.
画出递归树,计算每一层的代价,再数出树的深度。
For T of n equals 2 T of n over 2 plus n, every level costs n and there are log n levels, so the total is n log n.
对于 T(n) = 2 T(n/2) + n,每一层的代价都是 n,共有 log n 层,因此总代价是 n log n。
The master method compares the root's work with the leaves: 4 T of n over 2 plus n is n squared.
主定理比较根节点的工作量和叶节点的工作量:4 T(n/2) + n 的结果是 n 的平方。
Merge sort on 64 elements used 310 comparisons.
对 64 个元素进行归并排序用了 310 次比较。
Read this recursion tree level by level.
逐层看这棵递归树。
The root does n work.
根节点的工作量为 n。
The next level has two halves, each doing n over 2, still n in total.
下一层有两个规模减半的子问题,各自的工作量为 n/2,合计仍为 n。
Each level halves the size and doubles the count, so every level costs the same n.
每下一层,规模减半,数量加倍,因此每一层的代价都是相同的 n。
There are log n levels, so multiply: n log n.
共有 log n 层,所以相乘得到 n log n。
Level cost times depth is the analysis.
用每层代价乘以深度,就是这个分析方法。
Divide and conquer splits, solves and combines, and the recurrence prices the combine step.
分治法包括分解、求解和合并,而递推式用于计算合并步骤的代价。
Merge sort is n log n; binary search is log n.
归并排序的代价是 n log n;二分查找是 log n。
Quicksort is n log n with balanced pivots, but on sorted input with first-element pivots it becomes n squared: 2080 comparisons instead of about 300.
快速排序在枢轴划分均衡时是 n log n,但对于已排序的输入,如果总选第一个元素作为枢轴,代价就变成 n 的平方:需要 2080 次比较,而不是大约 300 次。
The maximum subarray crosses the middle too, so it costs n log n.
最大子数组也可能跨越中点,因此其代价是 n log n。
The same skeleton gives three different bills, depending on the combine step.
同样的基本结构,根据合并步骤的不同,会产生三种不同的代价。
Combining in constant time, as binary search does, gives log n.
像二分查找那样用常数时间合并,得到 log n。
Combining in linear time, as merge sort does, gives n log n.
像归并排序那样用线性时间合并,得到 n log n。
Unbalanced splits, as quicksort makes on sorted input, give n squared.
像快速排序处理已排序输入时那样划分不均衡,得到 n 的平方。
Charge the combine step honestly and the bill follows.
如实计算合并步骤的代价,就能得到结果。
A greedy algorithm takes the best choice now and never looks back.
贪心算法选择当前最好的方案,然后不再回头。
Picking the activity that finishes earliest selects four of the eleven course activities, and that is provably optimal.
选择最早结束的活动,可以从课程中的 11 个活动里选出 4 个,而且可以证明这是最优解。
The exchange argument shows any optimal schedule can start with the greedy choice.
交换论证表明,任何最优日程都可以从贪心选择开始。
With coins of 1, 3 and 4, greedy pays three coins for 6, while two coins of 3 suffice.
对于面值为 1、3 和 4 的硬币,贪心法凑出 6 要用三枚硬币,而两枚面值为 3 的硬币就足够了。
The criterion is the design.
选择标准决定了算法设计。
Earliest finish first leaves the most room for later activities.
优先选择最早结束的活动,能为后续活动留下最多空间。
The exchange argument then shows that an optimal solution can always be changed, step by step, into the greedy one without losing anything.
然后,交换论证表明,总可以逐步将一个最优解变成贪心解,而不会损失任何收益。
Where no such argument exists, as with those coins, greedy is only a heuristic.
在无法进行这种论证的情况下,比如刚才的硬币问题,贪心法只是一种启发式方法。
Dynamic programming remembers answers instead of recomputing them.
动态规划记住已有结果,避免重复计算。
Naive Fibonacci of 30 makes over 2.6 million calls; with memoisation, it solves about 30 subproblems.
朴素方法计算第 30 个斐波那契数会产生超过 260 万次调用;使用记忆化后,只需求解大约 30 个子问题。
For coins of 1, 3 and 4, the table gives 2 coins for 6, beating greedy.
对于面值为 1、3 和 4 的硬币,表格给出的结果是凑出 6 需要 2 枚硬币,优于贪心法。
For a knapsack of capacity 7, the best value is 9, using the items of weight 3 and 4.
对于容量为 7 的背包,选取重量为 3 和 4 的物品,最大价值为 9。
Each cell in this table reads cells to its left or below it, which have already been filled.
这张表中的每个单元格都读取其左侧或下方已经填好的单元格。
That dependency order is the fill order: work in the direction the arrows point, and every value you need is ready.
依赖关系的顺序就是填表顺序:沿箭头方向计算,所需的每个值就都已准备好。
The recurrence, the base cases and the fill order together are the whole design, and the finished table is the explanation.
递推关系、基本情况和填表顺序共同构成完整的设计,而填好的表就是解释。
Kruskal's algorithm sorts the edges and accepts each one unless it makes a cycle.
Kruskal 算法对边排序,依次选取每条边,除非它会形成环。
On the course graph it accepts weights 1, 2, 2, 3 and 5, for a minimum spanning tree of weight 13.
在课程给出的图中,它选取的边权为 1、2、2、3 和 5,得到权重为 13 的最小生成树。
The cut property proves this greedy choice is safe.
割性质证明了这种贪心选择是安全的。
But negative edges break Dijkstra's guarantee, so use Bellman-Ford instead.
但负权边会破坏 Dijkstra 算法的正确性保证,因此应改用 Bellman-Ford 算法。
A cut separates the visited part of the graph from the rest.
一个割将图中已访问的部分与其余部分分开。
The lightest edge crossing any cut is always safe to add to a minimum spanning tree.
跨越任意割的最轻边总可以安全地加入最小生成树。
One argument proves three algorithms: Kruskal, Prim and, with care, Dijkstra.
同一个论证可以证明三种算法:Kruskal、Prim,以及在适当条件下的 Dijkstra。
Swap any heavier crossing edge for the light one, and the tree's weight can only stay the same or fall.
用这条轻边替换任意一条更重的跨割边,树的权重只会保持不变或下降。
A reduction from A to B turns a B-solver into an A-solver, so B is at least as hard as A.
从 A 到 B 的归约可以把 B 的求解器变成 A 的求解器,因此 B 至少和 A 一样难。
SAT reduces to 3-SAT, then to CLIQUE, then to VERTEX COVER, by complementing the graph.
SAT 归约到 3-SAT,再归约到 CLIQUE,然后通过取图的补图归约到 VERTEX COVER。
To prove your problem hard, reduce a known hard problem to yours, never the other way round.
要证明你的问题是困难的,应当把一个已知的困难问题归约到你的问题,而绝不能反过来。
NP-complete problems are the hardest of the verifiable.
NP 完全问题是可验证问题中最难的一类。
Follow the chain: SAT to 3-SAT to CLIQUE to VERTEX COVER.
沿着这条链来看:SAT 到 3-SAT,到 CLIQUE,再到 VERTEX COVER。
Each arrow is a translation that runs in polynomial time.
每个箭头都是一个多项式时间内完成的转换。
So if anyone found a fast solver for the last problem, every problem in the chain would become fast.
因此,如果有人找到最后一个问题的快速求解方法,链上的所有问题都能快速求解。
The arrow carries the difficulty, which is why its direction matters so much.
箭头传递了问题的难度,所以它的方向非常重要。
Now test yourself from memory.
现在凭记忆测试一下自己。
In the recurrence 2 T of n over 2 plus n, what does one level cost?
在递推式 2 T(n/2) + n 中,一层的代价是多少?
What does 4 T of n over 2 plus n solve to?
4 T(n/2) + n 的解是什么?
How many calls does naive Fibonacci of 30 make?
朴素方法计算第 30 个斐波那契数会产生多少次调用?
To prove a problem B is NP-hard, which way must the reduction go?
要证明问题 B 是 NP 困难的,归约必须朝哪个方向进行?
Pause, answer, then check.
暂停视频,回答问题,然后核对。
Comments
No comments yet. Be the first to ask or share something.