BFS: Breadth first search

Table
print("News report: Banana dealer on the loose.")print("Being a detective, you decided to go investigate.")def banana_dealer(name):if name == 'Banana':return Truedef search(name):global not_dealerdealer = []dealer += graph[name]while dealer:person = dealer.pop(0)if banana_dealer(person):print(person + ' is Banana dealer.')return Trueelse:not_dealer.append(person)dealer += graph[person]print('You dont find the banana dealer.')return Falsenot_dealer = []graph = {'Tom': ['Ivan', 'Ira', 'Kevin'],'Ivan':['Peter'],'Ira':['Banana'],'Kevin':['Mary'],'Peter':[],'Banana':[],'Mary':[]}search('Tom')print('Here is the list of searched people : ', not_dealer)
Result

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store