Clone Graph - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

We use Breadth-First Search (BFS) to traverse the original graph level by level starting from an entry node, cloning each node along with its connections as we go. Here, each level represents all nodes that are at the same shortest-path distance (in number of edges) from the entry node. The algorithm maintains a mapping between every original node and its clone to prevent redundant cloning and efficiently handle cycles in the graph.

🧩 Key Concepts

  • Entry Node (node): The starting point of the traversal β€” it corresponds to the node parameter provided in the original problem. BFS begins from this node, and its clone becomes the entry point of the cloned graph.
  • Original β†’ Clone Mapping: A Map called nodeToCloneMap stores the reference between each original node and its cloned counterpart.
  • Queue: A BFS queue ensures nodes are processed level by level β€” that is, by increasing distance from the entry node.
  • Neighbor Linking: For every edge in the original graph, a corresponding edge is created between clones of the connected nodes.
  • Cycle and Structure Handling: If a neighbor has already been cloned, we reuse its existing clone instead of creating a new one. This guarantees that all connections reference the correct shared clone and so ensures that the original graph’s structure is preserved β€” and also naturally prevents infinite loops during traversal of cyclic graphs.

πŸ” Step-by-Step Process

  1. Initialization:
    • Create the first clone for the entry node.
    • Add the original node to the BFS queue and map it to its clone in nodeToCloneMap.
  2. BFS Traversal:
    • Dequeue a node (called the current node).
    • For each of its neighbors:
      • If the neighbor hasn’t been cloned yet:
        • Create its clone.
        • Add the neighbor to the queue for future traversal.
        • Store the mapping in nodeToCloneMap.
      • Connect the clone of the current node to the clone of the neighbor.
  3. Termination:
    • When the queue becomes empty, all nodes and their connections have been cloned.
    • Return the clone of the entry node β€” it serves as the entry point to the fully cloned graph.

πŸ“˜ Why BFS?

BFS processes the graph level by level β€” meaning all nodes at distance n from the entry node are processed before any node at distance n + 1. This ensures that each node’s clone is created before its neighbors are visited, so when a neighbor is later encountered, its clone already exists and can be linked immediately.

Time Complexity

Let V be the number of nodes and E be the number of edges in the connected component reachable from the entry node.

  • Each node is enqueued and dequeued at most once in BFS, so processing all nodes costs O(V).
  • Each edge is inspected twice in total β€” once from each endpoint: once when exploring one node and once again when its neighbor is later encountered, but the second time it is immediately skipped since that neighbor has already been visited. Thus, edge processing still costs O(E).

Therefore, the overall time complexity is O(V + E).

Space Complexity

Let V be the number of nodes and E be the number of edges in the connected component reachable from the entry node.

  • A Map (nodeToCloneMap) stores one entry per node, requiring O(V) space.
  • The BFS queue can temporarily hold up to all nodes at the widest level of traversal β€” in the worst case, this also reaches O(V).
  • The cloned graph itself occupies O(V + E) space, but this is the output. Excluding the output, the auxiliary memory includes only references to the nodes in the map and queue, totaling O(V). (The edges are stored as part of the cloned nodes’ neighbor lists, contributing only to the output space.)

Therefore, the auxiliary space complexity (excluding input and output) is O(V), while the total space complexity (including the cloned graph output) is O(V + E).

Visucodize editorial solution titled JS Solution for LeetCode's Clone Graph coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://leetcode.com/problems/clone-graph.