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