Given a directed graph, find out if a vertex v is reachable from another vertex u for all vertex pairs (u, v) in the given graph. Here reachable mean that there is a path from vertex u to v. The reach-ability matrix is called transitive closure of a graph.
For example, consider below graph Transitive closure of above graphs is 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1
Below are abstract steps of algorithm.
- Create a matrix tc[V][V] that would finally have transitive closure of given graph. Initialize all entries of tc as 0.
- Call DFS for every node of graph to mark reachable vertices in tc. In recursive calls to DFS, we don’t call DFS for an adjacent vertex if it is already marked as reachable in tc.
Below is implementation of the above idea. The code uses adjacency list representation of input graph and builds a matrix tc[V][V] such that tc[u][v] would be true if v is reachable from u.
Transitive closure matrix is 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1
This article is contributed by Aditya Goel. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Transitive closure of a graph
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Detect cycle in the graph using degrees of nodes of graph
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Convert undirected connected graph to strongly connected directed graph
- Dominant Set of a Graph
- Hypercube Graph
- Transpose graph
- BFS for Disconnected Graph
- Bridges in a graph
- Biconnected graph
- Islands in a graph using BFS
- Sum of dependencies in a graph
- Graph and its representations
- Check for star graph
- Graph representations using set and hash
- Check if a given graph is Bipartite using DFS
- Edge Coloring of a Graph
- Minimum Cost Graph