You are given a directed graph consisting of 'V' vertices and 'E' edges. You need to find whether a vertex i is reachable from all other vertices j for all pairs of vertices (i, j). A vertex 'j' is said to be reachable from vertex 'i' if and only if there exists a path that originates from vertex 'i' and terminates at the vertex 'j'.
Note :
1. Since it is a directed graph, all the edges are one way.
2. Every vertex is reachable to itself.
3. There are no self-loops or multiple edges between the same pair of nodes.
4. The graph may not be fully connected, there might be different disconnected components of the graph.
The first line contains an integer 'T' which denotes the number of test cases.
The first line of each test case contains two space-separated integers 'V', 'E' denoting the number of vertices and the number of edges respectively.
From the second line onwards, the next 'E' lines will denote the directed edge of the graphs.
Every edge is defined by two single space-separated integers 'a' and 'b', which signifies a directed edge from vertex 'a' to vertex 'b'.
Output Format :
The output will contain a V x V matrix, where each cell of the matrix will denote the reachability for the given pair of vertices. Refer sample input for further clarification.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function. Also in the function, the vertices are numbered from 0 to v - 1, where 'v' is the total number of vertices.
1 <= T <= 50
1 <= V <= 1000
0 <= E <= 1000
Time Limit: 1sec
where 'T' is the number of test cases, 'V' and 'E' are the number of vertices and edges respectively.
1
4 3
1 2
2 3
3 4
1 1 1 1
0 1 1 1
0 0 1 1
0 0 0 1
From node 1 we can reach node 2, from node 2 we can reach node 3, and from node 3 we can reach node 4. So for each of the corresponding cells in the closure matrix, we put 1 and for all other nodes, we put 0.
Try to use the simplest possible approach.
O(V^3), where ‘V’ is the number of vertices in the graph.
Since we are using Floyd Warshall's algorithm which takes O(V^3) time.
O(V^2), where ‘V’ is the number of vertices in the graph.
Since we are using an array/list of V * V for storing the reachability matrix. So the overall space complexity will be O(V^2).