🔹 Warshall’s Algorithm (Transitive Closure) 🚀
Warshall’s Algorithm is used to compute the transitive closure of a directed graph. It determines whether a path exists between every pair of nodes.
🔹 How It Works
Given a graph represented as an adjacency matrix, Warshall’s algorithm updates the matrix to indicate whether a path exists between each pair of vertices.
✅ Input: A boolean adjacency matrix (1 if there is an edge, 0 if there is none).
✅ Output: The transitive closure matrix, where reach[i][j] = 1
if there exists a path from i to j.
✅ Time Complexity: O(V³) (since it uses three nested loops).
🔹 Algorithm Steps
1️⃣ Initialize the adjacency matrix reach[][] (1 if an edge exists, else 0).
2️⃣ Iterate over all intermediate nodes (k):
- For every pair of vertices
(i, j)
, check if going throughk
provides a shorter path. - If
reach[i][k] = 1
andreach[k][j] = 1
, then setreach[i][j] = 1
.
3️⃣ Final matrix shows if a path exists between any two vertices.
🔹 Python Implementation
🔹 Example
Input Graph (Adjacency Matrix)
Transitive Closure Output
This means that from node 0, we can reach all other nodes.
🔹 Applications of Warshall’s Algorithm
✅ Database Systems: Finding indirect relationships (e.g., foreign key dependencies).
✅ Computer Networks: Identifying reachable nodes in a network.
✅ Social Networks: Finding if a person can be connected via mutual friends.
✅ Compiler Optimization: Analyzing dependencies between code statements.
No comments:
Post a Comment