1 / 31

Shortest Paths

Shortest Paths. 08-07-2006 PowerPoint adapted from Alan Tam’s Shortest Path, 2004 with slight modifications. Breadth First Search (BFS). Techniques to Enumerate all Vertices for Goal BFS needs O(V) queue and O(V) set for duplicate elimination and runs in O(V + E) time

sol
Download Presentation

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. Shortest Paths 08-07-2006 PowerPoint adapted from Alan Tam’sShortest Path, 2004 with slight modifications

  2. Breadth First Search (BFS) • Techniques to Enumerate all Vertices for Goal • BFS needs O(V) queue and O(V) set for duplicate elimination and runs in O(V + E) time • BFS can find Shortest Path if Graph is Not Weighted • BFS works because a Queue ensures a specific Order

  3. What Order? • Define d[v] be the length of 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 shortest known path has potential to become Black • The Head of Queue is always the case in an unweighted graph • Why?

  4. Weighted Graph • Queue does not promise smallest d[v] anymore • Expanding weighted edge causeds unnecessary searching of artificial vertices • We can simply pick the real vertex nearest to the starting 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

  5. 0 3 2 1 3 3 3 1

  6. 0 3 3 2 1 3 3 2 3 1 3

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

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

  9. 0 3 3 2 1 3 4 3 2 3 1 3

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

  11. Dijkstra’s Algorithm d ← ∞ d[s] ← 0 enqueue(queue, s) whilenot empty(queue) do u = dequeue(queue) for-eachvwhere (u, v) in E if d[v] > d[u] + wuvthen d[v] = d[u] + wuv enqueue(queue, v) • Note that d[v] represents the known shortest distance of v from s during the process and may not be the real shortest distance until v is marked black.

  12. Requirements for Priority Queue • Map<V, int> • Store temporary values of d[v] • PriorityQueue<V, function: V→int > • Extract vertices in increasing order of d[v] • Initially all d[v] are infinity • When we visit a White vertex • Set d[v] • Insert v into Queue • When we visit a Gray vertex • Update d[v] • What to do to the Priority Queue?

  13. Usage: Insert: V Extract: V Decrease: E - V Array Insert: O(1) Extract: O(n) Decrease: O(1) Sorted Array Insert: O(n) Extract: O(1) Decrease: O(n) Heap Insert: O(lg n) Extract: O(lg n) Decrease: O(lg n) Fibonacci Heap Insert: O(1) Extract: Amortized O(lg n) Decrease: O(1) Implementing Priority Queue?

  14. Time Complexity

  15. Time Complexity

  16. Another Attempt • 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

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

  18. Another Attempt • Number vertices as v1, v2, …, vV • Define no≤k-path as a path which does not pass through vertices v1, v2, …, vk • A path s-t must either be • a no≤1-path, or • concatenation of a no≤1-path s-v1 and a no≤1-path v1-t • A no≤1-path s-t must either be • a no≤2-path, or • concatenation of a no≤2-path s-v2 and a no≤2-path v2-t • By MI …

  19. For all S For all T S T Trivial Paths Direct Path from S to T

  20. For all R For all S Direct Path from R to S R S Direct Path from R to T For all S For all T Direct Path from S to T S T For S = vV

  21. For all S For all T S T no≤(V-1)-Paths Shortest Path from S to Tvia vV

  22. For all R For all S Shortest Path from R to Svia vV R S Shortest Path from R to Tvia vV For all S For all T Shortest Path from S to Tvia vV S T For S = vV-1

  23. For all S For all T S T no≤(V-2)-Paths Shortest Path from S to Tvia vV-1,vV

  24. For all S For all T S T no≤2-Paths Shortest Path from S to Tvia v3,v4,...,vV

  25. For all R For all S Shortest Path from R to Svia v3,v4,...,vV R S Shortest Path from R to Tvia v3,v4,...vV For all S For all T Shortest Path from S to Tvia v3,v4,...,vV S T For S = V2

  26. For all S For all T S T no≤1-Paths Shortest Path from S to Tvia v2,v3,...,vV

  27. For all R For all S Shortest Path from R to Svia v2,v3,...,vV R S Shortest Path from R to Tvia v2,v3,...vV For all S For all T Shortest Path from S to Tvia v2,v3,...,vV S T For S = V1

  28. For all S For all T S T Shortest Paths Shortest Path from S to Tvia v1,v2,...,vV

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

  30. Graph Modeling • Conversion of a problem into a graph problem • Sometimes a problem can be easily solved once its underlying graph model is recognized • Graph modeling appears almost every year in NOI or IOI (cx, 2004)

  31. Basics of Graph Modeling • A few steps: • identify the vertices and the edges • identify the objective of the problem • state the objective in graph terms • implementation: • construct the graph from the input instance • run the suitable graph algorithms on the graph • convert the output to the required format (cx, 2004)

More Related