Reverse the Directed Graph

Objective: Given a directed graph, write an algorithm to reverse the graph. 

Example: 

Approach:

Create a new graph with the same number of vertices. Traverse the given graph. Here we are using the adjacency list to represent the graph. Traverse each adjacency list and while traversing keep adding the reverse edges (making source as destination and destination as source). See the code for better understanding.

Code:

Output:

---------------Original Graph---------------------------
Vertex connected from vertex: 0
->1
Vertex connected from vertex: 1
->3->2
Vertex connected from vertex: 2
->3
Vertex connected from vertex: 3
->4
Vertex connected from vertex: 4
->0
---------------Reverse Graph---------------------------
Vertex connected from vertex: 0
->4
Vertex connected from vertex: 1
->0
Vertex connected from vertex: 2
->1
Vertex connected from vertex: 3
->2->1
Vertex connected from vertex: 4
->3

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

You may also like...

%d bloggers like this: