[leetcode] Clone Graph

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
         / \
         \_/

 

https://oj.leetcode.com/problems/clone-graph/

 

思路:dfs来复制,用一个hashmap标记已经处理过的节点防止无限循环。

 

public class Solution {
    private HashMap<Integer, UndirectedGraphNode> map = new HashMap<Integer, UndirectedGraphNode>();

    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
        map.clear();
        return clone(node);
    }

    private UndirectedGraphNode clone(UndirectedGraphNode node) {
        if (node == null)
            return null;
        if (map.containsKey(node.label))
            return map.get(node.label);

        UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
        map.put(node.label, copy);

        for (UndirectedGraphNode each : node.neighbors) {
            copy.neighbors.add(clone(each));
        }

        return copy;

    }

    public static void main(String[] args) {
        UndirectedGraphNode zero = new UndirectedGraphNode(0);
        UndirectedGraphNode one = new UndirectedGraphNode(1);
        UndirectedGraphNode two = new UndirectedGraphNode(2);
        zero.neighbors.add(one);
        zero.neighbors.add(two);
        one.neighbors.add(zero);
        one.neighbors.add(two);
        two.neighbors.add(zero);
        two.neighbors.add(one);
        two.neighbors.add(two);

        new Solution().cloneGraph(zero);
    }

}

 

第二遍记录:注意map的运用来终止无限循环,递归复制节点执行。

/**
 * Definition for undirected graph.
 * class UndirectedGraphNode {
 *     int label;
 *     List<UndirectedGraphNode> neighbors;
 *     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
 * };
 */
public class Solution {
    Map<Integer,UndirectedGraphNode> map = new HashMap<Integer,UndirectedGraphNode>();
    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
        if(node == null)
            return null;
        if(map.containsKey(node.label)){
            return map.get(node.label);
        }
        
        UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
        map.put(copy.label,copy);
        
        for(UndirectedGraphNode each:node.neighbors){
            copy.neighbors.add(cloneGraph(each));
        }
        return copy;   
    }
}

 

 

参考:

http://www.cnblogs.com/lautsie/p/3356392.html

http://blog.csdn.net/kenden23/article/details/18624191

http://www.programcreek.com/2012/12/leetcode-clone-graph-java/

 

 

 

posted @ 2014-07-06 23:18  jdflyfly  阅读(149)  评论(0编辑  收藏  举报