CSE 431/531: Analysis of Algorithms Lectures 25-28: Shortest Paths Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo Spring 2016 MoWeFr 3:00-3:50pm Knox 110 Algorithms for Single Source Shortest Paths Single Source Shortest Paths Input: (directed or undirected) graph G = (V, E), w : E → R (can be negative) s∈V Output: shortest paths from s to every vertex v ∈ V Properties Algorithms DAG Dynamic Programming w non-negative Dijkstra Bellman-Ford ∗ n = |V | and m = |E| Time∗ O(m + n) O(m + n log n) O(nm) Outline 1 Shortest Paths in DAG 2 Dijkstra’s Algorithm 3 Bellman-Ford Algorithm 4 All-Pair Shortest Paths and Floyd-Warshall Shortest Paths in DAG Single Source Shortest Paths Input: (directed or undirected) graph G = (V, E), w : E → R (can be negative) s∈V Output: shortest paths from s to every vertex v ∈ V Properties Algorithms DAG Dynamic Programming w non-negative Dijkstra Bellman-Ford ∗ n = |V | and m = |E| Time∗ O(m + n) O(m + n log n) O(nm) Shortest Paths in DAG Input: directed acyclic graph G = (V, E) and w : E → R. Assume V = {1, 2, 3 · · · , n} is topologically sorted: if (i, j) ∈ E, then i < j Output: the shortest path from 1 to i, for every i ∈ V 2 2 5 6 8 1 1 9 3 8 4 3 5 9 1 6 5 2 1 7 8 Shortest Paths in DAG Need a linked-list of incoming edges for every vertex i Shortest Paths in DAG 1 f [1] ← 0 2 for i ← 2 to n do 3 f [i] ← ∞ 4 for each j < i s.t. (j, i) ∈ E 5 if f [j] + w(j, i) < f [i] 6 f [i] ← f [j] + w(j, i) 7 π(i) ← j Print-Path(t) 1 2 3 4 5 if (t = 1) print(1) return Print-Path(π(t)) print(“,”, t) Example 2 0 1 1 2 9 2 3 8 4 10 1 5 6 8 7 5 9 8 1 6 5 9 1 7 3 2 11 8 Outline 1 Shortest Paths in DAG 2 Dijkstra’s Algorithm 3 Bellman-Ford Algorithm 4 All-Pair Shortest Paths and Floyd-Warshall Dijkstra’s Algorithm Single Source Shortest Paths Input: (directed or undirected) graph G = (V, E), w : E → R (can be negative) s∈V Output: shortest paths from s to every vertex v ∈ V Properties Algorithms DAG Dynamic Programming w non-negative Dijkstra Bellman-Ford ∗ n = |V | and m = |E| Time∗ O(m + n) O(m + n log n) O(nm) Q: How to compute shortest paths from s when all edges have length 1? A: Breadth first search from source s Computing Distances via Virtual BFS 1 replace (i, j) of length w(i, j) with a path w(i, j) edges of length 1, 2 run BFS virtually 3 d(v) = distance from s to v = time v visited by BFS Computing Distances via Virtual BFS 1 S ← {s}, d(s) ← 0 2 while |S| ≤ n 3 find a v ∈ / S that minimizes min u∈S:(u,v)∈E 4 5 {d(u) + w(u, v)} S ← S ∪ {v} d(v) ← minu∈S:(u,v)∈E {d(u) + w(u, v)} Virtual BFS: Example 0 s 2 2 9 4 c 4 3 5 5 a 6 4 7 d Time 10 4 b 3 e 10 Dijkstra’s Algorithm Dijkstra(G, w, s) 1 2 3 4 5 6 7 8 9 S ← ∅, d(s) ← 0 and d(v) ← ∞ for every v ∈ V \ {s} while S 6= V do u ← vertex in V \ S with the minimum d(u) add u to S for each v ∈ V \ S such that (u, v) ∈ E if d(u) + w(u, v) < d(v) then d(v) ← d(u) + w(u, v) π(v) ← u return (d, π) Running time = O(n2 ) s 3 3 16 12 1 5 4 1 13 4 7 2 10 u 15 Improving Running Time for Sparse Graphs 5 ptDijkstra(G, w, s) 1 S ← ∅, d(s) ← 0 and d(v) ← ∞ for every v ∈ V \ {s} 2 Q ← priority queue for V , with keys being d values 3 while S 6= V do 4 u ← vertex in V \ S with the minimum d(u) 5 add u to S 6 for each v ∈ V \ S such that (u, v) ∈ E 7 if d(u) + w(u, v) < d(v) then 8 d(v) ← d(u) + w(u, v) 9 π(v) ← u 10 update(Q, v) Improved Running Time Running time: n(time for exact-min) + m(time for update) Priority-Queue extract-min update Heap O(log n) O(log n) Fibonacci Heap O(log n) O(1) Time O(m log n) O(n log n + m) Outline 1 Shortest Paths in DAG 2 Dijkstra’s Algorithm 3 Bellman-Ford Algorithm 4 All-Pair Shortest Paths and Floyd-Warshall Bellman-Ford Algorithm Single Source Shortest Paths Input: (directed or undirected) graph G = (V, E), w : E → R (can be negative) s∈V Output: shortest paths from s to every vertex v ∈ V Properties Algorithms DAG Dynamic Programming w non-negative Dijkstra Bellman-Ford ∗ n = |V | and m = |E| Time∗ O(m + n) O(m + n log n) O(nm) Bellman-Ford Algorithm Bellman-Ford(G, w, s) 1 2 3 4 5 6 7 8 f (s) ← 0 and f (v) ← ∞ for every v ∈ V \ {s} repeat updated ← false for each (u, v) ∈ E if f (u) + w(u, v) < f (v) f (v) ← f (u) + w(u, v) updated ← true until not updated Bellman-Ford Algorithm Bellman-Ford(G, w, s) 1 2 3 4 5 6 7 8 9 10 f (s) ← 0 and f (v) ← ∞ for every v ∈ V \ {s} for i ← 1 to n updated ← false for each (u, v) ∈ E if f (u) + w(u, v) < f (v) f (v) ← f (u) + w(u, v) π[v] ← u updated ← true if not updated, then break if updated, then output “negative cycle exists” π[v]: last vertex before v in shortest path from s to v. Outline 1 Shortest Paths in DAG 2 Dijkstra’s Algorithm 3 Bellman-Ford Algorithm 4 All-Pair Shortest Paths and Floyd-Warshall All-Pair Shortest Paths All Pair Shortest Paths Input: (directed or undirected) graph G = (V, E), w : E → R (can be negative) Output: shortest paths from u to v for every u, v ∈ V Floyd-Warshall Assume V = {1, 2, 3, · · · , n}. Define i=j 0 W (i, j) = w(i, j) i 6= j and (i, j) ∈ E ∞ i 6= j and (i, j) ∈ /E 5 ptFloyd-Warshall(G, w) 1 f ←W 2 for k ← 1 to n 3 for i ← 1 to n 4 for j ← 1 to n 5 f (i, j) ← min{f (i, j), f (i, k) + f (k, j)} 5 ptFloyd-Warshall(G, w) 1 f ←W 2 for k ← 1 to n 3 for i ← 1 to n 4 for j ← 1 to n 5 f (i, j) ← min{f (i, j), f (i, k) + f (k, j)} Lemma Assume there are no negative cycles in G. After the k-th iteration of Floyd-Warshall algorithm, for every pair of vertices i, j, f (i, j) is at most the length of the shortest paths from i to j that only uses vertices in {1, 2, 3, · · · , k} as intermediate vertices.