Clone Graph
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.
Constraints:
- The number of nodes in the graph is in the range [0, 100]
- 1 <= Node.val <= 100
- Node.val is unique for each node
- There are no repeated edges and no self-loops in the graph
- The Graph is connected and all nodes can be visited starting from the given node
Input Format:
- A reference to a node in a connected undirected graph
Output Format:
- A deep copy (clone) of the graph
Examples:
Example 1:
Input:
adjList = [[2,4],[1,3],[2,4],[1,3]]
Output:
[[2,4],[1,3],[2,4],[1,3]]
Explanation:
There are 4 nodes in the graph. 1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4). 2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3). 3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4). 4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
Example 2:
Input:
adjList = [[]]
Output:
[[]]
Explanation:
Note that the input contains one node with val = 1 and no neighbors.
Example 3:
Input:
adjList = []
Output:
[]
Explanation:
This is an empty graph with no nodes.
Solutions
DFS with Hash Map
Use depth-first search with a hash map to keep track of cloned nodes.
BFS with Hash Map
Use breadth-first search with a hash map to keep track of cloned nodes.
Algorithm Walkthrough
Example input:
Step-by-step execution:
- Input: adjList = [[2,4],[1,3],[2,4],[1,3]]
- This represents a graph with 4 nodes:
- - Node 1 is connected to nodes 2 and 4
- - Node 2 is connected to nodes 1 and 3
- - Node 3 is connected to nodes 2 and 4
- - Node 4 is connected to nodes 1 and 3
- Start DFS with node 1 as the entry point
- Create a visited map to store cloned nodes
- Create a cloned node 1 and add to visited map
- Process neighbors of node 1: [2, 4]
- For neighbor 2:
- Create a cloned node 2 and add to visited map
- Process neighbors of node 2: [1, 3]
- For neighbor 1: Already visited, get from map
- For neighbor 3:
- Create a cloned node 3 and add to visited map
- Process neighbors of node 3: [2, 4]
- For neighbor 2: Already visited, get from map
- For neighbor 4:
- Create a cloned node 4 and add to visited map
- Process neighbors of node 4: [1, 3]
- For neighbor 1: Already visited, get from map
- For neighbor 3: Already visited, get from map
- Return the cloned node 1, which is now the head of a deep copy of the graph
Hints
Hint 1
Hint 2
Hint 3
Video Tutorial
Video tutorials can be a great way to understand algorithms visually
Visualization
Visualize the cloning process as the graph is traversed, showing the original graph and the cloned graph side by side.
Key visualization elements:
- current node being processed
- edges being created
- visited nodes
Implementation Notes
This problem tests your understanding of graph traversal (DFS or BFS) and creating deep copies of complex data structures. Both approaches have the same time and space complexity, but DFS is often more intuitive for this problem.