-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraph_search.py
35 lines (31 loc) · 861 Bytes
/
graph_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from collections import deque
graph = {}
graph['ty'] = ['alicja', 'bartek', 'cecylia']
graph['bartek'] = ['janusz', 'patrycja']
graph['alicja'] = ['patrycja']
graph['cecylia'] = ['tamara', 'jarek']
graph['janusz'] = []
graph['patrycja'] = []
graph['tamara'] = []
graph['jarek'] = []
def person_is_seller(person):
if person[-1] == 'a':
return True
else:
return False
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 + ' sprzedaje mango!')
return True
else:
search_queue += graph[person]
searched.append(person)
return False
# Search from root
search('ty')