from collections import deque
graph = dict()
graph['you'] = ['tom','alice', 'bob', 'claire']
graph['bob'] = ['anuj','peggy']
graph['alice'] = ['peggy']
graph['claire'] = ['thom', 'jonny']
graph['anuj'] = []
graph['peggy'] = []
graph['thom'] = []
graph['jonny'] = []
def search(name):
search_queue = deque()
search_queue += graph[name]
searched = []
while len(search_queue) > 0:
person = search_queue.popleft()
if person not in searched:
if person_is_seller(person):
print(person + ' is seller')
else:
search_queue += graph[person]
searched.append(person)
def person_is_seller(name):
return name[-1] == 'm'
search('you')
广度优先搜索
最新推荐文章于 2024-07-14 18:15:43 发布