![]() |
VOOZH | about |
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.
Example:
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.
Implementation:
0--> 2 1--> 0 4 2--> 3 3--> 0 4 4--> 0
Time Complexity:
The time complexity of the addEdge function is O(1), as it simply appends an element to the vector.
The time complexity of the displayGraph function is O(V + E), where V is the number of vertices and E is the number of edges, as it needs to traverse the adjacency list of each vertex and print out the adjacent vertices.
The time complexity of the transposeGraph function is also O(V + E), where V is the number of vertices and E is the number of edges, as it needs to traverse the adjacency list of each vertex and add the corresponding edges to the transpose graph's adjacency list.
Therefore, the overall time complexity of the program is O(V + E).
Space complexity:
In terms of space complexity, the program uses two arrays of vectors to represent the original graph and its transpose, each of which has a size of V (the number of vertices). Additionally, the program uses a constant amount of space to store integer variables and temporary data structures. Therefore, the space complexity of the program is O(V).
Note that the space complexity of the program could be larger if the input graph has a large number of edges, as this would require more memory to store the adjacency lists.