[leetcod] 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/ \\_/


题解:
引用:http://www.cnblogs.com/springfor/p/3874591.html
这道题考察对图的遍历和利用HashMap拷贝的方法。
对图的遍历就是两个经典的方法DFS和BFS。BFS经常用Queue实现,DFS经常用递归实现(可改为栈实现)。
拷贝方法是用用HashMap,key存原始值,value存copy的值,用DFS,BFS方法遍历帮助拷贝neighbors的值。

先复习下DFS和BFS。

DFS(Dpeth-first Search)
顾名思义,就是深度搜索,一条路走到黑,再选新的路。
记得上Algorithm的时候,教授举得例子就是说,DFS很像好奇的小孩,你给这个小孩几个盒子套盒子,好奇的小孩肯定会一个盒子打开后继续再在这个盒子里面搜索。
等把这一套盒子都打开完,再打开第二套的。
Wikipedia上的讲解是:“Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures.
One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible
along each branch before backtracking.”
通常来说简便的DFS写法是用递归,如果非递归的话就是栈套迭代,思想是一样的。
递归写法的DFS伪代码如下:
Input: A graph G and a root v of G
1   procedure DFS(G,v):
2       label v as discovered
3       for all edges from v to w in G.adjacentEdges(v) do
4           if vertex w is not labeled as discovered then
5               recursively call DFS(G,w)
非递归写法的DFS伪代码如下:
Input: A graph G and a root v of G
复制代码
1   procedure DFS-iterative(G,v):
2       let S be a stack
3       S.push(v)
4       while S is not empty
5             v ← S.pop() 
6             if v is not labeled as discovered:
7                 label v as discovered
8                 for all edges from v to w in G.adjacentEdges(v) do
9                     S.push(w)
复制代码



BFS(Breadth-first Search)
这个就是相对于BFS的另外一种对图的遍历方法,对于一个节点来说先把所有neighbors都检查一遍,再从第一个neighbor开始,循环往复。
由于BFS的这个特质,BFS可以帮助寻找最短路径。
Wikipedia上面对BFS的定义是:
“In graph theory, breadth-first search (BFS) is a strategy for searching in a graphwhen search is limited to essentially two operations: (a) visit and inspect a node of a graph; (b) gain access to visit the nodes that neighbor the currently visited node. The BFS begins at a root node and inspects all the neighboring nodes. Then for each of those neighbor nodes in turn, it inspects their neighbor nodes which were unvisited, and so on. Compare BFS with the equivalent, but more memory-efficient
Iterative deepening depth-first search and contrast with depth-first search.”

通常BFS用queue+循环实现,伪代码如下:
Input: A graph G and a root v of G
复制代码
1   procedure BFS(G,v) is
 2       create a queue Q
 3       create a set V
 4       add v to V
 5       enqueue v onto Q
 6       while Q is not empty loop
 7          t ← Q.dequeue()
 8          if t is what we are looking for then
 9             return t
10         end if
11         for all edges e in G.adjacentEdges(t) loop
12            u ← G.adjacentVertex(t,e)
13            if u is not in V then
14                add u to V
15                enqueue u onto Q
16            end if
17         end loop
18      end loop
19      return none
20  end BFS
复制代码
********************************************************************************************************************************
下面就是这道题的3种解题方法。

第一种实现方法是BFS的,就是先将头节点入queue,每一次queue出列一个node,然后检查这个node的所有的neighbors,如果没visited过,就入队,并更新neighbor。
然后更新新的neighbor列表。
代码如下:
复制代码
1     public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
 2         if(node == null)
 3             return null;
 4             
 5         HashMap<UndirectedGraphNode, UndirectedGraphNode> hm = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
 6         LinkedList<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
 7         UndirectedGraphNode head = new UndirectedGraphNode(node.label);
 8         hm.put(node, head);
 9         queue.add(node);
10         
11         while(!queue.isEmpty()){
12             UndirectedGraphNode curnode = queue.poll();
13             for(UndirectedGraphNode aneighbor: curnode.neighbors){//check each neighbor
14                 if(!hm.containsKey(aneighbor)){//if not visited,then add to queue
15                     queue.add(aneighbor);
16                     UndirectedGraphNode newneighbor = new UndirectedGraphNode(aneighbor.label);
17                     hm.put(aneighbor, newneighbor);
18                 }
19                 
20                 hm.get(curnode).neighbors.add(hm.get(aneighbor));
21             }
22         }
23         
24         return head;
25     }
复制代码
DFS的递归操作如下,迭代复制neighbors:
复制代码
1     public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
 2         if(node == null)
 3             return null;
 4             
 5         HashMap<UndirectedGraphNode, UndirectedGraphNode> hm = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
 6         UndirectedGraphNode head = new UndirectedGraphNode(node.label);
 7         hm.put(node, head);
 8         
 9         DFS(hm, node);//DFS
10         return head;
11     }
12     public void DFS(HashMap<UndirectedGraphNode, UndirectedGraphNode> hm, UndirectedGraphNode node){
13         if(node == null)
14             return;
15             
16         for(UndirectedGraphNode aneighbor: node.neighbors){ 
17             if(!hm.containsKey(aneighbor)){
18                 UndirectedGraphNode newneighbor = new UndirectedGraphNode(aneighbor.label);
19                 hm.put(aneighbor, newneighbor);
20                 DFS(hm, aneighbor);//DFS
21             }
22             hm.get(node).neighbors.add(hm.get(aneighbor));
23         }
24     }
复制代码


下面一种方法是DFS的非递归方法,中点是把BFS中的queue换成stack,因为出列方法不一样了,所以遍历的线路就不一样了。代码如下:
复制代码
1     public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
 2         if(node == null)
 3             return null;
 4             
 5         HashMap<UndirectedGraphNode, UndirectedGraphNode> hm = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
 6         LinkedList<UndirectedGraphNode> stack = new LinkedList<UndirectedGraphNode>();
 7         UndirectedGraphNode head = new UndirectedGraphNode(node.label);
 8         hm.put(node, head);
 9         stack.push(node);
10         
11         while(!stack.isEmpty()){
12             UndirectedGraphNode curnode = stack.pop();
13             for(UndirectedGraphNode aneighbor: curnode.neighbors){//check each neighbor
14                 if(!hm.containsKey(aneighbor)){//if not visited,then push to stack
15                     stack.push(aneighbor);
16                     UndirectedGraphNode newneighbor = new UndirectedGraphNode(aneighbor.label);
17                     hm.put(aneighbor, newneighbor);
18                 }
19                 
20                 hm.get(curnode).neighbors.add(hm.get(aneighbor));
21             }
22         }
23         
24         return head;
25     }
复制代码

 

转载于:https://www.cnblogs.com/fengmangZoo/p/4192777.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/459770.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【iOS7一些总结】9、与列表显示(在):列表显示UITableView

列表显示&#xff0c;顾名思义它是在一个列表视图的形式显示在屏幕上的数据的内容。于ios在列表视图UITableView达到。这个类在实际应用中频繁&#xff0c;是很easy理解。这里将UITableView的主要使用方法总结一下以备查。UITableView定义在头文件UITableView.h中&#xff0c;详…

SpringMVC(一):环境搭建

2019独角兽企业重金招聘Python工程师标准>>> //TODO 转载于:https://my.oschina.net/u/1020238/blog/505272

DockPanel 类

DockPanel 类 .NET Framework 4.5其他版本此主题尚未评级 - 评价此主题定义您可水平或垂直排列子元素的区域&#xff0c;互相。 继承层次结构 System.Object System.Windows.Threading.DispatcherObjectSystem.Windows.DependencyObjectSystem.Windows.Media.VisualSystem.Wind…

【分布式计算】MapReduce的替代者-Parameter Server

原文&#xff1a;http://blog.csdn.net/buptgshengod/article/details/46819051 首先还是要声明一下&#xff0c;这个文章是我在入职阿里云1个月以来&#xff0c;对于分布式计算的一点肤浅的认识&#xff0c;可能有些地方不够妥善&#xff0c;还请看官可以指出不足的地方&#…

iOS开发多线程篇—线程安全

iOS开发多线程篇—线程安全 一、多线程的安全隐患 资源共享 1块资源可能会被多个线程共享&#xff0c;也就是多个线程可能会访问同一块资源 比如多个线程访问同一个对象、同一个变量、同一个文件 当多个线程访问同一块资源时&#xff0c;很容易引发数据错乱和数据安全问题 示例…

Android Ion 框架 文件下载

为什么80%的码农都做不了架构师&#xff1f;>>> ion是的一个安卓异步网络和图片加载库。 特性 异步下载&#xff1a;下载图片 (用ImageViews 或者 Bitmaps显示)&#xff1b;下载JSON文件 (通过Gson解析)&#xff1b;下载字符串&#xff1b;下载文件&#xff1b;Flu…

CSS基础----元素分类

2019独角兽企业重金招聘Python工程师标准>>> 块级元素特点&#xff1a; 1、每个块级元素都从新的一行开始&#xff0c;并且其后的元素也另起一行。 2、元素的高度、宽度、行高以及顶和底边距都可设置。 3、元素宽度在不设置的情况下&#xff0c;是它本身父容器的100…

Linux 高可用(HA)集群之keepalived

一、keepalived介绍1、Keepalived 定义Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案&#xff0c;可以利用其来避免单点故障。一个LVS服务会有2台服务器运行Keepalived&#xff0c;一台为主服务器&#xff08;MASTER&#xff09;&#xff0c;一台为备份服务器&#…

java 将要死亡_人在即将死亡的时候,能意识到自己将要死亡了吗?科学家给出答案...

引言&#xff1a;自然界内大多数生物都敬畏生命&#xff0c;也害怕死亡。毕竟&#xff0c;死亡意味着结束与告别&#xff0c;将死之人多会留恋世间的人或事。那么&#xff0c;在临死之前&#xff0c;人会意识到自己将走向死亡吗&#xff1f;人类对长生不老的追求古已有之&#…

C#开发微信门户及应用(24)-微信小店货架信息管理

C#开发微信门户及应用(24)-微信小店货架信息管理 原文:C#开发微信门户及应用(24)-微信小店货架信息管理在前面微信小店系列篇《C#开发微信门户及应用(22)-微信小店的开发和使用》里面介绍了一些微信小店的基础知识&#xff0c;以及《C#开发微信门户及应用(23)-微信小店商品管理…

【百度地图API】——如何用label制作简易的房产标签

【百度地图API】——如何用label制作简易的房产标签 原文:【百度地图API】——如何用label制作简易的房产标签摘要&#xff1a; 最近&#xff0c;API爱好者们纷纷说&#xff0c;自定义marker太复杂了&#xff01;不仅定义复杂&#xff0c;连所有的dom事件都要自己重新定义。有没…

【svn】设置过滤文件

2019独角兽企业重金招聘Python工程师标准>>> .classpath target .project .settings *.o *.lo *.la *.al .libs *.so *.so.[0-9]* *.a *.pyc *.pyo *.rej *~ #*# .#* .*.swp .DS_Store 转载于:https://my.oschina.net/sourcecoding/blog/509169

php开启错误日志,PHP开启error_log 错误日志

对于一些访问没有明显错误提示的PHP页面&#xff0c;我们可以通过error_log来做进一步的判定。但出于种种原因&#xff0c;有些服务器并没有开启PHP的error_log功能。可以暂时开启一下错误日志&#xff1a;编辑php.ini&#xff0c;将log_errors设置为on&#xff1a;log_errors …

unity3d-小案例之角色简单漫游

准备资源 我这里从网上下载一个角色模型&#xff0c;里面有一组动画。有站立、奔跑、杀怪等 我们来实现角色的前后左后移动&#xff0c;即键盘上的WSDA键&#xff0c;这里因为没有行走的动画。索性就用奔跑代替了&#xff01;&#xff01; 暂时先不计较代码冗余的情况。因为我也…

PopupWindow在项目中的使用 并指定位置及加入动画效果

2019独角兽企业重金招聘Python工程师标准>>> 项目做到一期收尾阶段&#xff0c;突然要用到弹出窗口&#xff0c;于是自然而然的就想起了PopupWindow这个组件了&#xff0c;才发现平时用的少&#xff0c;对这个组件几乎是完全无知的状态啊。 于是恶补一番 现在放出学…

php 图片无法删除,php如何删除上传的图片

php删除上传的图片的方法&#xff1a;首先检查上传文件是否在允许上传的类型&#xff1b;然后获取图片的完整路径&#xff1b;最后通过“unlink(“uppic/”.$img);”方法删除图片即可。简单的PHP上传图片和删除图片示例代码分享一例简单的PHP上传图片和删除图片示例代码&#x…

贝克汉姆-囚

转载于:https://www.cnblogs.com/andyxl/p/4215954.html

Androida规划nt打包

1.准备工作 &#xff08;1&#xff09;首先安装好ant工具 &#xff08;2&#xff09;生成keystore 在jdk的bin文件夹下 输入keytool -genkey -alias android.keystore -keyalg RSA -validity 20000 -keystore android.keystore 按操作输入就可以&#xff0c;记住password。 &am…

[树结构]平衡二叉树AVL

平衡二叉树是一种二叉排序树&#xff0c;其中每一个节点的左子树和右子树的高度至多等于1&#xff0c;平衡二叉树又称为AVL树。 将二叉树节点的左子树深度减去右子树深度的值称为平衡因子BF&#xff0c;平衡二叉树上所有节点的平衡因子只可能是-1,0或者1。 距离插入点最近的&am…

发布《Linux工具快速教程》

发布《Linux工具快速教程》 阶段性的完成了这本书开源书籍&#xff0c;发布出来给有需要的朋友&#xff0c;同时也欢迎更多的朋友加入进来&#xff0c;完善这本书&#xff1b; 本书Github地址&#xff1a;https://github.com/me115/linuxtools_rst 在线阅读 缘起 Linux下有很多…