shortest paths
play

Shortest Paths A priority queue will turn out to be useful for - PDF document

2/21/2016 Dijkstras Algorithm The idea: reminiscent of BFS, but adapted to handle weights Grow the set of nodes whose shortest distance has been computed CSE373: Data Structures and Algorithms Nodes not in the set will have a


  1. 2/21/2016 Dijkstra’s Algorithm • The idea: reminiscent of BFS, but adapted to handle weights – Grow the set of nodes whose shortest distance has been computed CSE373: Data Structures and Algorithms – Nodes not in the set will have a “best distance so far” Shortest Paths – A priority queue will turn out to be useful for efficiency • An example of a greedy algorithm and Dijkstra's Algorithm – A series of steps – At each one the locally optimal choice is made Steve Tanimoto Winter 2016 This lecture material represents the work of multiple instructors at the University of Washington. Thank you to all who have contributed! Winter 2016 CSE 373: Data Structures & Algorithms 2 Dijkstra’s Algorithm: Idea The Algorithm 2 4 ∞ 0 2 2 B 3 A F H For each node v , set v.cost =  1. and v.known = false 1 2 1 5 10 4 2. Set source.cost = 0 9 3 ∞ G 1 C 3. While there are unknown nodes in the graph 2 11 1 D 4 E a) Select the unknown node v with lowest cost 12 7 b) Mark v as known Initially, start node has cost 0 and all other nodes have cost  c) For each edge (v,u) with weight w , • c1 = v.cost + w // cost of best path through v to u • At each step: // cost of best path to u previously known c2 = u.cost – Pick closest unknown vertex v // if the path through v is better if(c1 < c2){ – Add it to the “cloud” of known vertices u.cost = c1 – Update distances for nodes with edges from v u.path = v // for computing actual paths • That’s it! (But we need to prove it produces correct answers) Winter 2016 CSE 373: Data Structures & Algorithms 3 Winter 2016 CSE 373: Data Structures & Algorithms 4 Example #1 Example #1 ∞ 2 0 ∞ ∞ 0 ∞ ∞ 2 2 2 2 B 3 B 3 A F H A F H 1 1 2 1 2 1 5 10 5 10 4 4 9 3 ∞ 9 3 ∞ G G ∞ 1 C C 2 2 11 1 11 1 D D ∞ 4 E E ∞ ∞ 7 vertex known? cost path 7 vertex known? cost path A 0 A Y 0  2 B ?? B A  1 C ?? C A  4 D ?? D A Order Added to Known Set: Order Added to Known Set: E ?? E ?? F ?? F ?? A G ?? G ?? H ?? H ?? Winter 2016 CSE 373: Data Structures & Algorithms 5 Winter 2016 CSE 373: Data Structures & Algorithms 6 1

  2. 2/21/2016 Example #1 Example #1 2 2 ∞ ∞ 4 ∞ 0 0 2 2 2 2 B 3 B 3 A F H A F H 1 1 2 1 2 1 5 5 10 10 4 4 9 9 3 ∞ 3 ∞ G G 1 1 C C 2 2 11 1 11 1 D D 4 4 E E 12 12 7 7 vertex known? cost path vertex known? cost path A Y 0 A Y 0 B  2 A B Y 2 A C Y 1 A C Y 1 A D  4 A D  4 A Order Added to Known Set: Order Added to Known Set:  12  12 E C E C  4 F ?? F B A, C A, C, B G ?? G ?? H ?? H ?? Winter 2016 CSE 373: Data Structures & Algorithms 7 Winter 2016 CSE 373: Data Structures & Algorithms 8 Example #1 Example #1 2 2 4 ∞ 4 7 0 0 2 2 2 2 B 3 B 3 A F H A F H 1 1 2 1 2 1 5 10 5 10 4 4 9 3 ∞ 9 3 ∞ G G 1 1 C C 2 2 1 1 11 11 D D 4 4 E E 12 12 7 vertex known? cost path 7 vertex known? cost path A Y 0 A Y 0 B Y 2 A B Y 2 A C Y 1 A C Y 1 A D Y 4 A D Y 4 A Order Added to Known Set: Order Added to Known Set: E  12 C E  12 C F  4 B F Y 4 B A, C, B, D A, C, B, D, F G ?? G ??  7 H ?? H F Winter 2016 CSE 373: Data Structures & Algorithms 9 Winter 2016 CSE 373: Data Structures & Algorithms 10 Example #1 Example #1 2 2 0 4 7 0 4 7 2 2 2 2 B 3 B 3 A F H A F H 1 1 2 1 2 1 5 10 5 10 4 4 9 3 8 9 3 8 G G 1 1 C C 2 2 11 1 11 1 D D 4 4 E E 12 11 7 vertex known? cost path 7 vertex known? cost path A Y 0 A Y 0 B Y 2 A B Y 2 A C Y 1 A C Y 1 A D Y 4 A D Y 4 A Order Added to Known Set: Order Added to Known Set: E  12 C E  11 G F Y 4 B F Y 4 B A, C, B, D, F, H A, C, B, D, F, H, G G  8 H G Y 8 H H Y 7 F H Y 7 F Winter 2016 CSE 373: Data Structures & Algorithms 11 Winter 2016 CSE 373: Data Structures & Algorithms 12 2

  3. 2/21/2016 Example #1 Features 2 4 7 0 2 2 3 B A F H • When a vertex is marked known, 1 2 1 5 10 the cost of the shortest path to that node is known 4 9 3 8 G 1 – The path is also known by following back-pointers C 2 11 1 D 4 E 11 7 vertex known? cost path • While a vertex is still not known, A Y 0 another shorter path to it might still be found B Y 2 A C Y 1 A Note: The “Order Added to Known Set” is not important – A detail about how the algorithm works (client doesn’t care) D Y 4 A Order Added to Known Set: – Not used by the algorithm (implementation doesn’t care) E Y 11 G – It is sorted by path-cost, resolving ties in some way F Y 4 B A, C, B, D, F, H, G, E • Helps give intuition of why the algorithm works G Y 8 H H Y 7 F Winter 2016 CSE 373: Data Structures & Algorithms 13 Winter 2016 CSE 373: Data Structures & Algorithms 14 Interpreting the Results Stopping Short • Now that we’re done, how do we get the path from, say, A to E? • How would this have worked differently if we were only interested in: – The path from A to G? – The path from A to E? 2 2 0 4 7 0 4 7 2 2 2 2 B 3 B 3 A F H A F H vertex known? cost path vertex known? cost path 1 1 2 1 2 1 5 10 5 10 4 4 A Y 0 A Y 0 9 3 9 3 G 8 G 8 1 1 C C B Y 2 A B Y 2 A 2 2 1 1 11 11 D D 4 4 E C Y 1 A E C Y 1 A 11 11 7 7 D Y 4 A D Y 4 A Order Added to Known Set: Order Added to Known Set: E Y 11 G E Y 11 G F Y 4 B F Y 4 B A, C, B, D, F, H, G, E A, C, B, D, F, H, G, E G Y 8 H G Y 8 H H Y 7 F H Y 7 F Winter 2016 CSE 373: Data Structures & Algorithms 15 Winter 2016 CSE 373: Data Structures & Algorithms 16 Example #2 Example #2 ∞ ∞ 0 0 2 2 B B A 1 A 1 ∞ ∞ 1 1 5 5 2 E 2 E ∞ 1 1 1 D D 1 3 1 3 5 5 C C ∞ 2 ∞ ∞ 6 6 G vertex known? cost path G vertex known? cost path 2 2 ∞ 10 ∞ 10 A 0 A Y 0 F F B ?? B ??  2 C ?? C A  1 D ?? D A Order Added to Known Set: Order Added to Known Set: E ?? E ?? F ?? F ?? A G ?? G ?? Winter 2016 CSE 373: Data Structures & Algorithms 17 Winter 2016 CSE 373: Data Structures & Algorithms 18 3

  4. 2/21/2016 Example #2 Example #2 6 6 0 0 2 2 B B A A 1 1 2 2 1 1 5 5 2 E 2 E 1 1 1 1 D D 1 1 3 3 5 5 C C 2 2 6 6 6 6 G vertex known? cost path G vertex known? cost path 2 2 7 10 4 10 A Y 0 A Y 0 F F B  6 D B  6 D C  2 A C Y 2 A D Y 1 A D Y 1 A Order Added to Known Set: Order Added to Known Set:  2  2 E D E D  7  4 F D F C A, D A, D, C  6  6 G D G D Winter 2016 CSE 373: Data Structures & Algorithms 19 Winter 2016 CSE 373: Data Structures & Algorithms 20 Example #2 Example #2 3 3 0 0 2 2 B B A 1 A 1 2 2 1 1 5 5 2 E 2 E 1 1 1 1 D D 1 3 1 3 5 5 C C 2 2 6 6 6 6 G vertex known? cost path G vertex known? cost path 2 2 4 10 4 10 A Y 0 A Y 0 F F  3 B E B Y 3 E C Y 2 A C Y 2 A D Y 1 A D Y 1 A Order Added to Known Set: Order Added to Known Set: E Y 2 D E Y 2 D F  4 C F  4 C A, D, C, E A, D, C, E, B  6  6 G D G D Winter 2016 CSE 373: Data Structures & Algorithms 21 Winter 2016 CSE 373: Data Structures & Algorithms 22 Example #2 Example #2 3 3 0 0 2 2 B B A 1 A 1 2 2 1 1 5 5 2 E 2 E 1 1 1 1 D D 1 3 1 3 5 5 C C 2 2 6 6 6 6 G vertex known? cost path G vertex known? cost path 2 2 4 10 4 10 A Y 0 A Y 0 F F B Y 3 E B Y 3 E C Y 2 A C Y 2 A D Y 1 A D Y 1 A Order Added to Known Set: Order Added to Known Set: E Y 2 D E Y 2 D F Y 4 C F Y 4 C A, D, C, E, B, F A, D, C, E, B, F, G G  6 D G Y 6 D Winter 2016 CSE 373: Data Structures & Algorithms 23 Winter 2016 CSE 373: Data Structures & Algorithms 24 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend