4.4 Shortest Paths in a Graph

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
Shortest Paths1 C B A E D F
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Paths Definitions Single Source Algorithms
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
Shortest Paths1 C B A E D F
© 2004 Goodrich, Tamassia Shortest Paths1 C B A E D F
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Weighted Graphs In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances,
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.
© 2010 Goodrich, Tamassia Shortest Paths1 C B A E D F
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Shortest Paths C B A E D F
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
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 7 Greedy Graph.
Lecture 16. 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)
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
Lecture 13 Algorithm Analysis
1 Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 Chapter 5-2 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 Sections 2.5 and 4.4 Priority Queues and Dijkstra’s Algorithm For Shortest Paths Some Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley.
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,
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 10.
© 2010 Goodrich, Tamassia Shortest Paths1 C B A E D F
Shortest Paths C B A E D F Shortest Paths
Chapter 4 Greedy Algorithms
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,
Graph Algorithms BFS, DFS, Dijkstra’s.
Shortest Paths C B A E D F Shortest Paths 1
Shortest Paths C B A E D F Shortest Paths
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
Shortest Paths C B A E D F Shortest Paths
Chapter 4 Greedy Algorithms
EMIS 8374 Dijkstra’s Algorithm Updated 18 February 2008
Chapter 4 Greedy Algorithms
Data Structures and Algorithms CMPSC 465
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
4.1 Interval Scheduling.
Shortest Paths C B A E D F Shortest Paths
Shortest Paths C B A E D F Shortest Paths
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Minimum Spanning Tree Algorithms
Lecture 13 Algorithm Analysis
Slide Courtesy: Uwash, UT
Lecture 13 Algorithm Analysis
Shortest Paths.
CSE 373 Data Structures and Algorithms
Brad Karp UCL Computer Science
Slide Courtesy: Uwash, UT
Chapter 4 Greedy Algorithms
Graph Algorithms: Shortest Path
CSE 417: Algorithms and Computational Complexity
Dijkstra Algorithm examples
Lecture 12 Shortest Path.
Data Structures and Algorithm Analysis Lecture 8
Presentation transcript:

4.4 Shortest Paths in a Graph map from Microsoft Streets and Trips shortest path from Princeton CS department to Einstein's house

Shortest Path Problem Shortest path network. Directed graph G = (V, E). Source s, destination t. Length e = length of edge e. Shortest path problem: find shortest directed path from s to t. cost of path = sum of edge costs in path Cost of path s-2-3-5-t = 9 + 23 + 2 + 16 = 48. 2 23 3 9 s Cost of path s-6-5-t = 14 + 30 + 16 = 60. 18 14 2 6 6 30 4 19 5 11 15 5 6 Shortest path need not have fewest # edges 20 16 t 7 44

Applications of Shortest Path Algorithms Driving directions (Mapquest, Google Maps) Routing protocols on the Internet (BGP, OSPF) Decision making and AI Robot motion planning Games Image and speech analysis problems Finding associations

Dijkstra's Algorithm Dijkstra's algorithm. Maintain a set of explored nodes S for which we have determined the shortest path distance d(u) from s to u. Initialize S = { s }, d(s) = 0, (v) = ∞, for each v≠s Repeatedly choose unexplored node v which minimizes add v to S, and set d(v) = (v). shortest path to some u in explored part, followed by a single edge (u, v) e v d(u) u S s

Dijkstra's Algorithm Dijkstra's algorithm. Maintain a set of explored nodes S for which we have determined the shortest path distance d(u) from s to u. Initialize S = { s }, d(s) = 0 , (v) = ∞, for each v≠s Repeatedly choose unexplored node v which minimizes add v to S, and set d(v) = (v). S is only needed for the proof of correctness, not the algorithm itself shortest path to some u in explored part, followed by a single edge (u, v) e v d(u) u S s

Dijkstra's Algorithm: Proof of Correctness Invariant. For each node u  S, d(u) is the length of the shortest s-u path. Pf. (by induction on |S|) Base case: |S| = 1 is trivial. Inductive hypothesis: Assume true for |S| = k  1. Let v be next node added to S, and let u-v be the chosen edge. The shortest s-u path plus (u, v) is an s-v path of length (v). Consider any s-v path P. We'll see that it's no shorter than (v). Let x-y be the first edge in P that leaves S, and let P' be the subpath to x. P is already too long as soon as it leaves S. Consider any path P from s to v. It is already at least as long as (v) by the time it leaves S. P P' x y s S u v  (P)   (P') +  (x,y)  d(x) +  (x, y)  (y)  (v) nonnegative weights inductive hypothesis defn of (y) Dijkstra chose v instead of y

Dijkstra's Shortest Path Algorithm Find shortest path from s to t. 2 24 3 9 s 18 14 2 6 6 4 30 19 11 15 5 5 6 20 16 t 7 44

Dijkstra's Shortest Path Algorithm PQ = { s, 2, 3, 4, 5, 6, 7, t }   2 24 3 9 s 18 14  2 6 6  4 30  19 11 15 5 5 6 20 16 t 7 44  distance label 

Dijkstra's Shortest Path Algorithm PQ = { s, 2, 3, 4, 5, 6, 7, t } delmin   2 24 3 9 s 18 14  2 6 6  30  4 19 11 15 5 5 6 20 16 t 7 44  distance label 

Dijkstra's Shortest Path Algorithm PQ = { 2, 3, 4, 5, 6, 7, t } decrease key   X 9 2 24 3 9 s 18 14  X 14 2 6 6  4 30  19 11 15 5 5 6 20 16 t 7 44  distance label  15 X

Dijkstra's Shortest Path Algorithm PQ = { 2, 3, 4, 5, 6, 7, t } delmin   X 9 2 24 3 9 s 18 14  X 14 2 6 6  4 30  19 11 15 5 5 6 20 16 t 7 44  distance label  15 X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 6, 7, t }   X 9 2 24 3 9 s 18 14  X 14 2 6 6  30  4 19 11 15 5 5 6 20 16 t 7 44   15 X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 6, 7, t } decrease key  X 33  X 9 2 24 3 9 s 18 14  X 14 2 6 6   4 30 19 11 15 5 5 6 20 16 t 7 44   15 X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 6, 7, t }  X 33  X 9 2 24 3 9 delmin s 18 14  X 14 2 6 6   4 30 19 11 15 5 5 6 20 16 t 7 44   15 X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 7, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 30  X 4 19 11 15 5 5 6 20 16 t 7 44   15 X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, 7, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 30  X 4 19 11 15 5 5 6 20 16 t 7 44   15 delmin X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 X 35  X 4 30 19 11 15 5 5 6 20 16 t 7 44 59   15 X X

Dijkstra's Shortest Path Algorithm PQ = { 3, 4, 5, t } delmin 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 X 35 4 30  X 19 11 15 5 5 6 20 16 t 7 44 59   15 X X

Dijkstra's Shortest Path Algorithm PQ = { 4, 5, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 X 35 X 34 4 30  X 19 11 15 5 5 6 20 16 t 7 44 51 59   15 X X X

Dijkstra's Shortest Path Algorithm PQ = { 4, 5, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6  44 X 35 X 34 30  X 4 19 11 15 5 5 6 20 delmin 16 t 7 44 51 59   15 X X X

Dijkstra's Shortest Path Algorithm PQ = { 4, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X 30  X 4 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59   15 X X X

Dijkstra's Shortest Path Algorithm PQ = { 4, t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X 30  X 4 19 11 15 5 delmin 5 6 20 16 t 7 44 50 51 X 59   15 X X X

Dijkstra's Shortest Path Algorithm PQ = { t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X 30  X 4 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59   15 X X X

Dijkstra's Shortest Path Algorithm PQ = { t } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X 30  X 4 19 11 15 5 5 6 20 16 t 7 44 delmin 50 51 X 59   15 X X X

Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X  X 4 30 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59   15 X X X

Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { } 32  X 33 X  X 9 2 24 3 9 s 18 14  X 14 2 6 6 45  44 X 35 X 34 X  X 4 30 19 11 15 5 5 6 20 16 t 7 44 50 51 X 59   15 X X X

Dijkstra’s Implementation: Approach I In each iteration: For each v  S, compute π(v) = min(u,v)E: uS{d(u)+l(u,v)} O(deg(v)) steps Choose v so that π(v) is minimum O(n) steps Add v to S and set d(v)= π(v) Total running time: O(∑v deg(v) + n) = O(m+n) per iteration  O(nm) time overall

Dijkstra’s Implementation: Approach II Useful Property: If node u added to S in any iteration, π(v) can change only if (u,v) is an edge Modification: In any iteration if node u is added to S, update π(v) only for neighbors v of u  O(deg(u)) time to update π values in each iteration  O(∑u deg(u)) = O(m) time to update π values over all iterations What about the time to find the node with minimum π value in each iteration? Storing in a list  O(n) time in each iteration to find the minimum Can we do better?

Priority Queues Insert(P, v): O(log n) steps FindMin(P): O(1) steps Delete(P,i): O(log n) steps ExtractMin(P): O(log n) steps ChangeKey(P,v,k): O(log n) steps

Dijkstra’s Algorithm using Priority Queues Store each node v with key π(v) in PQ Choose node v with minimum π(v) by ExtractMin operation For each neighbor w of chosen node v, ChangeKey(PQ,w, π(w)) if π(w) changes Total running time: one ExtractMin per node and at most one ChangeKey per edge = O(m log n) time

Dijkstra's Algorithm: Implementation For each unexplored node, explicitly maintain Next node to explore = node with minimum (v). When exploring v, for each incident edge e = (v, w), update Efficient implementation. Maintain a priority queue of unexplored nodes, prioritized by (v). Priority Queue PQ Operation Dijkstra Array Binary heap d-way Heap Fib heap † Insert n n log n d log d n 1 ExtractMin n n log n d log d n log n ChangeKey m 1 log n log d n 1 IsEmpty n 1 1 1 1 Total n2 m log n m log m/n n m + n log n † Individual ops are amortized bounds

Edsger W. Dijkstra The question of whether computers can think is like the question of whether submarines can swim. Do only what only you can do. In their capacity as a tool, computers will be but a ripple on the surface of our culture. In their capacity as intellectual challenge, they are without precedent in the cultural history of mankind. The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offence. APL is a mistake, carried through to perfection. It is the language of the future for the programming techniques of the past: it creates a new generation of coding bums.