T(n) = 2T(n/2) + cn
T(n) = T(n/2) + c
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)
T(n) = 8T(n/2) + cn2.
By Master theorem, T(n) is Θ(n3).
T(n) = 7T(n/2) + cn2
By Master theorem, T(n) is Θ(nlog 7) ≈ Θ(n2.808)
directed acyclic graph,有向无环图。
acyclic:
时间复杂度:O(n + m)
有环不能被线性。
Linearizable ≡ Acyclicity ≡ No-Back-edgeness
The algorithm runs in time O(m + n).
strongly connected component
1.如果G是无环图,则每个节点都是scc,G中有n个sccs。
2.如果G是个环,则G本身是scc,G中有1个scc。
3.如果G是无向图,则检查scc和检查reachability一样。
The algorithm runs in time O(m + n).
时间复杂度分析:
领接表:O(n + m) 邻接矩阵: O(n2)
The running time of the BFS algorithm is O(m + n).
DFS:线性,scc,reachability,O(m + n)
BFS:最短路径,O(m + n)
Dijkstra’s Algorithm
Dijkstra’s Algorithm
Dijkstra’s Algorithm
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.(可能不唯一)
To Find MST(similar way as Dijkstra’s algorithm.)
1.访问dis.最短的节点
2.推出队列,更新到其他点的距离
3.链接节点,更新MST
Dijkstra’s: 选择到起点距离最短的节点
Prim’s: 选择连接到已知节点的edge中最短的
Kruskal’s: 选择最短的edge连接(不构成环)
描述:
思路:
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
Node1,2,3…代表能使用的节点
example:
time :Θ(mn).
Dijkstra’s and Bell-Ford algorithm both solves Single-Source Shortest Path Problem.
fk(i, j) 是经过 v1,…,vk 节点的vi, vj间的最短路径。
Time Complexity : O(n3)
访问每个新节点时,在以下情况中选择最大的:
1.延续上一个节点的值不变
2.相连的前个节点加1
答案:11222344
分析: