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 ...
随机推荐
- dubbo示例
Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 我也不明白这是什么意思,使用了之后大概就是提供一个将多个项目进行联合的一种分布式,使用的是一 ...
- 1、Shell命令行书写规则
学习目标Shell命令行书写规则 正文对Shell命令行基本功能的理解有助于编写更好的Shell程序,在执行Shell命令时多个命令可以在一个命令行上运行,但此时要使用分号(;)分隔命令,例如: ro ...
- Django的学习基础1
著名的MVC模式:所谓MVC就是把web应用分为模型(M),控制器(C),视图(V)三层:他们之间以一种插件似的,松耦合的方式连接在一起. Django的MTV模式本质上与MVC模式没有什么差别,也是 ...
- 001.開始使用ASP.NET Web API 2(一)
原文鏈接:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web ...
- tomcat绑定域名绑定端口及更换ROOT目录
一.更换ROOT目录 tomcat默认网站目录为 webapps/ROOT ,那么我们如何改为自己的网站目录呢? 1.打开并编辑tomcat目录下的 conf/server.xml 大约在148行的位 ...
- jmeter简单使用示例
1.下载后解压,运行bin目录下的jmeter.bat 2.add ThreadGroup 3.add request 4.add listener
- About custom Theme and Style
For android system, of course you can custom your own style and theme, but you can't break compatibi ...
- 多实例部署多个tomcat
注意点: 1.多实例tomcat的更新维护,需要考虑如何能“优雅”地对所有实例进行升级: 2.尽量不要影响应用程序,在更新tomcat时,一不小心就把conf目录等全部覆盖,所以尽量要把配置文件和安装 ...
- AMD 规范与CMD 规范概要
命名冲突和文件依赖,是前端开发过程中的两个经典问题.通过模块化开发来解决. AMD 规范在这里:https://github.com/amdjs/amdjs-api/wiki/AMD CMD 规范在这 ...
- css3之背景属性之background-size
一.相关属性: background-image: url(“./img/a.jpg”); //设置元素背景图片 background-repeat: repeat/no-repeat: //设置背景 ...