-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgo.py
More file actions
206 lines (193 loc) · 7.67 KB
/
algo.py
File metadata and controls
206 lines (193 loc) · 7.67 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
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
from collections import deque
from pygame.locals import*
from math import sqrt
from graph import*
import sys, heapq
class PriorityQ():
def __init__(self):
self.nodes = []
def __str__(self):
return "Priorityq: " + str(list(self.nodes))
def put(self, cost, node):
heapq.heappush(self.nodes, (cost, node))
def get(self):
return heapq.heappop(self.nodes)[1]
def isEmpty(self):
return len(self.nodes) == 0
def heuristic(node1, node2):
# Pythagorean Threorem
a = node1.X - node2.X
b = node1.Y - node2.Y
return sqrt(a*a + b*b)//BLOCKSIZE
def escapeCheck():
for event in py.event.get():
if event.type == QUIT:
print("Quiting...")
py.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_SPACE:
return reset(True)
def reset(animating: bool):
while(True):
mouse = py.mouse.get_pos()
for event in py.event.get():
if event.type == QUIT:
print("Quiting...")
py.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_SPACE and animating:
btnRestart(GREY)
txtRestart(dark('Restart'))
py.display.update(updateUI)
return False
elif event.key == K_ESCAPE:
return True
# Reset if pressed
elif event.type == MOUSEBUTTONDOWN:
if btnRestart(GREY).collidepoint(mouse):
return True
if btnRestart(ORANGE).collidepoint(mouse):
btnRestart(LIGHT)
txtRestart(fntRestart)
py.display.update(updateUI)
def drawPath(g:Graph, root: Point, target: Point, path: dict):
if target in path:
py.draw.rect(SCREEN, YELLOW, (target, (BLOCKSIZE-1, BLOCKSIZE-1)))
currentNode = target + path[target]
while (currentNode != root):
py.draw.rect(SCREEN, GREEN, (currentNode, (BLOCKSIZE-1, BLOCKSIZE-1)))
if currentNode in g.weights:
py.draw.rect(SCREEN, g.weightColors[g.weights[currentNode]], (currentNode+Point(5,5), (BLOCKSIZE-11, BLOCKSIZE-11)))
py.time.delay(50)
py.display.update(updateScreen)
currentNode += path[currentNode]
def BFS(g: Graph, root: Point, target: Point):
greyOutButtons('BFS')
newNodes = deque()
newNodes.appendleft(root)
path = {}
path[root] = None
py.draw.rect(SCREEN, RED, (root, (BLOCKSIZE-1, BLOCKSIZE-1)))
py.draw.rect(SCREEN, YELLOW, (target, (BLOCKSIZE-1, BLOCKSIZE-1)))
py.display.update(updateScreen)
while (len(newNodes) > 0 and target not in path):
if escapeCheck(): return
node = newNodes.pop()
if node != root:
py.draw.rect(SCREEN, BLUE, (node, (BLOCKSIZE-1, BLOCKSIZE-1)))
py.time.delay(20)
py.display.update(updateScreen)
for adj in g.find_Adj(node):
if adj not in path:
newNodes.appendleft(adj)
path[adj] = node - adj
py.draw.rect(SCREEN, TURQUOISE, (adj, (BLOCKSIZE-1, BLOCKSIZE-1)))
py.time.delay(20)
py.display.update(updateScreen)
drawPath(g, root, target, path)
reset(False)
def DFS(g: Graph, root: Point, target: Point):
greyOutButtons('DFS')
newNodes = []
newNodes.append(root)
path = {}
path[root] = None
py.draw.rect(SCREEN, RED, (root, (BLOCKSIZE-1, BLOCKSIZE-1)))
py.draw.rect(SCREEN, YELLOW, (target, (BLOCKSIZE-1, BLOCKSIZE-1)))
py.display.update(updateScreen)
while (len(newNodes) > 0 and target not in path):
if escapeCheck(): return
node = newNodes.pop()
if node != root:
py.draw.rect(SCREEN, BLUE, (node, (BLOCKSIZE-1, BLOCKSIZE-1)))
py.time.delay(20)
py.display.update(updateScreen)
for adj in g.find_Adj(node):
if adj not in path:
newNodes.append(adj)
path[adj] = node - adj
py.draw.rect(SCREEN, TURQUOISE, (adj, (BLOCKSIZE-1, BLOCKSIZE-1)))
py.time.delay(20)
py.display.update(updateScreen)
drawPath(g, root, target, path)
reset(False)
def dijkstra (g: Graph, root: Point, target: Point):
greyOutButtons('Dijkstra')
newNodes = PriorityQ()
newNodes.put(0, root)
path = {}
path[root] = None
cost = {}
cost[root] = 0
py.draw.rect(SCREEN, RED, (root, (BLOCKSIZE-1, BLOCKSIZE-1)))
py.draw.rect(SCREEN, YELLOW, (target, (BLOCKSIZE-1, BLOCKSIZE-1)))
py.display.update(updateScreen)
while not newNodes.isEmpty():
if escapeCheck(): return
node = newNodes.get()
if node != root:
py.draw.rect(SCREEN, BLUE, (node, (BLOCKSIZE-1, BLOCKSIZE-1)))
if node in g.weights:
py.draw.rect(SCREEN, g.weightColors[g.weights[node]], (node.shrink(), (BLOCKSIZE-7, BLOCKSIZE-7)))
elif node == target:
py.draw.rect(SCREEN, YELLOW, (node.shrink(), (BLOCKSIZE-7, BLOCKSIZE-7)))
py.time.delay(20)
py.display.update(updateScreen)
if node == target:
break
for adj in g.find_Adj(node):
adjCost = cost[node] + g.cost(adj)
if adj not in cost or adjCost < cost[adj]:
cost[adj] = adjCost
newNodes.put(adjCost, adj)
path[adj] = node - adj
py.draw.rect(SCREEN, TURQUOISE, (adj, (BLOCKSIZE-1, BLOCKSIZE-1)))
if adj in g.weights:
py.draw.rect(SCREEN, g.weightColors[g.weights[adj]], (adj.shrink(), (BLOCKSIZE-7, BLOCKSIZE-7)))
elif adj == target:
py.draw.rect(SCREEN, YELLOW, (adj.shrink(), (BLOCKSIZE-7, BLOCKSIZE-7)))
py.time.delay(20)
py.display.update(updateScreen)
drawPath(g, root, target, path)
reset(False)
def aStar (g: Graph, root: Point, target: Point):
greyOutButtons('AStar')
newNodes = PriorityQ()
newNodes.put(0, root)
path = {}
path[root] = None
cost = {}
cost[root] = 0
py.draw.rect(SCREEN, RED, (root, (BLOCKSIZE-1, BLOCKSIZE-1)))
py.draw.rect(SCREEN, YELLOW, (target, (BLOCKSIZE-1, BLOCKSIZE-1)))
py.display.update(updateScreen)
while not newNodes.isEmpty():
if escapeCheck(): return
node = newNodes.get()
if node != root:
py.draw.rect(SCREEN, BLUE, (node, (BLOCKSIZE-1, BLOCKSIZE-1)))
if node in g.weights:
py.draw.rect(SCREEN, g.weightColors[g.weights[node]], (node.shrink(), (BLOCKSIZE-7, BLOCKSIZE-7)))
elif node == target:
py.draw.rect(SCREEN, YELLOW, (node.shrink(), (BLOCKSIZE-7, BLOCKSIZE-7)))
py.time.delay(20)
py.display.update(updateScreen)
if node == target:
break
for adj in g.find_Adj(node):
adjCost = cost[node] + g.cost(adj)
if adj not in cost or adjCost < cost[adj]:
cost[adj] = adjCost
newNodes.put(adjCost + heuristic(adj, target), adj)
path[adj] = node - adj
py.draw.rect(SCREEN, TURQUOISE, (adj, (BLOCKSIZE-1, BLOCKSIZE-1)))
if adj in g.weights:
py.draw.rect(SCREEN, g.weightColors[g.weights[adj]], (adj.shrink(), (BLOCKSIZE-7, BLOCKSIZE-7)))
elif adj == target:
py.draw.rect(SCREEN, YELLOW, (adj.shrink(), (BLOCKSIZE-7, BLOCKSIZE-7)))
py.time.delay(20)
py.display.update(updateScreen)
drawPath(g, root, target, path)
reset(False)