Shortest Paths CS 320, Fall 2017 Dr. Geri Georg, Instructor georg@colostate.edu 320 ShortestPaths 3
Preliminaries Weighted, directed graphs Weight function: maps edges to real numbers Shortest path weight: δ(u,v) Shortest path estimate: v.d, v V Predecessor: v., v V 320 ShortestPaths 4
Negative weight edges & cycles a 3 1 b c d 2 7 3 i h g f 1 2 3 4 9 no negative weight cycles reachable from a means well defined δ(a,v i ) a 3 1 1 e b c d 2 i 2 7 3 h g f 1 3 δ(a, d) = δ(a, e) = δ(a, f) = negative weight cycles reachable from a means δ(a,v i ) NOT well defined, so δ(a,v i ) where v i = {h, g, i, c} is defined as 4 9 1 e 320 ShortestPaths 7
Cycles in shortest paths? Short answer: No. Last slide showed no for a negative weight cycle. For a 0 weight cycle, we can remove the cycle and get a path with the same weight. Positive weight cycle: d 5 1 e f 2 320 ShortestPaths 8
Shortest path properties 1 Triangle inequality: (u,v) E, δ(s, v) δ(s, u) + w(u,v) Upper bound: v V, v.d δ(s, v), and once v.d = δ(s, v) it never changes No path: If there is no path from s to v then δ(s, v) = Convergence: If s u v is a shortest path for some u, v V and if u.d = δ(s, u) at any time prior to relaxing (u,v) then u.d = δ(s, u) ever after 320 ShortestPaths 9
Shortest path properties 2 Path relaxation: If p = v 0,v 1, v k is a shortest path from s = v 0 to v k, and we relax the edges of p in the order (v 0, v 1 ), (v 1, v 2 ), (v k 1, v k ), then v k.d = δ(s, v k ) Predecessor subgraph: Once v.d = δ(s, v) for all v V, the predecessor subgraph is a shortest paths tree rooted at s 320 ShortestPaths 10
Relaxing Check to see if we can improve the shortest path to v found so far by going through u: Yes update v.d and v. No make no changes 320 ShortestPaths 11
Questions As we work through the Bellman Ford and Dijkstra algorithms, answer these questions: Why does Bellman Ford work with negative edges? Why doesn t Dijkstra work with negative edges? 320 ShortestPaths 12
Procedures INITIALIZE SINGLE SOURCE(G, s) 1 for each vertex v G.V 2 v.d = 3 v. = NIL 4 s.d = 0 RELAX(u, v, w) 1 if v.d > u.d + w(u,v) 2 v.d = u.d + w(u,v) 3 v. = u Complexity? 320 ShortestPaths 13
Bellman Ford BELLMAN FORD(G, w, s) 1 INITIALIZE-SINGLE-SOURCE(G, s) 2 for i = 1 to G.V - 1 3 for each edge (u,v) G.E 4 RELAX(u,v,w) 5 for each edge (u,v) G.E 6 if v.d > u.d + w(u,v) 7 return FALSE 8 return TRUE?? 320 ShortestPaths 14
Bellman Ford BELLMAN FORD(G, w, s) 1 INITIALIZE-SINGLE-SOURCE(G, s) 2 for i = 1 to G.V - 1 3 for each edge (u,v) G.E 4 RELAX(u,v,w) 5 for each edge (u,v) G.E 6 if v.d > u.d + w(u,v) 7 return FALSE 8 return TRUE Complexity??? 320 ShortestPaths 15
INITIALIZE SINGLE SOURCE(G, s) for each vertex v G.V v.d = v. = NIL s.d = 0 RELAX(u, v, w) if v.d > u.d + w(u,v) v.d = u.d + w(u,v) v. = u BELLMAN FORD(G, w, s) INITIALIZE-SINGLE-SOURCE(G, s) for i = 1 to G.V - 1 for each edge (u,v) G.E RELAX(u,v,w) for each edge (u,v) G.E if v.d > u.d + w(u,v) return FALSE return TRUE 320 ShortestPaths 17
320 ShortestPaths 18
Dijkstra s Algorithm INITIALIZE SINGLE SOURCE(G, s) S = {} MinPQ = G.V while MinPQ u = EXTRACT-MIN(MinPQ) S = S {u} for each v G.Adj[u] RELAX(u,v,w) Complexity? min PQ insert min PQ extract min PQ decrease key If min PQ is an array from 1 to V? Insert, decrease key are O(1), extract min is O(V) 320 ShortestPaths 19
Dijkstra s Algorithm INITIALIZE SINGLE SOURCE(G, s) S = {} MinPQ = G.V while MinPQ u = EXTRACT-MIN(MinPQ) S = S {u} for each v G.Adj[u] RELAX(u,v,w) Complexity? min PQ insert min PQ extract min PQ decrease key If min PQ is an array from 1 to V? Insert, decrease key are O(1), extract min is O(V) If min PQ is binary min heap? Build is O(V), extract min, decrease key are O(lg V) 320 ShortestPaths 20
Dijkstra s Algorithm INITIALIZE SINGLE SOURCE(G, s) S = {} MinPQ = G.V while MinPQ u = EXTRACT-MIN(MinPQ) S = S {u} for each v G.Adj[u] RELAX(u,v,w) Complexity? min PQ insert min PQ extract min PQ decrease key If min PQ is an array from 1 to V? Insert, decrease key are O(1), extract min is O(V) If min PQ is binary min heap? Build is O(V), extract min, decrease key are O(lg V) If min PQ is a Fibonacci min heap? V extract min ops of O(lg V), E decrease key is O(1) 320 ShortestPaths 21
INITIALIZE SINGLE SOURCE(G, s) for each vertex v G.V v.d = v. = NIL s.d = 0 RELAX(u, v, w) if v.d > u.d + w(u,v) v.d = u.d + w(u,v) v. = u DIJKSTRA(G, w, s) INITIALIZE SINGLE SOURCE(G, s) S = {} MinPQ = G.V while MinPQ u = EXTRACT-MIN(MinPQ) S = S {u} for each v G.Adj[u] RELAX(u,v,w) c a 2 4 1 2 b causes re build Dijkstra s 6 d 6 h 3 1 f 4 1 1 2 g e 3 320 ShortestPaths 22
320 ShortestPaths 23
Difference Constraints Special case of linear programming Search for a feasible solution to Ax b Value in each node is δ(v 0, v i ) which is a feasible solution for x 320 ShortestPaths 24
320 ShortestPaths 25
Micro Survey 1 Can you just add to negative weight edges to make them have weights 0 so that Dijkstra s algorithm will work? If we add the absolute value of the largest negative weight (call it x) to each edge to get rid of negative weight, this won t work unless all shortest paths have the same length. As an example, look at a shortest path of 2 edges, then the total path length will be increased by 2x. But if we have another shortest path of 3 edges, then that total path length will be increased by 3x. In addition, if a longer path originally has a smaller weight, you can switch which path has the smaller weight: Consider a graph where there are 2 paths between 2 nodes, a direct edge of length 2, and an indirect route through 2 other nodes with lengths 1, 2, and 2. The second path is shorter. Now say you increase all edge weights by 2, so the direct path is now length 4, and the indirect path is length 6. The direct path is shorter now. 320 AllPairsShortestPaths 27
Micro Survey 2 What s the w in Dijkstra s algorithm? It s a function that returns the weight of the edge indicated by its arguments. What s the goal for difference constraints? To find a feasible solution for the x vector, or return false if there is no feasible solution. Recall the equation is: Ax b where A is a matrix and x and b are vectors. With the Bellman Ford algorithm, do we call edges on a negative cycle? No matter how many times we loop will the value ever change? What happens is that we use the edge values we have, and we go through, checking all the edges for V 1 times. Then we check one more time, and we find an instance of v.d > u.d + w(u,v) so we know there s a negative cycle. 320 AllPairsShortestPaths 28
Micro Survey 3 Is it true that difference constraints only work for matrices that reduce to each row containing a 1 and a 1? What really happens is that a real problem can directly be stated as a set of difference constraints. What real problems exist for difference constraints? Consider a VLSI layout problem. Constrain it to horizontal layout of a bunch of features and you measure them all from the left edge. You have to have some specified distance between them, maybe so that there s room for routing lines. In that case you can talk about the difference in the horizontal distance of the left edges for each pair of features. This yields difference constraints. And the minimum path will make for a smaller chip. Plus this algorithm tells you if there is no feasible solution to the constraints. 320 AllPairsShortestPaths 29
320 ShortestPaths 30
Image Credits seattlespt.png: http://graphserver.sourceforge.net/gallery.html critical path trainset.jpg, trainset.jpg: http://www.pmexamsmartnotes.com/how to calculate critical path float and early and late startsand finishes/ 320 ShortestPaths 32