Given an undirected graph with multiple connected components, the task is to clone the graph. Cloning a graph with a single connected component can be seen here.
An example of an undirected graph with 3 connected components:
The idea is to follow the same approach posted for cloning connected graph, but with every node so that we can clone graphs with multiple connected components.
We are going to use a GraphNode class and a Graph class. The Graph class is compulsory, since we might have multiple connected components (see example above), and we cannot deal with them having only a GraphNode as an input. For the Graph class, what we actually need is a list of GraphNodes. It’s also possible to make a list of nodes instead of creating a class, both ways work.
To keep track of the visited nodes, we need a data structure; a map is an appropriate one, as we can map from the “old” nodes to the “new” ones (the cloned). So, we are defining a main function, which creates the map, and uses a helper function to fill it. Once the map is created, a new graph can be created, using the cloned nodes in the map.
The helper function is going to put connections between nodes (besides filling the map). As we are dealing with a whole connected component, a similar approach to the BFS is going to be followed.
Notice that in the main function, we don’t call the helper function for each node in the Graph; if the node is stored in the map, it means that we’ve already visited it and dealt with its connected component, so no need to repeat the steps again.
In order to check if the graph has been correctly cloned, we can print the memory addresses of the nodes, and compare them to see whether we’ve cloned, or we’ve copied them.
Below is the implementation of the above approach:
INITIAL GRAPH Node 1 - GraphNode@232204a1 Node 2 - GraphNode@4aa298b7 Node 3 - GraphNode@7d4991ad Node 2 - GraphNode@4aa298b7 Node 1 - GraphNode@232204a1 Node 4 - GraphNode@28d93b30 Node 3 - GraphNode@7d4991ad Node 1 - GraphNode@232204a1 Node 4 - GraphNode@28d93b30 Node 4 - GraphNode@28d93b30 Node 2 - GraphNode@4aa298b7 Node 3 - GraphNode@7d4991ad Node 5 - GraphNode@1b6d3586 Node 6 - GraphNode@4554617c Node 6 - GraphNode@4554617c Node 5 - GraphNode@1b6d3586 CLONED GRAPH Node 1 - GraphNode@74a14482 Node 2 - GraphNode@1540e19d Node 3 - GraphNode@677327b6 Node 2 - GraphNode@1540e19d Node 1 - GraphNode@74a14482 Node 4 - GraphNode@14ae5a5 Node 3 - GraphNode@677327b6 Node 1 - GraphNode@74a14482 Node 4 - GraphNode@14ae5a5 Node 4 - GraphNode@14ae5a5 Node 2 - GraphNode@1540e19d Node 3 - GraphNode@677327b6 Node 6 - GraphNode@7f31245a Node 5 - GraphNode@6d6f6e28 Node 5 - GraphNode@6d6f6e28 Node 6 - GraphNode@7f31245a
- Connected Components in an undirected graph
- Sum of the minimum elements in all connected components of an undirected graph
- Maximum number of edges among all connected components of an undirected graph
- Program to count Number of connected components in an undirected graph
- Clone an Undirected Graph
- Cycles of length n in an undirected and connected graph
- Number of single cycle components in an undirected graph
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Strongly Connected Components
- Number of connected components in a 2-D matrix of strings
- Tarjan's Algorithm to find Strongly 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
- Check if a directed graph is connected or not
- All vertex pairs connected with exactly k edges in a 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 email@example.com. 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.