VOOZH about

URL: https://www.geeksforgeeks.org/dsa/clone-an-undirected-graph-with-multiple-connected-components/

⇱ Clone an undirected graph with multiple connected components - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Clone an undirected graph with multiple connected components

Last Updated : 11 Jul, 2025

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.

Examples:  

An example of an undirected graph 
with 3 connected components:

Approach: 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: 


Output
 INITIAL GRAPH
Node 1 - 0x1104050
 Node 2 - 0x1104090
 Node 3 - 0x11040d0
Node 2 - 0x1104090
 Node 1 - 0x1104050
 Node 4 - 0x1104110
Node 3 - 0x11040d0
 Node 1 - 0x1104050
 Node 4 - 0x1104110
Node 4 - 0x1104110
 Node 2 - 0x1104090
 Node 3 - 0x11040d0
Node 5 - 0x1104150
 Node 6 - 0x1104190
Node 6 - 0x1104190
 Node 5 - 0x1104150


 CLONED GRAPH
Node 1 - 0x1104780
 Node 2 - 0x1104850
 Node 3 - 0x11048d0
Node 2 - 0x1104850
 Node 1 - 0x1104780
 Node 4 - 0x1104970
Node 3 - 0x11048d0
 Node 1 - 0x1104780
 Node 4 - 0x1104970
Node 4 - 0x1104970
 Node 2 - 0x1104850
 Node 3 - 0x11048d0
Node 5 - 0x1104810
 Node 6 - 0x1104ab0
Node 6 - 0x1104ab0
 Node 5 - 0x1104810

Complexity Analysis:

  • Time Complexity: O(V + E) where V and E are the numbers of vertices and edges in the graph respectively.
  • Auxiliary Space: O(V + E).  
Comment
Article Tags:
Article Tags: