Jobs
Interviews

Solve Transitive Closure of a Graph using Python Language

Solve Transitive Closure of a Graph using Python to enhance your skills with python coding practice , master coding concepts, and prepare for interviews with practical exercises and detailed solutions.

Transitive Closure of a Graph

Difficulty : Medium

Categories :

  • Graphs
  • Dynamic programming

Given a directed graph represented as an adjacency matrix with N vertices, find its transitive closure. The transitive closure is a reachability matrix where matrix[i][j] indicates whether vertex j is reachable from vertex i through a path.

Constraints:

  • 1 ≤ N ≤ 100
  • Graph is represented as NxN adjacency matrix
  • Matrix values are either 0 or 1

Examples:

Input:
N = 4
graph = [
  [1,1,0,1],
  [0,1,1,0],
  [0,0,1,1],
  [0,0,0,1]
]
Output: [
  [1,1,1,1],
  [0,1,1,1],
  [0,0,1,1],
  [0,0,0,1]
]
Explanation: Output[i][j] = 1 if vertex j is reachable from vertex i
Input:
N = 3
graph = [
  [1,0,0],
  [0,1,0],
  [0,0,1]
]
Output: [
  [1,0,0],
  [0,1,0],
  [0,0,1]
]
Explanation: Each vertex can only reach itself

Coding works best on a laptop or desktop, with a keyboard and a wider editor. You can read the problem here and solve it there.

Problem Solving

Input

What You'll Find Here

Interactive Exercises Practice coding with problems designed for beginners and experts.

Step-by-Step Solutions Understand every step of the solution process.

Real-World Scenarios Apply your skills to real-world problems and boost your confidence.

Choose from the following categories