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 #.

First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
Second node is labeled as 1. Connect node 1 to node 2.
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
         / \
         \_/

Solution 1:

使用BFS来解决此问题。用一个Queue来记录遍历的节点,遍历原图,并且把复制过的节点与原节点放在MAP中防止重复访问。

图的遍历有两种方式,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的节点。

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

 /*
Iteration Solution:
*/
public UndirectedGraphNode cloneGraph1(UndirectedGraphNode node) {
if (node == null) {
return null;
} UndirectedGraphNode root = null; // store the nodes which are cloned.
HashMap<UndirectedGraphNode, UndirectedGraphNode> map =
new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); Queue<UndirectedGraphNode> q = new LinkedList<UndirectedGraphNode>(); q.offer(node);
UndirectedGraphNode rootCopy = new UndirectedGraphNode(node.label); // 别忘记这一行啊。orz..
map.put(node, rootCopy); // BFS the graph.
while (!q.isEmpty()) {
UndirectedGraphNode cur = q.poll();
UndirectedGraphNode curCopy = map.get(cur); // bfs all the childern node.
for (UndirectedGraphNode child: cur.neighbors) {
// the node has already been copied. Just connect it and don't need to copy.
if (map.containsKey(child)) {
curCopy.neighbors.add(map.get(child));
continue;
} // put all the children into the queue.
q.offer(child); // create a new child and add it to the parent.
UndirectedGraphNode childCopy = new UndirectedGraphNode(child.label);
curCopy.neighbors.add(childCopy); // Link the new node to the old map.
map.put(child, childCopy);
}
} return rootCopy;
}

2014.12.30 Redo:

 /*
SOLUTION 3: The improved Version.
*/
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null) {
return null;
} HashMap<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); // BUG 1: can't use queue , should use LinkedList.
Queue<UndirectedGraphNode> q = new LinkedList<UndirectedGraphNode>(); q.offer(node); // copy the root node. and then put it into the map.
UndirectedGraphNode nodeCopy = new UndirectedGraphNode(node.label);
map.put(node, nodeCopy); while (!q.isEmpty()) {
UndirectedGraphNode cur = q.poll(); // get out the copy node. We guarantee that it has been copied. Because we always put it into the map before
// put it into the queue.
UndirectedGraphNode curCopy = map.get(cur); // go through all the children node.
// Line 71: java.util.ConcurrentModificationException. use cur instead of curCopy
for (UndirectedGraphNode child: cur.neighbors) { if (map.containsKey(child)) {
curCopy.neighbors.add(map.get(child));
} else {
// Only add the child into the map when it is not visited.
q.offer(child); // BUG 3: forget to add the new node into the map.
UndirectedGraphNode childCopy = new UndirectedGraphNode(child.label);
curCopy.neighbors.add(childCopy);
map.put(child, childCopy);
}
}
} return map.get(node);
}

Solution 2:

同样的,我们也可以使用递归DFS来解决此题,思路与上图一致,但为了避免重复运算产生死循环。当进入DFS时,如果发现map中已经有了拷贝过的值,直接退出即可。

题目虽然简单,但主页君仍然考虑了递归的特性使程序简洁。比如:我们拷贝只拷贝根节点,而子节点的拷贝由recursion来完成,这样可以使程序更加简洁。

注意:要先加入到map,再调用rec ,否则会造成不断地反复拷贝而死循环。

 /*
Solution 2: Recursion version.
*/
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null) {
return null;
} return rec(node, new HashMap<UndirectedGraphNode, UndirectedGraphNode>());
} public UndirectedGraphNode rec(UndirectedGraphNode root, HashMap<UndirectedGraphNode, UndirectedGraphNode> map) {
// If it has been copied, just return the copy node from the map.
UndirectedGraphNode rootCopy = map.get(root);
if (rootCopy != null) {
return rootCopy;
} // if the root is not copied, create a new one.
rootCopy = new UndirectedGraphNode(root.label);
map.put(root, rootCopy); // copy all the child node.
for (UndirectedGraphNode child: root.neighbors) {
// call the recursion to create all the children and add the new children to the copy node.
rootCopy.neighbors.add(rec(child, map));
} return rootCopy;
}

2014.12.30 Redo:

 public UndirectedGraphNode cloneGraph1(UndirectedGraphNode node) {
if (node == null) {
return null;
} return rec(node, new HashMap<UndirectedGraphNode, UndirectedGraphNode>());
} // SOLUTION 1:
// Try to return a copied cloneGraph.
public UndirectedGraphNode rec(UndirectedGraphNode node, HashMap<UndirectedGraphNode, UndirectedGraphNode> map) {
// The base case:
if (map.containsKey(node)) {
// If the map has been copied, just return the node.
return map.get(node);
} // create a new node.
UndirectedGraphNode nodeCopy = new UndirectedGraphNode(node.label);
// BUG 2: should put it into the map first. Because we don't want to copy the same node again in the recursion. map.put(node, nodeCopy);
for (int i = 0; i < node.neighbors.size(); i++) {
// BUG 1: forget a parameter.
// copy all the children node.
nodeCopy.neighbors.add(rec(node.neighbors.get(i), map));
} return nodeCopy;
}

Ref: http://m.blog.csdn.net/blog/hellobinfeng/17497883

Code:

CloneGraph.java

LeetCode: Clone Graph 解题报告的更多相关文章

  1. 【LeetCode】133. Clone Graph 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  2. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  3. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  4. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  5. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  6. 【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...

  7. [LeetCode] Clone Graph 无向图的复制

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

  8. [LeetCode] Clone Graph 克隆无向图

    Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph ...

  9. [leetcode]Clone Graph @ Python

    原题地址:https://oj.leetcode.com/problems/clone-graph/ 题意:实现对一个图的深拷贝. 解题思路:由于遍历一个图有两种方式:bfs和dfs.所以深拷贝一个图 ...

随机推荐

  1. sqlite 附加和分离数据库

    附加数据库 ATTACH DATABASE 'testDB.db' as 'TEST'; 分离数据库 DETACH DATABASE 'Test';

  2. xml DTD中的ELEMENT和ATTLIST

    是W3C的一个文档类型定义规则文件,是用来让浏览器根据你定义的DTD(文档类型定义)来解释页面代码的. doctype声明指出阅读程序应该用什么规则集来解释文档中的标记.在Web文档的情况下,“阅读程 ...

  3. 安装到LG手机出错

    [2013-07-10 07:44:31 - txrjsms] ERROR: Application requires API version 11. Device API version is 8 ...

  4. Java中网络相关API的应用——InetAddress&URL

    一.InetAddress类 标识网络上的硬件资源 package com.homework; import java.net.InetAddress; import java.net.Unknown ...

  5. 理解ROC和AUC

    分类器各种各样,如何评价这些分类器的性能呢?(这里只考虑二元分类器,分类器的输出为概率值) 方法一:概率定义法 从正样本中随机选取元素记为x,从负样本中随机选取元素记为y,x的置信度大于y的概率 计算 ...

  6. 可以尝试用Google Font API来摆脱网页字体的单调 仅仅抛砖引玉

    http://www.nowamagic.net/librarys/veda/detail/2513

  7. eclipse 垃圾回收器,内存释放

    http://zhangrong-0825-163-com.iteye.com/blog/7334071.如何在eclipse里修改web工程的访问路径,步骤如下: 点击web工程——>选择pr ...

  8. 在js或css后加?v= 版本号不让浏览器缓存

    客户端会缓存css或js文件,改变版本号,客户端浏览器就会重新下载新的js或css文件,在js或css后加?v= 版本号的用法如下 代码如下: <span style="font-si ...

  9. MVC公司架构介绍-事件机制

  10. STL之内存处理

    说明:本文仅供学习交流,转载请标明出处,欢迎转载! STL中与内存配置相关的类是allocator类,头文件为:#include<alllocator>这是一个模板类,用于内存的分配.对象 ...