1 / 77

Shortest path problems

Shortest path problems. [Adapted from K.Wayne]. Shortest path problems. [Adapted from K.Wayne]. Shortest path problems. [Adapted from K.Wayne]. Single-Source Shortest Paths. Given graph (directed or undirected) G = (V,E) with weight function w: E  R and a vertex s  V,

wcrawford
Download Presentation

Shortest path problems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shortest path problems [Adapted from K.Wayne]

  2. Shortest path problems [Adapted from K.Wayne]

  3. Shortest path problems [Adapted from K.Wayne]

  4. Single-Source Shortest Paths Given graph (directed or undirected) G = (V,E) with weight function w: E  R and a vertex sV, find for all vertices vV the minimum possible weight for path from s to v. • We will discuss two general case algorithms: • Dijkstra's (positive edge weights only) • Bellman-Ford (positive end negative edge weights) If all edge weights are equal (let's say 1), the problem is solved by BFS in (V+E) time.

  5. Dijkstra’s Algorithm - Relax Relax(vertex u, vertex v, weight w) if d[v] > d[u] + w(u,v) then d[v]  d[u] + w(u,v) p[v]  u [Adapted from K.Wayne]

  6. Dijkstra’s Algorithm - Idea [Adapted from K.Wayne]

  7. Dijkstra’s Algorithm - SSSP-Dijkstra SSSP-Dijkstra(graph (G,w), vertex s) InitializeSingleSource(G, s) S   Q  V[G] while Q  0 do u  ExtractMin(Q) S  S  {u} for v  Adj[u] do Relax(u,v,w) InitializeSingleSource(graph G, vertex s) for v  V[G] do d[v]   p[v]  0 d[s]  0

  8. Dijkstra’s Algorithm - Example 1 10 2 9 3 4 6 7 5 2

  9. Dijkstra’s Algorithm - Example 1   10 2 9 3 0 4 6 7 5   2

  10. Dijkstra’s Algorithm - Example 1 10  10 2 9 3 0 4 6 7 5 5  2

  11. Dijkstra’s Algorithm - Example 1 10  10 2 9 3 0 4 6 7 5 5  2

  12. Dijkstra’s Algorithm - Example 1 8 14 10 2 9 3 0 4 6 7 5 5 7 2

  13. Dijkstra’s Algorithm - Example 1 8 14 10 2 9 3 0 4 6 7 5 5 7 2

  14. Dijkstra’s Algorithm - Example 1 8 13 10 2 9 3 0 4 6 7 5 5 7 2

  15. Dijkstra’s Algorithm - Example 1 8 13 10 2 9 3 0 4 6 7 5 5 7 2

  16. Dijkstra’s Algorithm - Example 1 8 9 10 2 9 3 0 4 6 7 5 5 7 2

  17. Dijkstra’s Algorithm - Example 1 8 9 10 2 9 3 0 4 6 7 5 5 7 2

  18. Dijkstra’s Algorithm - Complexity SSSP-Dijkstra(graph (G,w), vertex s) InitializeSingleSource(G, s) S   Q  V[G] while Q  0 do u  ExtractMin(Q) S  S  {u} for u  Adj[u] do Relax(u,v,w) executed (V) times (E) times in total InitializeSingleSource(graph G, vertex s) for v  V[G] do d[v]   p[v]  0 d[s]  0 (V) Relax(vertex u, vertex v, weight w) if d[v] > d[u] + w(u,v) then d[v]  d[u] + w(u,v) p[v]  u (1) ?

  19. Dijkstra’s Algorithm - Complexity InitializeSingleSource TI(V,E) = (V) Relax TR(V,E) = (1)? SSSP-Dijkstra T(V,E) = TI(V,E) + (V) + V (log V) + E TR(V,E) = = (V) + (V) + V (log V) + E (1) = (E + V log V)

  20. Dijkstra’s Algorithm - Complexity [Adapted from K.Wayne]

  21. Dijkstra’s Algorithm - Correctness [Adapted from K.Wayne]

  22. Dijkstra’s Algorithm - negative weights? [Adapted from K.Wayne]

  23. Bellman-Ford Algorithm - negative cycles? [Adapted from K.Wayne]

  24. Bellman-Ford Algorithm - Idea [Adapted from X.Wang]

  25. Bellman-Ford Algorithm - SSSP-BellmanFord SSSP-BellmanFord(graph (G,w), vertex s) InitializeSingleSource(G, s) for i  1 to|V[G]  1|do for (u,v) E[G] do Relax(u,v,w) for (u,v) E[G] do if d[v] > d[u] + w(u,v)then return false return true

  26. Bellman-Ford Algorithm - Example -2 5 6 -3 8 7 -4 2 7 9

  27. Bellman-Ford Algorithm - Example -2  5  6 -3 8 0 7 -4 2 7   9

  28. Bellman-Ford Algorithm - Example -2 6 5  6 -3 8 0 7 -4 2 7 7  9

  29. Bellman-Ford Algorithm - Example -2 6 5 4 6 -3 8 0 7 -4 2 7 7 2 9

  30. Bellman-Ford Algorithm - Example -2 2 5 4 6 -3 8 0 7 -4 2 7 7 2 9

  31. Bellman-Ford Algorithm - Example -2 2 5 4 6 -3 8 0 7 -4 2 7 7 -2 9

  32. Bellman-Ford Algorithm - Complexity SSSP-BellmanFord(graph (G,w), vertex s) InitializeSingleSource(G, s) for i  1 to|V[G]  1|do for (u,v) E[G] do Relax(u,v,w) for (u,v) E[G] do if d[v] > d[u] + w(u,v)then return false return true executed (V) times (E) (1) (E)

  33. Bellman-Ford Algorithm - Complexity InitializeSingleSource TI(V,E) = (V) Relax TR(V,E) = (1)? SSSP-BellmanFord T(V,E) = TI(V,E) + V E TR(V,E) + E = = (V) + V E (1) + E = = (V E)

  34. Bellman-Ford Algorithm - Correctness [Adapted from T.Cormen, C.Leiserson, R. Rivest]

  35. Bellman-Ford Algorithm - Correctness [Adapted from T.Cormen, C.Leiserson, R. Rivest]

  36. Bellman-Ford Algorithm - Correctness [Adapted from T.Cormen, C.Leiserson, R. Rivest]

  37. Bellman-Ford Algorithm - Correctness [Adapted from T.Cormen, C.Leiserson, R. Rivest]

  38. Shortest Paths in DAGs - SSSP-DAG • SSSP-DAG(graph (G,w), vertex s) • topologically sort vertices of G • InitializeSingleSource(G, s) • for each vertex u taken in topologically sorted order do • for each vertex v Adj[u]do • Relax(u,v,w)

  39. Shortest Paths in DAGs - Example 6 1 5 2 7 -1 -2 4 3 2

  40. Shortest Paths in DAGs - Example 6 1 5 2 7 -1 -2  0     4 3 2

  41. Shortest Paths in DAGs - Example 6 1 5 2 7 -1 -2  0     4 3 2

  42. Shortest Paths in DAGs - Example 6 1 5 2 7 -1 -2  0     4 3 2

  43. Shortest Paths in DAGs - Example 6 1 5 2 7 -1 -2  0 2 6   4 3 2

  44. Shortest Paths in DAGs - Example 6 1 5 2 7 -1 -2  0 2 6 6 4 4 3 2

  45. Shortest Paths in DAGs - Example 6 1 5 2 7 -1 -2  0 2 6 6 4 4 3 2

  46. Shortest Paths in DAGs - Example 6 1 5 2 7 -1 -2  0 2 6 5 4 4 3 2

  47. Shortest Paths in DAGs - Example 6 1 5 2 7 -1 -2  0 2 6 5 4 4 3 2

  48. Shortest Paths in DAGs - Example 6 1 5 2 7 -1 -2  0 2 6 5 3 4 3 2

  49. Shortest Paths in DAGs - Example 6 1 5 2 7 -1 -2  0 2 6 5 3 4 3 2

  50. Shortest Paths in DAGs - Complexity • SSSP-DAG(graph (G,w), vertex s) • topologically sort vertices of G • InitializeSingleSource(G, s) • for each vertex u taken in topologically sorted order do • for each vertex v Adj[u]do • Relax(u,v,w) T(V,E) = (V + E) + (V) + (V) + E (1) = (V + E)

More Related