Transitive Closure of Directed Graph Problem Statement

Given a directed graph with 'V' vertices and 'E' edges, determine if a vertex i is reachable from vertex j for all pairs of vertices (i, j). A vertex j is considered reachable from vertex i if there is a path from i to j.

Input:

The input begins with an integer 'T', the number of test cases.
For each test case, the first line contains two space-separated integers 'V' and 'E'.
Following this, each of the next 'E' lines contains two space-separated integers 'a' and 'b', indicating a directed edge from vertex 'a' to vertex 'b'.

Output:

Produce a V x V matrix for each test case, showing reachability between each pair of vertices.

Example:

Input:
1
4 4
0 1
1 2
2 3
3 0
Output:
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

Constraints:

  • 1 <= T <= 50
  • 1 <= V <= 1000
  • 0 <= E <= 1000
  • Time Limit: 1 sec

Note:

The graph edges are one-way (directed).
Each vertex is reachable from itself.
No self-loops or duplicate edges exist.
Graph might have disconnected components.
Be the first one to answer
Add answer anonymously...
Goldman Sachs Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter