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 ...
随机推荐
- android studio Cannot resolve symbol '@drawable/XXX'等问题解决办法
方法1."Build " -> "Clean project" 方法 2."Tools" -> "Android&qu ...
- 入门系列之在Ubuntu上使用MySQL设置远程数据库优化站点性能
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由苏子晨 发表于云+社区专栏 介绍 随着您的应用程序或网站的增长,您可能已经超出了当前的服务器设置.如果您在同一台计算机上托管Web服务 ...
- JS中的事件冒泡和事件捕获
事件捕获阶段:事件从最上一级标签开始往下查找,直到捕获到事件目标(target). 事件冒泡阶段:事件从事件目标(target)开始,往上冒泡直到页面的最上一级标签. 用图示表示如下: 1.冒泡事件: ...
- DDD中的EFCore
EFCore在DDD中的使用 在DDD中,我们对聚合根的操作都会通过仓储去获取聚合实例. 因为聚合根中可能会含有实体属性,值对象属性,并且,在DDD中,我们所设计的领域模型都是充血模型.所以,在对聚合 ...
- jQuery比较时间处理过程(日期转换为时间戳比较)
不管在JS中还是PHP中,终会遇到时间比较,但是很尴尬的是,举个例子,2017-04-12和2017-5-21是不一样或者说不能比较的,这在PHP和JS都是这种情况,这种情况怎么办呢,换成统一的格式比 ...
- angular 与 layer 集成过程
layer 的提示框和弹层确实也好用,在使用angular的前提下,使用layer遇到诸多麻烦,记录下来. 在类型是1页面层,主要问题在遮罩方面,造成无法编辑. 开始:引入layer 样式,angul ...
- java爬取百度首页源代码
爬虫感觉挺有意思的,写一个最简单的抓取百度首页html代码的程序.虽然简单了一点,后期会加深的. package test; import java.io.BufferedReader; import ...
- 一、mysql架构
一.简介 mysql是一个开源的数据库管理系统,它相对于oracle更加地轻量.成本低,随着功能的日益完善,它变得备受企业喜爱,尤其是中小企业. mysql的整体架构大体包括以下几个方面: 1)主体结 ...
- 很多个java面试题
1. 为什么说Java是一门平台无关语言? 平台无关实际的含义是“一次编写到处运行”.Java 能够做到是因为它的字节码(byte code)可以运行在任何操作系统上,与底层系统无关. 2. 为什么 ...
- Alice's Print Service
Alice's Print Service Time Limit: 2 Seconds Memory Limit: 65536 KB Alice is providing print ser ...