1.Divide-and-Conquer
1.1Divide-and-Conquer![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/85f7ae216e802de912535f6d6af3f84a.png)
1.2 Karatsuba’s Algorithm
1.3Merge Sort
T(n) = 2T(n/2) + cn
1.4Binary Search
T(n) = T(n/2) + c
1.5Master Theorem
T(n) = aT(n/b) + f (n)
a ≥ 1, b > 1 are constants and f (n) is positive .
Case 1: f(n) < nlogba
T(n) = nlogba
Case 2: f(n) = nlogba
T(n) = nlogba logn
Case 3: f(n) > nlogba
T(n) = f(n)
1.6Matrix Multiplications
T(n) = 8T(n/2) + cn2.
By Master theorem, T(n) is Θ(n3).
1.7Strassen’s Algorithm
T(n) = 7T(n/2) + cn2
By Master theorem, T(n) is Θ(nlog 7) ≈ Θ(n2.808)
2.Graph
2.1 Graph
无向图:{} 有向图:()
2.1.1 Adjacency Matrix
Size:O(n2)
2.1.2 Adjacency List
Size:O(m)
2.2 Weighted Graphs
2.3 DAGs
directed acyclic graph,有向无环图。
acyclic:
时间复杂度:O(n + m)
2.4 edge 的分类
图中蓝色的即为tree edge。
2.5 Linearisations
有环不能被线性。
Linearizable ≡ Acyclicity ≡ No-Back-edgeness
The algorithm runs in time O(m + n).
2.6 SCC , Kosaraju-Sharir algorithm
strongly connected component
1.如果G是无环图,则每个节点都是scc,G中有n个sccs。
2.如果G是个环,则G本身是scc,G中有1个scc。
3.如果G是无向图,则检查scc和检查reachability一样。
find scc
The algorithm runs in time O(m + n).
3. Depth First Search
3.1 Recursive Implementation 递归实现
3.2 Stack Implementation 堆栈实现![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4a649b1eed27c3e6c3553cf743781ff4.png)
时间复杂度分析:
领接表:O(n + m) 邻接矩阵: O(n2)
4. Breadth First Search
The running time of the BFS algorithm is O(m + n).
5. DFS VS BFS
DFS:线性,scc,reachability,O(m + n)
BFS:最短路径,O(m + n)
6. Dijkstra’s Algorithm
Dijkstra’s Algorithm
Dijkstra’s Algorithm
Dijkstra’s Algorithm
7.Spanning Trees
A spanning tree of G is a connected subgraph that contains all nodes in V and no cycles.
A minimal spanning tree of a weighted graph is a spanning tree whose total weight is minimal.(可能不唯一)
8.Prim’s Algorithm
To Find MST(similar way as Dijkstra’s algorithm.)
1.访问dis.最短的节点
2.推出队列,更新到其他点的距离
3.链接节点,更新MST
9.Kruskal’s Algorithm
选择图中最短的edge合并
10.Greedy Algorithms
Dijkstra’s: 选择到起点距离最短的节点
Prim’s: 选择连接到已知节点的edge中最短的
Kruskal’s: 选择最短的edge连接(不构成环)
11. Example : 小偷问题
描述:
思路:
W:总可容纳重量
w:各物品的重量
v:各物品的价值
P:优先队列,(1,5)表示index为1,价值/重量比为5
(比值最高的在队列最前)
S:输出的方案,(1,25)表示index为1,重量为25
w’:w的余,0代表被取出
W’:剩余的总重量
TotalValue:取得的物品总价值
答案:S = {(1, 25), (4, 30), (0, 20), (3, 25)}
TotalValue = 315 + 50 = 365
伪码描述:
12.Bellman-Ford Algorithm
Node1,2,3…代表能使用的节点
example:
time :Θ(mn).
Dijkstra’s and Bell-Ford algorithm both solves Single-Source Shortest Path Problem.
13. Floyd-Warshall Algorithm
fk(i, j) 是经过 v1,…,vk 节点的vi, vj间的最短路径。
Time Complexity : O(n3)
最短路径问题总结
Single-Source Weights:
- Positive :
- Dijkstra’s algorithm :
- List O(n2)
- Binary /Binomial Heap O((n + m) log n)
- Fibonacci Heap O(m + n log n)
- Dijkstra’s algorithm :
- Positive/Negative:
- Bellman-Ford(动态): algorithm: O(nm)
All-Pair Weights:
- Floyd-Warshall(动态): algorithm:O(n3)
14. Example: Longest Increasing Subsequence
访问每个新节点时,在以下情况中选择最大的:
1.延续上一个节点的值不变
2.相连的前个节点加1
答案:11222344
分析:
15. Edit Distance
一行一行更新,访问新的点时,在以下情况中选择最小的:
- 左边的点 +1
- 上面的点 +1
- 左上角的点
- 假如相同 +0
- 假如不同 +1
distance 为3