-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithms.py
193 lines (147 loc) · 5.18 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
import pygame
from queue import PriorityQueue
from queue import Queue
from queue import LifoQueue
def manhattan_distance(p1, p2):
x1, y1 = p1
x2, y2 = p2
return abs(x1 - x2) + abs(y1 - y2)
def reconstruct_path(came_from, current, draw):
while current in came_from:
current = came_from[current]
current.make_path()
draw()
def a_star(draw, grid, start, end):
count = 0
q = PriorityQueue()
q.put((0, count, start))
came_from = {}
distMin = {spot: float("inf") for row in grid for spot in row}
distMin[start] = 0
f_dist = {spot: float("inf") for row in grid for spot in row}
f_dist[start] = manhattan_distance(start.get_pos(), end.get_pos())
open_set_hash = {start}
while not q.empty():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
current = q.get()[2]
open_set_hash.remove(current)
if current == end:
reconstruct_path(came_from, end, draw)
end.make_end()
return True
for neighbor in current.neighbors:
minimumDistanceToGetHere = distMin[current] + 1
if minimumDistanceToGetHere < distMin[neighbor]:
came_from[neighbor] = current
distMin[neighbor] = minimumDistanceToGetHere
f_dist[neighbor] = minimumDistanceToGetHere + manhattan_distance(neighbor.get_pos(), end.get_pos())
if neighbor not in open_set_hash:
count += 1
q.put((f_dist[neighbor], count, neighbor))
open_set_hash.add(neighbor)
neighbor.make_open()
draw()
if current != start:
current.make_closed()
return False
def dijkstra(draw, grid, start, end):
count = 0
q = PriorityQueue()
q.put((0, count, start))
came_from = {}
distMin = {spot: float("inf") for row in grid for spot in row}
distMin[start] = 0
open_set_hash = {start}
while not q.empty():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
current = q.get()[2] # aici deja am scos din queue
open_set_hash.remove(current)
if current == end:
reconstruct_path(came_from, end, draw)
end.make_end()
return True
for neighbor in current.neighbors:
minimumDistanceToGetHere = distMin[current] + 1
if minimumDistanceToGetHere < distMin[neighbor]:
came_from[neighbor] = current
distMin[neighbor] = minimumDistanceToGetHere
if neighbor not in open_set_hash:
count += 1
q.put((distMin[neighbor], count, neighbor))
open_set_hash.add(neighbor)
neighbor.make_open()
draw()
if current != start:
current.make_closed()
return False
def bfs(draw, grid, start, end):
count = 0
q = Queue()
q.put((count, start))
came_from = {}
distMin = {spot: float("inf") for row in grid for spot in row}
distMin[start] = 0
open_set_hash = {start}
while not q.empty():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
current = q.get()[1] # aici deja am scos din queue
open_set_hash.remove(current)
if current == end:
reconstruct_path(came_from, end, draw)
end.make_end()
return True
for neighbor in current.neighbors:
minimumDistanceToGetHere = distMin[current] + 1
if minimumDistanceToGetHere < distMin[neighbor]:
came_from[neighbor] = current
distMin[neighbor] = minimumDistanceToGetHere
if neighbor not in open_set_hash:
count += 1
q.put((count, neighbor))
open_set_hash.add(neighbor)
neighbor.make_open()
draw()
if current != start:
current.make_closed()
return False
def dfs(draw, grid, start, end):
count = 0
stack = LifoQueue()
stack.put((count, start))
came_from = {}
# distMin = {spot: float("inf") for row in grid for spot in row}
# distMin[start] = 0
open_set_hash = {start}
while not stack.empty():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
top = stack.get()
stack.put(top)
current=top[1]
if current == end:
reconstruct_path(came_from, end, draw)
end.make_end()
return True
amNod = False
for neighbor in current.neighbors:
if neighbor not in open_set_hash:
count += 1
came_from[neighbor] = current
stack.put((count, neighbor))
open_set_hash.add(neighbor)
neighbor.make_open()
amNod = True
break
if (amNod==False):
stack.get()
draw()
if current != start:
current.make_closed()
return False