1 / 36

All-Pair Shortest Paths Algorithm in Graph Theory

Learn how to compute shortest paths in a weighted, directed graph using dynamic programming. Understand Floyd's algorithm for solving the problem efficiently.

lgoldberg
Download Presentation

All-Pair Shortest Paths Algorithm in Graph Theory

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. Graph Algorithms, 5 Binhai Zhu Computer Science Department, Montana State University

  2. All-Pair Shortest Paths Algorithm Given a weighted, directed graph D=(V,E,w) of n vertices, we want to compute the shortest paths from u to v for all vertex pairs u,v ε V. This is one of the most important problems in computer science. For example, it has applications in transportation networks.

  3. All-Pair Shortest Paths Algorithm Given a weighted, directed graph D=(V,E,w) of n vertices, we want to compute the shortest paths from u to v for all vertex pairs u,v ε V. Using what we have learnt, how do we solve the problem?

  4. All-Pair Shortest Paths Algorithm Given a weighted, directed graph D=(V,E,w) of n vertices, we want to compute the shortest paths from u to v for all vertex pairs u,v ε V. Using what we have learnt, how do we solve the problem? Run Dijkstra at each vertex! It takes O(n |E|∙log |V|)=O(n3log n) time.

  5. All-Pair Shortest Paths Algorithm Given a weighted, directed graph D=(V,E,w) of n vertices, we want to compute the shortest paths from u to v for all vertex pairs u,v ε V. Using what we have learnt, how do we solve the problem? Run Dijkstra at each vertex! It takes O(n |E|∙log |V|)=O(n3log n) time. Can we do better?

  6. Dynamic Programming Idea Input: weighted, directed graph D=(V,E,w) of n vertices, say V={1,2,…,n}. Let P(i,j) be the shortest path between i and j.

  7. Dynamic Programming Idea Input: weighted, directed graph D=(V,E,w) of n vertices, say V={1,2,…,n}. Let P(i,j) be the shortest path between i and j. j i

  8. Dynamic Programming Idea Input: weighted, directed graph D=(V,E,w) of n vertices, say V={1,2,…,n}. Let P(i,j) be the shortest path between i and j. DP-related Question: How can we decompose the problem into some simpler ones? j i

  9. Dynamic Programming Idea Input: weighted, directed graph D=(V,E,w) of n vertices, say V={1,2,…,n}. Let P(i,j) be the shortest path between i and j. DP-related Question: How can we decompose the problem into some simpler ones? A: It should go through some vertex k. j i

  10. Dynamic Programming Idea Input: weighted, directed graph D=(V,E,w) of n vertices, say V={1,2,…,n}. Let P(i,j) be the shortest path between i and j. DP-related Question: How can we decompose the problem into some simpler ones? A: It should go through some vertex k. j k i

  11. Dynamic Programming Idea Let P(i,j) be the shortest path between i and j. Let Vk={1,2,…k}. Sk(i,j) = length of the shortest path between i and j whose interior vertices are all in Vk. j k i

  12. Dynamic Programming Idea Let P(i,j) be the shortest path between i and j. Let Vk={1,2,…k}. Sk(i,j) = length of the shortest path between i and j whose interior vertices are all in Vk. What is the length of the shortest path between i and j? j k i

  13. Dynamic Programming Idea Let P(i,j) be the shortest path between i and j. Let Vk={1,2,…k}. Sk(i,j) = length of the shortest path between i and j whose interior vertices are all in Vk. What is the length of the shortest path between i and j? It is Sn(i,j). j k i

  14. Dynamic Programming Idea Let Vk={1,2,…k}. Sk(i,j) = length of the shortest path between i and j whose interior vertices are all in Vk. If k is on the shortest path from i to j, Sk(i,j)= j k i

  15. Dynamic Programming Idea Let Vk={1,2,…k}. Sk(i,j) = length of the shortest path between i and j whose interior vertices are all in Vk. If k is on the shortest path from i to j, Sk(i,j) = Sk-1(i,k) + Sk-1(k,j) j k i

  16. Dynamic Programming Idea Let Vk={1,2,…k}. Sk(i,j) = length of the shortest path between i and j whose interior vertices are all in Vk. • If k is on the shortest path from i to j, • Sk(i,j) = Sk-1(i,k) + Sk-1(k,j). • If k is not on the shortest path from i to j • Sk(i,j) = Sk-1(i,j). j k i

  17. Dynamic Programming Formula Let Vk={1,2,…k}. Sk[i,j] = length of the shortest path between i and j whose interior vertices are all in Vk. • S0[i,j] = w(i,j). • Sk[i,j] = min{ Sk-1[i,j], Sk-1[i,k] + Sk-1[k,j] }. j k i

  18. Dynamic Programming Algorithm Floyd(W[1:n,1:n],S[1:n,1:n]) For i =1 to n For j =1 to n if <i,j> in E then S[i,j] ← W[i,j] else S[i,j] ← ∞ For k = 1 to n For i = 1 to n For j = 1 to n if S[i,j] > S[i,k] + S[k,j] then S[i,j] ← S[i,k] + S[k,j]

  19. A detailed example j=1 2 5 i=1 4 2 1 2 1 6 S0=W= 3 4 3 2 1 2 4 5 5 3

  20. A detailed example j=1 2 5 i=1 0 4 ∞ ∞ 3 4 2 1 2 ∞ 0 6 ∞ 2 1 6 S0=W= ∞ ∞ ∞ 1 0 3 4 3 2 ∞ 4 2 0 3 1 2 ∞ ∞ ∞ 1 0 4 5 5 3

  21. A detailed example j=1 2 5 i=1 0 4 ∞ ∞ 3 4 2 1 2 ∞ 0 6 ∞ 2 1 6 S0=W= ∞ ∞ ∞ 1 0 3 4 3 2 ∞ 4 2 0 3 1 2 ∞ ∞ ∞ 1 0 4 5 5 3 i=1 0 4 ∞ ∞ 3 2 ∞ 0 6 ∞ 2 ∞ S1= 1 0 4 2 0 3 0 ∞ ∞ ∞ 1 5

  22. A detailed example j=1 2 5 i=1 0 4 ∞ ∞ 3 4 2 1 2 ∞ 0 6 ∞ 2 1 6 S0=W= ∞ ∞ ∞ 1 0 3 4 3 2 ∞ 4 2 0 3 1 2 ∞ ∞ ∞ 1 0 4 5 5 3 i=1 0 4 ∞ ∞ 3 2 ∞ 0 6 ∞ 2 ∞ S1= 5 4 1 0 8 4 2 0 3 0 ∞ ∞ ∞ 1 5

  23. A detailed example j=1 2 5 i=1 0 4 ∞ ∞ 3 4 2 1 2 ∞ 0 6 ∞ 2 1 6 S0=W= ∞ ∞ ∞ 1 0 3 4 3 2 ∞ 4 2 0 3 1 2 ∞ ∞ ∞ 1 0 4 5 5 3 i=1 0 4 5 6 3 2 4 0 3 7 2 7 S5= 5 4 1 0 7 3 2 0 3 0 6 5 2 1 5

  24. How do we retrieve the actual path? We maintain Pk[i,j] as follows: P0[i,j] = 0 for all i,j in V Pk[i,j] = Pk-1[i,j], if Sk[i,j] = Sk-1[i,j] Pk[i,j] = k, if Sk[i,j] ≠ Sk-1[i,j]

  25. How do we retrieve the actual path? We maintain Pk[i,j] as follows: P0[i,j] = 0 for all i,j in V Pk[i,j] = Pk-1[i,j], if Sk[i,j] = Sk-1[i,j] Pk[i,j] = k, if Sk[i,j] ≠ Sk-1[i,j] To reconstruct the shortest path i,j in Pn[-,-] —— If Pn[i,j]=0, edge <i,j> is the shortest path. If Pn[i,j]=k, then k is an interior vertex on the path, other interior vertices can be obtained by checking Pn[i,k] and Pn[k,j].

  26. Floyd’s Algorithm (with matrix P) Floyd(W[1:n,1:n],S[1:n,1:n],P[1:n,1:n]) For i =1 to n For j =1 to n P[i,j] ← 0 if <i,j> in E then S[i,j] ← W[i,j] else S[i,j] ← ∞ For k = 1 to n For i = 1 to n For j = 1 to n if S[i,j] > S[i,k] + S[k,j] then S[i,j] ← S[i,k] + S[k,j] P[i,j] ← k

  27. Dynamic Programming (“It is all about filling tables”) Sk (k=1,2,3,4,5) i k j i j k

  28. Dynamic Programming (“It is all about filling tables”) Sk (k=1,2,3,4,5) i k j i j k Where is the final shortest path length from node 3 to 5?

  29. Dynamic Programming (“It is all about filling tables”) Sk (k=1,2,3,4,5) i k j i j k Where is the final shortest path length from node 3 to 5?

  30. A detailed example j=1 2 5 i=1 0 0 4 0 2 4 2 1 2 5 0 5 5 0 1 6 P5= 1 1 2 0 0 3 4 3 2 3 3 0 0 0 1 2 3 3 3 0 0 4 5 5 3 i=1 0 4 5 6 3 2 4 0 3 7 2 7 S5= 5 4 1 0 7 3 2 0 3 0 6 5 2 1 5

  31. A detailed example j=1 2 5 i=1 0 0 4 0 2 4 2 1 2 5 0 5 5 0 1 6 P5= 1 1 2 0 0 3 4 3 2 3 3 0 0 0 1 2 3 3 3 0 0 4 5 5 3 How do we retrieve the shortest path from 2 to 5? i=1 0 4 5 6 3 2 4 0 3 7 2 7 S5= 5 4 1 0 7 3 2 0 3 0 6 5 2 1 5

  32. A detailed example j=1 2 5 i=1 0 0 4 0 2 4 2 1 2 5 0 5 5 0 1 6 P5= 1 1 2 0 0 3 4 3 2 3 3 0 0 0 1 2 3 3 3 0 0 4 5 5 3 How do we retrieve the shortest path from 3 to 5? i=1 0 4 5 6 3 2 4 0 3 7 2 7 S5= 5 4 1 0 7 3 2 0 3 0 6 5 2 1 5

  33. A detailed example j=1 2 5 i=1 0 0 4 0 2 4 2 1 2 5 0 5 5 0 1 6 P5= 1 1 2 0 0 3 4 3 2 3 3 0 0 0 1 2 3 3 3 0 0 4 5 5 3 How do we retrieve the shortest path from 3 to 5? P[3,5]=2, so 3…2…5 i=1 0 4 5 6 3 2 4 0 3 7 2 7 S5= 5 4 1 0 7 3 2 0 3 0 6 5 2 1 5

  34. A detailed example j=1 2 5 i=1 0 0 4 0 2 4 2 1 2 5 0 5 5 0 1 6 P5= 1 1 2 0 0 3 4 3 2 3 3 0 0 0 1 2 3 3 3 0 0 4 5 5 3 How do we retrieve the shortest path from 3 to 5? P[3,5]=2, so 3…2…5 P[3,2]=1,so 3…1…2…5 P[3,1]=P[2,5]=P[1,2]=0 So: 3→1→2→5. i=1 0 4 5 6 3 2 4 0 3 7 2 7 S5= 5 4 1 0 7 3 2 0 3 0 6 5 2 1 5

  35. Transitive Closure of a directed graph D The transitive closure of a directed graph D=(V,E) is a graph D*=(V,E*), where E*={(i,j): there is a path from vertex i to vertex j in graph D}.

  36. Transitive Closure of a directed graph D The transitive closure of a directed graph D=(V,E) is a graph D*=(V,E*), where E*={(i,j): there is a path from vertex i to vertex j in graph D}. Q: How can we compute D*?

More Related