当前位置:网站首页>克隆无向图[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 克隆无向图
边栏推荐
- The concept and significance of mean, variance, standard deviation and covariance
- High performance and high availability computing architecture of "microblog comments" in microblog system
- 打造一个 API 快速开发平台,牛逼!
- Basic tutorial for installing monggodb in win10
- Set up security groups, record domain names, and apply for SSL certificates
- Yunhe enmo Guoqiang, identifiez - le, saisissez - le, avant l'ébullition de la base de données nationale
- AI赋能新零售,「智」胜之道在于生态思维|数智夜话直播精选摘录
- Bee常用配置
- Sword finger offer 15 Number of 1 in binary
- AI首席架构师9-胡晓光 《飞桨模型库与行业应用》
猜你喜欢

The concept and significance of mean, variance, standard deviation and covariance

數莓派 4怎麼樣?可能的玩法有哪些?

QT learning 02 GUI program example analysis

Matlab exercises -- program control process exercise

Common knowledge of ECS security settings

6.29日刷题题解

西门子低代码平台通过Database Connector 连接Mysql 实现增删改查

云和恩墨盖国强,识别它、抓住它,在国产数据库沸腾以前

小程序插件接入、开发与注意事项

爬虫入门实战:斗鱼弹幕数据抓取,附送11节入门笔记
随机推荐
What is flush software? Is it safe to open an account online?
FPGA Development (2) -- IIC communication
Construction of module 5 of actual combat Battalion
[译]在软件开发行业工作 6 年后,那些年我曾改过的观念
What is online account opening? In addition, is it safe to open a mobile account?
架构实战营模块5作业
6.29 problem solving
西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
Embedded development: Hardware in the loop testing
招商证券靠谱吗?开股票账户安全吗?
Implementation of aut, a self-developed transport layer protocol for sound network -- dev for dev column
Basic tutorial for installing monggodb in win10
请指教什么是在线开户?另外,手机开户安全么?
LC: maximum subarray and
Is China Merchants Securities reliable? Is it safe to open a stock account?
剑指 Offer 14- I. 剪绳子
Siemens low code version 9.14: meet different needs
西门子低代码 9.14版本: 满足不同需求
Simple understanding of B tree and b+ tree
QPainter的使用入门:绘制象棋界面