CS138A Single Source Shortest Paths Peter Schröder
CS138A Definitions Shortest path problems weighted directed graph G=(V,E) with real valued weight function w weight of a path v 0,v 1,…,v k is sum of edge costs shortest path negative weight edges negative weight cycles representation of shortest path
CS138A Shortest Paths Predecessor subgraph G p =(V p,E p ) contains all vertices reachable from source and edges induced by predecessor relationship eventually G p will be shortest paths tree shortest paths are not necessarily unique nor are the shortest path trees
CS138A Relaxation Finding shortest paths by relaxation Lemma 1: optimal substructure property; any subpath of a shortest path is itself a shortest path Corollary 2: suppose a shortest path from s to v can decomposed into a path from s to u and the edge (u,v) then the cost of the shortest path from s to v is the sum of the cost of the shortest path from s to u plus w(u,v)
CS138A Relaxation Shortest path properties Lemma 3: Let G=(V,E) be a weighted directed graph with weight function w and source s. Then for all edges (u,v) in E we have all algorithms will use relaxation; each vertex contains a field d[] containing an upper bound for the shortest path length; this will repeatedly be reduced
CS138A Shortest Paths Algorithm 1. Initialize-Single-Source(G,s) 2. for( v in V[G] ) 3. d[v] = infinity; p[v] = nil; 4. d[s] = 0 1. Relax(u,v,w) 2. if( d[v] > d[u] + w(u,v) ) 3. d[v] = d[u] + w(u,v) 4. p[v] = u;
CS138A Algorithms Main differences order in which edges are relaxed number of times each edge is relaxed Properties Lemma 4: Let G=(V,E) be weighted, directed graph with weight function w; then immediately after relaxing (u,v) in E we have
CS138A Relaxation Properties Lemma 5: Let G=(V,E) be a weighted, directed graph with weight function w and source s; after intialization for all v in V and this invariant is maintained during relaxation. Moreover, once d[v] achieves its lower bound it never changes again Corollary 6: suppose a vertex is not reachable; then the above still holds
CS138A Relaxation Properties Lemma 7: Let G=(V,E) bawdgwwfw and source s; let s to u to v be a shortest path in G for some vertices u,v; after initialization suppose a sequence of relaxation steps including Relax(u,v,w) is run; if any time prior to the call then anytime thereafter
CS138A Dijkstra’s Algorithm Single source shortest paths algorithm for a directed, weighted graph G=(V,E) with all edge weights non-negative 1. Dijkstra(G,w,s) 2. Initialize-Single-Source(G,s) 3. S = {}; Q = V[G]; 4. while( !Q.empty() ) 5. u = Q.extractMin(); S = S + {u} 6. for( v in Adj[u] ) 7. Relax(u,v,w)
CS138A Analysis Complexity Correctness greedy strategy… optimal substructure property already established Theorem 10: when running Dijkstra on a weighted directed graph with non-negative weight function w from s then at termination for all u in V
CS138A Bellman-Ford Single source shortest paths algorithm allows for negative weight edges but of course no negative weight cycles 1. Bellmann-Ford(G,s) 2. Initialize-Single-Source(G,s) 3. for( i = 1 to |V[G]|-1 ) 4. for( (u,v) in E[G] ) 5. Relax(u,v,w) 6. for( (u,v) in E[G] ) 7. if( d[v] > d[u] + w(u,v) ) 8. return false 9. return true
CS138A Analysis Complexity Correctness Lemma 12: Let G=(V,E) be a weighted directed graph with source s and weight function w and assume it contains no negative weight cycles reachable from s. Then upon termination of Bellman-Ford we have for all vertices u reachable from s
CS138A Analysis Correctness Theorem 14: Let Bellman-Ford be run on a wdgwssawfw and no negative weight cycles reachable from s then the algorithm returns true and for all u in V and the predecessor subgraph G p is a shortest paths tree rooted at s. If G contains a negative weight cycles reachable from s it will return false.
CS138A Predecessor Graphs Still have to prove things about predecessor trees… Lemma 8: Let G=(V,E) be awdgwssawfw with no negative weight cycles reachable from s. Then after the graph is initialized the predecessor subgraph G p forms a rooted (at s) tree and any sequence of relaxation steps maintains this invariant.
CS138A Predecessor Subgraphs Correctness Lemma 9: Let G=(V,E) be awdgwssawfw with no negative weight cycles reachable from s. After initialization and any relaxation sequence which produces for all u in V, then the predecessor subgraph G p is a shortest paths tree rooted at s.
CS138A All Pairs Shortest paths between all pairs of vertices could run single source algorithms |V| times O(VE ln V) for Dijkstra O(V 2 E) for Bellman-Ford which can be very high… mostly use adjacency matrix representation yield predecessor matrices Example: dynamic programming solution
CS138A Approach Recall dynamic programming characterize the structure of an optimal solution recursively define the value of an optimal solution compute the solution bottom up Structure optimal substructure property (Lemma 1)
CS138A All Pairs 1. Extend-Shortest-Paths(D,W) 2. n = rows[D] 3. for( i = 1 to n ) 4. for( j = 1 to n ) 5. d’ ij = infinity 6. for( k = 1 to n ) 7. d’ = min( d’ ij, d ik + w kj ) 8. return D’