Solution: Clone Graph

Let's solve the Clone Graph problem using the Graphs pattern.

Statement#

Given a graph that has nodes with data and a list of neighbors, create a deep copy of the graph. The graph has the following properties:

  • Undirected: The edges of the graph are bidirectional.

  • Connected: A path will always exist between any two nodes.

In a deep copy, a new instance of every node is created with the same data as in the original graph. Any modifications made to the new graph are not reflected in the original graph.

For simplicity, we are creating a graph using an adjacency list, where the data of each node is represented by its index in the adjacency list. Each list in the adjacency list describes the set of neighbors of a node in the graph. The index in the adjacency list starts from 1. For example, for [[2, 3], [1, 3], [1, 2]], there are three nodes in the graph:

1st1^{st} node (data = 1): Neighbors are 2nd2^{nd} node (data = 2) and 3rd3^{rd} node (data = 3).

2nd2^{nd} node (data = 2): Neighbors are 1st1^{st} node (data = 1) and 3rd3^{rd} node (data = 3).

3rd3^{rd} node (data = 3): Neighbors are 1st1^{st} node (data = 1) and 2nd2^{nd} node (data = 2).

Constraints:

  • 00 \leq Number of nodes 100\leq 100
  • 11 \leq Node.data 100\leq 100
  • Node.data is unique for each node.
  • The graph is undirected, i.e., there are no self-loops or repeated edges.
  • The graph is connected, i.e., any node can be visited from a given node.

Solution#

We use depth-first traversal and create a copy of each node while traversing the graph. We use a hash map to store each visited node to avoid getting stuck in cycles. We do not revisit nodes that exist in the hash map. The hash map key is a node in the original graph, and its value is the corresponding node in the cloned graph.

In order to solve this problem using the methodology discussed above, we create a recursive function, clone_helper, that takes two arguments: the current node being cloned and a hash map. The steps of this recursive function are given below:

  1. If graph is empty, return NULL. This will also work as a base case for our recursive function.
  2. If the current node is not NULL, create a new Node with the same data as the current node, and add the current node as key and its clone as value to the hash map.
  3. Iterate through all the neighbors of the current node. For each neighbor, check if the neighbor is already cloned by looking up the neighbor in the hash map:
    • If the neighbor is not cloned yet, recursively call the function with the neighbor as the current node.
    • If the neighbor is already cloned, add the cloned neighbor to the new node’s neighbors.
  4. Finally, return the new node.

The clone function is the main function that creates a deep copy of the graph. It takes a single argument, which is a reference to the root node of the graph. The function creates an empty hash map to keep track of nodes that have already been cloned. Then it calls the clone_helper function, passing in the root node and the hash map.

Created with Fabric.js 3.6.6 key value We will create a deep copy of the graph aboveusing depth-first search.

1 of 6

Created with Fabric.js 3.6.6 key 1 value 1 Start from root node 1. Clone node 1. Putthe original node 1 as a key and its clone as avalue in the hash map.

2 of 6

Created with Fabric.js 3.6.6 key 1 2 value 1 2 Move to node 2, create a copy of this, and add itto the node 1 neighbor. Put the original node as akey and the cloned node as a value in the hash map.Check its neighbors, 1 and 3. 1 is already cloned.Add it to its neighbor. Next, we will move to 3.

3 of 6

Created with Fabric.js 3.6.6 key 1 2 3 value 1 2 3 Move to node 3, and create a copy of this. Addit to the node 2 neighbor. Put the original node asthe key and the cloned node as a value in thehash map. Check its neighbors, 2 and 4. 2 is alreadycloned. Add it to its neighbor. Next, we will move to 4.

4 of 6

Created with Fabric.js 3.6.6 key 1 2 3 4 value 1 2 3 4 Move to node 4, and create a copy of this. Add itto the node 3 neighbor. Put the original node asthe key and the cloned node as a value in the hash map.Check its neighbors, 3 and 5. 3 is already cloned. Add it toits neighbor. Next, we will move to 5.

5 of 6

Created with Fabric.js 3.6.6 key 1 2 3 4 5 value 1 2 3 4 5 Move to node 5, and create a copy of this. Addit to the node 4 neighbor. Put the original nodeas the key and the cloned node as a value in thehash map. Check its neighbors, 1 and 4. 1 and 4 arealready cloned. Add them to its neighbors.

6 of 6

main.py
Node.py
graph_utility.py
Clone Graph

Time complexity#

The time complexity of this code is O(n+m)O(n+m), where nn is the number of nodes, and mm is the number of edges.

Space complexity#

The space complexity of this code is O(n)O(n), where nn is the number of nodes in the dictionary.

Note: We can also solve this problem using BFS.

Clone Graph

Trie: Introduction