Greedy Algorithms 4.4 Shortest Paths in a Graph

Greedy Algorithms 4.4 Shortest Paths in a Graph

CSE202 Greedy algorithms 4.4 Shortest Paths in a Graph shortest path from Princeton CS department to Einstein's house 4.4 Shortest Paths in a Graph shortest path from Princeton CS department to Einstein's house Shortest path tree in Bay area Trees with at most 4 edges G is a tree on n vertices. G is connected with no cycle. G is connected with n-1 edges. G is formed by adding a leaf to a tree of n-1 vertices. There is a unique path between any two vertices. 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 2 23 3 9 s 18 Cost of path s-2-3-5-t 14 2 6 6 = 9 + 23 + 2 + 16 30 4 19 = 48. 11 5 15 5 6 20 16 t 7 44 45 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. ! Repeatedly choose unexplored node v which minimizes " (v) = min d(u) + ! e , e = (u,v) : u!S 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 46 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. ! Repeatedly choose unexplored node v which minimizes " (v) = min d(u) + ! e , e = (u,v) : u!S 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 47 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, P and let P' be the subpath to x. x y ! P is already too long as soon as it leaves S. P' s S u v ! (P) " ! (P') + ! (x,y) " d(x) + ! (x, y) " #(y) " #(v) nonnegative inductive defn of #(y) Dijkstra chose v weights hypothesis instead of y 48 Dijkstra's Algorithm: Implementation For each unexplored node, explicitly maintain !(v) = min d(u) + ! e . e = (u,v) : u" S ! Next node to explore = node with minimum !(v). ! When exploring v, for each incident edge e = (v, w), update !(w) = min { !(w), !(v)+ ! }. e 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 2 Total n m log n m log m/n n m + n log n † Individual ops are amortized bounds 49 Dijkstra's Shortest Path Algorithm Find shortest path from s to t. 2 24 3 9 s 18 14 2 6 6 30 4 19 11 15 5 5 6 20 16 t 7 44 50 Dijkstra's Shortest Path Algorithm S = { } PQ = { s, 2, 3, 4, 5, 6, 7, t } ! ! 2 24 3 0 9 s 18 14 ! 2 6 6 ! 30 4 19 ! 11 15 5 5 6 20 16 t 7 44 distance label ! ! 51 Dijkstra's Shortest Path Algorithm S = { } PQ = { s, 2, 3, 4, 5, 6, 7, t } delmin ! ! 2 24 3 0 9 s 18 14 ! 2 6 6 ! 30 4 19 ! 11 15 5 5 6 20 16 t 7 44 distance label ! ! 52 Dijkstra's Shortest Path Algorithm S = { s } PQ = { 2, 3, 4, 5, 6, 7, t } decrease key ! X! 9 2 24 3 0 9 s 18 14 X 14 ! 2 6 6 ! 30 4 19 ! 11 15 5 5 6 20 16 t 7 44 distance label X! 15 ! 53 Dijkstra's Shortest Path Algorithm S = { s } PQ = { 2, 3, 4, 5, 6, 7, t } delmin ! !X 9 2 24 3 0 9 s 18 14 X 14 ! 2 6 6 ! 30 4 19 ! 11 15 5 5 6 20 16 t 7 44 distance label X! 15 ! 54 Dijkstra's Shortest Path Algorithm S = { s, 2 } PQ = { 3, 4, 5, 6, 7, t } ! !X 9 2 24 3 0 9 s 18 14 X 14 ! 2 6 6 ! 30 4 19 ! 11 15 5 5 6 20 16 t 7 44 X! 15 ! 55 Dijkstra's Shortest Path Algorithm S = { s, 2 } PQ = { 3, 4, 5, 6, 7, t } decrease key X! 33 !X 9 2 24 3 0 9 s 18 14 X 14 ! 2 6 6 ! 30 4 19 ! 11 15 5 5 6 20 16 t 7 44 X! 15 ! 56 Dijkstra's Shortest Path Algorithm S = { s, 2 } PQ = { 3, 4, 5, 6, 7, t } X! 33 !X 9 2 24 3 9 0 delmin s 18 14 X 14 ! 2 6 6 ! 30 4 19 ! 11 15 5 5 6 20 16 t 7 44 X! 15 ! 57 Dijkstra's Shortest Path Algorithm S = { s, 2, 6 } PQ = { 3, 4, 5, 7, t } 32 X! 33X !X 9 2 24 3 0 9 s 18 14 X 14 ! 2 6 6 44 ! 30 X 4 19 ! 11 15 5 5 6 20 16 t 7 44 X! 15 ! 58 Dijkstra's Shortest Path Algorithm S = { s, 2, 6 } PQ = { 3, 4, 5, 7, t } 32 X! 33X !X 9 2 24 3 0 9 s 18 14 X 14 ! 2 6 6 44 ! 30 X 4 19 ! 11 15 5 5 6 20 16 t 7 44 X! 15 delmin ! 59 Dijkstra's Shortest Path Algorithm S = { s, 2, 6, 7 } PQ = { 3, 4, 5, t } 32 X! 33X !X 9 2 24 3 0 9 s 18 14 X 14 ! 2 6 6 44X 35 ! 30 X 4 19 ! 11 15 5 5 6 20 16 t 7 44 X! 15 59 !X 60 Dijkstra's Shortest Path Algorithm S = { s, 2, 6, 7 } PQ = { 3, 4, 5, t } delmin 32 X! 33X !X 9 2 24 3 0 9 s 18 14 X 14 ! 2 6 6 44X 35 ! 30 X 4 19 ! 11 15 5 5 6 20 16 t 7 44 X! 15 59 !X 61 Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 6, 7 } PQ = { 4, 5, t } 32 X! 33X !X 9 2 24 3 0 9 s 18 14 X 14 ! 2 6 6 44X 35X 34 ! 30 X 4 19 ! 11 15 5 5 6 20 16 t 7 44 X! 15 51 59X !X 62 Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 6, 7 } PQ = { 4, 5, t } 32 X! 33X !X 9 2 24 3 0 9 s 18 14 X 14 ! 2 6 6 44X 35X 34 ! 30 X 4 19 ! 11 15 5 5 6 20 delmin 16 t 7 44 X! 15 51 59X !X 63 Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 5, 6, 7 } PQ = { 4, t } 32 X! 33X !X 9 2 24 3 0 9 s 18 14 X 14 ! 2 6 6 44X 35X 34 45 !X 30 X 4 19 ! 11 15 5 5 6 20 16 t 7 44 X! 15 50 51X 59X !X 64 Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 5, 6, 7 } PQ = { 4, t } 32 X! 33X !X 9 2 24 3 0 9 s 18 14 X 14 ! 2 6 6 44X 35X 34 45 !X 30 X 4 19 ! 11 15 5 delmin 5 6 20 16 t 7 44 X! 15 50 51X 59X !X 65 Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7 } PQ = { t } 32 X! 33X !X 9 2 24 3 0 9 s 18 14 X 14 ! 2 6 6 44X 35X 34 45 !X 30 X 4 19 ! 11 15 5 5 6 20 16 t 7 44 X! 15 50 51X 59X !X 66 Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7 } PQ = { t } 32 X! 33X !X 9 2 24 3 0 9 s 18 14 X 14 ! 2 6 6 44X 35X 34 45 !X 30 X 4 19 ! 11 15 5 5 6 20 16 t 7 44 delmin X! 15 50 51X 59X !X 67 Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { } 32 X! 33X !X 9 2 24 3 0 9 s 18 14 X 14 ! 2 6 6 44X 35X 34 45 !X 30 X 4 19 ! 11 15 5 5 6 20 16 t 7 44 X! 15 50 51X 59X !X 68 Dijkstra's Shortest Path Algorithm S = { s, 2, 3, 4, 5, 6, 7, t } PQ = { } 32 X! 33X !X 9 2 24 3 0 9 s 18 14 X 14 ! 2 6 6 44X 35X 34 45 !X 30 X 4 19 ! 11 15 5 5 6 20 16 t 7 44 X! 15 50 51X 59X !X 69 Coin Changing Greed is good.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    41 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us