In the Clone Graph problem, you're given a reference node from a connected undirected graph. Each node contains a value and a list of neighbors. The task is to create a deep copy of the entire graph, such that each node in the new graph is a completely new object with identical structure and connections.
The major challenge in cloning a graph lies in preserving the structure while avoiding infinite loops due to cycles. Additionally, since nodes reference each other via their neighbors, we must ensure each original node maps to exactly one newly created node.
The approach uses Depth-First Search (DFS) or Breadth-First Search (BFS) with a hash map to keep track of cloned nodes:
First, handle the base case: if the input node is null
, return null
.
Next, initialize a hash map (let's call it o_to_n
) that will map each original node to its corresponding cloned node.
Then, define a recursive DFS function that:
o_to_n
.Finally, invoke the DFS function starting with the input node and return the resulting cloned node. The graph will be fully copied.
Since we visit each node exactly once and clone it, the time complexity is O(n), where n
is the number of nodes in the graph.
The space complexity is also O(n) due to the hash map and recursion stack (in case of DFS).
Cloning graphs is a fundamental task in many domains such as: