未访问
已入队
当前访问
已完成
访问顺序:空
选择起点,然后点击“开始 BFS”。
1 function BFS(start) {
2 const visited = new Set();
3 const queue = [];
4 visited.add(start);
5 queue.push(start);
6 while (queue.length > 0) {
7 const current = queue.shift();
8 visit(current);
9 for (const next of graph[current]) {
10 if (!visited.has(next)) {
11 visited.add(next);
12 queue.push(next);
13 }
14 }
15 }
16 }
队列为空