BFS:广度优先搜索
queue 队列方法来做
需要掌握的知识,队列queue(java中queue的用法)
dist(u) for every node u 需要一个包含所有点的方法
其次,开始探索整张图
bfs_explore(G,s) G代表这张图graph G,s代表开始的点 a starting node s
dist(s)=0
create an empty queue Q and an enqueue(Q,s)
while Q is not empty do
u to dequeue(Q) 就是Q会出队,先进先出
for any outgoing edge(u,v) do
if dist(v)=∞ then
enqueue(Q,v)
dist(v) to dist(u)+1
bfs(G)
INPUT:A graph G and a starting node s
OUTPUT:Labeling dist(u) for every u
for u 属于 V do dist(u) to ∞
for u 属于 V do
if dist(u)= ∞ then run bfs_explore(G,s)