
The transitive closure records every connection that can be made by following one or more directed edges. A relation matrix already records one-step connections. The computational problem is to discover the remaining connections without listing every possible walk.
This note uses the conventions in 1.4 Representing Relations: rows are sources, columns are targets, and matrix products are Boolean products. As in 1.5 Closures of Relations, denotes the transitive closure using positive-length walks; also includes zero-length walks.
1. Computing a Closure with Boolean Powers
Let , where , and fix this ordering for both rows and columns. For , the entry is 1 exactly when there is a walk of exactly edges from to . The brackets distinguish a Boolean power from an ordinary arithmetic power.
On a set with elements, every positive-length reachable pair has a witness of length at most . For distinct endpoints, suffices; a closed walk returning to its starting vertex may require . The finite reachability bound therefore gives
Here is entrywise OR. Merely computing would test exactly steps and could lose connections attainable only at other lengths.
Algorithm: Accumulate Boolean powers. Keep the current power and the accumulated reachability matrix :
procedure closure_by_powers(M: n by n zero-one matrix)
P := copy of M
B := copy of M
for ell := 2 to n
P := P Boolean-product M
B := B entrywise-OR P
return B
After iteration , and . Thus the returned matrix represents . For , the loop is empty and the input itself is already the transitive closure.
With the usual dense Boolean multiplication, each product examines intermediate indices for each of entries, costing Boolean operations. Performing such products gives time overall. The algorithm is correct, but it repeatedly solves many overlapping reachability problems.
Warshall’s algorithm changes the organizing question. Rather than increasing the allowed walk length, it increases the set of vertices allowed inside a walk.
2. Warshall’s Recurrence
Allowed Interior Vertices
An interior vertex is any occurrence of a vertex between the first and last positions of a walk. A directed walk may repeat vertices and edges. The endpoints need not belong to the allowed set of interior vertices.
Define by
For , no interior vertex is allowed. A one-edge walk has no interior vertices, including a loop that is already present. Hence
At , every vertex is allowed in the interior, so . The superscript counts allowed interior vertices, not edges in a walk.
Keeping an Existing Connection or Going through a New Vertex
At stage , a connection from to can already exist without using in its interior. Otherwise, it can be obtained by joining a connection from to to one from to . The recurrence is


Why the recurrence is correct. If the first term is 1, its witness remains valid when the allowed set grows. If both entries in the second term are 1, concatenate their positive-length witness walks at . Their interior vertices were already allowed, and the join adds the newly permitted vertex . Thus every 1 produced by the recurrence represents a valid walk.
Conversely, suppose there is an allowed positive-length walk from to , and choose a shortest such walk. Repeated portions can be removed, so an interior occurrence of is needed at most once. For equal endpoints, retain a shortest positive closed walk rather than deleting the whole walk to length zero. If does not occur in the interior, the first term is 1. If it does, split the walk at that occurrence. Both pieces have positive length and use only earlier interior vertices, so the second term is 1. This proves that the recurrence finds every permitted connection.
Starting with , induction on establishes the stated meaning of every stage. At the final stage, the matrix records all positive-length reachability and therefore represents the transitive closure.
Implementation and Complexity
The recurrence can be implemented with one mutable matrix:
procedure Warshall(M: n by n zero-one matrix)
W := copy of M
for k := 1 to n
for i := 1 to n
for j := 1 to n
W[i,j] := W[i,j] OR (W[i,k] AND W[k,j])
return W
Keep as the outermost loop. Each complete phase must permit one new intermediate vertex before the next phase begins. The loops over and may be interchanged.
In-place updates preserve the recurrence because row and column do not change during phase . For example, a row- update has the form
The corresponding column- identity is the same. Consequently, each update reads the phase’s original and even though other entries may already have changed.
The same recurrence gives a conditional row update. For fixed and , if , every entry in row stays the same. If , replace row by its entrywise OR with row . Row and column remain stable during the phase, so each test and row update uses the same values as the entrywise recurrence. Skipping rows can save work when many entries are zero. The worst-case time is still for scalar row updates.
There are updates, each using one AND and one OR. Under the Boolean-operation cost model, the time is . The matrix uses storage, with additional working storage if the input may be overwritten. Copying the input preserves it at the cost of one additional matrix.
| Method | What grows between stages | Time with standard dense operations |
|---|---|---|
| Successive Boolean powers | Maximum walk length included in the union | |
| Warshall | Set of vertices allowed in the interior |
3. A Complete Matrix Trace
Example: A two-vertex cycle followed by a chain. Let
Use the ordering for rows and columns. Run Warshall’s algorithm and find . At each phase, list the newly added ordered pairs. Give and explain which diagonal entries become 1.
Show every phase and its new connections
Initial matrix. Only the four given edges are present:
Phase . Vertex 1 may now occur in the interior. The walk adds :
Phase . Vertices 1 and 2 are now allowed in the interior. Going through 2 adds from and from :
For example, the update is
Phase . Going through 3 adds and . The first uses an already discovered connection , followed by the original edge :
Phase . Row 4 is all zeros, so no connection can continue from 4. The AND term cannot add any entry, and
The newly added pairs are summarized below. Existing pairs remain present at every stage.
| Phase | Newly permitted interior vertex | Newly added pairs |
|---|---|---|
| 1 | 1 | |
| 2 | 2 | |
| 3 | 3 | |
| 4 | 4 | None |
Reading the final matrix gives
The cycle gives positive-length returns to 1 and 2, so their diagonal entries become 1. Vertex 3 has no way back from 4, and vertex 4 has no outgoing edge. Their diagonal entries remain 0. The algorithm has computed a transitive relation, which need not be reflexive.
4. Diagonal Entries and Self-Check
Positive-Length Reachability versus Zero-Length Reachability
Initialization determines which closure is computed:
| Initial matrix | Meaning of the final matrix |
|---|---|
| : reachability by one or more edges | |
| : reachability by zero or more edges |
For , a final diagonal 1 means that a positive-length closed walk exists at that vertex. It must be justified by an original loop or a longer return route. For , every diagonal entry is 1 from the beginning because a zero-length walk stays at its starting vertex.
Neither result counts walks or gives their lengths. A 1 records existence only. In particular, the stage at which an entry appears does not give the shortest walk length: stage concerns the largest permitted interior-vertex index.
Review Problems
- On , let . Find and explain why all its diagonal entries are 1 even though has no loops. Would be enough?
- On , let . Find and . Which diagonal entries should Warshall produce when initialized with ?
- Suppose that before phase , the entries , , and . What is the new value of , and what does this say about a walk from 1 to 4? Does it specify that walk’s exact length?
Show self-check answers
- Following the directed cycle reaches every vertex from every starting vertex. Thus , whose matrix is all 1s. Each return to its starting vertex takes three edges, so the diagonal pairs first occur in . The union lacks all three diagonal pairs and is insufficient. This illustrates why the general positive-length bound is , rather than .
- . There is no positive-length return to any vertex, so every diagonal entry stays 0 when initialized with . The reflexive transitive closure is .
- The update gives . Concatenating the known walk from 1 to 3 with the known walk from 3 to 4 proves positive-length reachability from 1 to 4, using only the permitted interior vertices. The entries do not record exact lengths, so the calculation alone does not supply one.
Glossary and Sources
| Term | Main idea |
|---|---|
| Transitive closure | Every pair connected by a positive-length directed walk |
| Reflexive transitive closure | , also admitting zero-length walks |
| Interior vertex | An occurrence between the first and last positions of a walk |
| Warshall phase | Permit in addition to the previously allowed interior vertices |
| In-place update | Reuse one matrix while preserving the recurrence’s required values |
- Course slides.
- The complete Warshall trace, implementation explanation, and self-checks are derived from the stated recurrence and definitions. Figures 1 and 2 are extracted from course slides and illustrate the two terms of that recurrence.
Updated: