广度优先搜索是一种用于图的查找算法,可以帮助回答两类问题。
第一,从节点A出发,有前往节点B的路径吗?
第二,从节点A出发,前往节点B的哪条路径最短?
广度优先搜索的运行时间为
O(人数 + 边数),这通常写作O(V + E),其中V为顶点(vertice)数,E为边数。
def search(name):
search_queue = deque()
search_queue += graph[name]
searched = []
#这个数组用于记录检查过的人
while search_queue:
person = search_queue.popleft()
if not person in searched:
#仅当这个人没检查过时才检查
if person_is_seller(person):
print person + " is a mango seller!"
return True
else:
search_queue += graph[person]
searched.append(person)
#将这个人标记为检查过
return False
search("you")
原文:https://www.cnblogs.com/everfight/p/grokking_algorithms_note_6.html