Given a directed graph, find out if a vertex j is reachable from another vertex i for all vertex pairs (i, j) in the given graph. Here reachable mean that there is a path from vertex i to j. The reach-ability matrix is called the 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 1The graph is given in the form of adjacency matrix say ‘graph[V][V]’ where graph[i][j] is 1 if there is an edge from vertex i to vertex j or i is equal to j, otherwise graph[i][j] is 0.
Floyd Warshall Algorithm can be used, we can calculate the distance matrix dist[V][V] using Floyd Warshall, if dist[i][j] is infinite, then j is not reachable from i. Otherwise, j is reachable and the value of dist[i][j] will be less than V.
Instead of directly using Floyd Warshall, we can optimize it in terms of space and time, for this particular problem. Following are the optimizations:
- Instead of an integer resultant matrix (dist[V][V] in floyd warshall), we can create a boolean reach-ability matrix reach[V][V] (we save space). The value reach[i][j] will be 1 if j is reachable from i, otherwise 0.
- Instead of using arithmetic operations, we can use logical operations. For arithmetic operation ‘+’, logical and ‘&&’ is used, and for a ‘-‘, logical or ‘||’ is used. (We save time by a constant factor. Time complexity is the same though)
Below is the implementation of the above approach:
C++
#include<stdio.h>
#define V 4
void printSolution(int reach[][V]);
void transitiveClosure(int graph[][V])
{
int reach[V][V], i, j, k;
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
reach[i][j] = graph[i][j];
for (k = 0; k < V; k++)
{
for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
reach[i][j] = reach[i][j] ||
(reach[i][k] && reach[k][j]);
}
}
}
printSolution(reach);
}
void printSolution(int reach[][V])
{
printf ("Following matrix is transitive");
printf("closure of the given graph\n");
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if(i == j)
printf("1 ");
else
printf ("%d ", reach[i][j]);
}
printf("\n");
}
}
int main()
{
int graph[V][V] = { {1, 1, 0, 1},
{0, 1, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1}
};
transitiveClosure(graph);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GraphClosure
{
final static int V = 4;
void transitiveClosure(int graph[][])
{
int reach[][] = new int[V][V];
int i, j, k;
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
reach[i][j] = graph[i][j];
for (k = 0; k < V; k++)
{
for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
reach[i][j] = (reach[i][j]!=0) ||
((reach[i][k]!=0) && (reach[k][j]!=0))?1:0;
}
}
}
printSolution(reach);
}
void printSolution(int reach[][])
{
System.out.println("Following matrix is transitive closure"+
" of the given graph");
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++) {
if ( i == j)
System.out.print("1 ");
else
System.out.print(reach[i][j]+" ");
}
System.out.println();
}
}
public static void main (String[] args)
{
int graph[][] = new int[][]{ {1, 1, 0, 1},
{0, 1, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1}
};
GraphClosure g = new GraphClosure();
g.transitiveClosure(graph);
}
}
|
Python3
from collections import defaultdict
class Graph:
def __init__(self, vertices):
self.V = vertices
def printSolution(self, reach):
print ("Following matrix transitive closure of the given graph ")
for i in range(self.V):
for j in range(self.V):
if (i == j):
print ("%7d\t" % (1),end=" ")
else:
print ("%7d\t" %(reach[i][j]),end=" ")
print()
def transitiveClosure(self,graph):
reach =[i[:] for i in graph]
for k in range(self.V):
for i in range(self.V):
for j in range(self.V):
reach[i][j] = reach[i][j] or (reach[i][k] and reach[k][j])
self.printSolution(reach)
g= Graph(4)
graph = [[1, 1, 0, 1],
[0, 1, 1, 0],
[0, 0, 1, 1],
[0, 0, 0, 1]]
g.transitiveClosure(graph)
|
C#
using System;
class GFG
{
static int V = 4;
void transitiveClosure(int [,]graph)
{
int [,]reach = new int[V, V];
int i, j, k;
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
reach[i, j] = graph[i, j];
for (k = 0; k < V; k++)
{
for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
reach[i, j] = (reach[i, j] != 0) ||
((reach[i, k] != 0) &&
(reach[k, j] != 0)) ? 1 : 0;
}
}
}
printSolution(reach);
}
void printSolution(int [,]reach)
{
Console.WriteLine("Following matrix is transitive" +
" closure of the given graph");
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++){
if (i == j)
Console.Write("1 ");
else
Console.Write(reach[i, j] + " ");
}
Console.WriteLine();
}
}
public static void Main (String[] args)
{
int [,]graph = new int[,]{{1, 1, 0, 1},
{0, 1, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1}};
GFG g = new GFG();
g.transitiveClosure(graph);
}
}
|
Javascript
<script>
var V = 4;
function transitiveClosure(graph) {
var reach = Array.from(Array(V), () => new Array(V));
var i, j, k;
for (i = 0; i < V; i++)
for (j = 0; j < V; j++) reach[i][j] = graph[i][j];
for (k = 0; k < V; k++) {
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
reach[i][j] =
reach[i][j] != 0 || (reach[i][k] != 0 && reach[k][j] != 0)
? 1
: 0;
}
}
}
printSolution(reach);
}
function printSolution(reach) {
document.write(
"Following matrix is transitive" + " closure of the given graph <br>"
);
for (var i = 0; i < V; i++) {
for (var j = 0; j < V; j++) {
if (i == j) document.write("1 ");
else document.write(reach[i][j] + " ");
}
document.write("<br>");
}
}
var graph = [
[1, 1, 0, 1],
[0, 1, 1, 0],
[0, 0, 1, 1],
[0, 0, 0, 1],
];
transitiveClosure(graph);
</script>
|
OutputFollowing matrix is transitiveclosure of the given graph
1 1 1 1
0 1 1 1
0 0 1 1
0 0 0 1
Time Complexity: O(V3) where V is number of vertices in the given graph.
See below post for a O(V2) solution.
Transitive Closure of a Graph using DFS