Graph 133. Clone Graph in three ways(bfs, dfs, bfs(recursive))
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
/ \
\_/
Basically just clone the graph like clone a list in leetcode 138.
there are three ways t solve this (just traverse the graph and put new node into map)
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution {
//dfs
Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if(node==null) return null;
//copy graph(deep copy), hashmap
map.put(node, new UndirectedGraphNode(node.label));
helper(node);
return map.get(node);
}
void helper(UndirectedGraphNode node){
for(int i = 0; i< node.neighbors.size(); i++){
UndirectedGraphNode neighbor = node.neighbors.get(i);
if(!map.containsKey(neighbor)){// not visited
UndirectedGraphNode newNode = new UndirectedGraphNode(neighbor.label);
map.put(neighbor, newNode);//visited
helper(neighbor);//why put helper here: where put stack where to recursive(update 1)
}
map.get(node).neighbors.add(map.get(neighbor)); //set the link of neighbors
}
}
}
Solution 2: bfs queue
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution {
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if(node==null) return null;
//bfs
LinkedList<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
queue.offer(node);
Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>(); UndirectedGraphNode newNode = new UndirectedGraphNode(node.label);
map.put(node,newNode);
while(!queue.isEmpty()){
UndirectedGraphNode cur = queue.poll();//pop
for(int i = 0; i<cur.neighbors.size(); i++){
UndirectedGraphNode neighbor = cur.neighbors.get(i);
if(!map.containsKey(neighbor)){
queue.offer(neighbor);
newNode = new UndirectedGraphNode(neighbor.label);
map.put(neighbor, newNode);
map.get(cur).neighbors.add(newNode);
}
//if contains the key
else map.get(cur).neighbors.add(map.get(neighbor));
}
}
return map.get(node);
}
}
Solution 3: dfs with all node connected.
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution {
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if(node == null) return null;
//dfs, if not visited, visited it and set it to visited, stack
LinkedList<UndirectedGraphNode> stack = new LinkedList<>();//add first
stack.push(node);
Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<>();
UndirectedGraphNode newNode = new UndirectedGraphNode(node.label);
map.put(node, newNode);
while(!stack.isEmpty()){
UndirectedGraphNode cur = stack.pop();//pop
for(int i = 0; i<cur.neighbors.size(); i++){
UndirectedGraphNode neighbor = cur.neighbors.get(i);//neighbor of current
if(!map.containsKey(neighbor)){//put neighbor into hashmap (visited)
newNode = new UndirectedGraphNode(neighbor.label);//copy neighbors
map.put(neighbor, newNode);
stack.push(neighbor);
}
//set the link of neighbors
map.get(cur).neighbors.add(map.get(neighbor)); } }
return map.get(node);
}
}
// relationship in hashmap
// key, value
// cur, map.get(cur)
// cur.neighbors, newNode/ map.get(eighbor)
What if nodes are not connnected partly: just write a loop to chekc all the node(call dfs for each node) in the graph
https://www.geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph/
How do you represent the graph(one way from leetcode, another from geekforgeek)
Lastly: think about the time complexity of them
Graph 133. Clone Graph in three ways(bfs, dfs, bfs(recursive))的更多相关文章
- 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表
133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...
- 【LeetCode】133. Clone Graph (3 solutions)
Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...
- 133. Clone Graph (3 solutions)——无向无环图复制
Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...
- [LeetCode] 133. Clone Graph 克隆无向图
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 【LeetCode】133. Clone Graph 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- leetcode 133. Clone Graph ----- java
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 133. Clone Graph
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- Java for LeetCode 133 Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 133. Clone Graph(图的复制)
Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph contains ...
随机推荐
- 关于echart没有数据显示暂无数据
对于echart当没有数据的时候怎么显示, 首先,如果你的series的值为空值的话,曲线将是一片空白,什么都不会有,所以在这里就要进行一个判断,如果没有值的话,人为的添加一个键 if(Object. ...
- 【Css】一个简单的选项卡
这次来做一个简单的选项卡. 选项卡其实就分3个部分:html代码,用于显示的内容:css代码,用于显示的样式:javascript代码,用于点击事件. 老规矩,先写一个html坯子. <!DOC ...
- JBoss7.1.1远程无法访问
一般情况下在JBoss7.1.1.Final版本中配置standalone/configuration/standalone.xml<interfaces>里面name为public的&l ...
- 记一个bug的排查过程---复盘
公众号做了新需求:菜单的click事件,支持多条客服消息. 上线后,只有一个功能不好使,是点击菜单,预期发一条文本类型的客服消息. 实际操作时,点这个菜单项后,什么也没有发生. elk上看日志,也没有 ...
- js验证营业执照号码是否合规
需求:最近要做实名验证的功能,但是验证我们要验证严谨一点,参考了网上关于营业执照号码规则和一些大侠的代码的代码,总结一下. 营业执照号码规则:规则 代码: //方法一:function checkLi ...
- Tool Scripts
1. Function: 16进制转字符串 Create FUNCTION [dbo].[f_hextostr] (@hexstring VARCHAR(max)) RETURNS VARCHAR(m ...
- Hadoop实战之四~hadoop作业调度详解(2)
这篇文章将接着上一篇wordcount的例子,抽象出最简单的过程,一探MapReduce的运算过程中,其系统调度到底是如何运作的. 情况一:数据和运算分开的情况 wordcount这个例子的是hado ...
- ASP.NET MVC4 新手入门教程之九 ---9.查询详情和删除方法
在本教程的这一部分,您会检查自动生成的Details和Delete方法. 检查详细信息和删除方法 打开Movie控制器并检查的Details的方法. public ActionResult Detai ...
- git config简写命令
在多人协作开发时,一般用git来进行代码管理.git有一些命令如:git pull . git push等等,这些命令可以设置alias,也就是缩写.如:git pull 是 git pl, git ...
- Linux中的叹号命令
在shell环境下操作,需要积累点快捷输入的小技巧: 最常用的技巧恐怕就是Tab自动补全以及上方向键来回退上几条历史命令了,这些对于csh,bash,ksh,zsh都适用. 最近还找到一种快速回退上一 ...