Presentation is loading. Please wait.

Presentation is loading. Please wait.

The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)

Similar presentations


Presentation on theme: "The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)"— Presentation transcript:

1 The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest) path from s to every other node in V (sometimes we only want the cost of these paths, sometimes we want the path itself because path determines efficient reachability). Notice here we are concerned about the entire path length. Single-Source Shortest Paths (Ch. 24) s  b = 2 s  c = 5 s  d = 6 s  e = 5 s  f = 3 s  g =  s  h = 7 abc d f e gh 2 5 3 6 1 4 2 2 3 S

2 Weights in SSSP algorithms can include distances, times, hops, cost, etc. These algorithms are used in many routing applications. Note: BFS finds the shortest paths for the special case when all edge weights are 1. Running time = O(V + E) The result of a SSSP algorithm can be viewed as a tree rooted at s, containing a shortest (wrt weight) path from s to all other nodes. Single-Source Shortest Paths

3 Some graphs that are inputs to an SSSP algorithm may contain negative edge weights. How might such edges occur? Suppose vertices in G represent cities and the weights of edges in G represent how much money it costs to go from one city to another. If someone is willing to pay us to go from, say JFK to ORD, then the “cost” of the edge (JFK, ORD) would be negative. We will see an algorithm that is correct if negative-weight edges can occur and one that is not correct in this situation. Negative-weight edges

4 Some graphs may have negative-weight cycles and these are a problem for SSSP algorithms because they indicate no truly shortest path can be found. Negative-weight cycles a b c d e -12 10 3 6 -2 9 2 4 6 What is the shortest path from a to d?  path a-c-e-d = -12+3-2 = -11  path a-c-e-d-a-c-e-d = -12+3-2+(10-12+3-2)= -12 S 8 To avoid this problem, SSSP algorithms require that the graph has no negative weight cycles, otherwise a solution does not exist. If we keep going around the cycle (d-a- c-e-d), we keep shortening the weight of the path. So the shortest path has weight - 

5 Question: Can a shortest path contain a positive-weight cycle? Single-Source Shortest Paths No. Suppose we have a shortest path p =  v 0, v 1,..., v k  and c =  v i, v i+1,..., v j  is a positive-weight cycle on p, so that v i = v j and w(c) > 0. Then the path p' obtained from splicing out c is: p' =  v 0, v 1,..., v i, v j+1, v j+2,...v k  and has w(p') = w(p) – w(c) < w(p). So p can’t be a shortest path. Therefore, we can assume, wlog, that shortest paths have no cycles.

6 Chapter 24 presents 3 different SSSP algorithms, we will study two of them: 1.Dijkstra's algorithm: Finds SSSPs in weighted, directed graph when all edge weights are non-negative. 2.Bellman-Ford Algorithm: General case in which edge weights may be negative. Detects negative-weight cycles. Single-Source Shortest Path Algorithms

7 Procedures used by SSSP Algorithms Relax (u,v,w) 1. if d[v] > d[u] + w(u,v) 2. d[v] = d[u] + w(u,v) 3. π [v] = u Initialize-Single-Source (G,s) 1. for each vertex v  G 2. d[v] =  3. π [v] = NIL 4. d[s] = 0 Attribute d[v] is an upper bound on the weight of the shortest path from source s to v, or a "shortest path estimate" Relax procedure lowers shortest path estimates and changes predecessors. Decreases the value of a key.

8 Dijkstra’s SSSP Algorithm Algorithm SSSP-Dijkstra (G, s) 1. Initialize-Single-Source(G, s) 2. S =  3. Q = V[G] // all nodes enqueued 4. while not Q.isempty() 5. u = Extract-Min(Q) 6. S = S  {u} 7. for each vertex v  Adj[u] 8. Relax(u,v,w) Tree nodes are extracted from Q (and added to S, the set of shortest paths). Fringe nodes are nodes in Q with d[v] <  Unseen nodes are nodes in Q with d[v] =  G is graph representation and s is source node. S is spanning tree

9 Dijkstra’s SSSP Algorithm The labeling and mechanics of Dijkstra's algorithm are like Prim's. Both construct an expanding subtree of vertices by selecting the next vertex from the priority queue of the remaining vertices. However, the two algorithms solve different problems Dijkstra's algorithm compares entire, multi-hop path lengths and therefore must incrementally add edge weights, while Prim's algorithm compares the edge weights of only 1-hop paths, with no summation along a path.

10 d a b c e 12 10 3 6 2 9 2 4 6 S 8 iteration 0: before line 4 a b c d e d[v] 0     Q d[v] iteration 1: Q = Q – {a} a b c d e 0 2 12   Q x iteration 2: Q = Q – {b} a b c d e d[v] 0 2 10  11 Q x x iteration 3: Q = Q – {c} a b c d e d[v] 0 2 10 16 11 Q x xx Algorithm SSSP-Dijkstra (G, s) 1. Initialize-Single-Source(G,s) 2. S =  3. Q = V[G] 4. while Q ≠  5. u = Extract-Min(Q) 6. S = S  {u} 7. for each vertex v  Adj[u] 8. Relax(u,v,w)

11 d a b c e 12 10 3 6 2 9 2 4 6 S 8 iteration 4: Q = Q – {e} a b c d e d[v] 0 2 10 13 11 Q iteration 5: Q = Q – {d} a b c d e d[v] 0 2 10 13 11 Q DONE x x x x x x x x x Algorithm SSSP-Dijkstra (G, s) 1. Initialize-Single-Source(G,s) 2. S =  3. Q = V[G] 4. while Q ≠  5. u = Extract-Min(Q) 6. S = S  {u} 7. for each vertex v  Adj[u] 8. Relax(u,v,w)

12 Running Time of Dijkstra’s SSSP Alg Steps 1-3: O(V) time Steps 4-8: V iterations Suppose Extract-Min takes O(X Q ) time Total: O(VX Q + EY Q ) Steps 7-8: E iterations overall (looks at each edge once) Suppose Relax takes O(Y Q ) time. Algorithm SSSP-Dijkstra (G, s) 1. Initialize-Single-Source(G,s) 2. S =  3. Q = V[G] 4. while Q ≠  5. u = Extract-Min(Q) 6. S = S  {u} 7. for each vertex v  Adj[u] 8. Relax(u,v,w)

13 Running Time of Dijkstra’s SSSP Alg If G is dense (i.e.,  (V 2 ) edges): Asymptotically speaking, there is no point in being clever about Extract-Min. Store each d[v] in the vth entry of an array. Each insert and decrease-key takes O(1) time. Extract-Min takes O(V) time (why?) Total time: O(V 2 + E) = O(V 2 ) If G is sparse (i.e., o(V 2 ) edges): Try to minimize Extract-Min, use binary heap (heapsort) so time for Extract-Min & Decrease-Key = O(lgV) Total time: O(VlgV + ElgV) = O((V + E) lgV)

14 Correctness of Dijkstra’s SSSP Alg Proof Idea: The proof argues that the shortest path to a node must include only nodes that have already been extracted from Q and added to the set S. It uses contradiction and the fact that there are no negative edge weights allowed to show that, if there is a shorter path to a particular node v that does not include all nodes in S, then other vertices along that path would have been extracted from Q (and added to S) before v. Theorem: Dijkstra's algorithm finds shortest paths from a designated source vertex to all other vertices in G. s v w SSSP S

15 Correctness of Dijkstra’s SSSP Alg Proof: We use induction to show that at each iteration i of the while- loop, when u is extracted from the queue, d[u] is the length of a shortest path from s to u. Basis: i = 1, u = s, d[s] = 0. The length of a shortest path from s to s is 0, so the basis is true. Inductive Hypothesis (IHOP): Assume the theorem is true up to the ith iteration of the while loop. Inductive Step: Suppose that v is extracted from Q in the ith iteration and that for all vertices j previously deleted from Q, d[j] is the length of a shortest path from s to j (by the IHOP). Theorem: Dijkstra's algorithm finds shortest paths from a designated source vertex to all other vertices in G.

16 Correctness of Dijkstra’s SSSP Alg Proof (cont): Claim: if there is a path from s to a node k whose length d[k] is less than d[v], then k was previously extracted from Q. Suppose, in contradiction, that k is still in Q when v is extracted but that v is reached via a shortest path that includes k. Let P be a shortest path from s to k, let k' be the vertex nearest to s on P that is still in Q, and let k'' be the predecessor of k' on P. Then k'' is not in Q, so by the IHOP, d[k''] is the length of a shortest path from s to k''. Now we have d[k'] ≤ d[k''] + w(k'’,k ' ) ≤ length of P < d[v]. Dijkstra's algorithm finds shortest paths from a designated source vertex to all other vertices in G.

17 Correctness of Dijkstra’s SSSP Alg Proof (cont): d[k'] ≤ d[k''] + w(k'’,k ' ) ≤ length of P < d[v]. This inequality shows that v is not the vertex in Q with minimum d value at iteration i (d[k'] is smaller). So v would not be the vertex extracted in iteration i, a contradiction. Thus, if there is a path from s to a vertex k whose length is less than d[v], then k was previously extracted from Q. If there were a path from s to v whose length is less than d[v], v would already have been extracted from Q. Therefore, every path from s to v has length at least d[v]. Dijkstra's algorithm finds shortest paths from a designated source vertex to all other vertices in G.

18 Bellman-Ford SSSP Algorithm Computes single-source shortest paths even when some edges have negative weight. Can detect if there are any negative-weight cycles in the graph. The algorithm has 2 parts: Part 1: Computing shortest paths tree:  |V| - 1 iterations.  Iteration i computes the shortest path from s using paths of up to i edges. Part 2: Checking for negative-weight cycles.

19 Bellman-Ford SSSP Algorithm Bellman-Ford (G, w, s) // Part I – find shortest paths 1. Initialize-Single-Source(G,s) 2. for i = 1 to |V| - 1 3. for each edge (u, v)  E 4. Relax(u,v,w) // Part II – check for negative weight cycles 5. for each edge (u, v)  E 6. if d[v] > d[u] + wt(u, v) 7. return false 8. return true Boolean value returned is false if there is a negative weight cycle in G and true otherwise.

20 Correctness of Bellman-Ford Algorithm Theorem Suppose there are no negative-weight cycles in G. After |V| - 1 iterations of the for loop, d[v] =  (s,v) for all vertices v that are reachable from s. Proof: -If v is reachable from s, then there is a path from s to v - Say s = u 0, u 1, u 2,..., u k = v, where k < |V|. -There are k edges in this path. -After the first iteration, u 0, u 1 is a shortest path; after the second pass, u 0, u 1, u 2 is a shortest path; after k passes, u 0, u 1, u 2,..., u k is a shortest path. Let  (s,v) be the actual shortest path distance from s to v

21 Complexity of Bellman-Ford Algorithm Initialization = O(|V|) decrease-key (in Relax) is called (|V| - 1)  |E| times Test for negative-weight cycle = O(|E|) Total: O(|V||E|) -- so more expensive than Dijkstra's, but also more general, since it works for graphs with negative edge weights.


Download ppt "The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)"

Similar presentations


Ads by Google