Transpose of a directed graph G is another directed graph on the same set of vertices with all of the edges reversed compared to the orientation of the corresponding edges in G. That is, if G contains an edge (u, v) then the converse/transpose/reverse of G contains an edge (v, u) and vice versa.
Given a graph (represented as adjacency list), we need to find another graph which is the transpose of the given graph.
Input : figure (i) is the input graph. Output : figure (ii) is the transpose graph of the given graph.
We traverse the adjacency list and as we find a vertex v in the adjacency list of vertex u which indicates an edge from u to v in main graph, we just add an edge from v to u in the transpose graph i.e. add u in the adjacency list of vertex v of the new graph. Thus traversing lists of all vertices of main graph we can get the transpose graph. Thus the total time complexity of the algorithm is O(V+E) where V is number of vertices of graph and E is the number of edges of the graph.
Note : It is simple to get the transpose of a graph which is stored in adjacency matrix format, you just need to get the transpose of that matrix.
0--> 2 1--> 0 4 2--> 3 3--> 0 4 4--> 0
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- BFS for Disconnected Graph
- Hypercube Graph
- Sum of dependencies in a graph
- Dominant Set of a Graph
- Graph and its representations
- Biconnected graph
- Bridges in a graph
- Check whether a given graph is Bipartite or not
- Difference between graph and tree
- Transitive closure of a graph
- Depth First Search or DFS for a Graph
- A Peterson Graph Problem
- Check for star graph
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.