1 Graph Algorithms Single source shortest paths problem Dana Shapira.

Slides:



Advertisements
Similar presentations
Advanced Algorithm Design and Analysis Jiaheng Lu Renmin University of China
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)
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 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.
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.
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.
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.
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.
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.
Master Method (4. 3) Recurrent formula T(n) = a  T(n/b) + f(n) 1) if for some  > 0 then 2) if then 3) if for some  > 0 and a  f(n/b)  c  f(n) for.
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.
Lecture 13 Algorithm Analysis
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
David Luebke 1 3/1/2016 CS 332: Algorithms Dijkstra’s Algorithm Disjoint-Set Union.
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])
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Data Structures and Algorithms
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
CS200: Algorithm Analysis
Many slides here are based on D. Luebke slides
CS 332: Algorithms Dijkstra’s Algorithm Continued Disjoint-Set Union
CS6045: Advanced Algorithms
SINGLE-SOURCE SHORTEST PATHS
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Chapter 24: Single-Source Shortest Paths
CS 3013: DS & Algorithms Shortest Paths.
Presentation transcript:

1 Graph Algorithms Single source shortest paths problem Dana Shapira

2 Given vertex s, for every vertex v  V find a shortest path from s to v. Dijkstra’s algorithm solves this problem efficiently for the case in which all weights are nonnegative. Single Source Shortest Path Problem

3 Single Source Shortest Path Problem We are given a weighted, directed graph G = (V, E), with weight function  : E → R mapping edges to real valued weights. The weight of a path is

4 Shortest Path Properties We have optimal substructure: Let p = be the shortest path between v 1 and v k. The sub-path between v i and v j, where 1 ≤ i,j ≤ k, p ij = is a shortest path. Proof: suppose some subpath is not a shortest path There must then exist a shorter subpath Could substitute the shorter subpath for a shorter path But then overall path is not shortest path. Contradiction

5 Shortest Path Properties Define  (u,v) to be the weight of the shortest path from u to v Triangle inequality:  (u,v)   (u,x) +  (x,v) For each edge (x,v)  (u,v)   (u,x) +  (x,v) (why?) x u v

6 Shortest Path Properties In graphs with negative weight cycles, some shortest paths will not exist (Why?): < 0

7 Negative weight cycles a b c e d

8 Relaxation For all v, maintain upper bound d[v] on  (s,v) Relax(u,v,w) { if (d[v] > d[u]+w(u,v)) then d[v]=d[u]+w(u,v); } Relax

9 Dijkstra’s Algorithm Dijkstra(G) for each v  V d[v] =  ; d[s] = 0; S =  ; Q = V;  (s)=NULL; while (Q   ) u = ExtractMin(Q); S = S  {u}; for each v  Adj[u] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v);  (v)=u Relaxation Step B C DA What will be the total running time?

10 Correctness Of Dijkstra's Algorithm d[v]   (s,v)  v Let u be first vertex in S such that d[u]   (s,u) when it is removed from Q. s x y u p2p2 p1p1 We must show that when u is removed from Q, it has already converged

11 Correctness Of Dijkstra's Algorithm Let y be first vertex  V-S on actual shortest path from s  u  d[y] =  (s,y) Because d[x] is set correctly for y's predecessor x  S on the shortest path (u was the first vertex in S such that d[u]   (s,u)), When we put x into S, we relaxed (x,y), giving d[y] the correct value s x y u p2p2 p1p1

12 Correctness Of Dijkstra's Algorithm d[u]>  (s,u) =  (s,y) +  (y,u) (Why?) = d[y] +  (y,u)  d[y]But if d[u] > d[y], wouldn't have chosen u. Contradiction. s x y u p2p2 p1p1

13 Question Can you give an example of a graph that includes negative weights, and does not work with Dijkstra’s algorithm? A B D C A B D C

14 Bellman-Ford Algorithm BellmanFord() for each v  V d[v] =  ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v)  E Relax(u,v, w(u,v));  (v)=u; for each edge (u,v)  E if (d[v] > d[u] + w(u,v)) return “no solution”; Relax(u,v,w): if (d[v] > d[u]+w(u,v)) then d[v]=d[u]+w(u,v); Initialize d[], which will converge to shortest-path value  Relaxation: Make |V|-1 passes, relaxing each edge Test for solution Under what condition do we get a solution?

15 Bellman-Ford Algorithm BellmanFord() for each v  V d[v] =  ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v)  E Relax(u,v, w(u,v));  (v)=u; for each edge (u,v)  E if (d[v] > d[u] + w(u,v)) return “no solution”; Relax(u,v,w): if (d[v] > d[u]+w(u,v)) then d[v]=d[u]+w(u,v); What will be the running time? O(|V||E|)

16 Bellman-Ford Algorithm BellmanFord() for each v  V d[v] =  ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v)  E Relax(u,v, w(u,v));  (v)=u; for each edge (u,v)  E if (d[v] > d[u] + w(u,v)) return “no solution”; Relax(u,v,w): if (d[v] > d[u]+w(u,v)) then d[v]=d[u]+w B E DC A Example s

17 Bellman-Ford Note that order in which edges are processed affects how quickly it converges. Correctness: show d[v] =  (s,v) after |V|-1 passes Lemma: d[v]   (s,v) always Initially true Let v be first vertex for which d[v] <  (s,v) Let u be the vertex that caused d[v] to change: d[v] = d[u] +  (u,v) Then d[v]<  (s,v)   (s,u) +  (u,v)  d[u] +  (u,v) (Why?) So d[v] < d[u] +  (u,v). Contradiction.

18 Bellman-Ford Prove: after |V|-1 passes, all d values correct Consider shortest path from s to v: s  v 1  v 2  v 3  …  v Initially, d[s] = 0 is correct, and doesn’t change (Why?) After 1 pass through edges, d[v 1 ] is correct (Why?) and doesn’t change After 2 passes, d[v 2 ] is correct and doesn’t change … Terminates in |V| - 1 passes: (Why?) What if it doesn’t?

19 Let be a negative cycle. Suppose that the algorithm does not find any problem in the last iteration. Since contradiction Bellman-Ford

20 Bellman-Ford Example 5    s zy x t -4

21 DAG Shortest Paths Problem: finding shortest paths in DAG Bellman-Ford takes O(|V||E|) time. How can we do better? Idea: use topological sort If were lucky and processes vertices on each shortest path from left to right, would be done in one pass Every path in a DAG is subsequence of topologically sorted vertex order, so processing vertices in that order, we will do each path in forward order (will never relax edges out of vertices before doing all edges into vertices). Thus: just one pass. What will be the running time?

22 DAG Shortest Paths Example s ∞ 3 a b cd