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

Slides:



Advertisements
Similar presentations
Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy.
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
Data Structures and Algorithms Graphs Single-Source Shortest Paths (Dijkstra’s Algorithm) PLSD210(ii)
Introduction to Algorithms 6.046J/18.401J/SMA5503
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Chapter 23 Minimum Spanning Trees
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.
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
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
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.
Dijkstra's algorithm.
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.
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.
CISC 235: Topic 11 Shortest Paths Algorithms. CISC 235 Topic 112 Outline Single-Source Shortest Paths Algorithm for Unweighted Graphs Algorithm for Weighted,
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.
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.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Introduction to Algorithms L ECTURE 14 (Chap. 22 & 23) Greedy Algorithms I 22.1 Graph representation 23.1 Minimum spanning trees 23.1 Optimal substructure.
Spring 2015 Lecture 11: Minimum Spanning Trees
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.
F d a b c e g Prim’s Algorithm – an Example edge candidates choosen.
Lecture 13 Algorithm Analysis
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,
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.
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.
CS38 Introduction to Algorithms Lecture 3 April 8, 2014.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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.
Algorithm Analysis Fall 2017 CS 4306/03
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
Advanced Algorithms Analysis and Design
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
Introduction to Algorithms: Greedy Algorithms (and Graphs)
CS 3013: DS & Algorithms Shortest Paths.
Presentation transcript:

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

2 Edsger Wybe Dijkstra (May 11, 1930 – August 6, 2002)May August Born in Rotterdam, Dijkstra studied theoretical physics at Leiden University, but he quickly realized he was more interested in computer science.Rotterdamtheoretical physics Leiden University Originally employed by the Mathematisch Centrum in Amsterdam, he held a professorship at the Eindhoven University of Technology in the Netherlands, worked as a research fellow for Burroughs Corporation in the early 1970s, and later held the Schlumberger Centennial Chair in Computer Sciences at The University of Texas at Austin, in the United States. He retired in 2000Mathematisch CentrumEindhoven University of TechnologyBurroughs CorporationThe University of Texas at Austin

3 Paths in graphs Consider a digraph G = (V, E) with edge-weight function w : E →. The weight of path p = v 1 → v 2 →  → v k is defined to be Example :

4 Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest- path weight from u to v is defined as  (u, v) = min{w(p) : p is a path from u to v}. Note:  (u, v) =  if no path from u to v exists.

5 Optimal substructure Theorem. A subpath of a shortest path is a shortest path. Proof. Cut and paste:

6 Triangle inequality Theorem. For all u, v, x  V, we have  (u, v)   (u, x) +  (x, v). Proof.

7 Well-definedness of shortest paths If a graph G contains a negative-weight cycle, then some shortest paths may not exist. Example:

8 Single-source shortest paths Problem. From a given source vertex s  V, find the shortest-path weights  (s, v) for all v  V. If all edge weights w(u, v) are nonnegative, all shortest-path weights must exist. IDEA: Greedy. 1.Maintain a set S of vertices whose shortest-path distances from s are known. 2.At each step add to S the vertex v  V – S whose distance estimate from s is minimal. 3.Update the distance estimates of vertices adjacent to v.

9 d[s] ← 0 for each v  V – {s} do d[v] ←  S ←  Q ← V ⊳  Q is a priority queue maintaining V – S while Q ≠  do u ← Extract-Min(Q) S ← S  {u} for each v  Adj[u] do Dijkstra’s algorithm Implicit D ECREASE -KEY relaxation step if d[v] > d[u] + w(u, v) then d[v] ← d[u] + w(u, v)

10 Example of Dijkstra’s algorithm Graph with nonnegative edge weights:

11 Example of Dijkstra’s algorithm S : {}

12 Example of Dijkstra’s algorithm S : {A}

13 Example of Dijkstra’s algorithm S : {A}

14 Example of Dijkstra’s algorithm S : {A,C}

15 Example of Dijkstra’s algorithm

16 Example of Dijkstra’s algorithm

17 Example of Dijkstra’s algorithm

18 Example of Dijkstra’s algorithm

19 Example of Dijkstra’s algorithm

20 Example of Dijkstra’s algorithm

21 Correctness — Part I Lemma. Initializing d[s] ← 0 and d[v] ←  for all v  V – {s} establishes d[v] ≥  (s, v) for all v  V, and this invariant is maintained over any sequence of relaxation steps. Proof. Suppose not. Let v be the first vertex for which d[v] <  (s, v), and let u be the vertex that caused d[v] to change: d[v] = d[u] + w(u, v). Then, d[v] <  (s, v) supposition ≤  (s, u) +  (u, v) triangle inequality ≤  (s,u) + w(u, v) sh. path ≤ specific path ≤ d[u] + w(u, v) v is first violation Contradiction.

22 Correctness — Part II Theorem. Dijkstra’s algorithm terminates with d[v] =  (s, v) for all v  V. Proof. It suffices to show that d[v] =  (s, v) for every v  V when v is added to S. Suppose u is the first vertex added to S for which d[u] ≠  (s, u). Let y be the first vertex in V – S along a shortest path from s to u, and let x be its predecessor:

23 Correctness — Part II (continued) Since u is the first vertex violating the claimed invariant, we have d[x] =  (s, x). Since subpaths of shortest paths are shortest paths, it follows that d[y] was set to  (s, x) + w(x, y) =  (s, y) when (x, y) was relaxed just after x was added to S. Consequently, we have d[y] =  (s, y) ≤  (s, u) ≤ d[u]. But, d[u] ≤ d[y] by our choice of u, and hence d[y] =  (s, y) =  (s, u) = d[u]. Contradiction.

24 Analysis of Dijkstra |V| times degree( u) times while Q ≠  do u ← EXTRACT-MIN(Q) S ← S  {u} for each v  Adj[u] do if d[v] > d[u] + w(u, v) then d[v] ← d[u] + w(u, v) Handshaking Lemma   (E) implicit D ECREASE -KEY’s. Time =  (V)·T EXTRACT-MIN +  (E)·T DECREASE-KEY Note: Same formula as in the analysis of Prim’s minimum spanning tree algorithm.

25 Analysis of Dijkstra (continued) Time =  (V)·T EXTRACT-MIN +  (E)·T DECREASE-KEY Q T EXTRACT-MIN T DECREASE-KEY Total arrayO(V)O(V)O(1)O(V2)O(V2) binary heap O(lg V) O(E lg V) Fibonacci heap O(lg V) amortized O(1) amortized O(E + V lg V) worst case

26 Unweighted graphs Suppose w(u, v) = 1 for all (u, v)  E. Can the code for Dijkstra be improved? Use a simple FIFO queue instead of a priority queue. Breadth-first search while Q ≠  do u ← D EQUEUE (Q) for each v  Adj[u] do if d[v] =  then d[v] ← d[u] + 1 E NQUEUE (Q, v) Analysis: Time = O(V + E).