BFS的标准套路:
for head in all_node()
if head 没有visited过 # 以上两步是为了防止有孤立的部分存在而被遗漏,如果没有孤立的部分可以不写,直接放queue里一个就开始循环即可
queue.append(head)
mark head
# 主干部分
while queue is not empty:
cur_node = queue.pop() # 我们不需要关心node将会以何种顺序出queue,我们只在乎如何往queue里进就行了。
# 找邻居
for 所有 cur_node 的邻居们:
if cur_neighbour 没有visit过:
deal -> cur_neighbour
mark -> cur_neighbour
inject -> cur_neighbour