forked from vannynguyen/campus_map
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.py
More file actions
26 lines (20 loc) · 753 Bytes
/
bfs.py
File metadata and controls
26 lines (20 loc) · 753 Bytes
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
from collections import deque
from vertex import Vertex
from load_graph import load_graph
def bfs(start, end):
vertices = load_graph("dartmouth_graph.txt")
q = deque()
q.append(start)
while len(q) != 0:
for adjacent_vertex in vertices[q[0]].list:
if adjacent_vertex.back_pointer == None :
q.append(adjacent_vertex.name)
adjacent_vertex.back_pointer = vertices[q[0]]
if q.popleft() == end:
route = []
end_vertex = vertices[end]
while end_vertex.name != start:
route.append(end_vertex)
end_vertex = end_vertex.back_pointer
route.append(vertices[start])
return route