GFG App
Open App
Browser
Continue

# Transitive Closure of a Graph using DFS

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 means 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

We have discussed an O(V3) solution for this here. The solution was based on Floyd Warshall Algorithm. In this post, an O(V(V+E)) algorithm for the same is discussed. So for dense graph, it would become O(V3) and for sparse graph, it would become O(V2).

Below are the abstract steps of the algorithm.

1. Create a matrix tc[V][V] that would finally have transitive closure of the given graph. Initialize all entries of tc[][] as 0.
2. Call DFS for every node of the 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 the 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.

Implementation:

## Python3

 # Python program to print transitive # closure of a graph. from collections import defaultdict    class Graph:        def __init__(self,vertices):         # No. of vertices         self.V = vertices            # default dictionary to store graph         self.graph = defaultdict(list)            # To store transitive closure         self.tc = [[0 for j in range(self.V)] for i in range(self.V)]        # function to add an edge to graph     def addEdge(self, u, v):         self.graph[u].append(v)        # A recursive DFS traversal function that finds     # all reachable vertices for s     def DFSUtil(self, s, v):            # Mark reachability from s to v as true.         if(s == v):             if( v in self.graph[s]):               self.tc[s][v] = 1         else:             self.tc[s][v] = 1            # Find all the vertices reachable through v         for i in self.graph[v]:             if self.tc[s][i] == 0:                 if s==i:                    self.tc[s][i]=1                 else:                    self.DFSUtil(s, i)        # The function to find transitive closure. It uses     # recursive DFSUtil()     def transitiveClosure(self):            # Call the recursive helper function to print DFS         # traversal starting from all vertices one by one         for i in range(self.V):             self.DFSUtil(i, i)                   print(self.tc)    # Create a graph given in the above diagram g = Graph(4) g.addEdge(0, 1) g.addEdge(0, 2) g.addEdge(1, 2) g.addEdge(2, 0) g.addEdge(2, 3) g.addEdge(3, 3)    g.transitiveClosure()

## Javascript



Output

Transitive closure matrix is
1 1 1 1
1 1 1 1
1 1 1 1
0 0 0 1

Time Complexity : O(V^2) where V is the number of vertexes .

Space complexity : O(V^2) where V is number of vertices.

My Personal Notes arrow_drop_up