Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@
* [Spiral Print](matrix/spiral_print.py)
* Tests
* [Test Matrix Operation](matrix/tests/test_matrix_operation.py)
* [Transitive Closure](matrix/transitive_closure.py)
* [Validate Sudoku Board](matrix/validate_sudoku_board.py)

## Networking Flow
Expand Down
49 changes: 49 additions & 0 deletions matrix/transitive_closure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
def transitive_closure(graph: list[list[int]]) -> list[list[int]]:
"""
Computes the transitive closure of a directed graph using the
Floyd-Warshall algorithm.

Args:
graph (list[list[int]]): Adjacency matrix representation of the graph.

Returns:
list[list[int]]: Transitive closure matrix.

>>> graph = [
... [0, 1, 1, 0],
... [0, 0, 1, 0],
... [1, 0, 0, 1],
... [0, 0, 0, 0]
... ]
>>> result = transitive_closure(graph)
>>> for row in result:
... print(row)
[1, 1, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1]
[0, 0, 0, 1]
"""
n = len(graph)
ans = [[graph[i][j] for j in range(n)] for i in range(n)]

# Transtive closure of (i, i) will always be 1
for i in range(n):
ans[i][i] = 1

# Apply floyd Warshall Algorithm
# For each intermediate node k
for k in range(n):
for i in range(n):
for j in range(n):
# Check if a path exists between i to k and
# between k to j.
if ans[i][k] == 1 and ans[k][j] == 1:
ans[i][j] = 1

return ans


if __name__ == "__main__":
import doctest

doctest.testmod()