Clone Graph - Complete Solution Guide
Clone Graph is LeetCode problem 133, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a reference of a node in a connected undirected graph. Return a deep copy (clone) of the graph. Each node in the graph contains a value ( int ) and a list ( List[Node] ) of its neighbors. class Node { public int val; public List<Node> neighbors; } Test case format: For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with val == 1 , the second node with val == 2 , and so on. The graph is represented in the test case using an adjacency l
Detailed Explanation
The problem asks us to create a deep copy of a given undirected graph. A deep copy means creating a completely new graph with the same structure and values as the original, such that modifying the copy doesn't affect the original and vice versa. The input is a reference to a node in the original graph (specifically, the node with value 1). Each node has a value and a list of neighbors. The output should be a reference to the corresponding node in the newly created cloned graph. The graph is guaranteed to be connected.
Solution Approach
The provided solution uses Depth-First Search (DFS) to traverse the original graph and create a clone. It uses a HashMap (`old_to_new` in Python, `oldToNew` in Java/C++, `oldToNew` array in C) to store the mapping between the original nodes and their corresponding cloned nodes. The DFS function recursively explores the graph, creating a new node for each original node encountered and adding it to the HashMap. For each neighbor of a node, the DFS function is recursively called to clone the neighbor. The cloned neighbor is then added to the neighbor list of the cloned node.
Step-by-Step Algorithm
- Step 1: Check if the input node is null. If so, return null (handles the case of an empty graph).
- Step 2: Initialize a HashMap (or array in C) to store the mapping between original nodes and their cloned nodes.
- Step 3: Define a recursive DFS function that takes an original node as input.
- Step 4: Inside the DFS function, check if the original node is already present in the HashMap. If so, it means we have already created a clone for this node; return the cloned node from the HashMap.
- Step 5: If the original node is not in the HashMap, create a new node with the same value as the original node.
- Step 6: Add the original node and its newly created clone to the HashMap.
- Step 7: Iterate through the neighbors of the original node.
- Step 8: For each neighbor, recursively call the DFS function to get the cloned neighbor.
- Step 9: Add the cloned neighbor to the neighbor list of the cloned node.
- Step 10: Return the cloned node.
Key Insights
- Insight 1: We need to avoid infinite loops. Since the graph is undirected, simply traversing the graph will lead to revisiting nodes. We must keep track of the nodes we have already visited.
- Insight 2: A HashMap (or similar data structure) is crucial to map each original node to its corresponding cloned node. This allows us to efficiently check if a node has already been copied.
- Insight 3: We can use either Depth-First Search (DFS) or Breadth-First Search (BFS) to traverse the graph and create the clone.
Complexity Analysis
Time Complexity: O(V+E)
Space Complexity: O(V)
Topics
This problem involves: Hash Table, Depth-First Search, Breadth-First Search, Graph.
Companies
Asked at: Docusign, Flexport, Google, Grammarly, Nutanix, Pocket Gems, ThousandEyes, TikTok, Uber, Wix, Yahoo.