-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algorithms.py
333 lines (289 loc) · 10.5 KB
/
Algorithms.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from settings import *
from grid_class import *
from queue import Queue
from queue import PriorityQueue
import pygame
class BreadthFirstSearch:
def __init__(self, app, start, end, grid):
self.app = app
self.start = start
self.end = end
self.grid = grid
self.par = {}
# If there exits a path then retrace path from end to start
def construct_path(self, curr):
while curr in self.par:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.app.running = False
pygame.quit()
curr = self.par[curr]
curr.make_path()
self.grid.draw()
pygame.display.update()
curr.make_start()
self.grid.draw()
pygame.display.update()
# BFS algorithm Implementation
def execute(self):
# dist represents no.of steps to reach any cell from start node
dist = {cell: float("inf") for row in self.grid.grid for cell in row}
par = {}
q = Queue()
dist[self.start] = 0
q.put(self.start)
while not q.empty():
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.app.running = False
pygame.quit()
curr = q.get()
if curr == self.end:
self.end.make_end()
self.construct_path(curr)
return True
# Iterate through all neighbors of curr cell and if we can relax any of the neighbor then update it and add to queue
for neighbor in curr.neighbors:
if dist[neighbor] > dist[curr] + 1:
dist[neighbor] = dist[curr] + 1
self.par[neighbor] = curr
q.put(neighbor)
neighbor.make_open()
self.grid.draw()
pygame.display.update()
if curr != self.end and curr != self.start:
curr.make_closed()
return False
class Dijkastra:
def __init__(self, app, start, end, grid):
self.app = app
self.start = start
self.end = end
self.grid = grid
self.par = {}
def construct_path(self, curr):
while curr in self.par:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.app.running = False
pygame.quit()
curr = self.par[curr]
curr.make_path()
self.grid.draw()
pygame.display.update()
curr.make_start()
self.grid.draw()
pygame.display.update()
def execute(self):
dist = {cell: float("inf") for row in self.grid.grid for cell in row}
dist[self.start] = 0
pq = PriorityQueue()
pq.put((0, self.start))
while not pq.empty():
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.app.running = False
pygame.quit()
d, curr = pq.get()
if curr == self.end:
self.end.make_end()
self.construct_path(curr)
return True
if d > dist[curr]:
continue
for neighbor in curr.neighbors:
if dist[neighbor] > dist[curr] + 1:
dist[neighbor] = dist[curr] + 1
self.par[neighbor] = curr
pq.put((dist[neighbor], neighbor))
neighbor.make_open()
self.grid.draw()
pygame.display.update()
if curr != self.end and curr != self.start:
curr.make_closed()
return False
class DepthFirstSearch:
def __init__(self, app, start, end, grid):
self.app = app
self.start = start
self.end = end
self.grid = grid
self.par = None
self.vis = None
def construct_path(self, curr):
while curr in self.par:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.app.running = False
pygame.quit()
curr = self.par[curr]
curr.make_path()
self.grid.draw()
pygame.display.update()
curr.make_start()
self.grid.draw()
pygame.display.update()
def execute(self, curr):
if self.vis == None:
self.vis = set()
if self.par == None:
self.par = {}
self.vis.add(curr)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if curr == self.end:
self.end.make_end()
self.construct_path(curr)
self.start.make_start()
return True
for neighbor in curr.neighbors:
if neighbor not in self.vis:
self.par[neighbor] = curr
neighbor.make_open()
self.grid.draw()
pygame.display.update()
if self.execute(neighbor):
return True
curr.make_closed()
return False
class Astar:
def __init__(self, app, start, end, grid):
self.app = app
self.start = start
self.end = end
self.grid = grid
self.par = None
self.vis = None
self.pq = PriorityQueue()
def construct_path(self, curr):
while curr in self.par:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.app.running = False
pygame.quit()
curr = self.par[curr]
curr.make_path()
self.grid.draw()
pygame.display.update()
curr.make_start()
self.grid.draw()
pygame.display.update()
def execute(self):
self.par = {}
self.g_score = {cell: float("inf") for row in self.grid.grid for cell in row}
self.g_score[self.start] = 0
self.f_score = {cell: float("inf") for row in self.grid.grid for cell in row}
self.f_score[self.start] = self.start.heuristic(self.end)
self.vis = set()
self.vis.add(self.start)
self.pq.put((self.f_score[self.start], self.start))
while not self.pq.empty():
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.app.running = False
pygame.quit()
curr = self.pq.get()[1]
self.vis.remove(curr)
if curr == self.end:
self.end.make_end()
self.construct_path(curr)
self.start.make_start()
return True
for neighbor in curr.neighbors:
if self.g_score[neighbor] > self.g_score[curr] + 1:
self.g_score[neighbor] = self.g_score[curr] + 1
self.par[neighbor] = curr
self.f_score[neighbor] = self.g_score[neighbor] + neighbor.heuristic(self.end)
if neighbor not in self.vis:
self.pq.put((self.f_score[neighbor], neighbor))
self.vis.add(neighbor)
neighbor.make_open()
self.grid.draw()
pygame.display.update()
if curr != self.end and curr != self.start:
curr.make_closed()
return False
class Bidirectional():
def __init__(self, app, start, end, grid):
self.app = app
self.start = start
self.end = end
self.grid = grid
self.fwd_par = {}
self.back_par = {}
self.dist = {}
self.fwd_q = Queue()
self.back_q = Queue()
self.vis_fwd = set()
self.vis_back = set()
def construct_path(self, curr):
fwd_curr = curr
back_curr = curr
curr.make_path()
while fwd_curr in self.fwd_par and back_curr in self.back_par:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.app.running = False
pygame.quit()
fwd_curr = self.fwd_par[fwd_curr]
back_curr = self.back_par[back_curr]
fwd_curr.make_path()
back_curr.make_path()
self.grid.draw()
pygame.display.update()
while fwd_curr in self.fwd_par:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.app.running = False
pygame.quit()
fwd_curr = self.fwd_par[fwd_curr]
fwd_curr.make_path()
self.grid.draw()
pygame.display.update()
while back_curr in self.back_par:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.app.running = False
pygame.quit()
back_curr = self.back_par[back_curr]
back_curr.make_path()
self.grid.draw()
pygame.display.update()
back_curr.make_end()
self.grid.draw()
pygame.display.update()
def execute(self):
self.fwd_q.put(self.start)
self.back_q.put(self.end)
self.vis_fwd.add(self.start)
self.vis_back.add(self.end)
while not self.fwd_q.empty() and not self.back_q.empty():
curr_f = self.fwd_q.get()
curr_b = self.back_q.get()
for n_f, n_b in zip(curr_f.neighbors, curr_b.neighbors):
if n_f not in self.vis_fwd:
self.vis_fwd.add(n_f)
self.fwd_par[n_f] = curr_f
self.fwd_q.put(n_f)
n_f.make_open()
if n_b not in self.vis_back:
self.vis_back.add(n_b)
self.back_par[n_b] = curr_b
self.back_q.put(n_b)
n_b.make_open()
if curr_f in self.vis_back:
self.construct_path(curr_f)
self.start.make_start()
return True
elif curr_b in self.vis_fwd:
self.end.make_end()
self.construct_path(curr_b)
self.start.make_start()
return True
self.grid.draw()
pygame.display.update()
if curr_f != self.start or curr_b != self.end:
curr_f.make_closed()
curr_b.make_closed()
return False