1 / 38

Shortest Path

Shortest Path. HKOI 2006 March 11, 2006. Graph. Graph G = (V, E) Vertex/Node V Number |V| or simply V Degree deg[ v ],in-deg[ v ],out-deg[ v ] Edge E Number |E| or simply E Direction e = ( u , v ) or { u , v } Weight w e , w uv E ≤ V 2 i.e. deg[ v ] ≤ |V|.

winola
Download Presentation

Shortest Path

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 HKOI 2006 March 11, 2006

  2. Graph • Graph G = (V, E) • Vertex/Node V • Number |V| or simply V • Degree deg[v],in-deg[v],out-deg[v] • Edge E • Number |E| or simply E • Direction e = (u, v) or {u, v} • Weight we, wuv • E ≤ V2 i.e. deg[v] ≤ |V|

  3. Graph Representations

  4. Breadth First Search • BFS needs O(V) queue and O(V) set for visiting state information and runs in O(V + E) time • BFS can find Shortest Path if Graph is NOTweighted

  5. Order of Vertices • BFS works because a Queue ensures a specific Order • Define d[v] be the shortest path from s to v • At any time, vertices are classified into: • Black With known d[v] • Grey With some known path • White Not yet touched • The Grey vertex with smallest d[v] has potential to become Black

  6. S 3 2 1 3 T 3 3 1

  7. S T

  8. S T

  9. S T

  10. S T

  11. S T

  12. S T

  13. S T

  14. S T

  15. S T

  16. S T

  17. S T

  18. Done? S T

  19. Weighted Graph • Queue does not promise smallest d[v] anymore • Expanding path caused unnecessary searching of artificial vertices • We can simply pick the shortest real vertex • We need “Sorted Queue” which “dequeues” vertices in increasing order of d[v]. • It is called a “Priority Queue” and negation of d[v] is called the Priority of vertex v

  20. 0 3 2 1 3 3 3 1

  21. 0 3 3 2 1 3 3 2 3 1

  22. Done? 0 3 3 2 1 3 5 3 2 3 1 3

  23. Done? 0 3 3 2 1 3 4 3 2 3 1 3

  24. 0 3 3 2 1 3 4 3 2 3 1 3

  25. Done? 0 3 3 2 1 3 4 3 2 3 1 3

  26. Dijkstra’s Algorithm Lazy Deletion for-each v, d[v] ← ∞ d[s] ← 0 Q.Insert(s,d[s]) whilenot Q.Empty() do u = Q.ExtractMin() for-eachvwhere (u, v) in E if d[v] > d[u] + wuvthen d[v] = d[u] + wuv Q.Insert(v,d[v])

  27. Dijkstra’s Algorithm for-each v, d[v] ← ∞ d[s] ← 0 Q.Insert(s,d[s]) whilenot Q.Empty() do u = Q.ExtractMin() for-eachvwhere (u, v) in E if d[v] > d[u] + wuvthen d[v] = d[u] + wuv Q.DecreaseKey(v,d[v])

  28. Implementations of Priority Queue

  29. Complexity

  30. Another idea • Consider the following code segment: For each edge (u, v) If d[v] > d[u] + wuv d[v] ← d[u] + wuv • Assume one of the shortest paths is (s, v1, v2, …, vk) • If d[vi] = its shortest path from s • After this loop, d[vi+1] = its shortest path from s • By MI, After k such loops, found shortest path from s to vk

  31. Bellman-Ford Algorithm • All v1, v2, …,vk distinct for-each v, d[v] ← ∞ d[s] ← 0 Do V-1 times for-each (u, v) in E if d[v] > d[u] + wuv then d[v] ← d[u] + wuv • O(VE) • Support Negative-weight Edges

  32. Negative Cycle • A negative cycle is a cycle whose sum of edge weights is negative • What happens of there are negative cycles in the graph? • Doesn’t matter if the negative cycle is not reachable from the source • If a negative cycle is reachable from the source, can we detect it? • Answer: one more round of relaxations

  33. All-pairs Shortest Path • Sometimes we want to find the distances between any pair of vertices • Intermediate vertices may shorten the distance between two vertices • Label the vertices as v1, v2, … , vn • Let Vk-path be a path which uses only v1, v2, … , vk as intermediate vertices • A s-t-V1-path must either be • a s-t-V0-path, or • concatenation of a s-v1-V0-path and v1-t-V0-path • A s-t-V2-path must either be • a s-t-V1-path, or • concatenation of a s-v2-V1-path and v2-t-V1-path • By MI …

  34. Recurrence Relation • A s-t-Vk-path must either be • a s-t-Vk-1-path, or • concatenation of a s-vk-Vk-1-path and vk-t-Vk-1-path • dij(k):=length of the shortest vi-vj-Vk-path • dij(k) = wij if k=0 min(dij(k-1), dik(k-1) + dkj(k-1) ) if k>=1

  35. Warshall’s Algorithm dij(k) = wij if k=0 min(dij(k-1), dik(k-1) + dkj(k-1) ) if k>=1 d ← ∞ for-each (u, v) in E d[u][v] ← wuv for-eachk in V e ← d for-eachiin V for-eachjin V if e[i][j] > d[i][k] + d[k][j] e[i][j] ← d[i][k] + d[k][j] d ← e • Time Complexity: O(V3)

  36. Warshall-Floyd Algorithm d ← ∞ for-each (u, v) in E d[u][v] ← wuv for-eachk in V for-eachiin V for-eachjin V if d[i][j] > d[i][k] + d[k][j] d[i][j] ← d[i][k] + d[k][j] Time Complexity: O(V3)

  37. Summary • Dijkstra’s Algorithm • Bellman-Ford Algorithm • Warshall-Floyd Algorithm

  38. Construction of Shortest Path • If the whole shortest path from source vertex s to any vertex v is stored during computation, time and space complexities of the algorithms will increase • For single-source shortest path algorithms, only the parent vertex p[v] of a vertex v is stored • Shortest path may be constructed based on p[v] • A shortest path tree Gp is formed with root s • How to contruct shortest paths in Floyd-Warshall algorithm?

More Related