Clone Graph
来源:互联网 发布:有了阿里云ems如何建站 编辑:程序博客网 时间:2024/06/06 00:01
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 / \ \_/
解题思路:这个题目其实就是无向图的拷贝,关于图我们知道有两种遍历方法,分别为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; }}
代码是不是和遍历的代码差不多?通过这个题目我发现其实很多的题目都是建立在基础知识上的,有的甚至是基础的变形。掌握好基础,多刷刷题目,多做做项目,相信会成长不少。
- Clone Graph
- Clone Graph
- Clone Graph
- Clone Graph
- Clone Graph
- Clone Graph
- Clone Graph
- Clone Graph
- Clone Graph
- Clone Graph
- Clone Graph
- Clone Graph
- Clone Graph
- Clone Graph
- Clone Graph
- Clone Graph
- Clone Graph
- Clone Graph
- 把解压缩版的tomcat6注册成服务并设置自启动
- 剑指Offer 链表相关问题--从尾到头打印链表、合并两个排序的链表
- 记录每天学习机器学习的经历,对照机器学习实战这本书
- 包导入基础
- Makefile 中:= ?= += =的区别
- Clone Graph
- 牛逼的大学教育果然非常受用
- iOS开发之图片查看器
- 南昌大学软件学院大神养成计划脚印-11.16
- Java用ArrayList实现栈
- delegate为什么用assign声明
- 网络编程最佳实践
- 61. 文件系统
- Unity中用到的C#补充(七)- 委托与事件