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.

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.
Single-Source Shortest- paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
Data Structures and Algorithms Graphs Single-Source Shortest Paths (Dijkstra’s Algorithm) PLSD210(ii)
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
Length of a Path The weight (length) of a path is the sum of the weights of its edges. adcbe Path p: Edge weights: w(a, b) = 7, w(b, c) = 2, w(c,
Introduction to Algorithms 6.046J/18.401J/SMA5503
CS420 lecture twelve Shortest Paths wim bohm cs csu.
CSE 780 Algorithms Advanced Algorithms Shortest path Shortest path tree Relaxation Bellman-Ford Alg.
Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
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)
Shortest Paths Definitions Single Source Algorithms
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.
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.
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Topological Sorting and Least-cost Path Algorithms.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
CISC 235: Topic 11 Shortest Paths Algorithms. CISC 235 Topic 112 Outline Single-Source Shortest Paths Algorithm for Unweighted Graphs Algorithm for Weighted,
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
CS223 Advanced Data Structures and Algorithms 1 The Bellman-Ford Shortest Path Algorithm Neil Tang 03/11/2010.
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.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
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.
Introduction to Algorithms Jiafen Liu Sept
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)
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.
Length of a Path The weight (length) of a path is the sum of the weights of its edges. adcbe Path p: Edge weights: w(a, b) = 7, w(b, c) = 2, w(c,
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])
Algorithms and Data Structures Lecture XIII
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Basic Graph Algorithms
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Advanced Algorithms Analysis and Design
Single-Source Shortest Path & Minimum Spanning Trees
Advanced Algorithms Analysis and Design
Presentation transcript:

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. Want to compute a shortest path for each possible destination.  Similar to BFS. We will assume either  no negative-weight edges, or  no reachable negative-weight cycles. Algorithm will compute a shortest-path tree.  Similar to BFS tree.

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 2 Outline General Lemmas and Theorems.  CLRS now does this last. We’ll still do it first. Bellman-Ford algorithm. DAG algorithm. Dijkstra’s algorithm. We will skip Section 24.4.

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 3 Corollary: Let p = SP from s to v, where p = s u  v. Then, δ(s, v) = δ(s, u) + w(u, v). Corollary: Let p = SP from s to v, where p = s u  v. Then, δ(s, v) = δ(s, u) + w(u, v). General Results (Relaxation) Lemma 24.1: Let p = ‹v 1, v 2, …, v k › be a SP from v 1 to v k. Then, p ij = ‹v i, v i+1, …, v j › is a SP from v i to v j, where 1  i  j  k. Lemma 24.1: Let p = ‹v 1, v 2, …, v k › be a SP from v 1 to v k. Then, p ij = ‹v i, v i+1, …, v j › is a SP from v i to v j, where 1  i  j  k. So, we have the optimal-substructure property. Bellman-Ford’s algorithm uses dynamic programming. Dijkstra’s algorithm uses the greedy approach. Let δ(u, v) = weight of SP from u to v. Lemma 24.10: Let s  V. For all edges (u,v)  E, we have δ(s, v)  δ(s, u) + w(u,v). Lemma 24.10: Let s  V. For all edges (u,v)  E, we have δ(s, v)  δ(s, u) + w(u,v).

Jim Anderson Lemma 24.1 holds because one edge gives the shortest path, so the other edges must give sums that are at least as large. Comp 122, Fall 2003 Single-source SPs - 4

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 5 Relaxation Initialize(G, s) for each v  V[G] do d[v] :=  ;  [v] := NIL od; d[s] := 0 Initialize(G, s) for each v  V[G] do d[v] :=  ;  [v] := NIL od; d[s] := 0 Relax(u, v, w) if d[v] > d[u] + w(u, v) then d[v] := d[u] + w(u, v);  [v] := u fi Relax(u, v, w) if d[v] > d[u] + w(u, v) then d[v] := d[u] + w(u, v);  [v] := u fi Algorithms keep track of d[v],  [v]. Initialized as follows: These values are changed when an edge (u, v) is relaxed:

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 6 Properties of Relaxation d[v], if not , is the length of some path from s to v. d[v] either stays the same or decreases with time Therefore, if d[v] =  (s, v) at any time, this holds thereafter Note that d[v]   (s, v) always After i iterations of relaxing on all (u,v), if the shortest path to v has i edges, then d[v] =  (s, v).

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 7 Properties of Relaxation Consider any algorithm in which d[v], and  [v] are first initialized by calling Initialize(G, s) [s is the source], and are only changed by calling Relax. We have: Lemma 24.11: (  v:: d[v]   (s, v)) is an invariant. Implies d[v] doesn’t change once d[v] =  (s, v). Proof: Initialize(G, s) establishes invariant. If call to Relax(u, v, w) changes d[v], then it establishes: d[v] = d[u] + w(u, v)   (s, u) + w(u, v), invariant holds before call.   (s, v), by Lemma Corollary 24.12: If there is no path from s to v, then d[v] = δ(s, v) =  is an invariant. Corollary 24.12: If there is no path from s to v, then d[v] = δ(s, v) =  is an invariant.

Jim Anderson For lemma 24.11, note that initialization makes the invariant true at the beginning. Comp 122, Fall 2003 Single-source SPs - 8

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 9 More Properties Lemma 24.14: Let p = SP from s to v, where p = s u  v. If d[u] = δ(s, u) holds at any time prior to calling Relax(u, v, w), then d[v] = δ(s, v) holds at all times after the call. Lemma 24.14: Let p = SP from s to v, where p = s u  v. If d[u] = δ(s, u) holds at any time prior to calling Relax(u, v, w), then d[v] = δ(s, v) holds at all times after the call. Proof: After the call we have: d[v]  d[u] + w(u, v), by Lemma =  (s, u) + w(u, v), d[u] =  (s, u) holds. =  (s, v), by corollary to Lemma By Lemma 24.11, d[v]  δ(s, v), so d[v] = δ(s, v). Lemma 24.13: Immediately after relaxing edge (u, v) by calling Relax(u, v, w), we have d[v]  d[u] + w(u, v). Lemma 24.13: Immediately after relaxing edge (u, v) by calling Relax(u, v, w), we have d[v]  d[u] + w(u, v).

Jim Anderson Lemma follows simply from the structure of Relax. Lemma shows that the shortest path will be found one vertex at a time, if not faster. Thus after a number of iterations of Relax equal to V(G) - 1, all shortest paths will be found. Comp 122, Fall 2003 Single-source SPs - 10

Jim Anderson Bellman-Ford returns a compact representation of the set of shortest paths from s to all other vertices in the graph reachable from s. This is contained in the predecessor subgraph. Comp 122, Fall 2003 Single-source SPs - 11

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 12 Predecessor Subgraph Lemma 24.16: Assume given graph G has no negative-weight cycles reachable from s. Let G  = predecessor subgraph. G  is always a tree with root s (i.e., this property is an invariant). Lemma 24.16: Assume given graph G has no negative-weight cycles reachable from s. Let G  = predecessor subgraph. G  is always a tree with root s (i.e., this property is an invariant). Proof: Two proof obligations: (1) G  is acyclic. (2) There exists a unique path from source s to each vertex in V . Proof of (1): Suppose there exists a cycle c = ‹v 0, v 1, …, v k ›, where v 0 = v k. We have  [v i ] = v i-1 for i = 1, 2, …, k. Assume relaxation of (v k-1, v k ) created the cycle. We show cycle has a negative weight. Note: Cycle must be reachable from s. (Why?)

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 13 Proof of (1) (Continued) Before call to Relax(v k-1, v k, w):  [v i ] = v i-1 for i = 1, …, k–1. Implies d[v i ] was last updated by “d[v i ] := d[v i-1 ] + w(v i-1, v i )” for i = 1, …, k–1. [Because Relax updates .] Implies d[v i ]  d[v i-1 ] + w(v i-1, v i ) for i = 1, …, k–1. [Lemma 24.13] Because  [v k ] is changed by call, d[v k ] > d[v k-1 ] + w(v k-1, v k ). Thus,

Jim Anderson Comment on Proof d[v i ]  d[v i-1 ] + w(v i-1, v i ) for i = 1, …, k–1 because when Relax(v i-1, v i, w) was called, there was an equality, and d[v i-1 ] may have gotten smaller by further calls to Relax. d[v k ] > d[v k-1 ] + w(v k-1, v k ) before the last call to Relax because that last call changed d[v k ]. Comp 122, Fall 2003 Single-source SPs - 14

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 15 Proof of (2) Proof of (2): (  v: v  V  :: (  path from s to v)) is an invariant. So, for any v in V ,  at least 1 path from s to v. Show  1 path. Assume 2 paths. su y x zv impossible!

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 16 Lemma Lemma 24.17: Same conditions as before. Call Initialize & repeatedly call Relax until d[v] = δ(s, v) for all v in V. Then, G  is a shortest-path tree rooted at s. Lemma 24.17: Same conditions as before. Call Initialize & repeatedly call Relax until d[v] = δ(s, v) for all v in V. Then, G  is a shortest-path tree rooted at s. Proof: Key Proof Obligation: For all v in V , the unique simple path p from s to v in G  (path exists by Lemma 24.16) is a shortest path from s to v in G. Let p = ‹v 0, v 1, …, v k ›, where v 0 = s and v k = v. We have d[v i ] = δ(s, v i ) d[v i ]  d[v i-1 ] + w(v i-1, v i ) (reasoning as before) Implies w(v i-1, v i )  δ(s, v i ) – δ(s, v i-1 ).

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 17 Proof (Continued) So, equality holds and p is a shortest path.

Jim Anderson And note that this shortest path tree will be found after V(G) - 1 iterations of Relax. Comp 122, Fall 2003 Single-source SPs - 18

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 19 Bellman-Ford Algorithm Can have negative-weight edges. Will “detect” reachable negative-weight cycles. Initialize(G, s); for i := 1 to |V[G]| –1 do for each (u, v) in E[G] do Relax(u, v, w) od od; for each (u, v) in E[G] do if d[v] > d[u] + w(u, v) then return false fi od; return true Initialize(G, s); for i := 1 to |V[G]| –1 do for each (u, v) in E[G] do Relax(u, v, w) od od; for each (u, v) in E[G] do if d[v] > d[u] + w(u, v) then return false fi od; return true Time Complexity is O(VE).

Jim Anderson So if Bellman-Ford has not converged after V(G) - 1 iterations, then there cannot be a shortest path tree, so there must be a negative weight cycle. Comp 122, Fall 2003 Single-source SPs - 20

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 21 Example 0    z uv x y 6 5 – –2 –4 2

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 22 Example 0  7  6 z uv x y 6 5 – –2 –4 2

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 23 Example z uv x y 6 5 – –2 –4 2

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 24 Example z uv x y 6 5 – –2 –4 2

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 25 Example z uv x y 6 5 – –2 –4 2

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 26 Another Look Note: This is essentially dynamic programming. Let d(i, j) = cost of the shortest path from s to i that is at most j hops. d(i, j) = 0 if i = s  j = 0  if i  s  j = 0 min({d(k, j–1) + w(k, i): i  Adj(k)}  {d(i, j–1)}) if j > 0 zuvxy  106  7  –2 j i

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 27 Lemma 24.2 Lemma 24.2: Assuming no negative-weight cycles reachable from s, d[v] =  (s, v) holds upon termination for all vertices v reachable from s. Lemma 24.2: Assuming no negative-weight cycles reachable from s, d[v] =  (s, v) holds upon termination for all vertices v reachable from s. Proof: Consider a SP p, where p = ‹v 0, v 1, …, v k ›, where v 0 = s and v k = v. Assume k  |V| – 1, otherwise p has a cycle. Claim: d[v i ] =  (s, v i ) holds after the i th pass over edges. Proof follows by induction on i. By Lemma 24.11, once d[v i ] =  (s, v i ) holds, it continues to hold.

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 28 Correctness Claim: Algorithm returns the correct value. (Part of Theorem Other parts of the theorem follow easily from earlier results.) Case 1: There is no reachable negative-weight cycle. Upon termination, we have for all (u, v): d[v] =  (s, v), by lemma 24.2 (last slide) if v is reachable; d[v] =  (s, v) =  otherwise.   (s, u) + w(u, v), by Lemma = d[u] + w(u, v) So, algorithm returns true.

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 29 Case 2 Case 2: There exists a reachable negative-weight cycle c = ‹v 0, v 1, …, v k ›, where v 0 = v k. We have  i = 1, …, k w(v i-1, v i ) < 0. (*) Suppose algorithm returns true. Then, d[v i ]  d[v i-1 ] + w(v i-1, v i ) for i = 1, …, k. (because Relax didn’t change any d[v i ] ). Thus,  i = 1, …, k d[v i ]   i = 1, …, k d[v i-1 ] +  i = 1, …, k w(v i-1, v i ) But,  i = 1, …, k d[v i ] =  i = 1, …, k d[v i-1 ]. Can show no d[v i ] is infinite. Hence, 0   i = 1, …, k w(v i-1, v i ). Contradicts (*). Thus, algorithm returns false.

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 30 Shortest Paths in DAGs Topologically sort vertices in G; Initialize(G, s); for each u in V[G] (in order) do for each v in Adj[u] do Relax(u, v, w) od Topologically sort vertices in G; Initialize(G, s); for each u in V[G] (in order) do for each v in Adj[u] do Relax(u, v, w) od

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 31 Example  0  r s t u v w 5 27 –1 –

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 32 Example  0  r s t u v w 5 27 –1 –

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 33 Example  026  r s t u v w 5 27 –1 –

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 34 Example  r s t u v w 5 27 –1 –

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 35 Example  r s t u v w 5 27 –1 –

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 36 Example  r s t u v w 5 27 –1 –

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 37 Example  r s t u v w 5 27 –1 –

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 38 Dijkstra’s Algorithm Assumes no negative-weight edges. Maintains a set S of vertices whose SP from s has been determined. Repeatedly selects u in V–S with minimum SP estimate (greedy choice). Store V–S in priority queue Q. Initialize(G, s); S :=  ; Q := V[G]; while Q   do u := Extract-Min(Q); S := S  {u}; for each v  Adj[u] do Relax(u, v, w) od Initialize(G, s); S :=  ; Q := V[G]; while Q   do u := Extract-Min(Q); S := S  {u}; for each v  Adj[u] do Relax(u, v, w) od

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 39 Example 0    s uv x y

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 40 Example 0  5  10 s uv x y

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 41 Example s uv x y

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 42 Example s uv x y

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 43 Example s uv x y

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 44 Example s uv x y

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 45 Correctness Theorem 24.6: Upon termination, d[u] = δ(s, u) for all u in V (assuming non-negative weights). Theorem 24.6: Upon termination, d[u] = δ(s, u) for all u in V (assuming non-negative weights). Proof: By Lemma 24.11, once d[u] = δ(s, u) holds, it continues to hold. We prove: For each u in V, d[u] =  (s, u) when u is inserted in S. Suppose not. Let u be the first vertex such that d[u]   (s, u) when inserted in S. Note that d[s] =  (s, s) = 0 when s is inserted, so u  s.  S   just before u is inserted (in fact, s  S).

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 46 Proof (Continued) Note that there exists a path from s to u, for otherwise d[u] =  (s, u) =  by Corollary  there exists a SP from s to u. SP looks like this: x s y u S p1p1 p2p2

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 47 Proof (Continued) Claim: d[y] =  (s, y) when u is inserted into S. We had d[x] =  (s, x) when x was inserted into S. Edge (x, y) was relaxed at that time. By Lemma 24.14, this implies the claim. Now, we have: d[y] =  (s, y), by Claim.   (s, u), nonnegative edge weights.  d[u], by Lemma Because u was added to S before y, d[u]  d[y]. Thus, d[y] =  (s, y) =  (s, u) = d[u]. Contradiction.

Jim Anderson Comp 122, Fall 2003 Single-source SPs - 48 Complexity Running time is O(V 2 ) using linear array for priority queue. O((V + E) lg V) using binary heap. O(V lg V + E) using Fibonacci heap. (See book.)