Download presentation
Presentation is loading. Please wait.
Published byJesse Oliver Modified over 9 years ago
1
Graph (II) Shortest path, Minimum spanning tree GGuy 19-3-2011
2
Review DFS (Depth-First-Search) Graph traverse Topological sort BFS (Breadth-First-Search) Finding shortest path
3
Shortest Path Problem In a weighted graph G We want to find a path from s to t, such that the sum of edge weight is minimum
4
Shortest Path S T 3 1 3 1 3 2 3
5
S T 3 1 3 1 3 2 3
6
Algorithms Single source? All pairs? Negative weight edge? Negative cycles?
7
Dijkstra’s algorithm for-each v, d[v] ← ∞ Q.Insert(s,0) while not Q.Empty() do (u, w) = Q.ExtractMin() if (visited[u]) continue; d[u] ← w for-each v where (u, v) in E Q.Insert(v, d[u] + weight uv )
8
Dijkstra’s algorithm S T 3 1 3 1 3 2 3
9
0 T 3 1 3 1 3 2 3
10
0 2 T 3 1 3 1 3 2 3
11
0 2 T 3 1 3 1 3 2 3
12
0 3 2 T 3 1 3 1 3 2 3
13
0 3 2 T 3 1 3 1 3 2 3
14
0 3 3 2 T 3 1 3 1 3 2 3
15
0 3 3 2 T 3 1 3 1 3 2 3
16
0 3 3 2 T 3 1 3 1 3 2 3
17
0 3 3 2 4 3 1 3 1 3 2 3
18
0 3 3 2 4 3 1 3 1 3 2 3
19
0 3 3 2 4 3 1 3 1 3 2 3
20
Implement Q using binary heap (priority queue) At most E edges in the heap Time complexity: O(E log (E)) = O(E log (V)) Space complexity: O(V+E)
21
Bellman Ford for-each v, d[v] ← ∞ d[s] ← 0 Do V-1 times for each edge (u,v) if (d[u] + weight uv < d[v]) d[v] ← d[u] + weight uv
22
Bellman Ford’s algorithm S T 3 1 -2 1 4 2 3
23
Bellman Ford’s algorithm S 4 3 2 T 3 1 -2 1 4 2 3
24
Bellman Ford’s algorithm S 3 3 2 2 3 1 -2 1 4 2 3
25
Bellman Ford’s algorithm S 3 3 2 1 3 1 -2 1 4 2 3
26
Bellman Ford’s algorithm Assume the shortest path from s->t is P 0 P 1 P 2 … P m-1 where m < |V| and s is fixed In the 1st iteration, d[P 1 ] is shortest In the 2nd iteration, d[P 2 ] is shortest … In the m-1 iteration, d[P m-1 ] is shortest
27
Bellman Ford’s algorithm After |V|-1 iterations, If there exists edge (u,v) where d[u] + weight uv < d[v] Negative cycle!
28
Bellman Ford’s algorithm Time complexity: O(VE) Space complexity: O(V)
29
Floyd Warshall’s algorithm for all pairs(i,j), d[i][j] ← ∞ for all edges(u,v),d[u][v] ← weight uv for k=1 to V for i=1 to V for j=1 to V d[i][j] = min(d[i][j], d[i][k]+d[k][j])
30
Floyd Warshall’s algorithm Assume the shortest path from s->t is P 0 P 1 P 2 … P m-1 for any s,t For example, 5->3->1->2->4->6 i=1: 5->1->6 i=2: 5->1->2->6 i=3: 5->3->1->2->6 i=4: 5->3->1->2->4->6 i=5: 5->3->1->2->4->6 i=6: 5->3->1->2->4->6 We won’t miss any shortest path!
31
Floyd Warshall’s algorithm Time complexity: O(V 3 ) Space complexity: O(V 2 )
32
Summary Negative edges?Negative cycle?Time complexity DijkstraSingle sourceNo O(E log V) Bellman FordSingle sourceYes O(VE) Floyd WarshallAll pairsYesMaybe (modified) O(V 3 )
33
Tree What is a tree? G is connected and acyclic G is connected and |E| = |V| - 1 G is acyclic and |E| = |V| - 1 For any pairs in G, there is a unique path
34
Spanning Tree 3 1 3 1 3 2 3
35
Minimum Spanning Tree 3 1 3 1 3 2 3
36
Kruskal’s algorithm T ← empty set of edges Sort the edges in increasing order For each edge e (in increasing order) if T + e does not contain a cycle add e to T
37
Kruskal’s algorithm How to detect a cycle? Disjoint Set
38
Kruskal’s algorithm If we choose an edge (u,v) Union(u, v)
39
Kruskal’s algorithm 3 1 2 1 2 2 2
40
3 1 2 1 2 2 2
41
3 1 2 1 2 2 2
42
3 1 2 1 2 2 2
43
3 1 2 1 2 2 2
44
3 1 2 1 2 2 2
45
3 1 2 1 2 2 2
46
3 1 2 1 2 2 2
47
Krustal’s algorithm Sorting: O(E log V) Select edge: O(E) Check union: O(α(V)) Overall: O(E log V + E α(V))
48
The Red Rule The Red Rule states that for any cycle in G, the largest weight edge will NOT be contained in any Minimum Spanning Tree.
49
Prim’s algorithm T ← node 1 while size of T < V choose a vertex u that is not in V and the cost adding it to V is minimum add u to V
50
Prim’s algorithm 3 1 2 1 2 2 2
51
3 1 2 1 2 2 2
52
3 1 2 1 2 2 2
53
3 1 2 1 2 2 2
54
3 1 2 1 2 2 2
55
Use a min heap to maintain Dijkstra like implement Time complexity: O(E log V)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.