-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.py
More file actions
213 lines (183 loc) · 6.06 KB
/
dijkstra.py
File metadata and controls
213 lines (183 loc) · 6.06 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
207
208
209
210
211
212
213
import pygame
from queue import PriorityQueue
import sys
DISPLAY_SIZE = 800
NUMBER_OF_ROWS = 40
display = pygame.display.set_mode((DISPLAY_SIZE, DISPLAY_SIZE))
pygame.display.set_caption("Pathfinding visualizer (Dijkstra's algorithm)")
# colours from https://www.webucator.com/blog/2015/03/python-color-constants-module/
VIOLETRED = (208,32,144)
VIOLETRED4 = (139,34,82)
DEEPSKYBLUE3 = (0,154,205)
DEEPSKYBLUE4 = (0,104,139)
SNOW3 = (205,201,201)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
class Node:
# Node constructor
def __init__(self, row, col, blockSize):
self.row = row
self.col = col
self.x = row * blockSize
self.y = col * blockSize
self.blockSize = blockSize
self.colour = SNOW3
def isNotBarrier(self):
return self.colour != BLACK
def setColour(self, colour):
self.colour = colour
def isNotDefaultColour(self):
return self.colour != SNOW3
# we will only redraw the node (not the whole display)
def drawNode(self, display):
rect = pygame.Rect(self.x, self.y, self.blockSize, self.blockSize)
pygame.draw.rect(display, self.colour, rect)
pygame.draw.rect(display, BLACK, rect, 1)
pygame.display.update(rect)
# basic defintion to compare nodes (ie. just return True)
def __lt__(self, other):
return True
def makeGrid(display, size, numRows):
grid = []
blockSize = size // numRows
for i in range(numRows):
grid.append([])
for j in range(numRows):
node = Node(i, j, blockSize)
node.drawNode(display)
grid[i].append(node)
return grid
def resetGrid(display, grid):
for row in grid:
for node in row:
if node.isNotDefaultColour():
node.setColour(SNOW3)
node.drawNode(display)
def getMousePosition(position, size, numRows):
blockSize = size // numRows
y, x = position
return y // blockSize, x // blockSize
def resetSearchingAnimation(display, size, startNode, endNode, grid):
for row in grid:
for node in row:
if node.isNotDefaultColour() and node.isNotBarrier() and node is not startNode and node is not endNode:
node.setColour(SNOW3)
node.drawNode(display)
def dijkstrasAlgorithm(display, size, startNode, endNode, grid):
queue = PriorityQueue()
queue.put((0, startNode))
previousNode = {}
distanceOfNode = {}
for row in grid:
for node in row:
distanceOfNode[node] = float("inf")
distanceOfNode[startNode] = 0
visited = {startNode}
clock = pygame.time.Clock()
while not queue.empty():
node = queue.get()[1]
visited.remove(node)
if node == endNode:
resetSearchingAnimation(display, size, startNode, endNode, grid)
path = []
node = previousNode[endNode]
while node is not startNode:
path.append(node)
node = previousNode[node]
path = list(reversed(path))
return path
listOfNeighbours = []
# up
if node.row - 1 >= 0 and grid[node.row - 1][node.col].isNotBarrier():
listOfNeighbours.append(grid[node.row - 1][node.col])
# down
if node.row + 1 < NUMBER_OF_ROWS and grid[node.row + 1][node.col].isNotBarrier():
listOfNeighbours.append(grid[node.row + 1][node.col])
# left
if node.col - 1 >= 0 and grid[node.row][node.col - 1].isNotBarrier():
listOfNeighbours.append(grid[node.row][node.col - 1])
# right
if node.col + 1 < NUMBER_OF_ROWS and grid[node.row][node.col + 1].isNotBarrier():
listOfNeighbours.append(grid[node.row][node.col + 1])
for neighbour in listOfNeighbours:
distance = distanceOfNode[node] + 1
if (distance < distanceOfNode[neighbour]):
previousNode[neighbour] = node
distanceOfNode[neighbour] = distance
if neighbour not in visited:
queue.put((distance, neighbour))
visited.add(neighbour)
if neighbour is not endNode:
neighbour.setColour(DEEPSKYBLUE4)
neighbour.drawNode(display)
if node is not startNode:
node.setColour(DEEPSKYBLUE3)
node.drawNode(display)
clock.tick(60)
def drawPath(display, size, grid, path, colour, animationTime):
for node in path:
node.setColour(colour)
node.drawNode(display)
pygame.time.wait(animationTime)
def main(display, size, numRows):
pygame.init()
grid = makeGrid(display, size, numRows)
startNode = None
endNode = None
path = []
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
# check if the user exits the pygame display
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# check if the user clicks enter key
if event.type == pygame.KEYDOWN:
# start finding the path
if event.key == pygame.K_RETURN:
path = dijkstrasAlgorithm(display, size, startNode, endNode, grid)
drawPath(display, size, grid, path, YELLOW, 50)
# rewatch the animation
elif event.key == pygame.K_p:
drawPath(display, size, grid, path, SNOW3, 0)
dijkstrasAlgorithm(display, size, startNode, endNode, grid)
drawPath(display, size, grid, path, YELLOW, 50)
# reset the display
elif event.key == pygame.K_r:
for row in grid:
for node in row:
resetGrid(display, grid)
startNode = None
endNode = None
# check if the user left or right clicks the mouse
leftClick = pygame.mouse.get_pressed()[0]
rightClick = pygame.mouse.get_pressed()[2]
if leftClick or rightClick:
position = pygame.mouse.get_pos()
row, col = getMousePosition(position, size, numRows)
node = grid[row][col]
# left click
if leftClick:
if startNode is None and node is not endNode:
node.setColour(VIOLETRED)
node.drawNode(display)
startNode = node
elif endNode is None and node is not startNode:
node.setColour(VIOLETRED4)
node.drawNode(display)
endNode = node
elif node is not startNode and node is not endNode:
node.setColour(BLACK)
node.drawNode(display)
# right click
else:
if node is startNode:
startNode = None
elif node is endNode:
endNode = None
node.setColour(SNOW3)
node.drawNode(display)
clock.tick(60)
if __name__ == "__main__":
main(display, DISPLAY_SIZE, NUMBER_OF_ROWS)