Solution: Clone Graph

Let's solve the Clone Graph problem using the Graphs pattern.

Statement

Given a reference of a node in a graph with data and a list of neighbors, create a deep copy of the graph. The graph has the following properties:

  • Undirected: The edges of the graph are bidirectional.

  • Connected: A path will always exist between any two nodes.

In a deep copy, a new instance of every node is created with the same data as in the original graph. Any modifications made to the new graph are not reflected in the original graph.

For simplicity, we are creating a graph using an adjacency list, where the data of each node is represented by its index in the adjacency list. Each list in the adjacency list describes the set of neighbors of a node in the graph. The index in the adjacency list starts from 1. For example, for [[2, 3], [1, 3], [1, 2]], there are three nodes in the graph:

1st1^{st} node (data = 1): Neighbors are 2nd2^{nd} node (data = 2) and 3rd3^{rd} node (data = 3).

2nd2^{nd} node (data = 2): Neighbors are 1st1^{st} node (data = 1) and 3rd3^{rd} node (data = 3).

3rd3^{rd} node (data = 3): Neighbors are 1st1^{st} node (data = 1) and 2nd2^{nd} node (data = 2).

Constraints:

  • 00 \leq Number of nodes 100\leq 100
  • 11 \leq Node.data 100\leq 100
  • Node.data is unique for each node.
  • The graph is undirected, i.e., there are no self-loops or repeated edges.
  • The graph is connected, i.e., any node can be visited from a given node.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.