Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
LeetCode-133: 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
and a list
of its neighbors.
We can use both DFS and BFS to solve this problem. Let us first visit the nodes using DFS.
HashMap
to store the mapping of the original node to the cloned node. This will help us avoid visiting the same node multiple times.HashMap
.Similarly, we can solve the problem using BFS.
HashMap
to store the mapping of the original node to the cloned node. Also, initialize a Queue
to store the nodes that need to be visited.HashMap
.HashMap
.HashMap
.Here is a Java implementation of the above approach using DFS.
private final Map<Node, Node> cache = new HashMap<>();
public Node cloneGraph(Node node) {
if(null == node){
return node;
}
// reuse the cloned node if already created
if(cache.containsKey(node)){
return cache.get(node);
}
// clone the node and add to the cache
Node clone = new Node(node.val);
cache.put(node, clone);
// clone the neighbors
for(Node neighbor : node.neighbors){
Node clonedNeighbor = cloneGraph(neighbor);
clone.neighbors.add(clonedNeighbor);
}
return clone;
}
Let us now implement the above approach using BFS.
public Node cloneGraph(Node node) {
if (null == node) {
return node;
}
Map<Node, Node> cache = new HashMap<>();
cache.put(node, new Node(node.val));
Queue<Node> queue = new LinkedList<>();
queue.add(node);
while (!queue.isEmpty()) {
Node root = queue.remove();
for (Node neighbor : root.neighbors) {
Node clonedNeighbor = null;
if (cache.containsKey(neighbor)) {
clonedNeighbor = cache.get(neighbor);
} else {
clonedNeighbor = new Node(neighbor.val);
queue.add(neighbor);
cache.put(neighbor, clonedNeighbor);
}
cache.get(root).neighbors.add(clonedNeighbor);
}
}
return cache.get(node);
}
The time complexity for both DFS and BFS is $O(N + E)$, where $N$ is the number of nodes and $E$ is the number of edges in the graph.
For DFS, the space complexity is $O(N + H) \approx O(N)$ due to the recursion Stack
and the HashMap
. Here, H
is the height of the graph. Similarly, for BFS, the space complexity is $O(N + E) \approx O(N)$ due to the Queue
and the HashMap
.
Be notified of new posts. Subscribe to the RSS feed.