Algorithm Design and Applications
Chapter 1 Algorithm Analysis
Similarly, computer scientists must also deal with scale,
but they deal with it primarily in terms of data volume rather than physical object size.
算法的 running time 和 space usage 用来 analysis 好坏。
1.1 Analyzing Algorithms
• Takes into account all possible inputs
• Allows us to evaluate the relative efficiency of any two algorithms in a way
that is independent from the hardware and software environment
• Can be performed by studying a high-level description of the algorithm without
actually implementing it or running experiments on it.
1.1.1 Pseudo-Code
Pseudo-code is a mixture of natural language and high-level programming constructs
that describe the main ideas behind a generic implementation of a data structure or algorithm.
1.1.2 The Random Access Machine (RAM) Model
Primitive operations 操作对应硬件指令,有固定操作时间,包括:
• Assigning a value to a variable
• Calling a method
• Performing an arithmetic operation (for example, adding two numbers)
• Comparing two numbers
• Indexing into an array
• Following an object reference
• Returning from a method.
1.1.3 Counting Primitive Operations
That is, designing for the worst case can lead to stronger algorithmic
“muscles,” much like a track star who always practices by running uphill.
1.1.4 Analyzing Recursive Algorithms
一个结束状态,一个递归函数。即便是理解了栈,也好难理解递归调用。
1.1.5 Asymptotic Notation
Let f(n) and g(n) be functions mapping nonnegative integers to real numbers.
1.1.6 The Importance of Asymptotic Notation
1.2 A Quick Mathematical Review
1.2.1 Summations
1.2.2 Logarithms and Exponents
floor = the largest integer less than or equal to x
ceiling = the smallest integer greater than or equal to x.
1.2.3 Simple Justification Techniques
counterexample(反例), contrapositive(对立), contradiction(矛盾), Induction(归纳), Loop Invariants(循环不变)
1.2.4 Basic Probability
probability space(概率空间), mutually independent(互相独立), Conditional Probability(条件概率),
random variables(随机变量), expected value(期望值), Chernoff Bounds(切尔诺夫界限),
1.3 A Case Study in Algorithm Analysis
maximum subarray problem(最大子序列),
1.3.1 A First Solution to the Maximum Subarray Problem
1 | int MaxSubSlow(const vector<int>& v) |
1.3.2 An Improved Maximum Subarray Algorithm
1 | int MaxSubFaster(const vector<int>& v) |
1.3.3 A Linear-Time Maximum Subarray Algorithm
1 | int MaxSubFastest(const vector<int>& v) |
1.4 Amortization
clearable table(可擦除表), amortized running time(平坦运行时间)
1.4.1 Amortization Techniques
The Accounting Method(会计方法), Potential Functions(),
1.4.2 Analyzing an Extendable Array Implementation
extendable array(扩展数组),
1.5 Exercises
Reinforcement(加固), Creativity(创意), Applications(应用)
Chapter Notes
Chapter 2 Basic Data Structures
producer-consumer model(生产者消费者模型), first-in, first-out (FIFO)
basic_data_structures.h
2.1 Stacks and Queues
2.1.1 Stacks
last in first-out (LIFO), decltype(auto) 这是个啥类型,难道是先用 auto 接收了返回值类型。
Using Stacks for Procedure Calls and Recursion:
More specifically, during the execution of a program thread, the runtime environment
maintains a stack whose elements are descriptors of the currently active
(that is, nonterminated) invocations of methods. These descriptors are called
frames(栈帧). A frame for some invocation of method cool stores the current values of
the local variables and parameters of method cool, as well as information on the
method that called cool and on what needs to be returned to this method.
Recursion(递归):
Each call of the same method will be associated with a different frame,
complete with its own values for local variables.
2.1.2 Queues
first-in first-out (FIFO),
2.2 Lists
2.2.1 Index-Based Lists
基于数组的实现。
2.2.2 Linked Lists
基于链表的实现。
2.3 Trees
A binary tree is proper if each internal node has two children.
2.3.1 A Tree Definition
树深度,根是 0 ,往下数深度。 树高度,叶子是 0 ,往上数高度,根最高,是树高。
2.3.2 Tree Traversal
Preorder Traversal(前序遍历, 先根后子)->阅读文档。Postorder Traversal(后续遍历, 先子后根)
2.3.3 Binary Trees
1 | // L -> L -> R - R 等到里面的左递归结束,正好开始执行父亲的右兄弟。\ |
2.3.4 Data Structures for Representing Trees
linked structure(链式存储结构),
2.4 Exercises
Reinforcement(巩固), Creativity(创造力), Applications(应用)
Chapter Notes
Chapter 3 Binary Search Trees
binary space partitioning(二进制空间分区), binary search tree(二叉查找树),
3.1 Searches and Updates . . . . . . . . . . . . . . . . . . . 91
1 | Algorithm BinarySearch(A, k, low, high): |
3.1.1 Binary Search Tree Definition
左 <= 根 <= 右
3.1.2 Searching in a Binary Search Tree
1 | Algorithm TreeSearch(k, v): |
3.1.3 Insertion in a Binary Search Tree
先找到位置然后插入,并满足BST。
Let w ← TreeSearch(k, T.root())
while w is an internal node do
// There is item with key equal to k in T in this case
Let w ← TreeSearch(k, T.leftChild(w))
Expand w into an internal node with two external-node children
Store (k, e) at w
3.1.4 Deletion in a Binary Search Tree
比较复杂,我们要先找到需要删除的点,然后删除最后移动恢复BST。
3.1.5 The Performance of Binary Search Trees
3.2 RangeQueries . . . . . . . . . . . . . . . . . . . . . . . . 101
1 |
|
3.3 Index-Based Searching . . . . . . . . . . . . . . . . . . 104
1 | Algorithm TreeSelect(i, v, T): |
3.4 Randomly Constructed Search Trees . . . . . . . . . . 107
随机构造的查找树,有点复杂,都是数学论证。
3.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Reinforcement(巩固), 习题都没怎么细看。后面刷题实际练习吧。
Chapter Notes
Chapter 4 Balanced Binary Search Trees
nearest-neighbor query(邻近查询), balanced binary search trees(平衡二叉查找树),
4.1 Ranks and Rotations . . . . . . . . . . . . . . . . . . . . 117
1 | Algorithm IterativeTreeSearch(k, T): |
rotation(旋转), of which there are four types. trinode restructuring(重组), which combines the four types of rotations into one action.
4.2 AVL Trees . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Height-balance Property: For every internal node, v, in T,
the heights of the children of v may differ by at most 1.
That is, if a node, v, in T has children, x and y, then |r(x) − r(y)| ≤ 1.
1 | Algorithm insertAVL(k, e, T): |
1 |
|
4.3 Red-Black Trees . . . . . . . . . . . . . . . . . . . . . . . 126
基于颜色的定义。
External-Node Property: Every external node is black.(根是黑色,可以是红??)
Internal-node Property: The children of a red node are black.
Black-depth Property: All the external nodes have the same black depth(到根黑色节点的个数,包括根),
that is, the same number of black nodes as proper ancestors.
等价的 Rank 定义。当进行插入和删除的时候更容易维护定义。
External-Node Property: Every external node has rank 0 and its parent, if it exists, has rank 1.
Parent Property: The rank difference of every node, other than the root, is 0 or 1.
Grandparent Property: Any node with rank difference 0 is either a child of the root or its parent has rank difference 1.
4.4 WeakAVL Trees . . . . . . . . . . . . . . . . . . . . . . . 130
结构上是红黑树的平衡树。have features of both AVL trees and red-black trees。
Every AVL tree is a weak AVL tree, and every weak AVL tree can be colored as a red-black tree.
Rank-Difference Property: The rank difference of any non-root node is 1 or 2.
External-Node Property: Every external node has rank 0.
Internal-Node Property: An internal node with two external-node children cannot be a 2, 2-node.
4.5 Splay Trees . . . . . . . . . . . . . . . . . . . . . . . . . . 139
zig-zig, zig-zag, zig, 没大看懂干嘛用的,反正就是各种 splay 然后满足一个比较小的高度。每次翻转减少高度。
4.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Reinforcement, Creativity, Applications
Chapter Notes
Chapter 5 Priority Queues and Heaps
matching engines(匹配引擎), continuous limit order book
5.1 Priority Queues . . . . . . . . . . . . . . . . . . . . . . . 157
• Reflexive property: k ≤ k.
• Antisymmetric property: if k1 ≤ k2 and k2 ≤ k1, then k1 = k2.
• Transitive property: if k1 ≤ k2 and k2 ≤ k3, then k1 ≤ k3.
5.2 PQ-Sort, Selection-Sort, and Insertion-Sort . . . . . . 158
1 | Algorithm PQ-Sort(C, P): |
5.2.1 Selection-Sort
1 | Algorithm SelectionSort(A): |
5.2.2 Insertion-Sort
1 | Algorithm InsertionSort(A): |
5.3 Heaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Heap-Order Property: In a heap T, for every node v other than the root,
the key stored at v is greater than or equal to the key stored at v’s parent.
Complete Binary Tree: A binary tree T with height h is complete if the levels
0, 1, 2, . . . , h−1 have the maximum number of nodes possible
(that is, level i has 2i nodes, for 0 ≤ i ≤ h − 1) and in level h − 1
all the internal nodes are to the left of the external nodes.
5.3.1 An Array-Based Structure for Binary Trees
p(v) 可以作为数组下标:
• If v is the root of T, then p(v) = 1.
• If v is the left child of node u, then p(v) = 2p(u).
• If v is the right child of node u, then p(v) = 2p(u) + 1.
5.3.2 Insertion in a Heap
up-heap bubbling(向上堆冒泡)
1 | Algorithm HeapInsert(k, e): |
5.3.3 Removal in a Heap
1 | Algorithm HeapRemoveMin(): |
5.4 Heap-Sort . . . . . . . . . . . . . . . . . . . . . . . . . . 174
1 | Algorithm BottomUpHeap(S): |
5.5 Extending Priority Queues . . . . . . . . . . . . . . . . 179
element(): Return the element of the item associated with Locators(定位器).
key(): Return the key of the item associated with Locators(定位器).
min(): Return the locator to an item of P with smallest key.
insert(k, e): Insert a new item with element e and key k into P and
return a locator to the item.
remove(): Remove from P the item with locator .
replaceElement(, e): Replace with e and return the element of the item of P with locator .
replaceKey(, k): Replace with k and return the key of the item of P with locator
5.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Reinforcement, Creativity, Applications
Chapter Notes
Chapter 6 Hash Tables
hash table(哈希表),
6.1 Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
dictionary or map(字典, 图), multimap(多重映射)
6.1.1 The Map Definition
get(k): If M contains an item with key equal to k, then return the value of such an item; else return a special element NULL.
put(k, v): Insert an item with key k and value v; if there is already an item with key k, then replace its value with v.
remove(k): Remove from M an item with key equal to k, and return this item. If M has no such item, then return the special element NULL.
6.1.2 Lookup Tables
bucket(桶),
6.2 Hash Functions . . . . . . . . . . . . . . . . . . . . . . . 192
collision(冲突),
The standard convention for hash functions is to view keys in one of two ways:
• Each key, k, is a tuple of integers, (x1, x2, . . . , xd), with each xi being an integer in the range [0,M − 1], for some M and d.
• Each key, k, is a nonnegative integer, which could possibly be very large.
6.2.1 Summing Components
6.2.2 Polynomial-Evaluation Functions
polynomial-evaluation(多项式求值),
6.2.3 Tabulation-Based Hashing
6.2.4 Modular Division
6.2.5 Random Linear and Polynomial Functions
random linear hash function(随机线性哈希函数), random polynomial hash function(随机多项式哈希函数),
6.3 Handling Collisions and Rehashing . . . . . . . . . . . 198
6.3.1 Separate Chaining
load factor(负载系数)
6.3.2 Open Addressing
开放寻址
6.3.3 Linear Probing
1 | get(k): |
1 | put(k, v): |
1 | remove(k): |
1 | Shift(i): |
6.3.4 Quadratic Probing
quadratic probing(二次探测),
6.3.5 Double Hashing
6.3.6 Rehashing
Insert all the existing hash-table elements into the new bucket array using the new hash function
6.4 Cuckoo Hashing. . . . . . . . . . . . . . . . . . . . . . . 206
It mimics the breeding habits of the Common Cuckoo bird.(两个窝)1
2
3
4
5
6get(k):
if T0[h0(k)] = NULL and T0[h0(k)].key = k then
return T0[h0(k)]
if T1[h1(k)] = NULL and T1[h1(k)].key = k then
return T1[h1(k)]
return NULL
1 | remove(k): |
1 | put(k, v): |
6.5 Universal Hashing . . . . . . . . . . . . . . . . . . . . . 212
全域散列
6.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Reinforcement, Creativity, Applications
Chapter Notes
Chapter 7 Union-Find Structures
并查集, social network(社交网络),
7.1 Union-Find and Its Applications . . . . . . . . . . . . . 221
7.1.1 Connected Components
1 | Algorithm UFConnectedComponents(S,E): |
7.1.2 Maze Construction and Percolation Theory
1 | Algorithm MazeGenerator(G,E): |
7.2 A List-Based Implementation . . . . . . . . . . . . . . . 225
1 | Algorithm makeSet(): |
1 | Algorithm find(x): |
1 | Algorithm union(u, v): |
7.3 A Tree-Based Implementation . . . . . . . . . . . . . . . 228
1 | Algorithm makeSet(): |
1 | Algorithm union(x, y): |
1 | Algorithm find(x): |
7.3.1 Analyzing the Tree-Based Implementation
Ackermann function(阿克曼函数),
7.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Reinforcement, Creativity, Applications
Chapter Notes
Chapter 8 Merge-Sort and Quick-Sort
inverted file(倒排文件), lexicographic(字典序),
8.1 Merge-Sort
merge-sort(归并排序),
8.1.1 Divide-and-Conquer
divide-and-conquer(分而治之), 分成小块进行处理,最后再 merge 到一起。大事化小,小事化了。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20Algorithm merge(S1, S2, S):
Input: Two arrays, S1 and S2, of size n1 and n2, respectively,
sorted in nondecreasing order, and an empty array, S, of size at least n1 + n2
Output: S, containing the elements from S1 and S2 in sorted order
i ← 1
j ← 1
while i ≤ n and j ≤ n do
if S1[i] ≤ S2[j] then
S[i + j − 1] ← S1[i]
i ← i + 1
else
S[i + j − 1] ← S2[j]
j ← j + 1
while i ≤ n do
S[i + j − 1] ← S1[i]
i ← i + 1
while j ≤ n do
S[i + j − 1] ← S2[j]
j ← j + 1
8.1.2 Merge-Sort and Recurrence Equations
8.2 Quick-Sort
每次选择一个 pivot 把小于 pivot 的放到 L,大于 pivot 的放到 G,循环直到不可分。可以采用递归或者迭代法。
8.2.1 Randomized Quick-Sort
增加随机选择 pivot 保证每次分配到 L 集合的合 G 集合的几乎均等,保证算法运行时间为 O(n log n).
8.2.2 In-Place Quick-Sort
使用本身的存储空间进行排序。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19Algorithm inPlacePartition(S, a, b):
Input: An array, S, of distinct elements; integers a and b such that a ≤ b
Output: An integer, l, such that the subarray S[a .. b] is partitioned into S[a..l−1] and S[l..b]
so that every element in S[a..l−1] is less than each element in S[l..b]
Let r be a random integer in the range [a, b]
Swap S[r] and S[b]
p ← S[b] // the pivot
l ← a // l will scan rightward
r ← b − 1 // r will scan leftward
while l ≤ r do // find an element larger than the pivot
while l ≤ r and S[l] ≤ p do
l ← l + 1
while r ≥ l and S[r] ≥ p do // find an element smaller than the pivot
r ← r − 1
if l < r then
Swap S[l] and S[r]
Swap S[l] and S[b] // put the pivot into its final place
return l
1 | Algorithm inPlaceQuickSort(S, a, b): |
1 | Algorithm CorrectInPlaceQuickSort(S, a, b): |
8.3 A Lower Bound on Comparison-Based Sorting
8.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Reinforcement, Creativity, Applications
Chapter Notes
Chapter 9 Fast Sorting and Selection
9.1 Bucket-Sort and Radix-Sort . . . . . . . . . . . . . . . . 267
9.1.1 Bucket-Sort
1 | Algorithm bucketSort(S): |
9.1.2 Radix-Sort
The radix-sort algorithm sorts a sequence of pairs such as S, by applying a stable bucket-sort on the sequence twice
9.2 Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Queries that ask for an element with a given rank are called order statistics(次序统计).
Prune-and-Search(剪枝和搜索)
9.2.1 Randomized Quick-Select
1 | Algorithm quickSelect(S, k): |
9.2.2 Deterministic Selection
1 | Algorithm DeterministicSelect(S, k): |
9.3 Weighted Medians . . . . . . . . . . . . . . . . . . . . . 276
1 | Algorithm SortedMedian(X): |
1 | Algorithm PruneMedian(X,W): |
9.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Reinforcement, Creativity, Applications
Chapter Notes
Chapter 10 The Greedy Method
multi-lot(多路), greedy method(贪心), knapsack(背包), fractional knapsack(部分背包)
The greedy method is applied to optimization problems—that is, problems that involve
searching through a set of configurations to find one that minimizes or maximizes
an objective function defined on these configurations
10.1 The Fractional Knapsack Problem . . . . . . . . . . . . 286
1 | Algorithm FractionalKnapsack(S,W): |
10.2 Task Scheduling. . . . . . . . . . . . . . . . . . . . . . . 289
1 | Algorithm TaskSchedule(T): |
10.3 Text Compression and Huffman Coding . . . . . . . . . 292
1 | Algorithm Huffman(C): |
10.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Reinforcement, Creativity, Applications
Chapter Notes
Chapter 11 Divide-and-Conquer
11.1 Recurrences and the Master Theorem. . . . . . . . . . 305
recurrence equation(递归方程), iterative substitution(迭代替换), recursion tree(递归树)
11.1.1 The Master Theorem
11.2 Integer Multiplication . . . . . . . . . . . . . . . . . . . . 313
fast Fourier transform(快速傅里叶变换)
11.3 Matrix Multiplication . . . . . . . . . . . . . . . . . . . . 315
Strassen’s Algorithm,
11.4 The Maxima-SetProblem . . . . . . . . . . . . . . . . . 317
1 | Algorithm MaximaSet(S): |
11.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Chapter Notes
Chapter 12 Dynamic Programming
12.1 Matrix Chain-Products . . . . . . . . . . . . . . . . . . . 325
12.2 The General Technique . . . . . . . . . . . . . . . . . . 329
12.3 Telescope Scheduling . . . . . . . . . . . . . . . . . . . 331
12.4 Game Strategies . . . . . . . . . . . . . . . . . . . . . . 334
12.5 The Longest Common Subsequence Problem . . . . . 339
12.6 The 0-1 Knapsack Problem . . . . . . . . . . . . . . . . 343
12.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . 346