> For the complete documentation index, see [llms.txt](https://gl01.gitbook.io/gfg-editorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gl01.gitbook.io/gfg-editorials/2023/10-2023-oct-16/17-transitive-closure-of-a-graph.md).

# 17. Transitive closure of a Graph

The problem can be found at the following link: [Question Link](https://practice.geeksforgeeks.org/problems/transitive-closure-of-a-graph0930/1)

![](https://badgen.net/badge/Level/Medium/yellow)

## My Approach

This is the standard Floyd-Warshall graph problem for finding the transitive closure of a graph.

* Initialize a transitive closure matrix with the given graph
* Set all diagonal elements to 1
* Use the Floyd-Warshall algorithm to compute the transitive closure by considering all pairs of vertices and updating the matrix based on the existence of a path between them through a third vertex.

For better understanding, refer to the following video: [Floyd-Warshall Algorithm](https://www.programiz.com/dsa/floyd-warshall-algorithm)

## Time and Auxiliary Space Complexity

* **Time Complexity**: `O(N^3)`, where `N` is the number of vertices in the graph
* **Auxiliary Space Complexity**: `O(N^2)` is the number of vertices in the graph

## Code (C++)

```cpp
class Solution {
public:
    vector<vector<int>> transitiveClosure(int N, vector<vector<int>> graph)
    {
        vector<vector<int>> transitive(N, vector<int>(N, 0));

        for (int i=0; i<N; i++)
        {
            for (int j=0; j<N; j++)
            {
                transitive[i][j] = graph[i][j];
                if (i == j)
                {
                    transitive[i][j] = 1;
                }
            }
        }

        for (int k=0; k<N; k++)
        {
            for (int i=0; i<N; i++)
            {
                for (int j=0; j<N; j++)
                {
                    if (transitive[i][k] && transitive[k][j])
                    {
                        transitive[i][j] = 1;
                    }
                }
            }
        }

        return transitive;
    }
};
```

## Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gl01.gitbook.io/gfg-editorials/2023/10-2023-oct-16/17-transitive-closure-of-a-graph.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
