Clone Graph

Program for Clone Graph in an Undirected Graph

You are given a node from a connected, undirected graph. Your task is to create an exact copy (a deep copy) of the entire graph.

Each node in the graph has two properties:

  • An integer value (val), which represents the node’s label.
  • A list of neighboring nodes (neighbors), which contains the nodes directly connected to it.
Clone Graph of an Undirected

The graph is represented using an adjacency list, which is a way to describe the connections between nodes. The adjacency list maps each node to a list of its neighbors.

For simplicity:

  • The node values range from 1 to n, where n is the total number of nodes.
  • The node’s position in the adjacency list matches its value (1-indexed). For example, the first position in the adjacency list corresponds to the node with value 1.

The input node will always be the first node in the graph, and its value will be 1.

Example 1:

Example of Clone Graph in an Undirected

Explanation:

There are 3 nodes in the graph-

  • Node 1: val = 1 and neighbors = [2].
  • Node 2: val = 2 and neighbors = [1, 3].
  • Node 3: val = 3 and neighbors = [2].

Explanation: The graph is empty.

Constraints :

  • 0 <= The number of nodes in the graph <= 100.
  • 1 <= Node.val <= 100
  • There are no duplicate edges and no self-loops in the graph.

Solution for Clone Graph in an Undirected Graph

Recommendation for Time and Space Complexity – Aim for O(V + E) time and O(E) space, where V is the number of vertices and E is the number of edges.

Hints for solving problems

Hint 1

To clone the graph, you must copy all nodes and their connections. Use a recursive approach to copy each node and its neighbors. A hash map can help store the original nodes and their cloned references.

Hint 2

Use Depth First Search (DFS) to traverse and clone the graph. Start at the given node, create its clone, and recursively clone its neighbors. Add the cloned neighbors to the current node’s neighbors list.

Hint 3

Stop the recursion when you encounter a node that is already cloned (tracked in the hash map). This ensures you don’t visit the same node twice. Return the clone of the input node after the process.

There are mainly 2 approach to solve this problem-

  1. Depth First Search Method
  2. Breadth First Search Method

1. Depth First Method

In the DFS method, you use recursion to traverse the graph starting from the given node. At each node, you create its clone, recursively clone its neighbors, and add the cloned neighbors to the current node’s neighbors list. A hash map keeps track of visited nodes to avoid duplicate cloning.

  • Time complexity: O(V + E)
  • Space complexity: O(V)

where V is the number of vertices and E is the number of edges.

Code

2. Breadth First Search Method

In the BFS method, you use a queue to traverse the graph level by level. Start by cloning the given node and pushing it into the queue. For each node in the queue, clone its neighbors, add the cloned neighbors to the node’s list, and enqueue unvisited neighbors. A hash map is used to track visited nodes.

  • Time complexity: O(V + E)
  • Space complexity: O(V)

where V is the number of vertices and E is the number of edges.

Code

More Articles