【算法图解】第六章:6.3(广度优先查找)
程序员文章站
2022-03-03 11:50:39
...
我就不说其他了,直接放代码了
from collections import deque
'''
在名字为name的关系网中找芒果经销商的人,使用广度优先,先找一级关系再找二级关系
'''
def search(name, AllDict):
search_queue = deque()
search_queue += AllDict[name]
searched = []
while search_queue:
person = search_queue.popleft()
if person not in searched:
if person[-1] == 'm':
print('{} is a maguo seller'.format(person))
return True
else:
search_queue += AllDict[person]
searched.append(person)
return False
if __name__ == '__main__':
graph = {"you": ["alice", "bob", "claire", "tom"], "bob": ["anuj", "peggy"], "alice": ["peggy"],
"claire": ["thom", "jonny"],
"anuj": [], "peggy": [], "thom": [], "jonny": []}
print(search(name='you', AllDict=graph))
上一篇: Gradle转Maven