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. openerp 7.0 来自外部的邮件会发送二次问题解决方法

    插入代码:\addons\mail\mail_mail.py #309 line this = self.pool.get('res.users').browse(cr, uid, uid, cont ...

  2. MVC时间对比及时间范围判断

    方法一:使用DateTime.Compare 方法 public static int Compare( DateTime t1, DateTime t2 ) t1 早于 t2:小于零t1 与 t2  ...

  3. IntelliJ IDEA 2017.2.2 的破解 有效期 2116年

      破解三部曲 下载破解文件 JetbrainsCrack-2.6.6-release-enc.jar http://idea.lanyus.com/jar/JetbrainsCrack-2.6.6- ...

  4. java Socket Tcp 浏览器和服务器(一)

    自定义服务端,使用已有的客户端IE,了解一下客户端给服务端发了什么请求? 发送的请求是: GET / HTTP/1.1  请求行  请求方式  /myweb/1.html  请求的资源路径   htt ...

  5. HTTP 304状态分析

       在看http log日志发现很多304状态的标识 HTTP 304: Not Modified   ###请求后读取本地缓存的文件标准解释是:Not Modified 客户端有缓冲的文档并发出了 ...

  6. 线代: N阶行列式

    线性变换 将 (x, y) 变成 (2 x + y, x - 3 y) 就叫做线性变换, 这就是矩阵乘法, 用于表示一切线性变换. 几何上看, 把平面上的每个点 (x, y) 都变到 (2 x + y ...

  7. Xcode编译 No such file or directory

    No such file or directory 差点儿相同算是Xcode比較常见的一个编译错误了.原因往往是加入或删除美术资源的时候出错.尽管是小问题,但出现的频率非常高. 解决方法(能够依次尝试 ...

  8. struts2 中请求转发与请求重定向方法

    本文转自:http://blog.csdn.net/a327736051/article/details/50240491 一.Chain Result:这个result调用另外的一个action,连 ...

  9. 【LeetCode】128. Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  10. OOAD和UML

    ooad: object oriented analysis designer 又有两个分支: ooa(object oriented analysis):what to do ood(object  ...