我对图的基本广度优先搜索遍历的理解是:
BFS
Start from any node.
Add it to queue.
Add it to visited array.
While queue is not empty:
Remove head from queue;
Print node.
add all unvisited direct subchilds to que;
mark them as visited
但是,如果我们必须从给定节点遍历有向图,并且并非所有节点都可以从给定节点访问(直接或间接),那么我们如何使用 BFS 来遍历这种情况的图呢?
您能否在此图中也解释一下:
a=> b => d => e => d
a=> c => d
在这里,如果起始节点是b
,我们从不打印a
and c
。
我在算法中遗漏了什么吗?
我曾经 HashMap<String, ArrayList> adj = new HashMap();
创建邻接列表来存储图形。