Dijkstra Algorithm.

Slides:



Advertisements
Similar presentations
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Advertisements

1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Minimum Spanning Tree Algorithms
1.1 Data Structure and Algorithm Lecture 11 Application of BFS  Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source.
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
Dijkstra's algorithm.
Topological Sorting and Least-cost Path Algorithms.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
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.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
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.
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)
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,
Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated ‘length’ c ij (cost, time, distance, …). Determine a path of shortest.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Shortest Paths.
ליאור שפירא, חיים קפלן וחברים
Shortest Path 6/18/2018 4:22 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Single-Source Shortest Paths
Algorithms and Data Structures Lecture XIII
Minimum Spanning Trees
Graphs Chapter 15 explain graph-based algorithms Graph definitions
SINGLE-SOURCE SHORTEST PATHS
Shortest Paths.
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Analysis and design of algorithm
Announcement 2: A 2 hour midterm (open book) will be given on March (Tuesday) during the lecture time. 2018/12/4.
CSE 373: Data Structures and Algorithms
2018/12/27 chapter25.
Advanced Algorithms Analysis and Design
Lecture 11 Topics Application of BFS Shortest Path
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Shortest Path Algorithms
Shortest path algorithm
Minimum Spanning Tree Algorithms
CSC 413/513: Intro to Algorithms
Floyd’s Algorithm (shortest-path problem)
Lecture 13 Algorithm Analysis
Slide Courtesy: Uwash, UT
Advanced Algorithms Analysis and Design
Lecture 13 Algorithm Analysis
CSE 373: Data Structures and Algorithms
Shortest Path Problems
Single-Source Shortest Paths
Shortest Paths.
Dijkstra’s Shortest Path Algorithm
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
CSC 380: Design and Analysis of Algorithms
Graph Algorithms: Shortest Path
Shortest Paths.
CSE 417: Algorithms and Computational Complexity
Dijkstra Algorithm examples
Graphs: Shortest path and mst
Single-Source Shortest Path & Minimum Spanning Trees
Topological Sorting Minimum Spanning Trees Shortest Path
Data Structures and Algorithm Analysis Lecture 8
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Dijkstra Algorithm

Single-Source Shortest Paths We wish to find the shortest route between Binghamton and NYC. Given a NYS road map with all the possible routes how can we determine our shortest route? We could try to enumerate all possible routes. It is certainly easy to see we do not need to consider a route that goes through Buffalo.

Modeling 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 the direction of the edge. The problem: Given a weighted digraph and a vertex s in the graph: find a shortest path from s to t

The distance of a shortest path Case: The graph may have negative edges but no negative cycles. The shortest distance from s to t can be computed. A B s t -3 1 8 d(s,t)=6

Shortest Path Algorithms Dijkstra’s algorithm does NOT allow negative edges. Uses a greedy heuristic. undirected/directed graph

Solving Optimization Problems Solving strategy: 2 stages: First Stage: solves an intermediate problem and saves an additional data structure. Second Stage: uses the data Structure to construct the solution to the problem.

Dijkstra’s shortest path algorithm 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 for finding 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, s1)  d(s, s2)  …  d(s, sn-1) In this case a shortest path from s to si may include any of the vertices {s1, s2 … si-1} but cannot include any sj where j > i. Dijkstra’s main idea is to select the nodes and compute the shortest distances in the order s, s1, s2 ,…, sn-1

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

Dijkstra’s greedy selection rule Assume s1, s2 … si-1 have been selected, and their shortest distances have been stored in Solution Select node si and save d(s, si) if si has the shortest distance from s on a path that may include only s1, s2 … si-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 s4 s3 s2 s s1 Solution = {(s, 0)} D[s1]=5 for path [s, s1] D[s2]=  for path [s, s2] D[s3]=10 for path [s, s3] D[s4]=15 for path [s, s4]. Solution = {(s, 0), (s1, 5) } D[s2]= 6 for path [s, s1, s2] D[s3]=9 for path [s, s1, s3] D[s4]=15 for path [s, s4] Solution = {(s, 0), (s1, 5), (s2, 6) } D[s3]=8 for path [s, s1, s2, s3] Solution = {(s, 0), (s1, 5), (s2, 6),(s3, 8), (s4, 15) } s s1 5 10 3 1 4 2 s3 s2 s4 15 8

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

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 ) D[near ] = 5 2 Solution after adding near 3 2 s D[ v ] = 9 is updated to D[v]=5+2=7 3 6

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

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. PQ make-PQ(D,V) 5. while PQ   6. do near  PQ.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. PQ.decreasePriorityValue (D[v], v ) 11. return the label D[u] of each vertex u

Time Analysis 1. for each v  V 2. do D [ v ]   3. D [ s ]  0 4. PQ make-PQ(D,V) 5. while PQ   6. do near  PQ.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. PQ.decreasePriorityValue (D[v ], v ) 11. return the label D[u] of each vertex u Assume a node in PQ can be accessed in O(1) ** Decrease key for v requires O(lgV ) provided the node in heap with v’s data can be accessed in O(1) Using Heap implementation Lines 1 -4 run in O (V ) Max Size of PQ is | V | (5) Loop = O (V ) - Only decreases (6+(5)) O (V )  O( lg V ) (7+(5)) Loop = O(deg(near) ) = O( E ) (8+(7+(5))) O(1)O( E ) (9) O(1) (10+(7+(5))) Decrease- Key operation on the heap can be implemented in O( lg V)  O( E ). So total time for Dijkstra's Algorithm is O ( V lg V + E lg V ) For Sparse Graph = O(V lg V ) For Dense Graph = O(V2 lg V ) In order to achieve a O(lg V ) cost for decrease key you can use an additional data structure like a Hashtable to store the location in the PQ.

Example a 4 4 2 b c 2 1 10 5 d e

Solution for example S D(a) D(b) D(c) D(d) D(e) a 0 ( ) 4 (a, b ) 1 4 2 b c 2 1 10 5 d e S D(a) D(b) D(c) D(d) D(e) a 0 ( ) 4 (a, b ) 1 (a, c )  ( ) c 3 ( c, b) 2 (c, d) 3 (c, e) d 3 (c, b) e b