当前位置:网站首页>克隆无向图[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 克隆无向图
边栏推荐
- How about counting Berry Pie 4? What are the possible ways to play?
- Why is JSX syntax so popular?
- Analysis of common vlog parameters
- Zhongkang holdings opens the offering: it plans to raise HK $395million net, and it is expected to be listed on July 12
- [Shangshui Shuo series] day 8
- Cacti settings for spin polling
- MySQL multi table query
- Web APIs 环境对象 丨黑马程序员
- 请指教同花顺软件究竟是什么?究竟网上开户是否安全么?
- Sword finger offer 14- I. cut rope
猜你喜欢

Set up security groups, record domain names, and apply for SSL certificates

The role of VMware virtual machine

西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
![[wechat applet] understand the basic composition of the applet project](/img/71/98894fbb9cda4facfd2b83c8ec8f9a.png)
[wechat applet] understand the basic composition of the applet project

6.29日刷题题解

一步步教你在Edge浏览器上安装网风笔记

Sword finger offer 14- ii Cutting rope II

@Scheduled注解的坑,我替你踩了

Matlab exercises -- program control process exercise

大厂试水 HPE推出Arm CPU通用服务器产品
随机推荐
云和恩墨盖国强,识别它、抓住它,在国产数据库沸腾以前
Bee common configuration
Unity about failure (delay) of destroy and ondestroy
How to view the CPU cores and threads in win11? Win11 view the tutorial of how many cores and threads the CPU is
Cartoon security HIDS, EDR, NDR, XDR
开始“收割”!钉钉调整“钉钉Teambition”免费用人数上限,超十人将无法正常用
剑指 Offer 13. 机器人的运动范围
[译]在软件开发行业工作 6 年后,那些年我曾改过的观念
What is IGMP? What is the difference between IGMP and ICMP?
二叉搜索树 230. 二叉搜索树中第K小的元素 1038. 从二叉搜索树到更大和树
[Shangshui Shuo series] day 8
What is flush software? Is it safe to open an account online?
Yunhe enmo, gaiguoqiang, identify it and grasp it before the domestic database boils
Ingenious application of golang generics to prevent null pointer errors of variables and structural fields
After working in the software development industry for six years, I changed my ideas in those years
除子
New CorelDRAW technical suite2022 latest detailed function introduction
Why is JSX syntax so popular?
打造一个 API 快速开发平台,牛逼!
Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus