Dijkstra s Algorithm. Single source shortest paths for a directed acyclic graph

Similar documents
Single Source Shortest Path

Week 12: Minimum Spanning trees and Shortest Paths

Shortest path problems

COMP251: Single source shortest paths

Shortest Path Problem

Dijkstra s Algorithm

Shortest Paths. Nishant Mehta Lectures 10 and 11

Minimum Spanning Trees

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

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

Shortest Paths. Nishant Mehta Lectures 10 and 11

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

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

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

Lecture 11: Analysis of Algorithms (CS ) 1

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1

Single Source Shortest Path (SSSP) Problem

Algorithm Design and Analysis

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

Algorithms. All-Pairs Shortest Paths. Dong Kyue Kim Hanyang University

Shortest Path Algorithms

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

Introduction to Algorithms. Lecture 11

CS350: Data Structures Dijkstra s Shortest Path Alg.

Description of The Algorithm

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

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

TIE Graph algorithms

Single-Source Shortest Paths. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Chapter 9 Graph Algorithms

Lecture 4: Graph Algorithms

A Parallel Algorithm for the Single-Source Shortest Path Problem

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

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

Minimum Spanning Tree

MST & Shortest Path -Prim s -Djikstra s

MA 252: Data Structures and Algorithms Lecture 36. Partha Sarathi Mandal. Dept. of Mathematics, IIT Guwahati

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

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

Minimum Spanning Trees

Introduction to Algorithms

Parallel Graph Algorithms

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

Unit 2: Algorithmic Graph Theory

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

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

Introduction to Algorithms

Algorithm Design and Analysis

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

Lecture I: Shortest Path Algorithms

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

Parallel Graph Algorithms

Sample Solutions to Homework #4

Shortest Paths. CSE 373 Data Structures Lecture 21

More Graph Algorithms: Topological Sort and Shortest Distance

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Routing Protocols and the IP Layer

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

Chapter 9 Graph 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

Elementary Graph Algorithms: Summary. Algorithms. CmSc250 Intro to Algorithms

Minimum Spanning Trees My T. UF

Greedy Approach: Intro

Design and Analysis of Algorithms 演算法設計與分析. Lecture 13 December 18, 2013 洪國寶

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

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

The Shortest Path Problem

Single Source Shortest Path: The Bellman-Ford Algorithm. Slides based on Kevin Wayne / Pearson-Addison Wesley

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

Algorithms for Data Science

Graph Algorithms Introduction to Data Structures. Ananda Gunawardena 7/31/2011 1

Data Structures and Algorithms

Dijkstra s Shortest Path Algorithm

Design and Analysis of Algorithms

Graph Algorithms. A Brief Introduction. 高晓沨 (Xiaofeng Gao) Department of Computer Science Shanghai Jiao Tong Univ.

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Heaps. (our first advanced data structure)

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

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

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

Chapter 25: All-Pairs Shortest-Paths

Lecture 18 Graph-Based Algorithms. CSE373: Design and Analysis of Algorithms

Minimum Spanning Trees

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

Chapter 9 Graph Algorithms

Lecture 5: Graph algorithms 1

problem statement Prim s algorithm a multiple key map representation for a tree code for Prim s algorithm

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Data Structures and Algorithms. Werner Nutt

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

Java II Graph Algorithms II

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

Ma/CS 6a Class 27: Shortest Paths

Lecture 14: Shortest Paths Steven Skiena

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

LEC13: SHORTEST PATH. CSE 373 Analysis of Algorithms Fall 2016 Instructor: Prof. Sael Lee. Lecture slide courtesy of Prof.

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

Transcription:

Dijkstra s Algorithm Single source shortest paths for a directed acyclic graph

Single-Source Shortest Paths We want to find the shortest paths between Binghamton and New York City, Boston, and Washington DC. Given a US road map with all the possible routes how can we determine our shortest paths?

Single Source Shortest Paths Problem We can model this problem with a directed graph. Intersections correspond to vertices, roads between intersections correspond to edges and distance corresponds to weights. One way roads correspond to directed edges. The problem Given a weighted digraph and a vertex s in the graph, find a shortest path from s to an arbitrary node t

Single Source Shortest Paths Problem To solve this problem, we may use Floyd s algorithm that finds all pairs shortest paths via dynamic programming. But, this is an overkill, because we have a single source now. Floyd s algorithm is Θ(n 3 ). Can we solve the single source shortest paths problem faster than Θ(n 3 )?

Dijkstra s shortest path algorithm Assume there is no negative edge There is no negative cycle, since we are dealing with directed acyclic graph Dijkstra s algorithm solves the single source shortest path problem in 2 stages. Stage1: A greedy algorithm computes the shortest distance from s to all other nodes in the graph and saves a data structure. Stage2 : Uses the data structure to find a shortest path from s to t.

Main idea Assume that the shortest distances from the starting node s to the rest of the nodes are d(s, s) d(s, s 1 ) d(s, s 2 ) d(s, s n-1 ) In this case a shortest path from s to s i may include any of the vertices {s 1, s 2 s i-1 } but cannot include any s j where j > i. Dijkstra s main idea is to select the nodes and compute the shortest distances in the order s, s 1, s 2,, s n-1

Example s 4 d(s, s) =0 d(s, s 1 )=5 d(s, s 2 )=6 d(s, s 3 )=8 d(s, s 4 )=15 Note: The shortest path s 15 8 s 3 10 3 4 5 s 1 2 1 s 2 from s to s 2 includes s 1 as an intermediate node but cannot include s 3 or s 4.

Dijkstra s greedy selection rule Assume s 1, s 2 s i-1 have been selected, and their shortest distances have been stored in Solution Select node s i and save d(s, s i ) if s i has the shortest distance from s on a path that may include only s 1, s 2 s i-1 as intermediate nodes. We call such paths special To apply this selection rule efficiently, we need to maintain for each unselected node v the distance of the shortest special path from s to v, D[v].

Application Example Solution = {(s, 0)} D[s 1 ]=5 for path [s, s 1 ] D[s 2 ]= for path [s, s 2 ] D[s 3 ]=10 for path [s, s 3 ] D[s 4 ]=15 for path [s, s 4 ]. Solution = {(s, 0), (s 1, 5) } D[s 2 ]= 6 for path [s, s 1, s 2 ] D[s 3 ]=9 for path [s, s 1, s 3 ] D[s 4 ]=15 for path [s, s 4 ] Solution = {(s, 0), (s 1, 5), (s 2, 6) } D[s 3 ]=8 for path [s, s 1, s 2, s 3 ] D[s 4 ]=15 for path [s, s 4 ] s s 4 15 8 s 3 10 3 4 5 s 1 2 1 s 2 Solution = {(s, 0), (s 1, 5), (s 2, 6),(s 3, 8), (s 4, 15) }

Implementing the selection rule Node near is selected and added to Solution if D(near) D(v) for any v Solution. Solution = {(s, 0)} D[s 1 ]=5 D[s 2 ]= D[s 1 ]=5 D[s 3 ]=10 D[s 1 ]=5 D[s 4 ]=15 Node s 1 is selected Solution = {(s, 0), (s 1, 5) } s s 4 15 8 s 3 10 3 4 5 2 1 s 2 s 1

Updating D[ ] After adding near to Solution, D[v] of all nodes v Solution are updated if there is a shorter special path from s to v that contains node near, i.e., if (D[near] + w(near, v ) < D[v]) then D[v]=D[near] + w(near, v ) Solution after adding near s 3 3 2 6 D[near ] = 5 2 D[ v ] = 9 is updated to D[v]=5+2=7

Updating D Example Solution = {(s, 0)} D[s 1 ]=5, D[s 2 ]=, D[s 3 ]=10, D[s 4 ]=15. s 4 Solution = {(s, 0), (s 1, 5) } D[s 2 ]= D[s 1 ]+w(s 1, s 2 )=5+1=6, D[s 3 ]= D[s 1 ]+w(s 1, s 3 )=5+4=9, D[s 4 ]=15 Solution = {(s, 0), (s 1, 5), (s 2, 6) } D[s 3 ]=D[s 2 ]+w(s 2, s 3 )=6+2=8, D[s 4 ]=15 s 15 8 s 3 10 3 4 5 s 1 2 1 s 2 Solution = {(s, 0), (s 1, 5), (s 2, 6), (s 3, 8), (s 4, 15) }

Dijkstra s Algorithm for Finding the Shortest Distance from a Single Source Dijkstra(G,s) 1. for each v V 2. do D [ v ] 3. D [ s ] 0 4. MH make-mh(d,v ) // MH: MinHeap 5. while MH 6. do near MH.extractMin (Queue) 7. for each v Adj(near ) 8 if D [ v ] > D [ near ] + w( near,v ) 9. then D [ v ] D [ near ] + w( near, v ) 10. MH.decreaseDistance (D[v], v ) 11. return the label D[u] of each vertex u

Time Complexity Analysis 1. for each v V 2. do D [ v ] 3. D [ s ] 0 4. MH make-mh(d,v) 5. while MH 6. do near MH.extractMin () 7. for each v Adj(near ) 8 if D [ v ] > D [ near ] + w( near,v ) 9. then D [ v ] D[near] + w(near,v) 10. MH.decreaseDistance (D[v ], v ) 11. return the label D[u] of each vertex u Assume a node in MH can be accessed in O(1) Using Heap implementation Lines 1-4 run in O (V ) Max Size of MH is V (5) Loop = O (V ) (6) O(lg V) (5+6) O (V lg V ) (7, 8, 9) are O(1) and executed O(E) times in total (10) Decrease- Key operation on the heap takes O( lg V) time, and is executed O(E) times in total O(E lg V) So total time is O ( V lg V + E lg V )

Alternative way to implement Dijkstra s algorithm Use an array instead of a MinHeap Page 158-159 in the text Time Complexity O(V) to extract min O(1) for decreasedistance Thus, O(V 2 ) in total