learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

300 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.

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

Transitive Closure

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

Time: O(n³)
Space: O(n²)
Use Case: Reachability queries, dependency analysis, recursive database queries
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Transitive Closure (Warshall)

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.

How it works

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.

Applications

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.

Pseudocode

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 i

The 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).

Worked example, step by step

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.

  1. Through A. No arc ends at A, so no vertex reaches A, and the round adds nothing.
  2. Through B. A and D reach B, and B reaches C: A → C and D → C are new.
  3. Through C. A, B and D now reach C, and C reaches D and E. Six new pairs, among them D → D: D can come back to itself, it lies on a cycle.
  4. Through D. Every vertex that reaches D now also reaches what D reaches: B → B, C → B and C → C are new. B and C are on the cycle too.
  5. Through E. E reaches nothing, so the last round adds nothing either.

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.

Complexity, and where it comes from

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².

When to use Transitive Closure (Warshall), and when not to

The closure is one answer to "can i reach j". Other methods fit other question patterns.

AlternativePrefer it whenCost
BFS or DFS from one vertexA single source, or a few questions. Reads only what that vertex reaches.O(n + m) per source
BFS from every vertexThe full closure of a sparse graph.O(n·(n + m))
Strongly connected components firstLarge 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

Common pitfalls

  • Putting the k loop inside. The vertex allowed as a stop must be the outermost loop. With k innermost, a path that needs two stops in the wrong order is missed.
  • Assuming the diagonal is 1. In the transitive closure, R[i][i] = 1 only when i lies on a cycle. The reflexive-transitive closure sets every diagonal entry to 1. Know which one a problem asks for.
  • Using it on an undirected graph. Without directions every vertex reaches its whole connected component, so the closure just fills each component. Connected components give the same answer in linear time.

Frequently asked questions

What is the transitive closure of a graph?
The relation "j can be reached from i by a path", stored as an n × n matrix of 0s and 1s. As a graph, it has an arc i → j for every such pair.
How is Warshall different from Floyd-Warshall?
They are the same three loops. Warshall stores true or false and combines them with OR and AND, Floyd-Warshall stores distances and combines them with min and +.
Why does a 1 on the diagonal mean a cycle?
R[i][i] = 1 means there is a path of at least one arc from i back to i, which is a closed walk, and every closed walk contains a cycle through i.
Can the closure be updated when an arc is added?
Yes. When the arc u → v is added, every vertex that reaches u now also reaches everything v reaches, an O(n²) update. Deleting an arc is much harder to handle incrementally.

Related algorithms: Floyd-Warshall Algorithm, Tarjan's SCC Algorithm, Topological Sort, Breadth-First Search, Depth-First Search

An example graph: you can change it, or start blank.

Zoom Controls

30%
Nodes: 5
Edges: 5