Graph Algorithms 4 Binhai Zhu Computer Science Department
Graph Algorithms, 4 Binhai Zhu Computer Science Department, Montana State University 11/24/2020 1
Single-Source Shortest Paths Algorithm Given a weighted, undirected graph G=(V, E) of n vertices and a source vertex s, we want to compute the shortest paths from s to all other vertices. This is one of the most important problems in computer science. It has applications in almost all branches of computer science. 11/24/2020 2
Single-Source Shortest Paths Algorithm Given a weighted, undirected graph G=(V, E, w) of n vertices and a source vertex s, we want to compute the shortest paths from s to all other vertices. If the graph is unweighted and undirected, we have already learnt how to solve the problem. How? 11/24/2020 3
Single-Source Shortest Paths Algorithm Given a weighted, undirected graph G=(V, E, w) of n vertices and a source vertex s, we want to compute the shortest paths from s to all other vertices. If the graph is unweighted and undirected, we have already learnt how to solve the problem. How? Using BFS(s)! 11/24/2020 4
Shortest Path Property Given a weighted, undirected graph G=(V, E) of n vertices, let δ(s, t) be the shortest path between s and t, both s, t are in V. (When the context is clear, sometimes δ(s, t) is also used to denote the corresponding length. ) What kind of property of δ(s, t) can you see? 11/24/2020 5
Shortest Path Property Given a weighted, undirected graph G=(V, E) of n vertices, let δ(s, t) be the shortest path between s and t, both s, t are in V. (When the context is clear, sometimes δ(s, t) is also used to denote the corresponding length. ) What kind of property of δ(s, t) can you see? t s 11/24/2020 6
Shortest Path Property Given a weighted, undirected graph G=(V, E) of n vertices, let δ(s, t) be the shortest path between s and t, both s, t are in V. (When the context is clear, sometimes δ(s, t) is also used to denote the corresponding length. ) What kind of property of δ(s, t) can you see? t s vi vj 11/24/2020 7
Shortest Path Property Let δ(s, t) be the shortest path between s and t with δ(s, t)=<s=v 1, v 2, …, vi, …, vj…, vk=t>. Then <vi, vi+1, …, vj> is the shortest path between vi and vj. t s vi vj 11/24/2020 8
Shortest Path Property Let δ(s, t) be the shortest path between s and t with δ(s, t)=<s=v 1, v 2, …, vi, …, vj…, vk=t>. Then <vi, vi+1, …, vj> is the shortest path between vi and vj. How do we prove this? t s vi vj 11/24/2020 9
Shortest Path Property Let δ(s, t) be the shortest path between s and t with δ(s, t)=<s=v 1, v 2, …, vi, …, vj…, vk=t>. Then <vi, vi+1, …, vj> is the shortest path between vi and vj. Proof Sketch: Assume that <vi, u 1, u 2, …, ux, vj> be the shortest path between vi and vj. Then <v 1, v 2, …vi>U<vi, u 1, u 2, …, ux, vj>U<vj, vj+1, …, vk> would be shorter than δ(s, t). A contradiction! t s vi vj 11/24/2020 10
The Cut concept Given G=(V, E) and let T be a subgraph of G, Cut(T) contains all the edges of G which has exactly one vertex in T. v 4 1. 5 2. 9 1 v 1 1. 6 1. 7 v 5 2 v 2 1. 3 v 3 11/24/2020 11
The Cut concept Given G=(V, E) and let T be a subgraph of G, Cut(T) contains all the edges of G which has exactly one vertex in T. v 4 Q: On the right, T is in red, what are the edges in Cut(T)? 1. 5 2. 9 1 v 1 1. 6 1. 7 v 5 2 v 2 1. 3 v 3 11/24/2020 12
The Cut concept Given G=(V, E) and let T be a subgraph of G, Cut(T) contains all the edges of G which has exactly one vertex in T. v 4 Q: On the right, T is in red, what are the edges in Cut(T)? A: {(v 1, v 3), (v 3, v 4), (v 4, v 5)} 1. 5 2. 9 1 v 1 1. 6 1. 7 v 5 2 v 2 1. 3 v 3 11/24/2020 13
Dijkstra’s Algorithm First it is a greedy method. v 4 1. 5 4. 9 1 v 1 1. 6 3. 7 v 5 2 v 2 1. 3 v 3 11/24/2020 14
Dijkstra’s Algorithm Input: weighted graph G=<V={1, …, n}, E, w>, a vertex s. Output: a shortest path tree rooted at s. Dist[1: n]---- a distance array initialized to ∞ Dijkstra(G, s, T) T←s Dist[s] ← 0 While Cut(T) ≠ Ø v 4 1. 5 4. 9 1 v 1 1. 6 3. 7 v 5 2 v 2 1. 3 v 3 select an edge uv in Cut(T) s. t. Dist[u]+w(uv) is minimized. Dist[v] ← Dist[u] +w(uv) add vertex v and edge (u, v) into T 11/24/2020 15
Dijkstra’s Algorithm Input: weighted graph G=<V={1, …, n}, E, w>, a vertex s. Output: a shortest path tree rooted at s. Dist[1: n]---- a distance array initialized to ∞ Dijkstra(G, s, T) T←s Dist[s] ← 0 While Cut(T) ≠ Ø v 4 (s) 1. 5 4. 9 1 v 1 1. 6 3. 7 v 5 2 v 2 1. 3 v 3 select an edge uv in Cut(T) s. t. Dist[u]+w(uv) is minimized. Dist[v] ← Dist[u] +w(uv) add vertex v and edge (u, v) into T 11/24/2020 16
Dijkstra’s Algorithm Input: weighted graph G=<V={1, …, n}, E, w>, a vertex s. Output: a shortest path tree rooted at s. Dist[1: n]---- a distance array initialized to ∞ Dijkstra(G, s, T) T←s Dist[s] ← 0 While Cut(T) ≠ Ø v 4 (s) 1. 5 4. 9 1 v 1 1. 6 3. 7 v 5 2 v 2 1. 3 v 3 select an edge uv in Cut(T) s. t. Dist[u]+w(uv) is minimized. Dist[v] ← Dist[u] +w(uv) add vertex v and edge (u, v) into T 11/24/2020 17
Dijkstra’s Algorithm Input: weighted graph G=<V={1, …, n}, E, w>, a vertex s. Output: a shortest path tree rooted at s. Dist[1: n]---- a distance array initialized to ∞ Dijkstra(G, s, T) T←s Dist[s] ← 0 While Cut(T) ≠ Ø v 4 (s) 1. 5 4. 9 1 v 1 1. 6 3. 7 v 5 2 v 2 1. 3 v 3 select an edge uv in Cut(T) s. t. Dist[u]+w(uv) is minimized Dist[v] ← Dist[u] +w(uv) add vertex v and edge (u, v) into T 11/24/2020 18
Dijkstra’s Algorithm Input: weighted graph G=<V={1, …, n}, E, w>, a vertex s. Output: a shortest path tree rooted at s. Dist[1: n]---- a distance array initialized to ∞ Dijkstra(G, s, T) T←s Dist[s] ← 0 While Cut(T) ≠ Ø v 4 (s) 1. 5 4. 9 1 v 1 1. 6 3. 7 v 5 2 v 2 1. 3 v 3 select an edge uv in Cut(T) s. t. Dist[u]+w(uv) is minimized Dist[v] ← Dist[u] +w(uv) add vertex v and edge (u, v) into T 11/24/2020 19
Dijkstra’s Algorithm Input: weighted graph G=<V={1, …, n}, E, w>, a vertex s. Output: a shortest path tree rooted at s. Dist[1: n]---- a distance array initialized to ∞ Dijkstra(G, s, T) T←s Dist[s] ← 0 While Cut(T) ≠ Ø v 4 (s) 1. 5 4. 9 1 v 1 1. 6 3. 7 v 5 2 v 2 1. 3 v 3 select an edge uv in Cut(T) s. t. Dist[u]+w(uv) is minimized Dist[v] ← Dist[u] +w(uv) add vertex v and edge (u, v) into T 11/24/2020 20
A more detailed example 6 1 s 2 1 4 6 5 5 2 8 4 3 2 5 3 11/24/2020 21
A more detailed example 6 1 s 2 1 6 5 2 8 0 4 5 4 3 2 5 3 Dist[u] represents the length of the shortest path from s to u 11/24/2020 22
A more detailed example 1 6 1 s 2 1 6 5 2 8 0 4 5 4 3 2 5 3 Dist[u] represents the length of the shortest path from s to u 11/24/2020 23
A more detailed example 1 6 1 s 2 1 6 5 5 2 8 0 4 3 4 2 5 3 3 Dist[u] represents the length of the shortest path from s to u 11/24/2020 24
A more detailed example 7 1 6 1 s 2 1 6 5 5 2 8 0 4 3 4 2 5 3 3 Dist[u] represents the length of the shortest path from s to u 11/24/2020 25
A more detailed example 7 1 6 1 s 2 1 6 5 5 2 8 0 4 3 4 2 3 5 3 8 Dist[u] represents the length of the shortest path from s to u 11/24/2020 26
A more detailed example 7 1 6 1 s 2 1 6 5 5 2 8 4 11 0 3 4 2 3 5 3 8 Dist[u] represents the length of the shortest path from s to u 11/24/2020 27
Running time for Dijkstra’s algorithm? Without using a priority queue---Using a priority queue--- 11/24/2020 28
Running time for Dijkstra’s algorithm? Without using a priority queue---- O(n 2) Using a priority queue---O(|E| ∙ log |V|) 11/24/2020 29
Discussion on Dijkstra’s algorithm Q 1: What if the graph is directed? 11/24/2020 30
Discussion on Dijkstra’s algorithm Q 1: What if the graph is directed? A 1: Doesn’t matter. 11/24/2020 31
Discussion on Dijkstra’s algorithm Q 2: What if the graph contain negative edge weights? 11/24/2020 32
Discussion on Dijkstra’s algorithm Q 2: What if the graph contain negative edge weights? A 2: As long as those negatively-weighted edges do not form a cycle, it is fine. Otherwise, the shortest path problem might not be well-defined. 11/24/2020 33
Why Dijkstra’s algorithm works? 11/24/2020 34
Why Dijkstra’s algorithm works? We know we need to prove sth. But what to prove? 11/24/2020 35
Why Dijkstra’s algorithm works? We know we need to prove sth. But what to prove? Let Ti be the tree generated by Dijkstra after the i-th iteration (i=0, 1, , , n-1). For v ε V(Ti), let pi(v) be the path s→v in Ti. 11/24/2020 36
Why Dijkstra’s algorithm works? We know we need to prove sth. But what to prove? Let Ti be the tree generated by Dijkstra after the i-th iteration (i=0, 1, , , n-1). For v ε V(Ti), let pi(v) be the path s→v in Ti. FACT: For every vertex v ε V(Ti), pi(v) is a shortest path in G from s to v. 11/24/2020 37
Why Dijkstra’s algorithm works? We know we need to prove sth. But what to prove? Let Ti be the tree generated by Dijkstra after the i-th iteration (i=0, 1, , , n-1). For v ε V(Ti), let pi(v) be the path s→v in Ti. FACT: For every vertex v ε V(Ti), pi(v) is a shortest path in G from s to v. Proof by induction! 11/24/2020 38
Why Dijkstra’s algorithm works? We know we need to prove sth. But what to prove? Let Ti be the tree generated by Dijkstra after the i-th iteration (i=0, 1, , , n-1). For v ε V(Ti), let pi(v) be the path s→v in Ti. FACT: For every vertex v ε V(Ti), pi(v) is a shortest path in G from s to v. Proof by induction! Basis. When i=0, T 0 contains s only, s→s is a shortest path. 11/24/2020 39
Why Dijkstra’s algorithm works? We know we need to prove sth. But what to prove? Let Ti be the tree generated by Dijkstra after the i-th iteration (i=0, 1, , , n-1). For v ε V(Ti), let pi(v) be the path s→v in Ti. FACT: For every vertex v ε V(Ti), pi(v) is a shortest path in G from s to v. Proof by induction! Inductive Hypothesis. Assume that the fact is true for i≤d. Inductive Step? 11/24/2020 40
- Slides: 40