1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.

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
Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
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
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 8a-ShortestPathsMore Shortest Paths in a Graph (cont’d) Fundamental Algorithms.
Fonts: MTExtra:  (comment) Symbol:  Wingdings:  Graphs 6-Graphs.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
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.
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.
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs 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.
Shortest Paths Introduction to Algorithms Shortest Paths CSE 680 Prof. Roger Crawfis.
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
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
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,
David Luebke 1 9/13/2015 CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
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.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
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,
David Luebke 1 3/1/2016 CS 332: Algorithms Dijkstra’s Algorithm Disjoint-Set Union.
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.
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
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
CS 3013: DS & Algorithms Shortest Paths.
Presentation transcript:

1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms

2 8-ShortestPaths The Problems Given a directed graph G with edge weights, find –The shortest path from a given vertex s to all other vertices (Single Source Shortest Paths) –The shortest paths between all pairs of vertices (All Pairs Shortest Paths) where the length of a path is the sum of its edge weights.

3 8-ShortestPaths Shortest Paths: Applications Flying times between cities Distance between street corners Cost of doing an activity –Vertices are states –Edge weights are costs of moving between states

4 8-ShortestPaths Shortest Paths: Algorithms Single-Source Shortest Paths (SSSP) –Dijkstra’s –Bellman-Ford –DAG Shortest Paths All-Pairs Shortest Paths (APSP) –Floyd-Warshall –Johnson’s

5 8-ShortestPaths A Fact About Shortest Paths Theorem: If p is a shortest path from u to v, then any subpath of p is also a shortest path. Proof: Consider a subpath of p from x to y. If there were a shorter path from x to y, then there would be a shorter path from u to v. u xyv shorter?

6 8-ShortestPaths Single-Source Shortest Paths Given a directed graph with weighted edges, what are the shortest paths from some source vertex s to all other vertices? Note: shortest path to single destination cannot be done asymptotically faster, as far as we know s

7 8-ShortestPaths Path Recovery We would like to find the path itself, not just its length. We’ll construct a shortest-paths tree: s uv xy s u v x y

8 8-ShortestPaths Shortest-Paths Idea  (u,v)  length of the shortest path from u to v. All SSSP algorithms maintain a field d[u] for every vertex u. d[u] will be an estimate of  (s,u). As the algorithm progresses, we will refine d[u] until, at termination, d[u] =  (s,u). Whenever we discover a new shortest path to u, we update d[u]. In fact, d[u] will always be an overestimate of  (s,u): d[u]  (s,u) We’ll use  [u] to point to the parent (or predecessor) of u on the shortest path from s to u. We update  [u] when we update d[u].

9 8-ShortestPaths SSSP Subroutine RELAX(u, v, w)  (Maybe) improve our estimate of the distance to v  by considering a path along the edge (u, v). if d[u] + w(u, v) < d[v] then d[v]  d[u] + w(u, v)  actually, DECREASE-KEY  [v]  u  remember predecessor on path u v w(u,v) d[v]d[u]

10 8-ShortestPaths Dijkstra’s Algorithm (pronounced “DIKE-stra”) Assume that all edge weights are  0. Idea: say we have a set K containing all vertices whose shortest paths from s are known (i.e. d[u] =  (s,u) for all u in K). Now look at the “frontier” of K—all vertices adjacent to a vertex in K. the rest of the graph s K

11 8-ShortestPaths Dijkstra’s: Theorem At each frontier vertex u, update d[u] to be the minimum from all edges from K. Now pick the frontier vertex u with the smallest value of d[u]. Claim: d[u] =  (s,u) s min(4+2, 6+1) = 6 min(4+8, 6+3) = 9

12 8-ShortestPaths Dijkstra’s: Proof By construction, d[u] is the length of the shortest path to u going through only vertices in K. Another path to u must leave K and go to v on the frontier. But the length of this path is at least d[v], (assuming non-negative edge weights), which is  d[u].

13 8-ShortestPaths Proof Explained Why is the path through v at least d[v] in length? We know the shortest paths to every vertex in K. We’ve set d[v] to the shortest distance from s to v via K. The additional edges from v to u cannot decrease the path length. s u v d[u]  d[v] K shortest path to u another path to u, via v

14 8-ShortestPaths Dijkstra’s Algorithm, Rough Draft u K K u

15 8-ShortestPaths A Refinement Note: we don’t really need to keep track of the frontier. When we add a new vertex u to K, just update vertices adjacent to u.

16 8-ShortestPaths Example of Dijkstra’s s

17 8-ShortestPaths Code for Dijkstra’s Algorithm 1DIJKSTRA(G, w, s)  Graph, weights, start vertex 2 for each vertex v in V[G] do 3d[v]   4  [v]  NIL 5d[s]  0 6Q  BUILD-PRIORITY-QUEUE(V[G]) 7  Q is V[G] - K 8while Q is not empty do 9u = EXTRACT-MIN(Q) 10for each vertex v in Adj[u] 11RELAX(u, v, w) // DECREASE_KEY

18 8-ShortestPaths Running Time of Dijkstra Initialization:  (V) Building priority queue:  (V) “while” loop done |V| times |V| calls of EXTRACT-MIN Inner “edge” loop done |E| times At most |E| calls of DECREASE-KEY Total time:  (V + V  T EXTRACT-MIN + E  T DECREASE-KEY )

19 8-ShortestPaths Dijkstra Running Time (cont.) 1. Priority queue is an array. EXTRACT-MIN in  (n) time, DECREASE-KEY in  (1) Total time:  (V + VV + E) =  (V 2 ) 2. (“Modified Dijkstra”) Priority queue is a binary (standard) heap. EXTRACT-MIN in  (lgn) time, also DECREASE-KEY Total time:  (VlgV + ElgV) 3. Priority queue is Fibonacci heap. (Of theoretical interest only.) EXTRACT-MIN in  (lgn), DECREASE-KEY in  (1) (amortized) Total time:  (VlgV+E)

20 8-ShortestPaths The Bellman-Ford Algorithm Handles negative edge weights Detects negative cycles Is slower than Dijkstra a negative cycle

21 8-ShortestPaths Bellman-Ford: Idea Repeatedly update d for all pairs of vertices connected by an edge. Theorem: If u and v are two vertices with an edge from u to v, and s  u  v is a shortest path, and d[u] =  (s,u), then d[u]+w(u,v) is the length of a shortest path to v. Proof: Since s  u  v is a shortest path, its length is  (s,u) + w(u,v) = d[u] + w(u,v).

22 8-ShortestPaths Why Bellman-Ford Works On the first pass, we find  (s,u) for all vertices whose shortest paths have one edge. On the second pass, the d[u] values computed for the one-edge-away vertices are correct (=  (s,u)), so they are used to compute the correct d values for vertices whose shortest paths have two edges. Since no shortest path can have more than |V[G]|-1 edges, after that many passes all d values are correct. Note: all vertices not reachable from s will have their original values of infinity. (Same, by the way, for Dijkstra).

23 8-ShortestPaths Bellman-Ford: Algorithm BELLMAN-FORD(G, w, s) 1 foreach vertex v  V[G] do //INIT_SINGLE_SOURCE 2 d[v]  3  [v]  NIL 4 d[s]  0 5 for i  1 to |V[G]|-1 do  each iteration is a “pass” 6 for each edge (u,v) in E[G] do 7 RELAX(u, v, w) 8  check for negative cycles 9 for each edge (u,v) in E[G] do 10 if d[v] > d[u] + w(u,v) then 11 return FALSE 12 return TRUE Running time  VE)

24 8-ShortestPaths Negative Cycle Detection What if there is a negative-weight cycle reachable from s? Assume:d[u]  d[x]+4 d[v]  d[u]+5 d[x]  d[v]-10 Adding: d[u]+d[v]+d[x]  d[x]+d[u]+d[v]-1 Because it’s a cycle, vertices on left are same as those on right. Thus we get 0  -1; a contradiction. So for at least one edge (u,v), d[v] > d[u] + w(u,v) This is exactly what Bellman-Ford checks for. u x v

25 8-ShortestPaths SSSP in a DAG Recall: a dag is a directed acyclic graph. If we update the edges in topologically sorted order, we correctly compute the shortest paths. Reason: the only paths to a vertex come from vertices before it in the topological sort s

26 8-ShortestPaths SSSP in a DAG Theorem Theorem: For any vertex u in a dag, if all the vertices before u in a topological sort of the dag have been updated, then d[u] =  (s,u). Proof: By induction on the position of a vertex in the topological sort. Base case: d[s] is initialized to 0. Inductive case: Assume all vertices before u have been updated, and for all such vertices v, d[v]=  (s,v). (continued)

27 8-ShortestPaths Proof, Continued Some edge (v,u) where v is before u, must be on the shortest path to u, since there are no other paths to u. When v was updated, we set d[u] to d[v]+w(v,u) =  (s,v) + w(v,u) =  (s,u)

28 8-ShortestPaths SSSP-DAG Algorithm DAG-SHORTEST-PATHS(G,w,s) 1topologically sort the vertices of G 2initialize d and  as in previous algorithms 3for each vertex u in topological sort order do 4for each vertex v in Adj[u] do 5 RELAX(u, v, w) Running time:  (V+E), same as topological sort