360 likes | 375 Views
Learn how to compute shortest paths in a weighted, directed graph using dynamic programming. Understand Floyd's algorithm for solving the problem efficiently.
E N D
Graph Algorithms, 5 Binhai Zhu Computer Science Department, Montana State University
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.
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?
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.
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?
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.
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
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
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
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
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
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
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
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
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
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
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
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]
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
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
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
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
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
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]
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].
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
Dynamic Programming (“It is all about filling tables”) Sk (k=1,2,3,4,5) i k j i j k
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?
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?
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
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
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
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
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
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}.
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*?