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)

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
CS138A Single Source Shortest Paths Peter Schröder.
Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
Introduction to Algorithms 6.046J/18.401J/SMA5503
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
CPSC 411, Fall 2008: Set 9 1 CPSC 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Fall 2008.
CS420 lecture twelve Shortest Paths wim bohm cs csu.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Shortest Path Problems
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1.1 Data Structure and Algorithm Lecture 11 Application of BFS  Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
BellmanFord. BellmanFord(G,w,s) 1 InitializeSingleSource(G,s) 2 for i 1 to |V[G]| do for each (u,v) E[G] 4 do Relax(u,v,w) 5 for each edge (u,v)
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Path Algorithms
Shortest Paths Definitions Single Source Algorithms
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Analysis of Algorithms CS 477/677 Shortest Paths Instructor: George Bebis Chapter 24.
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
CPSC 311, Fall CPSC 311 Analysis of Algorithms More Graph Algorithms Prof. Jennifer Welch Fall 2009.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Dijkstra's algorithm.
Topological Sorting and Least-cost Path Algorithms.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems.
1 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: –Road map is a weighted.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj
Introduction to Algorithms Jiafen Liu Sept
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
1 Introduction to Algorithms L ECTURE 17 (Chap. 24) Shortest paths I 24.2 Single-source shortest paths 24.3 Dijkstra’s algorithm Edsger Wybe Dijkstra
Lecture 13 Algorithm Analysis
1 Weighted Graphs. 2 Outline (single-source) shortest path –Dijkstra (Section 4.4) –Bellman-Ford (Section 4.6) (all-pairs) shortest path –Floyd-Warshall.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Shortest paths Given: A single source vertex (given s) in a weighted, directed graph. Want to compute a shortest path for each possible destination. (Single.
Single-Source Shortest Paths (25/24) HW: 25-2 and 25-3 p. 546/24-2 and 24-3 p.615 Given a graph G=(V,E) and w: E   weight of is w(p) =  w(v[i],v[i+1])
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
Algorithms and Data Structures Lecture XIII
Minimum Spanning Trees
Advanced Algorithms Analysis and Design
Lecture 11 Topics Application of BFS Shortest Path
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Shortest Path Problems
Advanced Algorithms Analysis and Design
Presentation transcript:

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 S

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

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

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 What is the shortest path from a to d?  path a-c-e-d = = -11  path a-c-e-d-a-c-e-d = ( )= -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 - 

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.

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

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.

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

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.

d a b c e 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   Q x iteration 2: Q = Q – {b} a b c d e d[v]  11 Q x x iteration 3: Q = Q – {c} a b c d e d[v] 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)

d a b c e S 8 iteration 4: Q = Q – {e} a b c d e d[v] Q iteration 5: Q = Q – {d} a b c d e d[v] 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)

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)

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)

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

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.

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.

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.

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.

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| 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.

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

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.