Woodstock Blog

a tech blog for general algorithmic interview questions

[LeetCode 133] Clone Graph

Question

link

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.


OJ's undirected graph serialization:

Nodes are labeled uniquely.

We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.

As an example, consider the serialized graph {0,1,2#1,2#2,2}.

The graph has a total of three nodes, and therefore contains three parts as separated by #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

Visually, the graph looks like the following:

       1
      / \
     /   \
    0 --- 2
         / \
         \_/

Stats

Adjusted Difficulty 3
Time to use thought for a while

Ratings/Color = 1(white) 2(lime) 3(yellow) 4/5(red)

Analysis

This is a difficult coding question, although the idea is simple.

Solution

Two solution: BFS (recommended) and DFS. This is a good analysis article.

Let’s analyze this further by using the below example:

A simple graph

Assume that the starting point of the graph is A. First, you make a copy of node A (A2), and found that A has only one neighbor B. You make a copy of B (B2) and connects A2->B2 by pushing B2 as A2′s neighbor. Next, you find that B has A as neighbor, which you have already made a copy of. Here, we have to be careful not to make a copy of A again, but to connect B2->A2 by pushing A2 as B2′s neighbor. But, how do we know if a node has already been copied?

Basic idea is to use HashMap to store the already-copied nodes.

My first attempt is DFS by making use of a ‘visited’ Set to mark which node I have copied and which is not. This is a nice idea and it solved the problem neatly.

But after reading this article, I realize that ‘visited’ is not needed for BFS solution!

The trick is, whenever I do a ‘HashMap.put(curNode, newNode)’, I push ‘curNode’ to queue. This very well replaces the functionality of the ‘visited’ set. It also guarantees that when I pop a new element from the queue, I CAN ALWAYS FIND ITS CORRESPONDING COPY from the HashMap - always there.

Code

First, my DFS code

public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
    if (node == null) return null;
    UndirectedGraphNode newNode = new UndirectedGraphNode(node.label);
    HashMap<Integer, UndirectedGraphNode> map = 
        new HashMap<Integer, UndirectedGraphNode>();
    map.put(node.label, newNode);
    copy(node, newNode, map, new HashSet<Integer>());
    return newNode;
}

private void copy(UndirectedGraphNode orin, UndirectedGraphNode cp,
        HashMap<Integer, UndirectedGraphNode> map,
        HashSet<Integer> visited) {
    if (visited.contains(orin.label))
        return;
    else
        visited.add(orin.label);
    for (UndirectedGraphNode n : orin.neighbors) {
        if (map.containsKey(n.label)) {
            cp.neighbors.add(map.get(n.label));
        } else {
            UndirectedGraphNode newNode = new UndirectedGraphNode(n.label);
            map.put(n.label, newNode);
            cp.neighbors.add(map.get(n.label));
        }
    }
    // do DFS recursively
    for (int i = 0; i < orin.neighbors.size(); i++) {
        copy(orin.neighbors.get(i), cp.neighbors.get(i), map, visited);
    }
}

Second, my BFS code without using ‘visited’ HashSet

public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
    if (node == null) return null;
    HashMap<UndirectedGraphNode, UndirectedGraphNode> map 
            = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
    map.put(node, new UndirectedGraphNode(node.label));
    LinkedList<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
    queue.add(node);
    // queue is guaranteed to always have non-traversed nodes
    while ( !queue.isEmpty() ) {
        UndirectedGraphNode orin = queue.remove();
        UndirectedGraphNode cp = map.get(orin);
        for (UndirectedGraphNode n : orin.neighbors) {
            if ( !map.containsKey(n) ) {
                map.put(n, new UndirectedGraphNode(n.label));
                queue.add(n);
            }
            UndirectedGraphNode newNode = map.get(n);
            cp.neighbors.add(newNode);
        }
    }
    return map.get(node);
}