CSE 373: Data Structures & Algorithms Graph Traversals: Dijkstra’s

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
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.
CSE332: Data Abstractions Lecture 17: Shortest Paths Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
CSE373: Data Structures & Algorithms Lecture 17: Shortest Paths Nicki Dell Spring 2014.
CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.
CSE332: Data Abstractions Lecture 17: Shortest Paths Dan Grossman Spring 2012.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
CSE373: Data Structures & Algorithms Lecture 17: Dijkstra’s Algorithm Lauren Milne Summer 2015.
CSE 373: Data Structures & Algorithms Lecture 17: Topological Sort / Graph Traversals Linda Shapiro Winter 2015.
CSE373: Data Structures & Algorithms Lecture 19: Dijkstra’s algorithm and Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 14: Topological Sort / Graph Traversals Dan Grossman Fall 2013.
CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Section 7: Dijkstra’s Justin Bare and deric Pang
CSE 373 Topological Sort Graph Traversals
Instructor: Lilian de Greef Quarter: Summer 2017
May 12th – Minimum Spanning Trees
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
Cse 373 May 8th – Dijkstras.
Algorithms and Data Structures Lecture XIII
Instructor: Lilian de Greef Quarter: Summer 2017
Instructor: Lilian de Greef Quarter: Summer 2017
CSE373: Data Structures & Algorithms Lecture 17: Shortest Paths
Instructor: Lilian de Greef Quarter: Summer 2017
More Graph Algorithms.
CSE 373: Data Structures & Algorithms Lecture 17: Topological Sort / Graph Traversals Linda Shapiro Spring 2016.
Why do Java programmers wear glasses?
What do you call 8 Hobbits?
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
CSE 373: Data Structures and Algorithms
Algorithms and Data Structures Lecture XIII
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Section 7: Dijkstra’s & Midterm Postmortem
Based on slides of Dan Suciu
Directed Graph Algorithms
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Slide Courtesy: Uwash, UT
Fundamental Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE332: Data Abstractions Lecture 17: Shortest Paths
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
CSE 417: Algorithms and Computational Complexity
CSE 373: Data Structures and Algorithms
Graphs.
Richard Anderson Spring 2016
Topological Sorting Minimum Spanning Trees Shortest Path
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

CSE 373: Data Structures & Algorithms Graph Traversals: Dijkstra’s Riley Porter Winter 2017 CSE373: Data Structures & Algorithms

Course Logistics HW4 out  graphs! Topic Summary on Graphs coming out by tomorrow evening. We’ll add more after we finish Graphs next week. Grade computation guide (as best we can) out tonight. CSE373: Data Structures & Algorithms

Review: Graph Traversals For an arbitrary graph and a starting node v, find all nodes reachable from v (i.e., there exists a path from v) Basic idea of traversal: Keep following nodes But “mark” nodes after visiting them, so the traversal terminates and processes each reachable node exactly once CSE373: Data Structures & Algorithms

Review: DFS DFS (Node start) { initialize stack s to hold start mark start as visited while(s is not empty) { next = s.pop() // and “process” for each node u adjacent to next if(u is not marked) mark u and push onto s } A B D E C F H G A, C, F, H, G, B, E, D A different but perfectly fine depth traversal CSE373: Data Structures & Algorithms

Review: BFS BFS(Node start) { initialize queue q to hold start mark start as visited while(q is not empty) { next = q.dequeue() // and “process” for each node u adjacent to next if(u is not marked) mark u and enqueue onto q } A B D E C F H G A, B, C, D, E, F, G, H A “level-order” traversal CSE373: Data Structures & Algorithms

Review: Topological Sort One example output: 126 142 143 374 373 417 410 413 XYZ 415 labelAllAndEnqueueZeros(); while queue not empty { v = dequeue(); put v next in output for each w adjacent to v { w.indegree--; if (w.indegree==0) enqueue(v); } CSE 142 CSE 143 CSE 374 CSE 373 CSE 410 MATH 126 CSE 417 CSE 415 CSE 413 XYZ CSE373: Data Structures & Algorithms

Today: Shortest COST Path CSE373: Data Structures & Algorithms

Single source shortest paths Done: BFS to find the minimum path length from v to u in O(|E|+|V|) Actually, can find the minimum path length from v to every node Still O(|E|+|V|) No faster way for a “distinguished” destination in the worst-case Now: Weighted graphs Given a weighted graph and node v, find the minimum-cost path from v to every node As before, asymptotically no harder than for one destination Unlike before, BFS will not work -> only looks at path length. CSE373: Data Structures & Algorithms

Shortest Path: Applications Driving directions Cheap flight itineraries Network routing Critical paths in project management CSE373: Data Structures & Algorithms

Not as easy 10 5 100 100 100 100 A B -11 7 500 Why BFS won’t work: Shortest path may not have the fewest edges Annoying when this happens with costs of flights We will assume there are no negative weights Problem is ill-defined if there are negative-cost cycles Today’s algorithm is wrong if edges can be negative There are other, slower (but not terrible) algorithms CSE373: Data Structures & Algorithms

Dijkstra: an important CS “founder” Algorithm named after its inventor Edsger Dijkstra (1930-2002) A good Dijkstra quote: “computer science is no more about computers than astronomy is about telescopes” My favorite Dijkstra joke: “Well, obviously he had to go into computer science, he has ijk in his name! He’s basically built for writing loops” CSE373: Data Structures & Algorithms

Dijkstra’s algorithm The idea: reminiscent of BFS, but adapted to handle weights Grow the set of nodes whose shortest distance has been computed Nodes not processed yet will have a “best distance so far” A priority queue will turn out to be useful for efficiency CSE373: Data Structures & Algorithms

Dijkstra’s Algorithm: Idea 2 4  2 2 B 3 A F H 1 2 1 5 10 4 9 3 G  1 C 2 11 1 D 4 E 7 12 Initially, start node has cost 0 and all other nodes have cost  At each step: Pick closest unknown vertex v Add it to the “cloud” of known vertices Update distances for nodes with edges from v That’s it! (But we need to prove it produces correct answers) CSE373: Data Structures & Algorithms

The Algorithm For each node v, set v.cost =  and v.known = false Set source.cost = 0 While there are unknown nodes in the graph Select the unknown node v with lowest cost Mark v as known For each edge (v,u) with weight w, c1 = v.cost + w // cost of best path through v to u c2 = u.cost // cost of best path to u previously known if(c1 < c2){ // if the path through v is better u.cost = c1 u.path = v // for computing actual paths } CSE373: Data Structures & Algorithms

Example #1    2 2 B 3 A F H 1 2 1 5 10 4 9 3 G   C 2 11 1 D  E 7   2 2 B 3 A F H 1 2 1 5 10 4 9 3 G   C 2 11 1 D  E 7  vertex known? cost path A B  C D E F G H Order Added to Known Set: CSE373: Data Structures & Algorithms

Example #1 2   2 2 B 3 A F H 1 2 1 5 10 4 9 3 G  1 C 2 11 1 D 4 E 7  vertex known? cost path A Y B  2 C  1 D  4 E  F G H Order Added to Known Set: A CSE373: Data Structures & Algorithms

Example #1 2   2 2 B 3 A F H 1 2 1 5 10 4 9 3 G  1 C 2 11 1 D 4 E 7 12 vertex known? cost path A Y B  2 C 1 D  4 E  12 F  G H Order Added to Known Set: A, C CSE373: Data Structures & Algorithms

Example #1 2 4  2 2 B 3 A F H 1 2 1 5 10 4 9 3 G  1 C 2 11 1 D 4 E 7 12 vertex known? cost path A Y B 2 C 1 D  4 E  12 F G  H Order Added to Known Set: A, C, B CSE373: Data Structures & Algorithms

Example #1 2 4  2 2 B 3 A F H 1 2 1 5 10 4 9 3 G  1 C 2 11 1 D 4 E 7 12 vertex known? cost path A Y B 2 C 1 D 4 E  12 F  4 G  H Order Added to Known Set: A, C, B, D CSE373: Data Structures & Algorithms

Example #1 2 4 7 2 2 B 3 A F H 1 2 1 5 10 4 9 3 G  1 C 2 11 1 D 4 E 7 12 vertex known? cost path A Y B 2 C 1 D 4 E  12 F G  H  7 Order Added to Known Set: A, C, B, D, F CSE373: Data Structures & Algorithms

Example #1 2 4 7 2 2 B 3 A F H 1 2 1 5 10 4 9 3 G 8 1 C 2 11 1 D 4 E 7 12 vertex known? cost path A Y B 2 C 1 D 4 E  12 F G  8 H 7 Order Added to Known Set: A, C, B, D, F, H CSE373: Data Structures & Algorithms

Example #1 2 4 7 2 2 B 3 A F H 1 2 1 5 10 4 9 3 G 8 1 C 2 11 1 D 4 E 7 11 vertex known? cost path A Y B 2 C 1 D 4 E  11 G F 8 H 7 Order Added to Known Set: A, C, B, D, F, H, G CSE373: Data Structures & Algorithms

Example #1 2 4 7 2 2 B 3 A F H 1 2 1 5 10 4 9 3 G 8 1 C 2 11 1 D 4 E 7 11 vertex known? cost path A Y B 2 C 1 D 4 E 11 G F 8 H 7 Order Added to Known Set: A, C, B, D, F, H, G, E CSE373: Data Structures & Algorithms

Features When a vertex is marked known, the cost of the shortest path to that node is known The path is also known by following back-pointers While a vertex is still not known, another shorter path to it might still be found Note: The “Order Added to Known Set” is not important A detail about how the algorithm works (client doesn’t care) Not used by the algorithm (implementation doesn’t care) It is sorted by path-cost, resolving ties in some way Helps give intuition of why the algorithm works CSE373: Data Structures & Algorithms

Interpreting the Results Now that we’re done, how do we get the path from, say, A to E? A B D C F H E G 2 4 7 1 11 8 3 10 9 5 vertex known? cost path A Y B 2 C 1 D 4 E 11 G F 8 H 7 Order Added to Known Set: A, C, B, D, F, H, G, E CSE373: Data Structures & Algorithms

Interpreting the Results Now that we’re done, how do we get the path from, say, A to E? A B D C F H E G 2 4 7 1 11 8 3 10 9 5 vertex known? cost path A Y B 2 C 1 D 4 E 11 G F 8 H 7 Order Added to Known Set: A, C, B, D, F, H, G, E CSE373: Data Structures & Algorithms

Stopping Short How would this have worked differently if we were only interested in: The path from A to F? A B D C F H E G 2 4 7 1 11 8 3 10 9 5 vertex known? cost path A Y B 2 C 1 D 4 E 11 G F 8 H 7 Order Added to Known Set: A, C, B, D, F, H, G, E CSE373: Data Structures & Algorithms

Stopping Short 2 4 7 2 2 B 3 A F H 1 2 1 5 10 4 9 3 G  1 C 2 11 1 D 4 4 7 2 2 B 3 A F H 1 2 1 5 10 4 9 3 G  1 C 2 11 1 D 4 E 7 12 vertex known? cost path A Y B 2 C 1 D 4 E  12 F G  H  7 Order Added to Known Set: A, C, B, D, F CSE373: Data Structures & Algorithms

Example #2  2 B A 1  1 5 2 E  1 D 1 3 5 C   6 2 G  10 F 2 B A 1  1 5 2 E  1 D 1 3 5 C   6 2 G vertex known? cost path A B  C D E F G  10 F Order Added to Known Set: CSE373: Data Structures & Algorithms

Example #2  2 B A 1  1 5 2 E 1 1 D 1 3 5 C 2  6 2 G vertex known? cost path A Y B  C 2 D  1 E F G  10 F Order Added to Known Set: A CSE373: Data Structures & Algorithms

Example #2 6 2 B A 1 2 1 5 2 E 1 1 D 1 3 5 C 2 6 6 2 G vertex known? cost path A Y B  6 D C 2 1 E  2 F  7 G 7 10 F Order Added to Known Set: A, D CSE373: Data Structures & Algorithms

Example #2 6 2 B A 1 2 1 5 2 E 1 1 D 1 3 5 C 2 6 6 2 G vertex known? cost path A Y B  6 D C 2 1 E  2 F  4 G 4 10 F Order Added to Known Set: A, D, C CSE373: Data Structures & Algorithms

Example #2 3 2 B A 1 2 1 5 2 E 1 1 D 1 3 5 C 2 6 6 2 G vertex known? cost path A Y B  3 E C 2 D 1 F  4 G  6 4 10 F Order Added to Known Set: A, D, C, E CSE373: Data Structures & Algorithms

Example #2 3 2 B A 1 2 1 5 2 E 1 1 D 1 3 5 C 2 6 6 2 G vertex known? cost path A Y B 3 E C 2 D 1 F  4 G  6 4 10 F Order Added to Known Set: A, D, C, E, B CSE373: Data Structures & Algorithms

Example #2 3 2 B A 1 2 1 5 2 E 1 1 D 1 3 5 C 2 6 6 2 G vertex known? cost path A Y B 3 E C 2 D 1 F 4 G  6 4 10 F Order Added to Known Set: A, D, C, E, B, F CSE373: Data Structures & Algorithms

Example #2 3 2 B A 1 2 1 5 2 E 1 1 D 1 3 5 C 2 6 6 2 G vertex known? cost path A Y B 3 E C 2 D 1 F 4 G 6 4 10 F Order Added to Known Set: A, D, C, E, B, F, G CSE373: Data Structures & Algorithms

Example #3 1 1 1 1 X 60 50 80 70 … 90 Y As the algorithm runs, how will the best-cost-so-far for Y change? Is this expensive? CSE373: Data Structures & Algorithms

Example #3 1 1 1 1 X 60 50 80 70 … 90 Y As the algorithm runs, how will the best-cost-so-far for Y change? , 90, 81, 72, 63, 54, … Is this expensive? No, each edge is processed only once CSE373: Data Structures & Algorithms

A Greedy Algorithm A greedy algorithm: Example: Dijkstra’s algorithm At each step, irrevocably does what seems best at that step A locally optimal step, not known to be globally optimal Once a vertex is known, it is not revisited Turns out to be globally optimal Example: Dijkstra’s algorithm For single-source shortest paths in a weighted graph (directed or undirected) with no negative-weight edges CSE373: Data Structures & Algorithms

Where are We? Had a problem: Compute shortest paths in a weighted graph with no negative weights Learned an algorithm: Dijkstra’s algorithm What should we do after learning an algorithm? Prove it is correct Not obvious! We will sketch the key ideas Analyze its efficiency Will do better by using a data structure we learned earlier! CSE373: Data Structures & Algorithms

Correctness: Intuition Rough intuition: All the “known” vertices have the correct shortest path True initially: shortest path to start node has cost 0 If it stays true every time we mark a node “known”, then by induction this holds and eventually everything is “known” (we’ll get to talk about induction soon! ) When we mark a vertex “known” we won’t discover a shorter path later! This holds only because Dijkstra’s algorithm picks the node with the next shortest path-so-far The proof is by contradiction… CSE373: Data Structures & Algorithms

Correctness: The Cloud (Rough Sketch) The Known Cloud v Next shortest path from inside the known cloud w Better path to v? No! Source Suppose v is the next node to be marked known (“added to the cloud”) The best-known path to v must have only nodes “in the cloud” Else we would have picked a node closer to the cloud than v Suppose the actual shortest path to v is different It won’t use only cloud nodes, or we would know about it So it must use non-cloud nodes. Let w be the first non-cloud node on this path. The part of the path up to w is already known and must be shorter than the best-known path to v. So v would not have been picked. Contradiction. CSE373: Data Structures & Algorithms

Naïve asymptotic running time Similar to topological sort, if we think of the algorithm as: loop that runs based on # of vertices: loop to find the next vertex to process: process step cost based on # edges Then the algorithm is O(|V|2) due to each iteration looking for the node to process next We solved it in Topological Sort with a queue of zero-degree nodes But here we need the next lowest-cost node, not necessarily the first node we put in our pending set. And costs can change as we process edges! CSE373: Data Structures & Algorithms

Improving asymptotic running time Solution? A priority queue holding all unknown nodes, sorted by cost But must support decreaseKey operation Must maintain a reference from each node to its current position in the priority queue Conceptually simple, but can be a pain to code up CSE373: Data Structures & Algorithms

Efficiency, second approach We’ll use pseudocode to determine asymptotic run-time dijkstra(Graph G, Node start) { for each node: x.cost=infinity, x.known=false start.cost = 0 build-heap with all nodes while(heap is not empty) { b = deleteMin() b.known = true for each edge (b,a) in G if(!a.known) if(b.cost + weight((b,a)) < a.cost){ decreaseKey(a,“new cost – old cost”) a.path = b } CSE373: Data Structures & Algorithms

Efficiency, second approach We’ll use pseudocode to determine asymptotic run-time dijkstra(Graph G, Node start) { for each node: x.cost=infinity, x.known=false start.cost = 0 build-heap with all nodes while(heap is not empty) { b = deleteMin() b.known = true for each edge (b,a) in G if(!a.known) if(b.cost + weight((b,a)) < a.cost){ decreaseKey(a,“new cost – old cost”) a.path = b } O(|V|) O(|V|log|V|) O(|E|log|V|) O(|V|log|V|+|E|log|V|) CSE373: Data Structures & Algorithms

Today’s Takeaways Understand how Dijkstra’s algorithm works be able to write the code for shortest path on your HW4 graph (this is part of HW5) understand why it is correct Be able to analyze the efficiency of Dijkstra’s algorithm CSE373: Data Structures & Algorithms