shortest paths
play

Shortest Paths Carola Wenk Slides courtesy of Charles Leiserson - PowerPoint PPT Presentation

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk CMPS 6610 Algorithms Paths in graphs Consider a digraph G = ( V , E ) with an edge-weight function w : E .


  1. CMPS 6610 – Fall 2018 Shortest Paths Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk CMPS 6610 Algorithms

  2. Paths in graphs Consider a digraph G = ( V , E ) with an edge-weight function w : E  . The weight of path p = v 1  v 2  ...  v k is defined to be  k 1   w ( p ) w ( v i v , 1 ) .  i  i 1 Example: v 1 v 3 v 5 4 –2 –5 1 v 2 v 4 w ( p ) = –2 CMPS 6610 Algorithms 2

  3. Shortest paths A shortest path from u to v is a path of minimum weight from u to v . The shortest-path weight from u to v is defined as  ( u , v ) = min{ w ( p ) : p is a path from u to v }. Note:  ( u , v ) =  if no path from u to v exists. CMPS 6610 Algorithms 3

  4. Optimal substructure Theorem. A subpath of a shortest path is a shortest path. Proof. Cut and paste: CMPS 6610 Algorithms 4

  5. Triangle inequality Theorem. For all u , v , x  V , we have  ( u , v )   ( u , x ) +  ( x , v ). Proof. •  ( u , v ) minimizes  ( u , v ) over all paths from u u v to v • Concatenating two  ( u , x )  ( x , v ) shortest paths from u to x and from x to x v yields one specific path from u to v CMPS 6610 Algorithms 5

  6. Well-definedness of shortest paths If a graph G contains a negative-weight cycle, then some shortest paths may not exist. Example: - 6 5 < 0 - 8 2 u v CMPS 6610 Algorithms 6

  7. Single-source shortest paths Problem. From a given source vertex s  V , find the shortest-path weights  ( s , v ) for all v  V . Assumption: All edge weights w ( u , v ) are non-negative . It follows that all shortest-path weights must exist. IDEA: Greedy. 1. Maintain a set S of vertices whose shortest-path weights from s are known, i.e., d [ v ]=  ( s,v ) 2. At each step add to S the vertex u  V – S whose distance estimate d [ u ] from s is minimal. 3. Update the distance estimates d [ v ] of vertices v adjacent to u . CMPS 6610 Algorithms 7

  8. Dijkstra’s algorithm d [ s ]  0 for each v  V – { s } do d [ v ]   S   Vertices for which d [ v ]= d ( s,v ) Q  V Q is a priority queue maintaining V – S sorted by d -values d [ v ] while Q   do u  E XTRACT -M IN ( Q ) S  S  { u } for each v  Adj [ u ] do if d [ v ] > d [ u ] + w ( u , v ) then relaxation step d [ v ]  d [ u ] + w ( u , v ) implicit D ECREASE -K EY in Q CMPS 6610 Algorithms 8

  9. Q  V PRIM’s algorithm Dijkstra’s algorithm key [ v ]   for all v  V key [ s ]  0 for some arbitrary s  V d [ s ]  0 while Q   for each v  V – { s } do u  E XTRACT -M IN ( Q ) do d [ v ]   for each v  Adj [ u ] S   Vertices for which d [ v ]= d ( s,v ) do if v  Q and w ( u , v ) < key [ v ] Q  V Q is a priority queue maintaining V – S then key [ v ]  w ( u , v ) sorted by d -values d [ v ]  [ v ]  u while Q   do u  E XTRACT -M IN ( Q ) Difference to Prim’s: S  S  { u } • It suffices to only check v  Q, but it doesn’t hurt to check all v for each v  Adj [ u ] do • Add d [ u ] to the weight if d [ v ] > d [ u ] + w ( u , v ) then relaxation step d [ v ]  d [ u ] + w ( u , v ) implicit D ECREASE -K EY in Q CMPS 6610 Algorithms 9

  10. How to find the actual shortest paths? Store a predecessor tree: d [ s ]  0 for each v  V – { s } do d [ v ]   S   Vertices for which d [ v ]= d ( s,v ) Q  V Q is a priority queue maintaining V – S sorted by d -values d [ v ] while Q   do u  E XTRACT -M IN ( Q ) S  S  { u } for each v  Adj [ u ] do if d [ v ] > d [ u ] + w ( u , v ) then d [ v ]  d [ u ] + w ( u , v )  [ v ]  u CMPS 6610 Algorithms 10

  11. Example of Dijkstra’s algorithm 2 B D Graph with 10 nonnegative 8 A edge weights: 1 4 7 9 3 C E 2 while Q   do u  E XTRACT -M IN ( Q ) S  S  { u } for each v  Adj [ u ] do if d [ v ] > d [ u ] + w ( u , v ) then d [ v ]  d [ u ] + w ( u , v )  [v]  u CMPS 6610 Algorithms 11

  12. Example of Dijkstra’s algorithm   2 Initialize: B D 10 8 S: {} A 0 1 4 7 9 3 C E 2 Q: A B C D E       0 while Q   do u  E XTRACT -M IN ( Q ) S  S  { u } for each v  Adj [ u ] do if d [ v ] > d [ u ] + w ( u , v ) then d [ v ]  d [ u ] + w ( u , v )  [v]  u CMPS 6610 Algorithms 12

  13. Example of Dijkstra’s algorithm   2 “A”  E XTRACT -M IN ( Q ): B D 10 8 S: { A } A 0 1 4 7 9  : A B C D E 3      C E 2 Q: A B C D E       0 while Q   do u  E XTRACT -M IN ( Q ) S  S  { u } for each v  Adj [ u ] do if d [ v ] > d [ u ] + w ( u , v ) then d [ v ]  d [ u ] + w ( u , v )  [v]  u CMPS 6610 Algorithms 13

  14. Example of Dijkstra’s algorithm  10 2 Relax all edges B D 10 leaving A : 8 S: { A } A 0 1 4 7 9  : A B C D E 3     - C E 2 Q: A B C D E  3     0 while Q   do    10 u  E XTRACT -M IN ( Q ) S  S  { u } for each v  Adj [ u ] do if d [ v ] > d [ u ] + w ( u , v ) then d [ v ]  d [ u ] + w ( u , v )  [v]  u CMPS 6610 Algorithms 14

  15. Example of Dijkstra’s algorithm  10 2 Relax all edges B D 10 leaving A : 8 S: { A } A 0 1 4 7 9  : A B C D E 3     - C E 2 Q: A B C D E  3     0 while Q   do    10 u  E XTRACT -M IN ( Q ) S  S  { u } for each v  Adj [ u ] do if d [ v ] > d [ u ] + w ( u , v ) then d [ v ]  d [ u ] + w ( u , v )  [v]  u CMPS 6610 Algorithms 15

  16. Example of Dijkstra’s algorithm  10 2 “C”  E XTRACT -M IN ( Q ): B D 10 8 S: { A, C } A 0 1 4 7 9  : A B C D E 3     - C E 2 Q: A B C D E  3     0 while Q   do    10 u  E XTRACT -M IN ( Q ) S  S  { u } for each v  Adj [ u ] do if d [ v ] > d [ u ] + w ( u , v ) then d [ v ]  d [ u ] + w ( u , v )  [v]  u CMPS 6610 Algorithms 16

  17. Example of Dijkstra’s algorithm 7 11 2 Relax all edges B D 10 leaving C : 8 S: { A, C } A 0 1 4 7 9  : A B C D E 3     - C E 2 Q: A B C D E 3 5     0 while Q   do    10 u  E XTRACT -M IN ( Q ) 7 11 5 S  S  { u } for each v  Adj [ u ] do if d [ v ] > d [ u ] + w ( u , v ) then d [ v ]  d [ u ] + w ( u , v )  [v]  u CMPS 6610 Algorithms 17

  18. Example of Dijkstra’s algorithm 7 11 2 Relax all edges B D 10 leaving C : 8 S: { A, C } A 0 1 4 7 9  : A B C D E 3   C C C C E 2 Q: A B C D E 3 5     0 while Q   do    10 u  E XTRACT -M IN ( Q ) 7 11 5 S  S  { u } for each v  Adj [ u ] do if d [ v ] > d [ u ] + w ( u , v ) then d [ v ]  d [ u ] + w ( u , v )  [v]  u CMPS 6610 Algorithms 18

  19. Example of Dijkstra’s algorithm 7 11 2 “E”  E XTRACT -M IN ( Q ): B D 10 8 S: { A, C, E } A 0 1 4 7 9  : A B C D E 3   C C C C E 2 Q: A B C D E 3 5     0 while Q   do    10 u  E XTRACT -M IN ( Q ) 7 11 5 S  S  { u } for each v  Adj [ u ] do if d [ v ] > d [ u ] + w ( u , v ) then d [ v ]  d [ u ] + w ( u , v )  [v]  u CMPS 6610 Algorithms 19

  20. Example of Dijkstra’s algorithm 7 11 2 Relax all edges B D 10 leaving E : 8 S: { A, C, E } A 0 1 4 7 9  : A B C D E 3   C C C C E 2 Q: A B C D E 3 5     0 while Q   do    10 u  E XTRACT -M IN ( Q ) 7 11 5 S  S  { u } for each v  Adj [ u ] do 7 11 if d [ v ] > d [ u ] + w ( u , v ) then d [ v ]  d [ u ] + w ( u , v )  [v]  u CMPS 6610 Algorithms 20

  21. Example of Dijkstra’s algorithm 7 11 2 “B”  E XTRACT -M IN ( Q ): B D 10 8 S: { A, C, E, B } A 0 1 4 7 9  : A B C D E 3   C C C C E 2 Q: A B C D E 3 5     0 while Q   do    10 u  E XTRACT -M IN ( Q ) 7 11 5 S  S  { u } for each v  Adj [ u ] do 7 11 if d [ v ] > d [ u ] + w ( u , v ) then d [ v ]  d [ u ] + w ( u , v )  [v]  u CMPS 6610 Algorithms 21

  22. Example of Dijkstra’s algorithm 7 9 2 Relax all edges B D 10 leaving B : 8 S: { A, C, E, B } A 0 1 4 7 9  : A B C D E 3   C B C C E 2 Q: A B C D E 3 5     0 while Q   do    10 u  E XTRACT -M IN ( Q ) 7 11 5 S  S  { u } for each v  Adj [ u ] do 7 11 if d [ v ] > d [ u ] + w ( u , v ) then 9 d [ v ]  d [ u ] + w ( u , v )  [v]  u CMPS 6610 Algorithms 22

  23. Example of Dijkstra’s algorithm 7 9 2 “D”  E XTRACT -M IN ( Q ): B D 10 8 S: { A, C, E, B, D }0 A 1 4 7 9  : A B C D E 3   C B C C E 2 Q: A B C D E 3 5     0 while Q   do    10 u  E XTRACT -M IN ( Q ) 7 11 5 S  S  { u } for each v  Adj [ u ] do 7 11 if d [ v ] > d [ u ] + w ( u , v ) then 9 d [ v ]  d [ u ] + w ( u , v )  [v]  u CMPS 6610 Algorithms 23

Recommend


More recommend