Guest
Sign in to keep your graphs and progress.

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
Who can reach whom, in one matrix
Finds every pair of vertices joined by a path: round k lets vertex k be a stop on the way, and the reachability matrix fills in
Select an algorithm and generate steps to begin visualization
The transitive closure of a directed graph answers one question for every pair of vertices: is there a path from i to j? The answer is a matrix of 0s and 1s, the reachability matrix. It turns a relation such as "depends on" or "is a prerequisite of" into its full version: if A needs B and B needs C, then A needs C. Two vertices that reach each other lie on a common cycle, and a 1 on the diagonal means a vertex can come back to itself.
Warshall's algorithm (1962) starts from the adjacency matrix and runs n rounds. Round k allows vertex k as a stop on the way: for every pair, i reaches j if it already did, or if i reaches k and k reaches j. After round k, the matrix knows every path whose inner vertices are among the first k, so after the last round it knows every path. Three nested loops give O(n³) time and O(n²) space. It is Floyd-Warshall with "or" and "and" in place of "min" and "+". The visualizer highlights the pivot k, colours the new 1s of each round and draws every pair the closure adds as a dashed arc.
Reachability is everywhere: package managers and build systems need the full set of dependencies of a module, databases answer recursive queries (who reports to whom, directly or not), and compilers analyse which functions can call which. In databases and logic the closure of a relation is a basic operation, and graph databases store or compute it to answer "is X connected to Y" quickly. For a single question a BFS from one vertex is cheaper, the closure pays off when many pairs are asked.
Three nested loops over one matrix. The outer loop decides which vertex may be used as a stop on the way.
TransitiveClosure(G):
R = adjacency matrix of G // R[i][j] = 1 if there is an arc i → j
for k = 1 to n: // now k may be a stop on the way
for i = 1 to n:
if R[i][k] == 1:
for j = 1 to n:
if R[k][j] == 1:
R[i][j] = 1
return R // R[i][j] = 1: j is reachable from iThe test R[i][k] == 1 sits outside the inner loop: a row that does not reach k cannot gain anything this round. Updating R in place is safe, because round k never changes row k or column k (a path through k does not need k twice).
The closure of the five-vertex example loaded on this page.
Example graph: Arcs A → B, B → C, C → D, D → B and C → E. B, C and D form a cycle, A leads into it and E hangs off C.
16 reachable pairs instead of the 5 drawn arcs: 8 new arcs between different vertices, drawn dashed, plus the diagonal 1s of B, C and D, the three vertices on the cycle.
Time: O(n³) · Space: O(n²)
The matrix has n² entries, and each of the n rounds looks at each entry once, so the time is O(n³) whatever the number of arcs. Stored as bits, a row update becomes a few ORs of machine words, which divides the time by the word size (64) and makes Warshall fast in practice. For a sparse graph, a BFS from every vertex costs O(n·(n + m)), better when m is much smaller than n².
The closure is one answer to "can i reach j". Other methods fit other question patterns.
| Alternative | Prefer it when | Cost |
|---|---|---|
| BFS or DFS from one vertex | A single source, or a few questions. Reads only what that vertex reaches. | O(n + m) per source |
| BFS from every vertex | The full closure of a sparse graph. | O(n·(n + m)) |
| Strongly connected components first | Large graphs with cycles: shrink each component to one vertex, then close the acyclic graph that remains. | O(n + m) plus the closure of the DAG |
Read the full article: Floyd-Warshall Algorithm Explained
Read the full article: Strongly Connected Components: Tarjan & Kosaraju
Related algorithms: Floyd-Warshall Algorithm, Tarjan's SCC Algorithm, Topological Sort, Breadth-First Search, Depth-First Search