-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvisualize_bfs.py
More file actions
48 lines (37 loc) · 1.49 KB
/
visualize_bfs.py
File metadata and controls
48 lines (37 loc) · 1.49 KB
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
48
"""
Animate Breadth First Search over a randomly generated maze.
You can also modify the program to store snapshots of the progress in the images/bfs folder
"""
import random
import tkinter
from collections import deque
from maze import Maze, Square
from visualize_search import visualize, make_path, change_color, capture
def bfs(event, maze):
"""Compute BFS using recursive implementation and generate images suitable for animated GIF."""
st = deque() # Stack of Squares to process
visited = {} # Squares already visited
start = Square(0, maze.nc // 2)
end = Square(maze.nr-1, maze.nc // 2)
st.append(start) # For every square in the Stack
visited[start] = make_path(maze, start, 'lightblue') # has been visited
while st:
s = st.popleft()
change_color(maze, visited[s], 'blue')
capture(maze.w, 'bfs')
if s == end:
return s
for n in maze.neighbors(s):
if not maze.isWall(n):
if n not in visited:
st.append(n)
visited[n] = make_path(maze, n, 'lightblue')
return None
if __name__ == "__main__":
random.seed(32) # a maze with solution. Change as you like
N=16
M = Maze(N, N)
M.random(33/100) # Chooses a random maze
# If you add capture=True, then images will be generated in images/dfs folder
visualize(M, bfs, capture=True)
tkinter.mainloop()