1 / 32

Single-Source Shortest Paths

Single-Source Shortest Paths. Contents. Definition Optimal substructure of a shortest path Negative-weight edges Cycles Predecessor subgraph Relaxation The Bellman-Ford algorithm Single-source shortest paths in directed acyclic graphs Dijkstra's algorithm. Definition. Definition

tillie
Télécharger la présentation

Single-Source Shortest Paths

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. Single-Source Shortest Paths

  2. Contents • Definition • Optimal substructure of a shortest path • Negative-weight edges • Cycles • Predecessor subgraph • Relaxation • The Bellman-Ford algorithm • Single-source shortest paths in directed acyclic graphs • Dijkstra's algorithm

  3. Definition • Definition • edge weight • path weight • shortest-path weight: δ(u,v) • shortest path

  4. Definition • source, destination • single-source single-destination • single-source • single-destination • all pair

  5. Optimal substructure of a shortest path • Optimal substructure of a shortest path • the shortest path consists of shortest subpath

  6. Optimal substructure of a shortest path • Lemma 24.1 • Given a weighted, directed graph G = (V, E) with weight function w : E → R, let p = v1,v2,..., vk be a shortest path from vertex v1 to vkand, for any i and j such that 1 ≤ i ≤ j ≤k, let pij= vi, vi+1,..., vj be the subpath of p from vertex vito vj. Then, pijis a shortest path from vito vj.

  7. Optimal substructure of a shortest path • Proof • If we decompose path p into , v1 vivj vk then we have that w(p) = w(p1i) + w(pij) +w(pjk). Now, assume that there is a path from vito vjwith weight w(p′ij)< w(pij). Then, v1 vivj vk is a path from v1 to vkwhose weight w(p1i)+w(p′ij)+w(pjk) is less than w(p), which contradicts the assumption that p is a shortest path from v1 to vk. p1i pij pjk p1i pij pjk

  8. Negative-weight edges • Negative-weight edges • Negative-weight edges are permitted if negative-weight cycles are not reachable from s. b a -4 i h 3 -1 2 ∞ ∞ 4 3 g c s d 6 8 5 3 0 5 11 -∞ -8 -3 ∞ 2 f e 7 3 j -∞ -∞ -6 Figure 24.1

  9. Cycles • Cycles • A shortest path does not include cycles. • A shortest path is of length at most |V|-1.

  10. Predecessor subgraph • Predecessor subgraph • shortest-path tree • for example : shows a weighted , directed graph and two shortest-paths trees with the same root.

  11. Predecessor subgraph t x 6 3 9 3 4 2 1 s 0 2 7 3 5 5 11 6 y z Figure 24.2 (a)

  12. Predecessor subgraph t x 6 3 9 3 4 2 1 s 0 2 7 3 5 5 11 6 y z Figure 24.2 (b)

  13. Predecessor subgraph t x 6 3 9 3 4 2 1 s 0 2 7 3 5 5 11 6 y z Figure 24.2 (c)

  14. Relaxation • Relaxation • definition • path-relaxation property u v u v 2 2 9 6 5 5 Relax(u,v,w) Relax(u,v,w) u v u v 2 2 7 6 5 5

  15. The Bellman-Ford algorithm • The Bellman-Ford algorithm • Running time : O(VE) • it solves the single source shortest-paths problem in the general case in which edge weights may be negative.

  16. 5 5 t t x x -2 -2 6 ¥ ¥ ¥ 6 6 -3 -3 8 8 s 0 s 0 7 7 -4 -4 2 2 7 7 7 ¥ ¥ ¥ 9 9 y z y z The Bellman-Ford algorithm • order • (t,x), (t,y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y)

  17. 5 5 t t x x -2 -2 6 4 2 4 6 6 -3 -3 8 8 s 0 s 0 7 7 -4 -4 2 2 7 7 7 2 7 2 9 9 y z y z The Bellman-Ford algorithm • order • (t,x), (t,y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y)

  18. t x -2 2 4 6 -3 8 s 0 7 -4 2 7 7 -2 9 y z The Bellman-Ford algorithm • order • (t,x), (t,y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y) 5

  19. Single-source shortest paths in directed acyclic graphs • Single-source shortest paths in directed acyclic graphs • Running time : O(V+E) time • starts by topologically sorting the dag to impose a linear ordering on the vertices. • just one pass over the vertices

  20. Single-source shortest paths in directed acyclic graphs 6 1 y r s t x z 7 -1 5 2 -2 ∞ 0 ∞ ∞ ∞ ∞ 4 3 2 6 1 y s t x z r 7 -1 5 2 -2 ∞ 0 ∞ ∞ ∞ ∞ 4 3 2

  21. Single-source shortest paths in directed acyclic graphs 6 1 y r s t x z 7 -1 5 2 -2 ∞ 0 2 6 ∞ ∞ 4 3 2 6 1 y s t x z r 7 -1 5 2 -2 ∞ 0 2 6 6 4 4 3 2

  22. Single-source shortest paths in directed acyclic graphs 6 1 y r s t x z 7 -1 5 2 -2 ∞ 0 2 6 5 4 4 3 2 6 1 y s t x z r 7 -1 5 2 -2 ∞ 0 2 6 5 3 4 3 2

  23. Single-source shortest paths in directed acyclic graphs 6 1 y r s t x z 7 -1 5 2 -2 ∞ 0 2 6 5 3 4 3 2

  24. Dijkstra's algorithm • Dijkstra's algorithm • all edge weights are nonnegative. • the running time of Dijkstra's algorithm is lower than that of the Bellman-Ford algorithm. • Running time : • O (V^2) if we use array • O(VlgV + ElgV) if we use a heap • (VlgV + E) if we use a Fibonacci heap.

  25. Dijkstra's algorithm DIJKSTRA(G, w, s) 1 INITIALIZE-SINGLE-SOURCE(G, s) 2 S ← Ø 3 Q ← V[G] 4 while Q ≠ Ø 5 do u ← EXTRACT-MIN(Q) 6 S ← S {u} 7 for each vertex v Adj[u] 8 do RELAX(u, v, w)

  26. Dijkstra’s Algorithm t x 1 Q ∞ ∞ 10 9 2 3 4 6 s 0 5 7 ∞ ∞ 2 y z S

  27. Dijkstra’s Algorithm t x 1 Q 10 ∞ 10 9 2 3 4 6 s 0 5 7 5 ∞ 2 y z {s} S

  28. Dijkstra’s Algorithm t x 1 Q 10 ∞ 10 9 2 3 4 6 s 0 5 7 5 ∞ 2 y z {s, y} S

  29. Dijkstra’s Algorithm t x 1 Q 8 14 10 9 2 3 4 6 s 0 5 7 5 7 2 y z {s, y} S

  30. Dijkstra’s Algorithm t x 1 Q 8 13 10 9 2 3 4 6 s 0 5 7 5 7 2 y z {s, y, z, t} S

  31. Dijkstra’s Algorithm t x 1 Q 8 9 10 9 2 3 4 6 s 0 5 7 5 7 2 y z {s, y, z, t} S

  32. Dijkstra’s Algorithm t x 1 Q 8 9 10 9 2 3 4 6 s 0 5 7 5 7 2 y z {s, y, z, t, x} S

More Related