Clone Graph
To solve this coding challenge, we will implement a function to deep copy an undirected graph. The graph is represented using an adjacency list, and each node in the graph has a value and a list of neighbor nodes. Our goal is to return a deep copy of the graph, meaning that each node and its connections will be duplicated without sharing any reference with the original graph.
dictionary, we ensure each node is only cloned once, efficiently handling cycles in the graph while maintaining the structure and connections of the original graph in the newly cloned graph.
Explanation
In this challenge, we need to ensure that each node and its corresponding neighbors are copied correctly in such a way that the copied graph is identical to the original graph but without any shared references. We will employ Depth-First Search (DFS) to traverse the graph and clone each node. A crucial part of this process is maintaining a map (or dictionary) to record which original nodes have already been cloned, thus preventing infinite loops due to the cyclic nature of graphs. Hereβs a step-by-step breakdown of our approach:- Check for the Null Input:
- Use a Dictionary to Keep Track:
- Depth-First Search (DFS) function:
-
Check if the current node has already been cloned by looking it up in the
visited
- If it is already cloned, return the clone from the dictionary.
-
If it is not yet cloned, create a new node, add it to the
visited
- Clone the Graph:
- Initialization:
- Dictionary Setup:
- Define DFS Function:
-
This function takes a
currentNode
-
If
currentNode
visited
-
If not, create a new node (
clonedNode
currentNode
-
Add the new
clonedNode
visited
currentNode
-
For each neighbor of
currentNode
clonedNode
-
Return the
clonedNode
- Start DFS Cloning:
-
If the input node is
null
null
-
We use a dictionary named
visited
-
We define a recursive DFS function that will:
-
Use the DFS function to start cloning from the given initial node.
# Function to clone the graph
function cloneGraph(node):
# If the given node is null, return null as there is no graph to clone
if node is null:
return null
# Dictionary to keep track of visited nodes and their clones
visited = dictionary()
# Depth-First Search (DFS) function to clone the graph
function DFS(currentNode):
# If the current node is already visited, return its clone from visited dictionary
if currentNode in visited:
return visited[currentNode]
# Create a clone for the current node
clonedNode = Node(currentNode.val)
# Add the cloned node to the visited dictionary
visited[currentNode] = clonedNode
# Iterate through all the neighbors of the current node
for neighbor in currentNode.neighbors:
# Recursively clone the neighbors and add them to the clone's neighbors list
clonedNode.neighbors.append(DFS(neighbor))
# Return the cloned node
return clonedNode
# Start cloning process from the input node using DFS
return DFS(node)
Detailed Steps in Pseudocode
-
Begin by checking if the provided
node
null
null
-
Initialize an empty dictionary called
visited
-
Call the DFS function with the initial input
node
visited