-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathbfs.py
47 lines (37 loc) · 1.03 KB
/
bfs.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
36
37
38
39
40
41
42
43
44
45
46
47
def how_it_workd(data, start, end, visited=[]):
queue = [start]
while queue:
current_node = queue.pop(0)
visited.append(current_node)
print(visited)
for i in data[current_node] - set(visited):
print("for looped --> {}".format(i))
print()
queue.append(i)
print("wants the data being bf-searched")
print(data)
def bfs(data, start, end, visited=[]):
queue = [start]
while queue:
current_node = queue.pop(0)
if current_node==end:
print("Path: " + "->".join(visited) + "->" + end)
return
visited.append(current_node)
for i in data[current_node] - set(visited):
print("for looping -->",i)
queue.append(i)
print("Path does not exist!")
if __name__ == '__main__':
data = {
'A': {'B'},
'B': {'C', 'D'},
'C': {'E'},
'D': {'E'},
'E': {'F'},
'F': set()
}
print("how it works")
how_it_workd(data, "A", "D")
print("out come")
bfs(data, 'A', 'D')