博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode][JavaScript]Clone Graph
阅读量:5159 次
发布时间:2019-06-13

本文共 3643 字,大约阅读时间需要 12 分钟。

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         / \         \_/

 

节点是唯一的,就是要注意像例子中这样从2到2的环,有个test case是{0,0,0} DFS递归解法:
1 /** 2  * Definition for undirected graph. 3  * function UndirectedGraphNode(label) { 4  *     this.label = label; 5  *     this.neighbors = [];   // Array of UndirectedGraphNode 6  * } 7  */ 8 /** 9  * @param {UndirectedGraphNode} graph10  * @return {UndirectedGraphNode}11  */12 var cloneGraph = function(graph) {13     var visited = {};14     if(graph === null){15         return null;16     }else{17         return dfs(graph);18     }19 20     function dfs(node){21         var newNode = null;22         if(visited[node.label]){23             newNode = visited[node.label];24         }else{25             newNode = new UndirectedGraphNode(node.label);26             visited[node.label] = newNode;27         }28         for(var i = 0; i < node.neighbors.length; i++){29             if(node.neighbors[i].label !== node.label){30                 if(!visited[node.neighbors[i].label]){31                     newNode.neighbors.push(dfs(node.neighbors[i]));32                 }else{33                     newNode.neighbors.push(visited[node.neighbors[i].label]);34                 }35             }else{36                 newNode.neighbors.push(visited[node.label]);37             }38         }39         return newNode; 40     }   41 };

 

BFS非递归解法:

1 /** 2  * Definition for undirected graph. 3  * function UndirectedGraphNode(label) { 4  *     this.label = label; 5  *     this.neighbors = [];   // Array of UndirectedGraphNode 6  * } 7  */ 8  9 /**10  * @param {UndirectedGraphNode} graph11  * @return {UndirectedGraphNode}12  */13 var cloneGraph = function(graph) {14     var visited = {};15     var queue = [];16     if(graph === null){17         return null;18     }else{19         visited[graph.label] = new UndirectedGraphNode(graph.label);20         queue.push(graph);21         bfs();22         return visited[graph.label];23     }24 25     function bfs(){26         while(queue.length > 0){27             var node = queue.shift();28             var newNode = visited[node.label];29             for(var i = 0; i < node.neighbors.length; i++){30                 if(node.neighbors[i].label !== node.label){31                     var neighbor = node.neighbors[i];32                     var newNeighbor = null;33                     if(!visited[neighbor.label]){34                         newNeighbor = new UndirectedGraphNode(neighbor.label);35                         visited[neighbor.label] = newNeighbor;36                         queue.push(neighbor);37                     }else{38                         newNeighbor = visited[neighbor.label];39                     }40                     newNode.neighbors.push(newNeighbor);41                 }else{42                     newNode.neighbors.push(visited[node.label]);43                 }44             }45         }46     } 47 };

 

转载于:https://www.cnblogs.com/Liok3187/p/4516929.html

你可能感兴趣的文章
基于springCloud的分布式架构体系
查看>>
客服浮动效果实现
查看>>
常用ContentType对照表
查看>>
nginx部署注意应该将本来的conf和conf.default里面添加 include conf.d/*.conf;
查看>>
合并两个数组 - concat()
查看>>
[译]14种参与开源项目的方法,不需要你成为一名编程天才或者摇滚明星
查看>>
jieba gensim 相似度实现
查看>>
Redis PHP连接操作
查看>>
执法文书打印的实现(三)(word→png的实现)
查看>>
Looper.loop() android线程中的消息循环
查看>>
Dalvik虚拟机JNI方法的注册过程分析
查看>>
java中文乱码解决之道(四)—–java编码转换过程
查看>>
LeetCode - N-Queens II
查看>>
xxx征集系统项目目标文档
查看>>
djangoweb应用_mysql_cmd中文乱码问题
查看>>
javascript 购物车
查看>>
个人工作总结1
查看>>
Mysql之库、表、记录相关操作3
查看>>
json字符串转对象的时候,时间格式报错“不是有效的 AllXsd 值。”
查看>>
Java内存管理的小技巧
查看>>