Clone Graph
2015-11-16 22:58
190 查看
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:
解题思路:这个题目其实就是无向图的拷贝,关于图我们知道有两种遍历方法,分别为DFS(深度优先)和BFS(宽度优先)。两者没有优劣之说,通常DFS使用栈或者递归来处理,而BFS通常使用队列来解决问题。因为之前做的和图相关的题目大部分都是用的BFS,所以这里我们就看看BFS遍历的实现吧(java代码):
其实拷贝的过程就是每个节点都遍历一遍,然后将每个节点拷贝一遍。代码如下(java):
代码是不是和遍历的代码差不多?通过这个题目我发现其实很多的题目都是建立在基础知识上的,有的甚至是基础的变形。掌握好基础,多刷刷题目,多做做项目,相信会成长不少。
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 / \ \_/
解题思路:这个题目其实就是无向图的拷贝,关于图我们知道有两种遍历方法,分别为DFS(深度优先)和BFS(宽度优先)。两者没有优劣之说,通常DFS使用栈或者递归来处理,而BFS通常使用队列来解决问题。因为之前做的和图相关的题目大部分都是用的BFS,所以这里我们就看看BFS遍历的实现吧(java代码):
//定义一个类用于表示图中的节点 /* publice class UndirectedGraphNode{ int label; ArrayList<UndirectedGraphNode> neighbors; UndirectedGraphNode(int x){ label = x; neighbors = new ArrayList<UndirectedGraphNode> (); } } */ private static void bfs(UndirectedGraphNode node){ if(node == null) return; //声明一个链表(相当于队列)来存储下一个遍历的节点 LinkedList<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode> (); //使用HashMap来存储已经遍历过的节点(Interger表示此时的节点离顶点节点的距离) HashMap<UndirectedGraphNode,Interger> map = new HashMap<UndirectedGraphNode> (); //将顶点节点入队 queue.add(node); map.put(node,0); int order = 0; //当队列不为空的时候循环遍历 while(! queue.isEmpty()){ //弹出第一个节点 UndirectedGraphNode curr = queue.pop(); //邻居节点 ArrayList<UndirectedGraphNode> currNeighbors = curr.neighbors; order ++; //遍历打印出节点 System.out.printIn("The"+order+"th element"+curr.label+" Distance from the top element is:"+map.get(curr)); int distance = map.get(curr) + 1; //遍历邻居节点 for(UndirectedGraphNode neighbor : currNeighbors){ //如果该邻居节点还没遍历 if(! map.containsKey(neighbor)){ //将遍历的邻居节点加入到map中 map.put(neighbor,distance); //将节点加入队列中,下一次再遍历该节点的邻居节点 queue.add(neighbor); } } } }
其实拷贝的过程就是每个节点都遍历一遍,然后将每个节点拷贝一遍。代码如下(java):
public class Solution { public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if(node == null) return null; LinkedList<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>(); HashMap<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode,UndirectedGraphNode>(); UndirectedGraphNode newHead = new UndirectedGraphNode(node.label); queue.add(node); map.put(node, newHead); while(!queue.isEmpty()){ UndirectedGraphNode curr = queue.pop(); ArrayList<UndirectedGraphNode> currNeighbors = curr.neighbors; for(UndirectedGraphNode aNeighbor: currNeighbors){ if(!map.containsKey(aNeighbor)){ UndirectedGraphNode copy = new UndirectedGraphNode(aNeighbor.label); map.put(aNeighbor,copy); map.get(curr).neighbors.add(copy); queue.add(aNeighbor); }else{ map.get(curr).neighbors.add(map.get(aNeighbor)); } } } return newHead; } }
代码是不是和遍历的代码差不多?通过这个题目我发现其实很多的题目都是建立在基础知识上的,有的甚至是基础的变形。掌握好基础,多刷刷题目,多做做项目,相信会成长不少。
相关文章推荐
- mysql安装过程记录和后续学习
- DSS中的RTP打包传输h264时获取h264的sps和pps信息
- 内存泄露检测之使用CRT的Debug技术
- 信息安全系统设计基础实验二:固件设计
- Array-Remove Duplicates from Sorted Array
- asp.net字符串的数学表达式计算结果
- android 之 下拉刷新PullToRefresh
- 前端【学习心得】HTML基础 | 第一周【前端工程师养成计划】DW开发工具
- AngularJs学习笔记[01]——数据绑定
- Java基础学习总结——Java对象的序列化和反序列化
- UML整体认知
- Memcached详解
- 泛型中? super T和? extends T的区别
- web版canvas做飞机大战游戏 总结
- KBuild MakeFile 添加外部头文件
- 剑指Offer 链表相关问题--从尾到头打印链表、合并两个排序的链表
- 网络编程最佳实践
- delegate为什么用assign声明
- Java用ArrayList实现栈
- 南昌大学软件学院大神养成计划脚印-11.16