Problem Statement

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 (int) and a list (List[Node]) of its neighbors.

class Node {
    public int val;
    public List<Node> neighbors;
}

Test case format:

For simplicity, each node’s value is the same as the node’s index (1-indexed). For example, the first node with val == 1, the second node with val == 2, and so on. The graph is represented in the test case using an adjacency list.

An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.

The given node will always be the first node with val = 1. You must return the copy of the given node as a reference to the cloned graph.

Input: adjList = [[2,4],[1,3],[2,4],[1,3]]
Output: [[2,4],[1,3],[2,4],[1,3]]
Explanation: There are 4 nodes in the graph.
1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).

DFS: Recursive

"""
# Definition for a Node.
class Node:
    def __init__(self, val = 0, neighbors = None):
        self.val = val
        self.neighbors = neighbors if neighbors is not None else []
"""

class Solution:
    def __init__(self):
        self.mp = dict()

    def cloneGraph(self, node: 'Node') -> 'Node':
        # Return node if node is empty
        if not node:
            return node

        # If the node has been cloned (added to the map),
        # return the new node. This will terminate
        # the recursion as the recursive calls below
        # will never happen.
        if node in self.mp:
            return self.mp[node]
        
        # If the node has not been cloned, clone the node 
        # and add it to the hash map.
        self.mp[node] = Node(node.val)
        # Recursively loop through the neighbours and add
        # cloned nodes to the neighbors lists. The
        # recursive calls will terminate when previously
        # clone nodes are found in the hash map.
        self.mp[node].neighbors = [self.cloneGraph(nbr) for nbr in node.neighbors]

        return self.mp[node]

DFS: Iterative

"""
# Definition for a Node.
class Node:
    def __init__(self, val = 0, neighbors = None):
        self.val = val
        self.neighbors = neighbors if neighbors is not None else []
"""
from collections import deque
class Solution:
    def cloneGraph(self, node: 'Node') -> 'Node':
        if not node:
            return node

        mp = {}
        mp[node] = Node(node.val)

        stack = deque([node])
        
        while stack:
            curr = stack.pop()
            # Loop through neighbors and add cloned nodes to stack
            for nbr in curr.neighbors:
                if nbr not in mp:
                    mp[nbr] = Node(nbr.val)
                    stack.append(nbr)
                # Append cloned neighbor nodes to clone curr node 
                mp[curr].neighbors.append(mp[nbr])
        
        # Return original copy
        return mp[node]


        """
    stack = []; 3
    mp = {
        1;1*.[2*, 4*],
        2:2*.[1*, 3*],
        3:3*.[2*, 4*],
        4:4*.[1*, 3*],
        }
            #1:[2,4],
            #2:[1,3],
            #3:[2,4],
            #4:[1,3]
        """

Leetcode 133 Clone Graph