Welcome to Day 11 of your coding interview journey.
Yesterday, you learned:
- β Graph basics
- β DFS (Depth-First Search)
- β BFS (Breadth-First Search)
Today, we apply those concepts to a very common and important problem:
π Graph Cloning
π§ What Does βClone a Graphβ Mean?
You are given a reference to a node in a graph.
π Your task is to create a deep copy of the entire graph.
β οΈ Important
A deep copy means:
- New nodes must be created
- Connections must be preserved
- No references to the original graph
Example Graph:
1 -- 2
| |
4 -- 3
π After cloning, you should have a completely new graph with the same structure.
π Key Challenges
Graph cloning tests your ability to:
- Traverse a graph (DFS or BFS)
- Handle cycles (very important!)
- Avoid duplicating nodes
π‘ Core Idea
π Use a HashMap (dictionary) to store:
original_node -> cloned_node
This helps you:
- Avoid infinite loops
- Reuse already cloned nodes
π‘ Approach 1 β DFS (Recursive)
π§ Strategy
- If node is already cloned β return it
- Clone the current node
- Recursively clone neighbors
- Store mapping
π οΈ Solution in Python (DFS)
class Node:
def __init__(self, val=0, neighbors=None):
self.val = val
self.neighbors = neighbors if neighbors else []
def clone_graph(node):
if not node:
return None
clones = {}
def dfs(curr):
if curr in clones:
return clones[curr]
copy = Node(curr.val)
clones[curr] = copy
for neighbor in curr.neighbors:
copy.neighbors.append(dfs(neighbor))
return copy
return dfs(node)
β‘ Complexity (DFS)
- Time:
O(V + E) - Space:
O(V)
π‘ Approach 2 β BFS (Iterative)
π§ Strategy
- Use a queue to traverse
- Clone nodes level by level
π οΈ Solution in Python (BFS)
from collections import deque
def clone_graph_bfs(node):
if not node:
return None
clones = {node: Node(node.val)}
queue = deque([node])
while queue:
curr = queue.popleft()
for neighbor in curr.neighbors:
if neighbor not in clones:
clones[neighbor] = Node(neighbor.val)
queue.append(neighbor)
clones[curr].neighbors.append(clones[neighbor])
return clones[node]
π― DFS vs BFS for Cloning
| Approach | Style | When to Use |
|---|---|---|
| DFS | Recursive | Easier to write |
| BFS | Iterative | Avoid recursion limits |
β οΈ Common Mistakes
- β Not handling cycles β infinite recursion
- β Forgetting to store visited nodes
- β Creating duplicate nodes
- β Returning original nodes instead of cloned ones
π§ͺ Practice Problems
To reinforce graph concepts:
- Course schedule
- Number of connected components
- Reconstruct itinerary
- Word ladder
- Network delay time
π― Key Takeaways
- Graph cloning = Traversal + HashMap
- Always track visited nodes
- DFS and BFS both work
- Think in terms of:
- Nodes
- Neighbors
- Mapping
π Your Challenge (Day 11)
Solve this:
Clone a graph where each node also has a random pointer.
(Hint: similar idea, more mappings π)
π Final Thoughts
Graph cloning is a classic interview problem because it combines:
- Graph traversal
- Memory management
- Deep understanding of references
Mastering this makes you much stronger in graph problems.