Warshall’s Algorithm
Dr. Ying Lu
[email protected]
1
Warshall’s algorithm: transitive closure
• Computes the transitive closure of a relation
• (Alternatively: all paths in a directed graph)
• Example of transitive closure:
3 3
1 1
4 4 0 0 1 0
2 0 0 1 0 2
1 0 0 1 1 1 1 1
0 0 0 0 0 0 0 0
0 1 0 0 1 1 1 1
2
Warshall’s algorithm
• Main idea: a path exists between two vertices i, j, iff
•there is an edge from i to j; or
•there is a path from i to j going through intermediate
vertices which are drawn from set {vertex 1}; or
•there is a path from i to j going through intermediate
vertices which are drawn from set {vertex 1, 2}; or
•…
3
Warshall’s algorithm
• Main idea: a path exists between two vertices i, j, iff
•there is a path from i to j going through intermediate
vertices which are drawn from set {vertex 1, 2, … k-1}; or
•there is a path from i to j going through intermediate
vertices which are drawn from set {vertex 1, 2, … k}; or
•...
•there is a path from i to j going through any of the other
vertices
4
Warshall’s algorithm
Idea: dynamic programming
• Let V={1, …, n} and for k≤n, Vk={1, …, k}
• For any pair of vertices i, jV, identify all paths from i to j
whose intermediate vertices are all drawn from Vk: Pijk={p1, p2,
…}, if Pijk then Rk[i, j]=1 V
k
P1
i j
p2
• For any pair of vertices i, j: Rn[i, j], that is Rn
• Starting with R0=A, the adjacency matrix, how to get R1 …
Rk-1 Rk … Rn
5
Warshall’s algorithm
Idea: dynamic programming
• pPijk: p is a path from i to j with all intermediate vertices
in Vk
• If k is not on p, then p is also a path from i to j with all
intermediate vertices in Vk-1: pPijk-1
k Vk
Vk-1
p
i j
6
Warshall’s algorithm
Idea: dynamic programming
• pPijk: p is a path from i to j with all intermediate vertices
in Vk
• If k is on p, then we break down p into p1 and p2
– What are P1 and P2?
p
k Vk
p1
Vk-1 p2
i j
7
Warshall’s algorithm
Idea: dynamic programming
• pPijk: p is a path from i to j with all intermediate vertices
in Vk
• If k is on p, then we break down p into p1 and p2 where
– p1 is a path from i to k with all intermediate vertices in Vk-1
– p2 is a path from k to j with all intermediate vertices in Vk-1
p
k Vk
p1
Vk-1 p2
i j
8
Warshall’s algorithm
• In the kth stage determine if a path exists between two vertices
i, j using just vertices among 1, …, k
{
R(k)[i,j] =
R(k-1)[i,j]
or
(path using just 1, …, k-1)
(R(k-1)[i,k] and R(k-1)[k,j]) (path from i to k
and from k to j
k using just 1, …, k-1)
i
kth stage
j
9
Warshall’s algorithm
3
3 1
1
2 4
2 4 3 R2
R1 1 0 0 1 0
R0
0 0 1 0 0 0 1 0 1 0 1 1
1 0 0 1 1 0 1 1 0 0 0 0
2 4 1 1 1 1
0 0 0 0 0 0 0 0
0 1 0 0 0 1 0 0
3 R4
3 R3 1
1 0 0 1 0 0 0 1 0
1 0 1 1 1 1 1 1
0 0 0 0 4 0 0 0 0
2 1 1 1 1
2 4 1 1 1 1
10
Warshall’s algorithm
R0 = A R1 R2
0 0 1 0 0 0 1 0 0 0 1 0
1 0 0 1 1 0 1 1 1 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 1 0 0 1 1 1 1
R3 R4
0 0 1 0 0 0 1 0
1 0 1 1 1 1 1 1
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
11
In-class exercises
Apply Warshall’s algorithm to find the transitive closure of
the digraph defined by the following adjacency matrix
0 1 0 0
0 0 1 0
0 0 0 1
0 0 0 0
12