Sub topic 16
Transitive Closure as a Floyd-Warshall Variant
This chapter assumes you know the Floyd-Warshall triple loop and its recurrence over intermediate vertices from earlier chapters, and that you can read Python lists of lists as adjacency matrices.
Sometimes you only need to know whether a path exists from i to j. The path's length doesn't matter. Some typical questions are "Can task A eventually trigger task B?", "Which modules depend on this one, directly or indirectly?" and "Is every state reachable from the start state?" The answers form the transitive closure of the graph. It is a Boolean matrix reach where reach[i][j] is true exactly when some directed path leads from i to j.
Swapping the Operations in the Recurrence
Floyd-Warshall computes:
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
To get Warshall's algorithm, swap min for logical OR and + for logical AND:
reach[i][j] = reach[i][j] or (reach[i][k] and reach[k][j])
The invariant carries over unchanged. After processing k, reach[i][j] is true if and only if a path from i to j exists that uses only vertices 0..k as intermediates. Booleans have no weights, so negative edges and negative cycles stop being a concern. A cycle just means some vertices reach each other.
Implementation
def transitive_closure(n, edges):
# edges is a list of (u, v) pairs; weights are irrelevant here
reach = [[False] * n for _ in range(n)]
for i in range(n):
reach[i][i] = True # every vertex reaches itself by the empty path
for u, v in edges:
reach[u][v] = True
for k in range(n):
row_k = reach[k]
for i in range(n):
if not reach[i][k]:
continue # i cannot use k as a stepping stone
row_i = reach[i]
for j in range(n):
if row_k[j]:
row_i[j] = True
return reach
edges = [(0, 1), (1, 2), (3, 0)]
for row in transitive_closure(4, edges):
print([int(x) for x in row])
# [1, 1, 1, 0]
# [0, 1, 1, 0]
# [0, 0, 1, 0]
# [1, 1, 1, 1]The code skips any row where reach[i][k] is false. This mirrors the dist[i][k] == math.inf check in Floyd-Warshall, and it saves a lot of work on sparse graphs.
Reflexive or Strict Closure
The diagonal initialization is a design choice:
| Initialization | Meaning of `reach[i][i]` | Name |
|---|---|---|
| Set the diagonal to `True` | Always true (empty path) | Reflexive-transitive closure |
| Leave the diagonal `False` | True only if i lies on a cycle | Strict transitive closure |
The strict version is useful for cycle detection. Any true diagonal entry marks a vertex on a cycle. This is the Boolean counterpart of reading negative cycles from the Floyd-Warshall diagonal.
Faster with Bitsets
The inner loop ORs row k into row i. If you store each row as a Python integer used as a bitset, that loop becomes a single operation:
def transitive_closure_bits(n, edges):
reach = [1 << i for i in range(n)]
for u, v in edges:
reach[u] |= 1 << v
for k in range(n):
bit_k = 1 << k
for i in range(n):
if reach[i] & bit_k:
reach[i] |= reach[k]
return reach
rows = transitive_closure_bits(4, [(0, 1), (1, 2), (3, 0)])
print([format(r, '04b')[::-1] for r in rows]) # ['1110', '0110', '0010', '1111']The bitset version still runs in O(V³) time in theory, but the word-level parallelism divides the work by roughly the machine word size. In practice it is much faster. On a very sparse graph, a BFS or DFS from every vertex costs O(V·(V+E)) and can beat both versions.
Exercise
Using the strict convention (diagonal left False), compute the closure for n = 3 and edges [(0, 1), (1, 0), (2, 0)]. Which diagonal entries are true, and why?
Show answer
reach[0][0] and reach[1][1] are true because 0 and 1 form the cycle 0 -> 1 -> 0. reach[2][2] stays false because nothing leads back to 2. Vertex 2 still reaches 0 and 1.
Exercise
Why can Warshall's algorithm ignore negative cycles when Floyd-Warshall cannot?
Show answer
Reachability has no path length that could decrease forever. Going around a cycle again cannot change a value that is already true, so the result is always well defined.