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

Ref: http://www.cnblogs.com/feiling/p/3351921.html

解题思路]

图的遍历有两种方式,BFS和DFS

这里使用BFS来解本题,BFS需要使用queue来保存neighbors

但这里有个问题,在clone一个节点时我们需要clone它的neighbors,而邻居节点有的已经存在,有的未存在,如何进行区分?

这里我们使用Map来进行区分,Map的key值为原来的node,value为新clone的node,当发现一个node未在map中时说明这个node还未被clone,

将它clone后放入queue中处理neighbors。

使用Map的主要意义在于充当BFS中Visited数组,它也可以去环问题,例如A--B有条边,当处理完A的邻居node,然后处理B节点邻居node时发现A已经处理过了

处理就结束,不会出现死循环!

queue中放置的节点都是未处理neighbors的节点!!!!

/**
 * Definition for undirected graph.
 * class UndirectedGraphNode {
 *     int label;
 *     ArrayList<UndirectedGraphNode> neighbors;
 *     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
 * };
 */
public class Solution {
    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        if(node == null){
            return node;
        }
        UndirectedGraphNode result = new UndirectedGraphNode(node.label);
        LinkedList<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
        queue.add(node);
        Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
        map.put(node, result);
        
        while(!queue.isEmpty()){
            UndirectedGraphNode nodeInQueue = queue.poll();
            ArrayList<UndirectedGraphNode> neighbors = nodeInQueue.neighbors;
            for(int i = 0; i < neighbors.size(); i++){
                UndirectedGraphNode originN = neighbors.get(i);
                if(map.containsKey(originN)){
                    // originN has been cloned, only need to add to neightbors
                    //map.get(nodeInQueue) return current processing node || originN is the current node's neighbor. map.get(originN) return the neighbout's clone
                    map.get(nodeInQueue).neighbors.add(map.get(originN));
                } else {
                    UndirectedGraphNode cloneN = new UndirectedGraphNode(originN.label);
                    map.get(nodeInQueue).neighbors.add(cloneN);
                    map.put(originN, cloneN);
                    // the nodes in queue are the nodes that haven't been cloned 
                    queue.add(originN);
                }
            }
            
        }
        return result;
    }
}



原文地址:https://www.cnblogs.com/RazerLu/p/3553567.html