Title: Graph Algorithms, 5
1Graph Algorithms, 5
- Binhai Zhu
- Computer Science Department, Montana State
University -
2All-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 e V. This
is one of the most important problems in computer
science. For example, it has applications in
transportation networks.
3All-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 e V. Using
what we have learnt, how do we solve the problem?
4All-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 e V. Using
what we have learnt, how do we solve the
problem? Run Dijkstra at each vertex! It takes
O(n Elog V)O(n3log n) time.
5All-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 e V. Using
what we have learnt, how do we solve the
problem? Run Dijkstra at each vertex! It takes
O(n Elog V)O(n3log n) time. Can we do
better?
6Dynamic Programming Idea
Input weighted, directed graph D(V,E,w) of n
vertices, say V1,2,,n. Let P(i,j) be the
shortest path between i and j.
7Dynamic Programming Idea
Input weighted, directed graph D(V,E,w) of n
vertices, say V1,2,,n. Let P(i,j) be the
shortest path between i and j.
j
i
8Dynamic Programming Idea
Input weighted, directed graph D(V,E,w) of n
vertices, say V1,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
9Dynamic Programming Idea
Input weighted, directed graph D(V,E,w) of n
vertices, say V1,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
10Dynamic Programming Idea
Input weighted, directed graph D(V,E,w) of n
vertices, say V1,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
11Dynamic Programming Idea
Let P(i,j) be the shortest path between i and
j. Let Vk1,2,k. Sk(i,j) length of the
shortest path between i and j whose interior
vertices are all in Vk.
j
k
i
12Dynamic Programming Idea
Let P(i,j) be the shortest path between i and
j. Let Vk1,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
13Dynamic Programming Idea
Let P(i,j) be the shortest path between i and
j. Let Vk1,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
14Dynamic Programming Idea
Let Vk1,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
15Dynamic Programming Idea
Let Vk1,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
16Dynamic Programming Idea
- Let Vk1,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
17Dynamic Programming Formula
- Let Vk1,2,k.
- Ski,j length of the shortest path between i
and j whose interior vertices are all in Vk. - S0i,j w(i,j).
- Ski,j min Sk-1i,j, Sk-1i,k Sk-1k,j
.
j
k
i
18Dynamic Programming Algorithm
Floyd(W1n,1n,S1n,1n) For i 1 to n For
j 1 to n if lti,jgt in E then Si,j ?
Wi,j else Si,j ? 8 For k 1 to n For i
1 to n For j 1 to n if Si,j gt
Si,k Sk,j then Si,j ?
Si,k Sk,j
19A detailed example
j1
2
5
i1
4
2
1
2
1
6
S0W
4
3
3
2
1
2
4
5
5
3
20A detailed example
j1
2
5
i1
0
4
8
8
3
4
2
1
2
0
6
8
8
2
1
6
S0W
8
8
8
0
1
4
3
3
2
8
1
0
4
2
3
2
8
8
1
8
4
0
5
5
3
21A detailed example
j1
2
5
i1
0
4
8
8
3
4
2
1
2
0
6
8
8
2
1
6
8
8
8
S0W
0
1
4
3
3
2
8
1
0
4
2
3
2
8
8
1
8
4
0
5
5
3
i1
0
4
8
8
3
2
0
6
8
8
2
8
S1
0
1
0
4
2
3
0
8
8
1
8
5
22A detailed example
j1
2
5
i1
0
4
8
8
3
4
2
1
2
0
6
8
8
2
1
6
8
8
8
S0W
0
1
4
3
3
2
8
1
0
4
2
3
2
8
8
1
8
4
0
5
5
3
i1
0
4
8
8
3
2
0
6
8
8
2
8
4
5
S1
0
1
0
4
2
3
8
0
8
8
1
8
5
23A detailed example
j1
2
5
i1
0
4
8
8
3
4
2
1
2
0
6
8
8
2
1
6
8
8
8
S0W
0
1
4
3
3
2
8
1
0
4
2
3
2
8
8
1
8
4
0
5
5
3
i1
0
4
6
5
3
2
0
3
7
4
2
7
4
5
S5
0
1
0
3
2
3
7
0
6
5
1
2
5
24How do we retrieve the actual path?
- We maintain Pki,j as follows
- P0i,j 0 for all i,j in V
- Pki,j Pk-1i,j, if Ski,j Sk-1i,j
- Pki,j k, if Ski,j ? Sk-1i,j
25How do we retrieve the actual path?
- We maintain Pki,j as follows
- P0i,j 0 for all i,j in V
- Pki,j Pk-1i,j, if Ski,j Sk-1i,j
- Pki,j k, if Ski,j ? Sk-1i,j
- To reconstruct the shortest path i,j in Pn-,-
- If Pni,j0, edge lti,jgt is the shortest path.
- If Pni,jk, then k is an interior vertex on the
path, other interior vertices can be obtained by
checking Pni,k and Pnk,j.
26Floyds Algorithm (with matrix P)
Floyd(W1n,1n,S1n,1n,P1n,1n) For i 1
to n For j 1 to n Pi,j ? 0 if
lti,jgt in E then Si,j ? Wi,j else Si,j ?
8 For k 1 to n For i 1 to n For j
1 to n if Si,j gt Si,k Sk,j
then Si,j ? Si,k Sk,j
Pi,j ? k
27Dynamic Programming (It is all about filling
tables)
Sk (k1,2,3,4,5)
i
k
j
i
j
k
28Dynamic Programming (It is all about filling
tables)
Sk (k1,2,3,4,5)
i
k
j
i
j
k
Where is the final shortest path length from node
3 to 5?
29Dynamic Programming (It is all about filling
tables)
Sk (k1,2,3,4,5)
i
k
j
i
j
k
Where is the final shortest path length from node
3 to 5?
30A detailed example
j1
2
5
i1
0
4
0
2
0
4
2
1
2
0
5
0
5
5
1
6
2
1
1
P5
0
0
4
3
3
2
1
0
3
0
0
3
2
3
3
0
3
4
0
5
5
3
i1
0
4
6
5
3
2
0
3
7
4
2
7
4
5
S5
0
1
0
3
2
3
7
0
6
5
1
2
5
31A detailed example
j1
2
5
i1
0
4
0
2
0
4
2
1
2
0
5
0
5
5
1
6
2
1
1
P5
0
0
4
3
3
2
1
0
3
0
0
3
2
3
3
0
3
4
0
5
5
3
How do we retrieve the shortest path from 2 to 5?
i1
0
4
6
5
3
2
0
3
7
4
2
7
4
5
S5
0
1
0
3
2
3
7
0
6
5
1
2
5
32A detailed example
j1
2
5
i1
0
4
0
2
0
4
2
1
2
0
5
0
5
5
1
6
2
1
1
P5
0
0
4
3
3
2
1
0
3
0
0
3
2
3
3
0
3
4
0
5
5
3
How do we retrieve the shortest path from 3 to 5?
i1
0
4
6
5
3
2
0
3
7
4
2
7
4
5
S5
0
1
0
3
2
3
7
0
6
5
1
2
5
33A detailed example
j1
2
5
i1
0
4
0
2
0
4
2
1
2
0
5
0
5
5
1
6
2
1
1
P5
0
0
4
3
3
2
1
0
3
0
0
3
2
3
3
0
3
4
0
5
5
3
How do we retrieve the shortest path from 3 to
5? P3,52, so 325
i1
0
4
6
5
3
2
0
3
7
4
2
7
4
5
S5
0
1
0
3
2
3
7
0
6
5
1
2
5
34A detailed example
j1
2
5
i1
0
4
0
2
0
4
2
1
2
0
5
0
5
5
1
6
2
1
1
P5
0
0
4
3
3
2
1
0
3
0
0
3
2
3
3
0
3
4
0
5
5
3
How do we retrieve the shortest path from 3 to
5? P3,52, so 325 P3,21,so
3125 P3,1P2,5P1,20 So 3?1?2?5.
i1
0
4
6
5
3
2
0
3
7
4
2
7
4
5
S5
0
1
0
3
2
3
7
0
6
5
1
2
5
35Transitive 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.
36Transitive 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?