Section 7: Dijkstra’s Algorithm Slides adapted from Alex Mariakakis with material by Kellen Donohue, David Mailhot, Dan Grossman, Mike Ernst, Michael Hart, and Jacob Murphy
Review: Shortest Paths with BFS 1 B From Node B Destination Path Cost A 1 A <B,A> 1 1 1 B <B> 0 C <B,A,C> 2 1 D <B,D> 1 C D E <B,D,E> 2 1 1 E
Review: Shortest Paths with BFS 1 B From Node B Destination Path Cost A 1 A <B,A> 1 1 1 B <B> 0 C <B,A,C> 2 1 D <B,D> 1 C D E <B,D,E> 2 1 1 E
Shortest Paths with Weights 2 B B -> E = 106? A 100 100 3 2 C D 6 2 E
How can we find the shortest path with weights? 2 B A 100 100 3 2 C D 6 2 E
Shortest Paths with Weights 2 B From Node B Destination Path Cost A 100 A <B,A> 2 100 3 B <B> 0 C <B,A,C> 5 2 D <B,A,C,D> 7 C D E <B,A,C,E> 7 6 2 E Paths are not the same!
BFS vs. Dijkstra’s Algorithm 1 100 100 1 100 100 5 -10 500 BFS can find the most direct path, but not necessarily the shortest path! Note that Dijkstra’s Algorithm only works if there is not a negative cycle
Dijkstra’s Algorithm Named after its inventor Edsger Dijkstra (1930-2002) ◦ Among his contributions to the growing CS community was his work on Operating Systems, in which he motivated the design and structure of a large project, not just the code The Algorithm: similar to BFS, but incorporating weights ◦ Create a set of nodes to examine next, but instead of using the node that was next in line, use the node with the shortest distance ◦ How can you find the node with the shortest distance so far? Priority Queues (explained later)
Dijkstra’s Algorithm Give a node two fields: cost and finished, cost gives an upper bound to the distance from the origin to that node, and finished describing if the cost of the node is the minimum cost of travelling to that node. 1. For each node v , set v.cost = ∞ and v.finished = false 2. Set source.cost = 0 (source is the starting node of our path) 3. While there are unknown nodes in the graph Select the unknown node v with lowest cost a) Mark v as finalized b) For each edge ( v,u ) with weight w , c) 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 new path through v is better,update u.cost = c1 u.path = v // add v to the nodes u has traversed
Example #1 0 2 2 3 B A F H Goal: Find the best paths 1 2 1 5 10 from A to the other nodes 4 9 3 G C 2 11 D 1 E vertex known? cost path 7 A Y 0 ∞ B ∞ C ∞ D Order Added to Known Set: ∞ E ∞ F ∞ G ∞ H
Example #1 2 0 2 2 3 B A F H 1 2 1 5 10 4 9 3 G 1 C 2 11 D 1 4 E vertex known? cost path 7 A Y 0 ≤ 2 B A ≤ 1 C A ≤ 4 D A Order Added to Known Set: ∞ E ∞ F A ∞ G ∞ H
Example #1 2 0 2 2 3 B A F H 1 2 1 5 10 4 9 3 G 1 C 2 11 D 1 4 E vertex known? cost path 7 A Y 0 ≤ 2 B A C Y 1 A ≤ 4 D A Order Added to Known Set: ∞ E ∞ F A, C ∞ G ∞ H
Example #1 2 0 2 2 3 B A F H 1 2 1 5 10 4 9 3 G 1 C 2 11 D 1 4 E vertex known? cost path 12 7 A Y 0 ≤ 2 B A C Y 1 A ≤ 4 D A Order Added to Known Set: ≤ 12 E C ∞ F A, C ∞ G ∞ H
Example #1 2 0 2 2 3 B A F H 1 2 1 5 10 4 9 3 G 1 C 2 11 D 1 4 E vertex known? cost path 12 7 A Y 0 B Y 2 A C Y 1 A ≤ 4 D A Order Added to Known Set: ≤ 12 E C ∞ F A, C, B ∞ G ∞ H
Example #1 2 4 0 2 2 3 B A F H 1 2 1 5 10 4 9 3 G 1 C 2 11 D 1 4 E vertex known? cost path 12 7 A Y 0 B Y 2 A C Y 1 A ≤ 4 D A Order Added to Known Set: ≤ 12 E C ≤ 4 F B A, C, B ∞ G ∞ H
Example #1 2 4 0 2 2 3 B A F H 1 2 1 5 10 4 9 3 G 1 C 2 11 D 1 4 E vertex known? cost path 12 7 A Y 0 B Y 2 A C Y 1 A D Y 4 A Order Added to Known Set: ≤ 12 E C ≤ 4 F B A, C, B, D ∞ G ∞ H
Example #1 2 4 0 2 2 3 B A F H 1 2 1 5 10 4 9 3 G 1 C 2 11 D 1 4 E vertex known? cost path 12 7 A Y 0 B Y 2 A C Y 1 A D Y 4 A Order Added to Known Set: ≤ 12 E C F Y 4 B A, C, B, D, F ∞ G ∞ H
Example #1 2 4 7 0 2 2 3 B A F H 1 2 1 5 10 4 9 3 G 1 C 2 11 D 1 4 E vertex known? cost path 12 7 A Y 0 B Y 2 A C Y 1 A D Y 4 A Order Added to Known Set: ≤ 12 E C F Y 4 B A, C, B, D, F ∞ G ≤ 7 H F
Example #1 2 4 7 0 2 2 3 B A F H 1 2 1 5 10 4 9 3 G 1 C 2 11 D 1 4 E vertex known? cost path 12 7 A Y 0 B Y 2 A C Y 1 A D Y 4 A Order Added to Known Set: ≤ 12 E C F Y 4 B A, C, B, D, F, H ∞ G H Y 7 F
Example #1 2 4 7 0 2 2 3 B A F H 1 2 1 5 10 4 9 3 8 G 1 C 2 11 D 1 4 E vertex known? cost path 12 7 A Y 0 B Y 2 A C Y 1 A D Y 4 A Order Added to Known Set: ≤ 12 E C F Y 4 B A, C, B, D, F, H ≤ 8 G H H Y 7 F
Example #1 2 4 7 0 2 2 3 B A F H 1 2 1 5 10 4 9 3 8 G 1 C 2 11 D 1 4 7 E vertex known? cost path 12 A Y 0 B Y 2 A C Y 1 A D Y 4 A Order Added to Known Set: ≤ 12 E C F Y 4 B A, C, B, D, F, H, G G Y 8 H H Y 7 F
Example #1 2 4 7 0 2 2 3 B A F H 1 2 1 5 10 4 9 3 8 G 1 C 2 11 D 1 4 7 E vertex known? cost path 11 A Y 0 B Y 2 A C Y 1 A D Y 4 A Order Added to Known Set: ≤ 11 E G F Y 4 B A, C, B, D, F, H, G G Y 8 H H Y 7 F
Example #1 2 4 7 0 2 2 3 B A F H 1 2 1 5 10 4 9 3 8 G 1 C 2 11 D 1 4 E vertex known? cost path 11 7 A Y 0 B Y 2 A C Y 1 A D Y 4 A Order Added to Known Set: E Y 11 G F Y 4 B A, C, B, D, F, H, G, E G Y 8 H H Y 7 F
Interpreting the Results 2 4 7 0 vertex known? cost path 2 2 3 B A F H A Y 0 1 2 1 B Y 2 A 5 10 4 9 8 G 3 C Y 1 A 1 C 2 11 D D Y 4 A 4 1 E 11 7 E Y 11 G F Y 4 B G Y 8 H H Y 7 F
Interpreting the Results 2 4 7 0 vertex known? cost path 2 2 3 B A F H A Y 0 1 2 1 B Y 2 A 5 10 4 9 8 G 3 C Y 1 A 1 C 2 11 D D Y 4 A 4 1 E 11 7 E Y 11 G F Y 4 B G Y 8 H A H Y 7 F
Interpreting the Results 2 4 7 0 vertex known? cost path 2 2 3 B A F H A Y 0 1 2 1 B Y 2 A 5 10 4 9 8 G 3 C Y 1 A 1 C 2 11 D D Y 4 A 4 1 E 11 7 E Y 11 G F Y 4 B 2 G Y 8 H B A H Y 7 F 1 4 C D
Interpreting the Results 2 4 7 0 vertex known? cost path 2 2 3 B A F H A Y 0 1 2 1 B Y 2 A 5 10 4 9 8 G 3 C Y 1 A 1 C 2 11 D D Y 4 A 4 1 E 11 7 E Y 11 G F Y 4 B 2 2 G Y 8 H B A F H Y 7 F 1 4 C D
Interpreting the Results 2 4 7 0 vertex known? cost path 2 2 3 B A F H A Y 0 1 2 1 B Y 2 A 5 10 4 9 8 G 3 C Y 1 A 1 C 2 11 D D Y 4 A 4 1 E 11 7 E Y 11 G F Y 4 B 2 2 G Y 8 H 3 B A F H H Y 7 F 1 4 C D
Interpreting the Results 2 4 7 0 vertex known? cost path 2 2 3 B A F H A Y 0 1 2 1 B Y 2 A 5 10 4 9 8 G 3 C Y 1 A 1 C 2 11 D D Y 4 A 4 1 E 11 7 E Y 11 G F Y 4 B 2 2 G Y 8 H 3 B A F H H Y 7 F 1 1 4 G C D
Interpreting the Results 2 4 7 0 vertex known? cost path 2 2 3 B A F H A Y 0 1 2 1 B Y 2 A 5 10 4 9 8 G 3 C Y 1 A 1 C 2 11 D D Y 4 A 4 1 E 11 7 E Y 11 G F Y 4 B 2 2 G Y 8 H 3 B A F H H Y 7 F 1 1 4 G 3 C D E
Example #2 0 2 B A 1 1 5 E 2 1 D 1 3 5 C 6 G vertex known? cost path 2 10 A Y 0 F ∞ B ∞ C ∞ D Order Added to Known Set: ∞ E ∞ F ∞ G
Example #2 3 0 2 B A 1 2 1 5 E 2 1 1 D 1 3 5 C 2 6 6 G vertex known? cost path 2 4 10 A Y 0 F B Y 3 E C Y 2 A D Y 1 A Order Added to Known Set: E Y 2 D F Y 4 C A, D, C, E, B, F, G G Y 6 D
Pseudocode // pre-condition: start is the node to start at // initialize things active = new empty priority queue of paths from start to a given node // A path's “priority” in the queue is the total // cost of that path. finished = new empty set of nodes // Holds nodes for which we know the // minimum-cost path from start. // We know path start->start has cost 0 Add a path from start to itself to active
Pseudocode (cont.) while active is non-empty: minPath = active.removeMin() minDest = destination node in minPath if minDest is in finished: continue for each edge e = ⟨ minDest, child ⟩ : if child is not in finished: newPath = minPath + e add newPath to active add minDest to finished
Priority Queue Given a set of weighted paths, find the shortest path Increase efficiency by considering lowest cost unknown vertex with sorting instead of looking at all vertices PriorityQueue is like a queue, but returns elements by lowest value instead of FIFO
Recommend
More recommend