ADA:Algorithm Design and Analysis——review

1.Divide-and-Conquer

1.1Divide-and-ConquerADA:Algorithm Design and Analysis——review_第1张图片

1.2 Karatsuba’s Algorithm

ADA:Algorithm Design and Analysis——review_第2张图片

ADA:Algorithm Design and Analysis——review_第3张图片

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

ADA:Algorithm Design and Analysis——review_第4张图片

T(n) = 7T(n/2) + cn2
By Master theorem, T(n) is Θ(nlog 7) ≈ Θ(n2.808)

2.Graph

2.1 Graph

ADA:Algorithm Design and Analysis——review_第5张图片
无向图:{} 有向图:()

2.1.1 Adjacency Matrix

ADA:Algorithm Design and Analysis——review_第6张图片
Size:O(n2)

2.1.2 Adjacency List

ADA:Algorithm Design and Analysis——review_第7张图片
Size:O(m)

2.2 Weighted Graphs

ADA:Algorithm Design and Analysis——review_第8张图片
ADA:Algorithm Design and Analysis——review_第9张图片

2.3 DAGs

directed acyclic graph,有向无环图。
acyclic:
ADA:Algorithm Design and Analysis——review_第10张图片
时间复杂度:O(n + m)

2.4 edge 的分类

ADA:Algorithm Design and Analysis——review_第11张图片
图中蓝色的即为tree edge。

2.5 Linearisations

有环不能被线性。
Linearizable ≡ Acyclicity ≡ No-Back-edgeness
ADA:Algorithm Design and Analysis——review_第12张图片
ADA:Algorithm Design and Analysis——review_第13张图片
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
ADA:Algorithm Design and Analysis——review_第14张图片

ADA:Algorithm Design and Analysis——review_第15张图片
The algorithm runs in time O(m + n).

3. Depth First Search

3.1 Recursive Implementation 递归实现

ADA:Algorithm Design and Analysis——review_第16张图片
ADA:Algorithm Design and Analysis——review_第17张图片

3.2 Stack Implementation 堆栈实现ADA:Algorithm Design and Analysis——review_第18张图片

时间复杂度分析:
领接表:O(n + m) 邻接矩阵: O(n2)

4. Breadth First Search

ADA:Algorithm Design and Analysis——review_第19张图片
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
ADA:Algorithm Design and Analysis——review_第20张图片

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.)
ADA:Algorithm Design and Analysis——review_第21张图片

ADA:Algorithm Design and Analysis——review_第22张图片
1.访问dis.最短的节点
2.推出队列,更新到其他点的距离
3.链接节点,更新MST
ADA:Algorithm Design and Analysis——review_第23张图片
ADA:Algorithm Design and Analysis——review_第24张图片

9.Kruskal’s Algorithm

ADA:Algorithm Design and Analysis——review_第25张图片
选择图中最短的edge合并
ADA:Algorithm Design and Analysis——review_第26张图片
ADA:Algorithm Design and Analysis——review_第27张图片

10.Greedy Algorithms

Dijkstra’s: 选择到起点距离最短的节点
Prim’s: 选择连接到已知节点的edge中最短的
Kruskal’s: 选择最短的edge连接(不构成环)
ADA:Algorithm Design and Analysis——review_第28张图片

11. Example : 小偷问题

描述:
ADA:Algorithm Design and Analysis——review_第29张图片
思路:
ADA:Algorithm Design and Analysis——review_第30张图片
ADA:Algorithm Design and Analysis——review_第31张图片
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

伪码描述:
ADA:Algorithm Design and Analysis——review_第32张图片

12.Bellman-Ford Algorithm

ADA:Algorithm Design and Analysis——review_第33张图片
Node1,2,3…代表能使用的节点
ADA:Algorithm Design and Analysis——review_第34张图片
example:
ADA:Algorithm Design and Analysis——review_第35张图片
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间的最短路径。
ADA:Algorithm Design and Analysis——review_第36张图片
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)
  • Positive/Negative:
    • Bellman-Ford(动态): algorithm: O(nm)

All-Pair Weights:

  • Floyd-Warshall(动态): algorithm:O(n3)

14. Example: Longest Increasing Subsequence

ADA:Algorithm Design and Analysis——review_第37张图片
访问每个新节点时,在以下情况中选择最大的:
1.延续上一个节点的值不变
2.相连的前个节点加1
答案:11222344
ADA:Algorithm Design and Analysis——review_第38张图片
分析:
ADA:Algorithm Design and Analysis——review_第39张图片

15. Edit Distance

ADA:Algorithm Design and Analysis——review_第40张图片
一行一行更新,访问新的点时,在以下情况中选择最小的:

  • 左边的点 +1
  • 上面的点 +1
  • 左上角的点
    • 假如相同 +0
    • 假如不同 +1

ADA:Algorithm Design and Analysis——review_第41张图片
distance 为3
ADA:Algorithm Design and Analysis——review_第42张图片

你可能感兴趣的:(算法)