Warshalls Algorithm
#programming #algorithms #graphs
A formal algorithm to construct the Transitive Closure of a graph
It does not matter if the graph is directed.
For information on graphs, read Graphs
Warshall's algorithm is simply a boolean mask to represent whether an edge exists between
We start with
Core Idea
We progressively allow intermediate vertices.
So we define
After step
, {cpp}T[i][j] = 1if there is a path fromto using only vertices from as intermediates
Essentially, we ask
Does going through vertex
connect and ?
Relation
At step
We do
T[i][j] = T[i][j] || (T[i][k] && T[k][j]);
Pseudo code
Initialize T = adjacency matrix
for k = 1 to n:
for i = 1 to n:
for j = 1 to n:
T[i][j] = T[i][j] OR (T[i][k] AND T[k][j])