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. webservice系统学习笔记7-异常处理

    接口类:IMyService.java @WebResult(name="testExceptionResult") public void testException() thr ...

  2. 生成CFree 5.0 注册码

    C-Free 5.0真是一个很棒的编程软件,可今天用着用着却让我注册,下面的注册码分享给大家: 方法一.下载CFree 5.0之后,直接输入以下信息进行注册. 用户名:tianfang电子邮件:qua ...

  3. 如何监控tomcat性能

    如何监控tomcat性能:[1]工具一 | 浏览:155 | 更新:2014-12-13 10:06 1 2 3 4 5 6 分步阅读 tomcat经常被用作中间件,也有直接作WEB的,自带的工具不是 ...

  4. 设置/修改centos上的swap交换分区的方法

    设置centos上的swap交换分区的方法 作为linux世界里最稳定的服务器版本,rhas5一直有很大的应用面,之前一直关注的是freebsd,因为应用的需要,特别在配合mysql和oracle上r ...

  5. HDUOJ-------2149Public Sale

    Public Sale Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. C#搞工控的一些代码

    首先工控项目都会用到: using System.Runtime.InteropServices; 1.字节转化为单精度浮点数 2.单精度转成字节 3.使用结构体 4.使用动态链接库 5.ASCCII ...

  7. iOS - OC 与 C 互相操作

    前言 在 Objective-C 语言中,我们可以使用 C.Swift 语言编写代码,我们可以导入任意用 C.Swift 写的 Cocoa 平台框架.C 框架或 Swift 类库. 1.在 C 中使用 ...

  8. shutdown 与 close 函数 的区别

    假设server和client 已经建立了连接,server调用了close, 发送FIN 段给client(其实不一定会发送FIN段,后面再说),此时server不能再通过socket发送和接收数据 ...

  9. linux内存碎片防治技术

    Linux kernel组织管理物理内存的方式是buddy system(伙伴系统),而物理内存碎片正式buddy system的弱点之一,为了预防以及解决碎片问题,kernel采取了一些实用技术,这 ...

  10. Unix环境高级编程(九)信号

    信号时软中断,很多比较重要的应用程序都需要处理信号,信号提供了一种处理异步事件的方法.每个信号都一个名字,以SIG开头,在<signal.h>头文件中定义信号类型,都是正整数(信号编号). ...