2015年5月17日星期日

Clone Graph leetcode

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
         / \
         \_/
先将头节点入queue,建立一个hashmap放入节点和节点的复制.每一次queue出列一个node,然后检查这个node的所有的neighbors,如果nieghbor1不存在于hashmap 就把neighbor1 和他的复制入map 然后把map里面node的复制的neighbors集合里加入neighbor1的复制
时间是O(n) 空间小于O(n)


//BFS
public class Solution {
    
    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
        if (node == null) {
            return null;
        }
        Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
        HashMap<UndirectedGraphNode, UndirectedGraphNode> map= new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
        UndirectedGraphNode newnode = new UndirectedGraphNode(node.label);
        map.put(node, newnode);
        queue.offer(node);
        while (!queue.isEmpty()) {
            UndirectedGraphNode cur = queue.poll();
            for (int i = 0; i < cur.neighbors.size(); i++) {
                if (!map.containsKey(cur.neighbors.get(i))) {
                    queue.offer(cur.neighbors.get(i));
                    UndirectedGraphNode tem = new UndirectedGraphNode(cur.neighbors.get(i).label);
                    map.put(cur.neighbors.get(i), tem);
                }
                map.get(cur).neighbors.add(map.get(cur.neighbors.get(i)));
            }
        }
        return map.get(node);
    }
}
//DFS:
public class Solution {
    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
        if (node == null) {
            return null;
        }
        HashMap<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
        UndirectedGraphNode newnode = new UndirectedGraphNode(node.label);
        map.put(node, newnode);
        dfs(map, node);
        return map.get(node);
    }
    public void dfs(HashMap<UndirectedGraphNode, UndirectedGraphNode> map, UndirectedGraphNode node) {
        if (node == null) {
            return;
        }
        for (UndirectedGraphNode s : node.neighbors) {
            if (!map.containsKey(s)) {
                UndirectedGraphNode tem = new UndirectedGraphNode(s.label);
                map.put(s, tem);
                dfs(map, s);
            }
            map.get(node).neighbors.add(map.get(s));
        }
    }
}

没有评论:

发表评论