-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS.py
157 lines (129 loc) · 4.48 KB
/
DFS.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import pygame
import random
pygame.init()
# WINDOW
WINDOW_SIZE = 600
GRID_SIZE = 20
WINDOW = pygame.display.set_mode((WINDOW_SIZE, WINDOW_SIZE))
pygame.display.set_caption('DFS Algorithm Visualization')
# VARIABLES
clock = pygame.time.Clock()
FPS = 10
# COLORS
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
RED = (255, 0, 0)
GRAY = (128, 128, 128)
# CLASSES
class Node:
def __init__(self, row, col):
self.row = row
self.col = col
self.x = col * GRID_SIZE
self.y = row * GRID_SIZE
self.color = WHITE
self.neighbors = []
self.visited = False
def draw(self):
pygame.draw.rect(WINDOW, self.color, (self.x, self.y, GRID_SIZE, GRID_SIZE))
pygame.draw.rect(WINDOW, GRAY, (self.x, self.y, GRID_SIZE, GRID_SIZE), 1)
def add_neighbors(self, grid):
if self.row < len(grid) - 1 and not isinstance(grid[self.row + 1][self.col], Obstacle):
self.neighbors.append(grid[self.row + 1][self.col])
if self.row > 0 and not isinstance(grid[self.row - 1][self.col], Obstacle):
self.neighbors.append(grid[self.row - 1][self.col])
if self.col < len(grid[0]) - 1 and not isinstance(grid[self.row][self.col + 1], Obstacle):
self.neighbors.append(grid[self.row][self.col + 1])
if self.col > 0 and not isinstance(grid[self.row][self.col - 1], Obstacle):
self.neighbors.append(grid[self.row][self.col - 1])
class Obstacle(Node):
def __init__(self, row, col):
super().__init__(row, col)
self.color = BLACK
def make_grid(rows, cols, start, target):
grid = [[Node(i, j) for j in range(cols)] for i in range(rows)]
for _ in range(rows * cols // 5): # Add obstacles randomly
row = random.randint(0, rows - 1)
col = random.randint(0, cols - 1)
grid[row][col] = Obstacle(row, col)
grid[start[0]][start[1]] = Node(start[0], start[1])
grid[target[0]][target[1]] = Node(target[0], target[1])
for row in grid:
for node in row:
node.add_neighbors(grid)
return grid
def draw_grid(grid):
WINDOW.fill(WHITE)
for row in grid:
for node in row:
node.draw()
pygame.display.update()
def dfs(grid, start_node, target_node):
stack = [start_node]
while stack:
current_node = stack.pop()
if current_node.visited:
continue
current_node.visited = True
current_node.color = BLUE
draw_grid(grid)
yield
if current_node == target_node:
current_node.color = GREEN
draw_grid(grid)
return
for neighbor in current_node.neighbors:
if not neighbor.visited:
stack.append(neighbor)
current_node.color = YELLOW
draw_grid(grid)
yield
for row in grid:
for node in row:
if node.visited:
node.color = GREEN
draw_grid(grid)
def display_text(txt, y, size):
FONT = pygame.font.SysFont('Futura', size)
text = FONT.render(txt, True, BLACK)
text_rect = text.get_rect(center=(WINDOW_SIZE / 2, y))
WINDOW.blit(text, text_rect)
# MAIN FUNCTION
def main():
rows = cols = WINDOW_SIZE // GRID_SIZE
start = (0, 0)
target = (rows - 1, cols - 1)
grid = make_grid(rows, cols, start, target)
start_node = grid[start[0]][start[1]]
target_node = grid[target[0]][target[1]]
start_node.color = RED
target_node.color = RED
draw_grid(grid)
dfs_generator = dfs(grid, start_node, target_node)
run = True
searching = False
while run:
clock.tick(FPS)
if searching:
try:
next(dfs_generator)
except StopIteration:
searching = False
else:
draw_grid(grid)
display_text('DFS Algorithm Visualization: ', 30, 40)
display_text('Press SPACE to start/pause DFS and q to quit.', 70, 30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
searching = not searching
if event.key == pygame.K_q:
run = False
pygame.display.update()
pygame.quit()
main()