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 ...
随机推荐
- unity物理学材质Physic Material
物理材料 切换到脚本 在物理学材料是用来调整摩擦和碰撞对象的反弹效应. 要创建物理材质,请从菜单栏中选择“ 资源”>“创建”>“物理材质 ”.然后将“物理材质”从“项目视图”拖动到场景 ...
- UOJ #138. 【UER #3】开学前的涂鸦
Description 红包是一个有艺术细胞的男孩子. 红包由于NOI惨挂心情不好,暑假作业又多,于是他开始在作业本上涂鸦. 一开始,他在纸上画了一棵 n 个节点的树.但是他觉得这样的画太简单了,体现 ...
- Linux常用命令语法+示例
原文出自:https://blog.csdn.net/seesun2012 Linux常用命令:Linux查看日志命令总结:Tomcat相关:Linux配置网卡,连接外网:Linux下安装JDK:Li ...
- Fastreport史无前例5折,仅十天快抢!
慧都十年第一弹,与全球顶级报表控件厂商Fastreport携手同欢:仅仅十天,仅仅提供给慧都控件网,Fastreport旗下全部产品,全部5折!立即订购>> 慧都作为中国第一家与其建立合作 ...
- 001.開始使用ASP.NET Web API 2(一)
原文鏈接:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web ...
- axios发送post请求后台接受不到问题
axios发送post请求后台接受不到问题 1.首先这是前端的问题 2.解决方案不唯一,但这招肯定行 <!DOCTYPE html> <html> <head> & ...
- SSRS使用MySql作为数据源遇到的问题。
因为工作需求,SSRS需要取到MySql数据源,还好有了ODBC. 谷歌了很多,都是不完整的Solution,放上完整版的供大家评价参考. 下面是StepByStep. 问题1.使用ODBC数据源,填 ...
- LDAP概念了解
LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.LDAP支持TCP/IP,这对访问Internet是必须的. L ...
- mysql-配置主从数据库,实现读写分离
主从分离的原则:所有的写操作在主数据库中进行,因为主从分离的原理是涉及到同步数据,那就可能会出现延迟或者其他问题,就可能会出现脏数据. 所以,在从库中进行的读操作也必须是有一定容忍性的数据,例如日志等 ...
- dockerfile 踩坑记录
1.使用ADD/COPY命令 源文件必须和Dockfile位于同一目录下(使用绝对路径是没用的,会提示找不到你的源文件) 2.ADD命令会自动解压 3.尽量耗时且不容易变的部分放在dockerfile ...