当前位置:网站首页>克隆无向图[bfs访问每条边而不止节点]
克隆无向图[bfs访问每条边而不止节点]
2022-06-29 23:52:00 【REN_林森】
前言
BFS作为图操作的基础之一,对于无向图,可以利用hash来防止一个节点多次遍历,导致死循环。但是如果BFS需要访问每条边,而不止节点,那么一个节点可能会被访问多次,单纯的hash记录不能放在一个节点多次进入队列。
一、克隆无向图


二、BFS访问每条边
package everyday.hash;
import java.util.*;
public class CloneGraph {
// bfs
public Node cloneGraph(Node node) {
if (null == node) return null;
Node root = new Node(node.val);
// 防止无向图来回访问。
Set<Node> visited = new HashSet<>();
// 防止已经加入队列的节点,再次因为其他连接边,再次加入队列。(每个节点的度不止1)
Set<Node> queSet = new HashSet<>();
// 把已经clone的节点记录起来,防止无法拿到已经new出来的节点。
Map<Integer, Node> cloneNodes = new HashMap<>();
Queue<Node> que = new LinkedList<>();
Queue<Node> newQue = new LinkedList<>();
que.offer(node);
newQue.offer(root);
queSet.add(node);
cloneNodes.put(root.val, root);
while (!que.isEmpty()) {
// 取当前队首节点。
Node cur = que.poll();
Node newCur = newQue.poll();
// 该节点设置为已访问过,防止无向图循环访问。
visited.add(cur);
for (Node nei : cur.neighbors) {
// 访问过的,就已经把节点和边都搞好了,就不用再做其他事了。
if (!visited.contains(nei)) {
// 如果节点还没创立,就new出新的节点。
if (!cloneNodes.containsKey(nei.val)) {
Node next = new Node(nei.val);
cloneNodes.put(next.val, next);
}
Node next = cloneNodes.get(nei.val);
// 不能重复加入队列。
if (!queSet.contains(nei)) {
que.offer(nei);
newQue.offer(next);
queSet.add(nei);
}
// 无向图,设置双向边。
newCur.neighbors.add(next);
next.neighbors.add(newCur);
}
}
}
return root;
}
// Definition for a Node.
class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}
}
总结
1)BFS访问每条边,双hash防止死循环的同时,防止由多边访问同一节点,从而导致同一节点多次加入队列。
参考文献
[1] LeetCode 克隆无向图
边栏推荐
- FPGA Development (2) -- IIC communication
- Analysis of define incdir command in TCL script of Modelsim
- Virtual machine online migration based on openstack
- LC:最大子数组和
- After crossing, she said that the multiverse really exists
- 500 error occurred after importing skins folder into solo blog skin
- 数莓派 4怎么样?可能的玩法有哪些?
- 请指教什么是在线开户?另外,手机开户安全么?
- I wonder if I can open an account today? In addition, is it safe to open an account online now?
- LC: effective Sudoku + rotating image
猜你喜欢

How to view the CPU cores and threads in win11? Win11 view the tutorial of how many cores and threads the CPU is

Head on Amway! Good looking and practical motor SolidWorks model material see here

Zhongang Mining: Fluorite helps the construction and development of lithium battery in fluorine industry

ThinkPad VMware installation virtual machine: this host supports Intel VT-x, but Intel VT-x is disabled (problem resolution)

Start harvesting! Nailing: adjust the maximum number of free users of "nailing team". If the number exceeds 10, it will not work normally

Unity splashimage scaling problem

二叉树的序列化 力扣 297. 二叉树的序列化与反序列化 652. 寻找重复的子树

QT learning 01 GUI program principle analysis

Cartoon security HIDS, EDR, NDR, XDR

新钛云服荣膺“2022爱分析 · IT运维厂商全景报告”云管理平台CMP 代表厂商!...
随机推荐
Metaq cluster installation test
FPGA Development (1) -- serial port communication
Analysis of define incdir command in TCL script of Modelsim
Virtual machine online migration based on openstack
请指教同花顺软件究竟是什么?究竟网上开户是否安全么?
开始“收割”!钉钉调整“钉钉Teambition”免费用人数上限,超十人将无法正常用
500 error occurred after importing skins folder into solo blog skin
Viewing splitchunks code segmentation from MPX resource construction optimization
设置安全组、域名备案、申请ssl证书
Official website of Greentree
vlog常用参数解析
QT learning 01 GUI program principle analysis
AI empowers new retail, the way to win "wisdom" lies in ecological thinking | selected excerpts from digital intelligence night talk live broadcast
Matplotlib histogram of Matplotlib visualization plt bar()
Overseas digital authentication service provider advance AI was selected into the "2022 brand sea Service Market Research Report" of equalocean
Shell operator
After crossing, she said that the multiverse really exists
西门子低代码 9.14版本: 满足不同需求
Sword finger offer 14- I. cut rope
Machine learning: the concept and application of VC dimension