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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Detect cycle in the graph using degrees of nodes of graph
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Convert undirected connected graph to strongly connected directed graph
- Breadth First Search or BFS for a Graph
- Find if there is a path between two vertices in a directed graph
- Depth First Search or DFS for a Graph
- Detect Cycle in a Directed Graph
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Graph and its representations
- Transitive closure of a graph
- Check whether a given graph is Bipartite or not
- Shortest Path in Directed Acyclic Graph
- Bridges in a graph
- Articulation Points (or Cut Vertices) in a Graph
- Biconnected graph
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Eulerian path and circuit for undirected graph
- Longest Path in a Directed Acyclic 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.