Dijkstra s Shortest Path Algorithm

Similar documents
Introduction to Algorithms. Lecture 11

Single Source Shortest Path

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

CSE 502 Class 23. Jeremy Buhler Steve Cole. April BFS solves unweighted shortest path problem. Every edge traversed adds one to path length

from notes written mostly by Dr. Carla Savage: All Rights Reserved

Lecture 11: Analysis of Algorithms (CS ) 1

Shortest Path Problem

1 Dijkstra s Algorithm

Shortest path problems

Algorithm Design and Analysis

Minimum Spanning Trees

Algorithm Design and Analysis

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

Minimum Spanning Trees and Prim s Algorithm

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College

1 The Shortest Path Problem

COMP251: Single source shortest paths

Three Graph Algorithms

Three Graph Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Unit 2: Algorithmic Graph Theory

Outline. Computer Science 331. Definitions: Paths and Their Costs. Computation of Minimum Cost Paths

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Graph Representation

Shortest Paths. Nishant Mehta Lectures 10 and 11

Chapter 24. Shortest path problems. Chapter 24. Shortest path problems. 24. Various shortest path problems. Chapter 24. Shortest path problems

Single Source Shortest Path (SSSP) Problem

Shortest Paths. Nishant Mehta Lectures 10 and 11

Graph: representation and traversal

Week 12: Minimum Spanning trees and Shortest Paths

Chapter 22. Elementary Graph Algorithms

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Algorithms for Data Science

22 Elementary Graph Algorithms. There are two standard ways to represent a

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

Graph Search. Adnan Aziz

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

Lecture I: Shortest Path Algorithms

Solution. violating the triangle inequality. - Initialize U = {s} - Add vertex v to U whenever DIST[v] decreases.

Minimum Spanning Trees Ch 23 Traversing graphs

Outlines: Graphs Part-2

Algorithms on Graphs: Part III. Shortest Path Problems. .. Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar..

22 Elementary Graph Algorithms. There are two standard ways to represent a

Dr. Alexander Souza. Winter term 11/12

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CS 561, Lecture 10. Jared Saia University of New Mexico

Graph Algorithms. Definition

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

Graph implementations :

Elementary Graph Algorithms

Outline. (single-source) shortest path. (all-pairs) shortest path. minimum spanning tree. Dijkstra (Section 4.4) Bellman-Ford (Section 4.

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

09 B: Graph Algorithms II

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

CS 561, Lecture 10. Jared Saia University of New Mexico

The Shortest Path Problem

TIE Graph algorithms

Outline. Computer Science 331. Analysis of Prim's Algorithm

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Design and Analysis of Algorithms

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

DFS & STRONGLY CONNECTED COMPONENTS

Analysis of Algorithms, I

Single Source Shortest Paths

Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.

(Refer Slide Time: 00:18)

Introduction to Algorithms

CSI 604 Elementary Graph Algorithms

Shortest Path Algorithms

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

Section authors: Hamilton Clower, David Scott, Ian Dundore.

Classical Shortest-Path Algorithms

Introduction Single-source shortest paths All-pairs shortest paths. Shortest paths in graphs

CS2210 Data Structures and Algorithms

Dijkstra s Algorithm Last time we saw two methods to solve the all-pairs shortest path problem: Min-plus matrix powering in O(n 3 log n) time and the

SHORTEST PATHS. Weighted Digraphs. Shortest paths. Shortest Paths BOS ORD JFK SFO LAX DFW MIA

Minimum Spanning Trees

Algorithms for Data Science

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

Introduction to Algorithms

CS 6783 (Applied Algorithms) Lecture 5

Representations of Graphs

Kruskal s MST Algorithm

Week 11: Minimum Spanning trees

Lecture 17: Weighted Graphs, Dijkstra s Algorithm. Courtesy of Goodrich, Tamassia and Olga Veksler

1 i n (p i + r n i ) (Note that by allowing i to be n, we handle the case where the rod is not cut at all.)

Dijkstra s Algorithm

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

CS 310 Advanced Data Structures and Algorithms

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

Transcription:

Dijkstra s Shortest Path Algorithm DPV 4.4, CLRS 24.3 Revised, October 23, 2014 Outline of this Lecture Recalling the BFS solution of the shortest path problem for unweighted (di)graphs. The shortest path problem for weighted digraphs. Dijkstra s algorithm. Given for digraphs but easily modified to work on undirected graphs. 1

Recall: Shortest Path Problem for Graphs Let G = (V, E) be a (di)graph. The shortest path between two vertices is a path with the shortest length (least number of edges). Call this the link-distance. Breadth-first-search is an algorithm for finding shortest (link-distance) paths from a single source vertex to all other vertices. BFS processes vertices in increasing order of their distance from the root vertex. BFS has running time O( V + E ). 2

Shortest Path Problem for Weighted Graphs Let G = (V, E) be a weighted digraph, with weight function w : E R mapping edges to real-valued weights. If e = (u, v), we write w(u, v) for w(e). The length of a path p = v 0, v 1,..., v k is the sum of the weights of its constituent edges: length(p) = k i=1 w(v i 1, v i ). The distance from u to v, denoted δ(u, v), is the length of the minimum length path if there is a path from u to v; and is otherwise. a d 8 2 c 4 b 1 5 3 e length( a, b, c, e ) = 6 distance from a to e is 6 3

Single-Source Shortest-Paths Problem The Problem: Given a digraph with positive edge weights G = (V, E) and a distinguished source vertex, s V, determine the distance and a shortest path from the source vertex to every vertex in the digraph. Question: How do you design an efficient algorithm for this problem? 4

Single-Source Shortest-Paths Problem Important Observation: Any subpath of a shortest path must also be a shortest path. Why? Example: In the following digraph, a, b, c, e is a shortest path. The subpath a, b, c is also a shortest path. a d 8 2 c 4 b 1 5 3 e length( a, b, c, e ) = 6 distance from a to e is 6 Observation Extending this idea we observe the existence of a shortest path tree in which distance from source to vertex v is length of shortest path from source to vertex in original tree. 5

Intuition behind Dijkstra s Algorithm Report the vertices in increasing order of their distance from the source vertex. Construct the shortest path tree edge by edge; at each step adding one new edge, corresponding to construction of shortest path to the current new vertex. 6

The Rough Idea of Dijkstra s Algorithm Maintain an estimate d[v] of the length δ(s, v) of the shortest path for each vertex v. Always d[v] δ(s, v) and d[v] equals the length of a known path (d[v] = if we have no paths so far). Initially d[s] = 0 and all the other d[v] values are set to. The algorithm will then process the vertices one by one in some order. The processed vertex s estimate will be validated as being real shortest distance, i.e. d[v] = δ(s, v). Here processing a vertex u means finding new paths and updating d[v] for all v Adj[u] if necessary. The process by which an estimate is updated is called relaxation. When all vertices have been processed, d[v] = δ(s, v) for all v. 7

The Rough Idea of Dijkstra s Algorithm Question 1: How does the algorithm find new paths and do the relaxation? Question 2: In which order does the algorithm process the vertices one by one? 8

Answer to Question 1 Finding new paths. When processing a vertex u, the algorithm will examine all vertices v Adj[u]. For each vertex v Adj[u], a new path from s to v is found (path from s to u + new edge). Relaxation. If the new path from s to v is shorter than d[v], then update d[v] to the length of this new path. Remark: Whenever we set d[v] to a finite value, there exists a path of that length. Therefore d[v] δ(s, v). (Note: If d[v] = δ(s, v), then further relaxations cannot change its value.) 9

Implementing the Idea of Relaxation Consider an edge from a vertex u to v whose weight is w(u, v). Suppose that we have already processed u so that we know d[u] = δ(s, u) and also computed a current estimate for d[v]. Then There is a (shortest) path from s to u with length d[u]. There is a path from s to v with length d[v]. Combining this path from s to u with the edge (u, v), we obtain another path from s to v with length d[u] + w(u, v). If d[u]+w(u, v) < d[v], then we replace the old path s,..., w, v with the new shorter path s,..., u, v. Hence we update d[v] = d[u] + w(u, v) pred[v] = u (originally, pred[v] == w). s u w d[u] d[v] v 10

The Algorithm for Relaxing an Edge Relax(u,v) { if (d[u] + w(u, v) < d[v]) { d[v] = d[u] + w(u, v); pred[v] = u; } } Remark: The predecessor pointer pred[ ] is for determining the shortest paths. 11

Idea of Dijkstra s Algorithm: Repeated Relaxation Dijkstra s algorithm operates by maintaining a subset of vertices, S V, for which we know the true distance, that is d[v] = δ(s, v). Initially S =, the empty set, and we set d[s] = 0 and d[v] = for all others vertices v. One by one we select vertices from V \ S to add to S. The set S can be implemented using an array of vertex colors. Initially all vertices are white, and we set color[v] = black to indicate that v S. 12

The Selection in Dijkstra s Algorithm Recall Question 2: What is the best order in which to process vertices, so that the estimates are guaranteed to converge to the true distances. That is, how does the algorithm select which vertex among the vertices of V \ S to process next? Answer: We use a greedy algorithm. For each vertex in u V \ S, we have computed a distance estimate d[u]. The next vertex processed is always a vertex u V \ S for which d[u] is minimum, that is, we take the unprocessed vertex that is closest (by our estimate) to s. Question: How do we implement this selection of vertices efficiently? 13

The Selection in Dijkstra s Algorithm Question: How do we perform this selection efficiently? Answer: We store the vertices of V \ S in a priority queue, where the key value of each vertex v is d[v]. [Note: if we implement the priority queue using a heap, we can perform the operations Insert(), Extract Min(), and Decrease Key(), each in O(log n) time.] 14

Review of Priority Queues A Priority Queue is a data structure (can be implemented as a heap) which supports the following operations: insert(u, key): Insert u with the key value key in Q. u = extractmin(): Extract the item with the minimum key value in Q. decreasekey(u, new-key): Decrease u s key value to new-key. Remark: Priority Queues can be implemented such that each operation takes time O(log Q ). In Class we only saw insert, extractmin and Delete. DecreaseKey can either be implemented directly in O(log Q ) time or could be implemented by a delete followed by an insert. 15

Description of Dijkstra s Algorithm Dijkstra(G,w,s) { % Initialize for (each u V ) { d[u] = ; color[u] =white; } d[s] = 0; pred[s] = NIL; Q = (queue with all vertices); } while (Non-Empty(Q)) { u = Extract-Min(Q); for (each v Adj[u]) if (d[u] + w(u, v) < d[v]) { d[v] = d[u] + w(u, v); Decrease-Key(Q, v, d[v]); pred[v] = u; } color[u] =black; } % Process all vertices % Find new vertex % If estimate improves relax 16

Dijkstra s Algorithm Example: b inf 1 inf c s 0 7 2 3 2 inf a 8 5 4 5 inf d Step 0: Initialization. v s a b c d d[v] 0 pred[v] nil nil nil nil nil color[v] W W W W W Priority Queue: v s a b c d d[v] 0 17

Dijkstra s Algorithm Example: b 7 1 inf c s 0 7 2 3 2 2 a 8 5 4 5 inf d Step 1: As Adj[s] = {a, b}, work on a and b and update information. v s a b c d d[v] 0 2 7 pred[v] nil s s nil nil color[v] B W W W W Priority Queue: v a b c d d[v] 2 7 18

Dijkstra s Algorithm Example: s 0 7 2 b 5 1 10 c 8 3 2 4 5 2 7 a 5 d Step 2: After Step 1, a has the minimum key in the priority queue. As Adj[a] = {b, c, d}, work on b, c, d and update information. v s a b c d d[v] 0 2 5 10 7 pred[v] nil s a a a color[v] B B W W W Priority Queue: v b c d d[v] 5 10 7 19

Dijkstra s Algorithm Example: s 0 7 2 b 5 1 6 c 8 3 2 4 5 2 7 a 5 d Step 3: After Step 2, b has the minimum key in the priority queue. As Adj[b] = {a, c}, work on a, c and update information. v s a b c d d[v] 0 2 5 6 7 pred[v] nil s a b a color[v] B B B W W Priority Queue: v c d d[v] 6 7 20

Dijkstra s Algorithm Example: s 0 7 2 b 5 1 6 c 8 3 2 4 5 2 7 a 5 d Step 4: After Step 3, c has the minimum key in the priority queue. As Adj[c] = {d}, work on d and update information. v s a b c d d[v] 0 2 5 6 7 pred[v] nil s a b a color[v] B B B B W Priority Queue: v d d[v] 7 21

Dijkstra s Algorithm Example: s 0 7 2 b 5 1 6 c 8 3 2 4 5 2 7 a 5 d Step 5: After Step 4, d has the minimum key in the priority queue. As Adj[d] = {c}, work on c and update information. Priority Queue: Q =. v s a b c d d[v] 0 2 5 6 7 pred[v] nil s a b a color[v] B B B B B We are done. 22

Dijkstra s Algorithm Shortest Path Tree: T = (V, A), where A = {(pred[v], v) v V \ {s}}. The array pred[v] is used to build the tree. b 5 1 6 c 0 3 s 2 a 2 5 7 d Example: v s a b c d d[v] 0 2 5 6 7 pred[v] nil s a b a 23

Correctness of Dijkstra s Algorithm Lemma: When a vertex u is added to S (i.e., dequeued from the queue), d[u] = δ(s, u). Proof: Suppose to the contrary that at some point Dijkstra s algorithm first attempts to add a vertex u to S for which d[u] δ(s, u). By our observations about relaxation, d[u] > δ(s, u). Consider the situation just prior to the insertion of u. Consider the true shortest path from s to u. Because s S and u V \S, at some point this path must first take a jump out of S. Let (x, y) be the edge taken by the path, where x S and y V \ S (it may be that x = s and/or y = u). s S u x y 24

Correctness of Dijkstra s Algorithm Continued We now prove that d[y] = δ(s, y). We have done relaxation when processing x, so d[y] d[x] + w(x, y). (1) Since x is added to S earlier, by hypothesis, d[x] = δ(s, x). (2) Since s,..., x, y is subpath of a shortest path, by (2) δ(s, y) = δ(s, x) + w(x, y) = d[x] + w(x, y). (3) By (1) and (3), Hence d[y] δ(s, y). d[y] = δ(s, y). So y u (because we suppose d[u] > δ(s, u)). Now observe that since y appears midway on the path from s to u, and all subsequent edges are positive, we have δ(s, y) < δ(s, u), and thus d[y] = δ(s, y) < δ(s, u) d[u]. Thus y would have been added to S before u, in contradiction to our assumption that u is the next vertex to be added to S. 25

Proof of the Correctness of Dijkstra s Algorithm By the lemma, d[v] = δ(s, v) when v is added into S, that is when we set color[v] = black. At the end of the algorithm, all vertices are in S, then all distance estimates are correct. 26

Analysis of Dijkstra s Algorithm: Insert: Performed once for each vertex. n times.. Non-Empty(): Called one for each vertex. n times. Each time does one Extract-Min() and one color setting inner loop for (each v Adj[u]) : is called once for each of the m edges in the graph. O(m) times. Each call of the inner loop does O(1) work plus, possibly, one Decrease-Key operation. Recall that all of the priority queue operations require O(log Q ) = O(log n) time we have that the algorithm uses no(log n)+no(log n)+o(e log n) = O((n+e) log n) time. 27

Further Analysis of Dijkstra s Algorithm: Dijkstra s original implementation did not use a priority queue but, instead, ran through the entire list of white vertices at each step, finding the one with smallest weight. Each run through requires O(n) time and there are only n steps so his implementation runs in O(n 2 ) time. This is slightly better than the priority queue based algorithm for dense graphs but much worse for sparse ones. 28

Further Improvements to Dijkstra s Algorithm: A more advanced pririty queue data structure called a Fibonacci Heapimplements Insert:in O(1) time Extract-Min(): O(log n) time Decrease-Key: O(1) (amortized) time. This reduces the running time down to no(log n)+no(log n)+o(e) = O((n log n+e)) time. This is a huge improvement for dense graphs 29

Prove: Dijkstra s algorithm processes vertices in nondecreasing order of their actual distance from the source vertex. 30