Data Structures and Algorithms
University Computer Science Topic 5 7:45 English narration · English and 中文 subtitles you can switch
Chapters
Transcript
This course is about choosing a container by what it costs.
这门课讲的是如何根据开销选择容器。
The first unit is the yardstick: how cost grows with the size of the input.
第一个单元介绍衡量标准:开销如何随输入规模增长。
The next four are the containers: lists, stacks and queues, hash tables, trees and heaps, and graphs.
接下来的四个单元介绍容器:列表、栈和队列、哈希表、树和堆,以及图。
The last two are recipes: sorting and searching.
最后两个单元介绍算法:排序和搜索。
Every cost claim here was counted on the datasets printed in the handout.
这里所有关于开销的结论,都是在讲义中列出的数据集上统计得出的。
Big-O gives an upper bound on growth, and we drop constants and smaller terms.
大 O 表示法给出增长的上界,我们会忽略常数和低阶项。
At n equal to 1000, n log n is about ten thousand while n squared is a million, a hundred times more.
当 n 等于 1000 时,n log n 约为一万,而 n 的平方是一百万,是前者的一百倍。
To measure a program's class, double n: if the count doubles it is linear; if it quadruples, quadratic.
要衡量程序的增长类别,就把 n 加倍:如果计数加倍,就是线性增长;如果变为四倍,就是二次增长。
Nested loops at 100 and 200 show exactly four.
嵌套循环在 100 和 200 时的计数之比恰好是四。
Here the common growth classes share one axis.
这里把常见的增长类别画在同一坐标系中。
Logarithmic growth stays almost flat, linear rises steadily, n log n a little faster, and quadratic bends sharply upward.
对数增长几乎保持平坦,线性增长稳定上升,n log n 上升得稍快,而二次增长则急剧向上弯曲。
Match each curve to its name, then turn the name into a budget: for a million items, only the flatter curves finish in reasonable time.
把每条曲线与名称对应起来,再根据名称估算所需资源:对于一百万个数据项,只有较平缓的曲线能在合理时间内完成。
A Python list is a contiguous array, so indexing is one step, but inserting at the front slides everything along.
Python 列表是连续数组,因此按索引访问只需一步,但在开头插入元素会使所有元素向后移动。
A linked list splices cheaply but must walk to reach an index.
链表插入的开销很小,但要访问某个索引,必须沿链表遍历。
Doubling the array when full makes append cheap on average.
数组满时将容量加倍,可以使追加操作的平均开销较小。
A stack works at one end, last in first out; a queue uses both ends, first in first out.
栈在一端操作,后进先出;队列使用两端,先进先出。
On the left is one contiguous block, where each address is computed from the index.
左边是一块连续的存储空间,每个地址都由索引计算得出。
On the right are separate nodes chained by pointers.
右边是由指针串联起来的独立节点。
A stack and a queue differ only in which ends they use: the stack pushes and pops at the same end; the queue adds at the back and removes from the front.
栈和队列的区别只在于使用哪几端:栈在同一端压入和弹出;队列在队尾加入,在队头移除。
The ends decide the cost.
使用哪一端决定了开销。
In a stack, both push and pop act at the top, so the last item in is the first out: C comes off before B and A.
在栈中,压入和弹出都在栈顶进行,所以最后进入的元素最先出来:C 在 B 和 A 之前弹出。
In a queue, items join at the rear and leave from the front, so the first item in, A, is the first out.
在队列中,元素从队尾进入,从队头离开,所以最先进入的 A 最先出来。
A hash function spreads keys over buckets, and collisions are normal.
哈希函数把键分散到各个桶中,发生冲突是正常的。
With k mod 13, the keys 4, 17, 30 and 43 all land in bucket 4.
使用 k mod 13 时,键 4、17、30 和 43 都落入桶 4。
The load factor, keys over buckets, here 7 over 13 or about 0.54, predicts the chain length.
负载因子,也就是键的数量除以桶的数量,这里是 7 除以 13,约为 0.54,可以用来预测链的长度。
Real tables resize to keep it under about 0.75, which is why dictionaries stay fast.
实际的哈希表会调整大小,使负载因子保持在约 0.75 以下,这就是字典能保持快速的原因。
Count the chains in these thirteen buckets.
数一数这十三个桶中的链。
Bucket 4 holds four keys, and bucket 12 holds three: 12, 25 and 38.
桶 4 中有四个键,桶 12 中有三个:12、25 和 38。
Every other bucket is empty.
其他桶都是空的。
With chaining, each bucket hangs a list.
使用链地址法时,每个桶都连接一个链表。
With linear probing instead, those four keys need 1 plus 2 plus 3 plus 4, ten probes, and clusters grow longer.
如果改用线性探测,这四个键需要 1 加 2 加 3 加 4,也就是十次探测,而且聚集区会越来越长。
Keys A and B both hash to slot 2.
键 A 和 B 都哈希到槽位 2。
With linear probing, B goes into the next free slot, slot 3.
使用线性探测时,B 会进入下一个空槽位,也就是槽位 3。
With chaining, slot 2 points to a linked list holding A and then B.
使用链地址法时,槽位 2 指向一个链表,里面先是 A,然后是 B。
Either way, a search for B checks slot 2 first.
无论哪种方式,搜索 B 时都会先检查槽位 2。
In a binary search tree, smaller keys go left and larger keys right, so an inorder walk reads them sorted.
在二叉搜索树中,较小的键放在左边,较大的键放在右边,因此中序遍历会按排序后的顺序读出它们。
Inserting 50, 30, 70, 20, 40, 60 and 80 builds a balanced tree of height 2, and finding 40 takes three comparisons.
依次插入 50、30、70、20、40、60 和 80,会构成一棵高度为 2 的平衡树,查找 40 需要三次比较。
A heap is stored in an array: the children of index i sit at 2 i plus 1 and 2 i plus 2.
堆存储在数组中:索引 i 的子节点位于 2 i 加 1 和 2 i 加 2。
On the left is the search tree built from 50, 30, 70, 20, 40, 60 and 80, perfectly balanced.
左边是由 50、30、70、20、40、60 和 80 构建的搜索树,它完全平衡。
Inserting sorted keys instead would build a long chain, the trap of a skewed tree.
如果改为按顺序插入键,就会构成一条长链,这就是倾斜树的陷阱。
On the right is the min-heap array 1, 3, 8, 5 drawn as its tree.
右边是将最小堆数组 1、3、8、5 画成的树。
The array is the tree; the indices are the edges.
数组就是这棵树;索引决定了边的连接。
Every node has smaller values on its left and larger values on its right.
每个节点左边的值较小,右边的值较大。
To insert 35, go left at 50, right at 30 and left at 40.
要插入 35,在 50 处向左,在 30 处向右,在 40 处向左。
A search follows the same path, discarding half of the remaining tree at each level when the tree is balanced.
搜索遵循相同的路径,当树平衡时,每一层都会排除剩余树的一半。
A graph with 8 nodes and 10 undirected edges needs 20 entries in an adjacency list, but 64 cells in a matrix, so lists suit sparse graphs.
一个有 8 个节点和 10 条无向边的图,邻接表需要 20 个条目,而邻接矩阵需要 64 个单元格,因此邻接表适合稀疏图。
Breadth-first search uses a queue and visits in layers: A, then B and C, then D and E, and so on.
广度优先搜索使用队列,按层访问:先是 A,然后是 B 和 C,接着是 D 和 E,依此类推。
Each layer is the shortest hop count.
每一层对应最短路径的跳数。
Depth-first search uses a stack and goes deep first.
深度优先搜索使用栈,优先向深处搜索。
Follow the numbers on this graph: breadth-first search from A visits A, B, C, D, E, F, G and H, layer by layer.
按照图上的数字来看:从 A 开始的广度优先搜索逐层访问 A、B、C、D、E、F、G 和 H。
Everything one hop away comes before anything two hops away.
所有一跳可达的节点都会先于两跳可达的节点被访问。
That is why breadth-first search finds shortest paths in unweighted graphs, while depth-first search is better for exploring one path fully before backtracking.
这就是广度优先搜索能在无权图中找到最短路径的原因,而深度优先搜索更适合在回溯之前完整探索一条路径。
The list 5, 2, 9, 1, 7, 3 contains 8 inversions, pairs in the wrong order.
列表 5、2、9、1、7、3 包含 8 个逆序对,也就是顺序颠倒的元素对。
Every adjacent swap fixes exactly one, so swap-based sorts pay 8 moves.
每次交换相邻元素恰好消除一个逆序对,因此基于交换的排序需要 8 次移动。
Insertion sort makes 8 shifts and 11 comparisons here.
这里插入排序需要 8 次后移和 11 次比较。
Merge sort halves, sorts the halves and merges, using 10 comparisons.
归并排序将数据分成两半,分别排序后再合并,需要 10 次比较。
At a thousand items, the two families differ by a factor of a hundred.
对于一千个数据项,这两类算法相差一百倍。
Each row is one step of insertion sort.
每一行都是插入排序的一个步骤。
The pale part on the left is already sorted.
左边颜色较浅的部分已经排好序。
Take the next card and slide it left past every larger value until it fits.
取出下一张卡片,将它向左移动,越过所有比它大的值,直到放到合适的位置。
Inserting 1 needs three shifts, past 9, 5 and 2.
插入 1 需要后移三次,依次越过 9、5 和 2。
Insertion sort is stable, and fast on nearly sorted data.
插入排序是稳定的,而且处理接近有序的数据时速度很快。
Linear search works on any list but walks it item by item.
线性搜索适用于任何列表,但要逐项遍历。
Binary search needs sorted data, and halves the range each probe.
二分搜索要求数据有序,每次探测都会将范围缩小一半。
In 16 sorted slots, finding 43 takes four probes, while a linear walk takes eleven.
在 16 个有序槽位中,找到 43 需要四次探测,而线性遍历需要十一次。
A miss takes five probes, the worst case.
未找到时需要五次探测,这是最坏情况。
On unsorted data, binary search silently gives wrong answers.
对于无序数据,二分搜索会悄悄给出错误答案。
Follow the probe trail for 43.
跟着探测过程查找 43。
The middle, index 7, holds 31: too small, so search right.
中间的索引 7 处是 31:太小,所以向右搜索。
Index 11 holds 47: too big, so search left.
索引 11 处是 47:太大,所以向左搜索。
Index 9 holds 39: too small again.
索引 9 处是 39:还是太小。
Index 10 holds 43: found, in four probes.
索引 10 处是 43:找到了,一共探测了四次。
Each probe halves what is left, so sixteen slots never need more than five.
每次探测都会把剩余范围缩小一半,所以十六个槽位最多只需要五次探测。
The list is sorted, so look at the middle.
列表是有序的,所以先看中间。
At index 4 the value is 16, less than 23, so discard the left half.
索引 4 处的值是 16,小于 23,所以排除左半部分。
At index 6 the value is 38, greater than 23, so go left.
索引 6 处的值是 38,大于 23,所以向左找。
Index 5 holds 23.
索引 5 处是 23。
Three probes instead of the six a linear search would need.
只需要三次探测,而线性搜索需要六次。
Now test yourself from memory.
现在凭记忆测试一下自己。
If doubling n quadruples the work, what is the growth class?
如果 n 加倍时工作量变为四倍,增长类别是什么?
Under k mod 13, where do the keys 4, 17, 30 and 43 land?
使用 k mod 13 时,键 4、17、30 和 43 落在哪里?
Where are the children of heap index i?
堆中索引 i 的子节点在哪里?
What is the worst case for binary search in sixteen slots?
在十六个槽位中进行二分搜索,最坏情况需要多少次探测?
Pause, answer, then find it on the figure.
暂停,回答,然后在图中找到答案。
Comments
No comments yet. Be the first to ask or share something.