13. Clone an Undirected Graph
The problem can be found at the following link: Question Link
Problem Description
Given a connected undirected graph represented by an adjacency list, adjList[][]
, with n nodes (each node having a distinct label from 0
to n-1
), your task is to create a clone (deep copy) of this graph. Each node in the graph has:
An integer
val
A list
neighbors
containing references to its adjacent nodes
Structure
class Node {
int val;
List<Node> neighbors;
}
Return a reference to the cloned graph such that the cloned graph is identical to the original graph. The driver code will then print true
if the clone is correct; otherwise it prints false
.
Note: Sorry for uploading late, my Final Sem exam is going on.
Examples
Example 1
Input:
n = 4 adjList = [[1, 2], [0, 2], [0, 1, 3], [2]]
Output:
true
Explanation:
The cloned graph is structurally identical to the original. Driver code checks memory and structure integrity.
Example 2
Input:
n = 3 adjList = [[1, 2], [0], [0]]
Output:
true
Explanation:
The cloned graph is identical to the original one, hence driver code outputs true.
Constraints
$( 1 \leq n \leq 10^4 )$
$( 0 \leq \text{number of edges} \leq 10^5 )$
$( 0 \leq \text{adjList[i][j]} < n )$
My Approach
BFS-Based Graph Clone using Queue
Algorithm Steps:
Initialization:
If the starting node is
null
, returnnull
.Create a hash map (or dictionary) to store mappings from each original node to its clone.
Initialize a queue and enqueue the starting node.
BFS Traversal:
While the queue is not empty:
Dequeue the front node.
For each neighbor of the dequeued node:
If the neighbor hasn't been cloned (i.e., not present in the map), create its clone, store it in the map, and enqueue the neighbor.
Append the clone of the neighbor to the
neighbors
list of the clone corresponding to the current node.
Return:
After processing all nodes, return the clone corresponding to the starting node.
Time and Auxiliary Space Complexity
Expected Time Complexity:
O(N + M)
, whereN
is the number of nodes andM
is the number of edges. We visit each node and edge exactly once during the clone.Expected Auxiliary Space Complexity:
O(N)
, used by the hash map and the queue.
Code (C++)
// β
BFS-Based Graph Clone using Queue
class Solution {
public:
Node* cloneGraph(Node* node) {
if (!node) return 0;
unordered_map<Node*, Node*> m;
m[node] = new Node(node->val);
queue<Node*> q{{node}};
while (!q.empty()) {
for (auto n : q.front()->neighbors) {
if (!m[n]) q.push(n), m[n] = new Node(n->val);
m[q.front()]->neighbors.push_back(m[n]);
}
q.pop();
}
return m[node];
}
};
Code (Java)
class Solution {
Node cloneGraph(Node node) {
if (node == null) return null;
Map<Node, Node> m = new HashMap<>();
Queue<Node> q = new LinkedList<>();
m.put(node, new Node(node.val));
q.add(node);
while (!q.isEmpty()) {
for (Node n : q.peek().neighbors) {
if (!m.containsKey(n)) {
m.put(n, new Node(n.val));
q.add(n);
}
m.get(q.peek()).neighbors.add(m.get(n));
}
q.poll();
}
return m.get(node);
}
}
Code (Python)
class Solution:
def cloneGraph(self, node):
if not node: return None
m, q = {node: Node(node.val)}, [node]
while q:
for n in q[0].neighbors:
if n not in m:
m[n] = Node(n.val)
q.append(n)
m[q[0]].neighbors.append(m[n])
q.pop(0)
return m[node]
Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated