package com.jiucaiyuan.net.algrithm.graph;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
/**
* 图的广度优先遍历(宽度优先遍历)
*
* @Author jiucaiyuan 2022/4/14 17:15
* @mail services@jiucaiyuan.net
*/
public class GraphMaxWidthAccess {
/**
* 宽度优先遍历(Breadth First Search)
*
* @param node 从node开始遍历
*/
public static void bfs(Node node) {
if (node == null) {
return;
}
Queue<Node> queue = new LinkedList<>();
//标记已入过队列
HashSet<Node> set = new HashSet<>();
queue.add(node);
set.add(node);
while (!queue.isEmpty()) {
Node curr = queue.poll();
System.out.println(curr.value);
for (Node next : curr.nexts) {
if (!set.contains(next)) {
queue.add(next);
set.add(next);
}
}
}
}
public static void main(String[] args) {
}
}