from collections import deque
graph = {
}
graph["you"] = ["alice","bob","claire"]
graph["bob"] = ["anuj",'peggy']
graph["alice"] = ["peggy"]
graph["claire"] = ["thom","jonny"]
graph["anuj"] = []
graph["peggy"] = []
graph["thom"] = []
graph["jonny"] = []
def person_is_seller(name):
return name[-1] == 'm'
#以名字最后是不是m来判断是不是芒果经销商(有点nt233)
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")
广度优先搜索(python实现)
猜你喜欢
转载自blog.csdn.net/weixin_45253216/article/details/110198157
今日推荐
周排行