The idea is to do a BFS traversal of the graph and while visiting a node make a clone node of it (a copy of original node). If a node is encountered which is already visited then it already has a clone node.
How to keep track of the visited/cloned nodes?
A HashMap/Map is required in order to maintain all the nodes which have already been created.
Key stores: Reference/Address of original Node
Value stores: Reference/Address of cloned Node
A copy of all the graph nodes has been made, how to connect clone nodes?
While visiting the neighboring vertices of a node u get the corresponding cloned node for u , let’s call that cloneNodeU , now visit all the neighboring nodes for u and for each neighbor find the corresponding clone node(if not found create one) and then push into the neighboring vector of cloneNodeU node.
How to verify if the cloned graph is a correct?
Do a BFS traversal before and after the cloning of graph. In BFS traversal display the value of a node along with its address/reference.
Compare the order in which nodes are displayed, if the values are same but the address/reference is different for both the traversals than the cloned graph is correct.
Output in Java:
BFS traversal of a graph before cloning Value of Node 1 Address of Node GraphNode@15db9742 Value of Node 2 Address of Node GraphNode@6d06d69c Value of Node 4 Address of Node GraphNode@7852e922 Value of Node 3 Address of Node GraphNode@4e25154f BFS traversal of a graph after cloning Value of Node 1 Address of Node GraphNode@70dea4e Value of Node 2 Address of Node GraphNode@5c647e05 Value of Node 4 Address of Node GraphNode@33909752 Value of Node 3 Address of Node GraphNode@55f96302
Output in C++:
BFS Traversal before cloning Value of Node 1 Address of Node 0x24ccc20 Value of Node 2 Address of Node 0x24ccc50 Value of Node 4 Address of Node 0x24cccb0 Value of Node 3 Address of Node 0x24ccc80 BFS Traversal after cloning Value of Node 1 Address of Node 0x24cd030 Value of Node 2 Address of Node 0x24cd0e0 Value of Node 4 Address of Node 0x24cd170 Value of Node 3 Address of Node 0x24cd200
This article is contributed by Chirag Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Clone an undirected graph with multiple connected components
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Clone a Directed Acyclic Graph
- Eulerian Path in undirected graph
- Sum of degrees of all nodes of a undirected graph
- Detect cycle in an undirected graph
- Connected Components in an undirected graph
- Number of Triangles in an Undirected Graph
- Print all the cycles in an undirected graph
- Find k-cores of an undirected graph
- Detect cycle in an undirected graph using BFS
- Shortest cycle in an undirected unweighted graph
- Cycles of length n in an undirected and connected graph
- Count number of edges in an undirected graph
- Check if there is a cycle with odd weight sum in an undirected graph