Skip to content

Commit 13e3eb8

Browse files
committed
Flyod-Warshall algorithm, code cleanup.
1 parent 8bf89df commit 13e3eb8

16 files changed

+181
-174
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ After an algorithm has run:
4141
- <kbd>W</kbd>, <kbd>Left Click</kbd>, or <kbd>Right Click</kbd> to clear the Algorithm Mark-up
4242

4343
While an algorithm is running:
44+
4445
- Press <kbd>S</kbd> key to stop the algorithm
4546

4647
## Node Types
@@ -56,7 +57,7 @@ While an algorithm is running:
5657
## Algorithm Progress
5758

5859
```
59-
Currently implemented algorithms (11/33):
60+
Currently implemented algorithms (12/33):
6061
6162
- A*
6263
- Beam Search
@@ -66,6 +67,7 @@ Currently implemented algorithms (11/33):
6667
- B*
6768
- Depth First Search (DFS)
6869
- Dijkstra's Algorithms
70+
- Floyd-Warshall's algorithm
6971
- Greedy Best First Search (GBFS)
7072
- Random Walk
7173
- Theta*
@@ -86,7 +88,6 @@ Planned algorithms (Going to look at them):
8688
- Fast Iterative Method
8789
- Fast Marching Method
8890
- Fast Sweeping Method
89-
- Floyd-Warshall's algorithm
9091
- Fringe search
9192
9293
- Greedy Best Line Search

main/Algorithms.py

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from algorithms.BStar import b_star
77
from algorithms.DFS import dfs
88
from algorithms.Dijkstra import dijkstra
9+
from algorithms.FloydWarshall import floyd_warshall
910
from algorithms.GBFS import gbfs
1011
from algorithms.RandomWalk import rand_walk
1112
from algorithms.ThetaStar import theta_star
@@ -19,6 +20,7 @@
1920
"bstar",
2021
"dfs",
2122
"dijkstra",
23+
"floyd",
2224
"gbfs",
2325
"rand",
2426
"theta",
@@ -52,6 +54,8 @@ def algorithm(self, algorithm):
5254
dfs(self.draw, self.start, self.end)
5355
case "dijkstra":
5456
dijkstra(self.draw, self.start, self.end)
57+
case "floyd":
58+
floyd_warshall(self.draw, self.start, self.end, self.grid)
5559
case "gbfs":
5660
gbfs(self.draw, self.start, self.end)
5761
case "rand":

main/algorithms/AStar.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pygame
22
from queue import PriorityQueue
3-
from .RP import reconstruct_path, heuristic
3+
from .RP import reconstruct_path, heuristic, check
44

55

66
def a_star(draw, grid, start, end):
@@ -20,11 +20,7 @@ def a_star(draw, grid, start, end):
2020
run = True
2121

2222
while not open_set.empty() and run:
23-
for event in pygame.event.get():
24-
if event.type == pygame.QUIT:
25-
pygame.quit()
26-
if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
27-
run = False
23+
run = check(pygame.event.get(), run)
2824

2925
current = open_set.get()[2]
3026
open_set_hash.remove(current)

main/algorithms/BFS.py

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pygame
22
from queue import Queue
3-
from .RP import reconstruct_path
3+
from .RP import reconstruct_path, check, markup
44

55
"""
66
1. Add root node to the queue, and mark it as visited(already explored).
@@ -24,24 +24,14 @@ def bfs(draw, start, end):
2424
run = True
2525

2626
while nodes and not found and run:
27-
for event in pygame.event.get():
28-
if event.type == pygame.QUIT:
29-
pygame.quit()
30-
if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
31-
run = False
27+
run = check(pygame.event.get(), run)
3228

3329
current = nodes.pop(0)
3430

3531
if current.is_checked():
3632
continue
3733

38-
if not current.is_start() and not current.is_end():
39-
current.uncheck()
40-
41-
draw()
42-
43-
if not current.is_start() and not current.is_end():
44-
current.check()
34+
markup(draw, current)
4535

4636
for neighbor in current.neighbors:
4737
if not neighbor.is_checked():

main/algorithms/BStar.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pygame
22
import random
33
from queue import PriorityQueue
4-
from .RP import reconstruct_path, heuristic
4+
from .RP import reconstruct_path, heuristic, check
55

66

77
def b_star(draw, grid, start, end):
@@ -21,11 +21,7 @@ def b_star(draw, grid, start, end):
2121
run = True
2222

2323
while not open_set.empty() and run:
24-
for event in pygame.event.get():
25-
if event.type == pygame.QUIT:
26-
pygame.quit()
27-
if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
28-
run = False
24+
run = check(pygame.event.get(), run)
2925

3026
current = open_set.get()[2]
3127
open_set_hash.remove(current)

main/algorithms/BeamSearch.py

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pygame
22
import heapq
3-
from .RP import reconstruct_path, heuristic
3+
from .RP import reconstruct_path, heuristic, check, markup
44

55

66
def beam_search(draw, start, end, beam_size):
@@ -14,11 +14,7 @@ def beam_search(draw, start, end, beam_size):
1414

1515
# Continue searching until the beam is empty
1616
while beam and run:
17-
for event in pygame.event.get():
18-
if event.type == pygame.QUIT:
19-
pygame.quit()
20-
if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
21-
run = False
17+
run = check(pygame.event.get(), run)
2218
# Take the most promising node from the beam
2319
_, current = heapq.heappop(beam)
2420

@@ -28,13 +24,7 @@ def beam_search(draw, start, end, beam_size):
2824
end.make_end()
2925
break
3026

31-
if not current.is_start() and not current.is_end():
32-
current.uncheck()
33-
34-
draw()
35-
36-
if not current.is_start() and not current.is_end():
37-
current.check()
27+
markup(draw, current)
3828

3929
# Expand the node and add its children to the beam
4030
children = current.neighbors

main/algorithms/BellmanFord.py

+4-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pygame
22
from queue import Queue
3-
from .RP import reconstruct_path, get_unvisited_nodes
3+
from .RP import reconstruct_path, get_unvisited_nodes, check, markup
44

55

66
def bell_ford(draw, start, end, accuracy):
@@ -14,29 +14,14 @@ def bell_ford(draw, start, end, accuracy):
1414
run = True
1515

1616
while counter >= 0 and run:
17-
for event in pygame.event.get():
18-
if event.type == pygame.QUIT:
19-
pygame.quit()
20-
if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
21-
run = False
17+
run = check(pygame.event.get(), run)
2218

2319
for current in nodes:
2420
if not run:
2521
break
26-
for event in pygame.event.get():
27-
if event.type == pygame.QUIT:
28-
pygame.quit()
29-
if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
30-
run = False
31-
break
22+
run = check(pygame.event.get(), run)
3223

33-
if not current.is_start() and not current.is_end():
34-
current.uncheck()
35-
36-
draw()
37-
38-
if not current.is_start() and not current.is_end():
39-
current.check()
24+
markup(draw, current)
4025

4126
for neighbor in current.neighbors:
4227
if distance[current] + 1 < distance[neighbor]:

main/algorithms/BestFS.py

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pygame
22
from collections import deque
3-
from .RP import heuristic
3+
from .RP import heuristic, check, markup
44

55

66
def get_checked_neighbors(node):
@@ -31,11 +31,7 @@ def best_fs(draw, start, end):
3131
run = True
3232

3333
while len(queue) > 0 and run:
34-
for event in pygame.event.get():
35-
if event.type == pygame.QUIT:
36-
pygame.quit()
37-
if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
38-
run = False
34+
run = check(pygame.event.get(), run)
3935

4036
current = min(queue, key=lambda x: costs[x] + heuristic("manhattan", x, end))
4137
queue.remove(current)
@@ -45,14 +41,7 @@ def best_fs(draw, start, end):
4541
end.make_end()
4642
break
4743

48-
# Drawing stuff
49-
if current != start:
50-
current.uncheck()
51-
52-
draw()
53-
54-
if current != start:
55-
current.check()
44+
markup(draw, current)
5645

5746
for neighbor in current.neighbors:
5847
cost = costs[current] + 1

main/algorithms/DFS.py

+3-35
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
import pygame
2-
from .RP import reconstruct_path
3-
4-
"""
5-
Recursive
6-
1. Mark the current node as visited(initially current node is the root node)
7-
2. Check if current node is the goal, If so, then return it.
8-
3. Iterate over children nodes of current node, and do the following:
9-
1. Check if a child node is not visited.
10-
2. If so, then, mark it as visited.
11-
3. Go to it's sub tree recursively until you find the goal node(In other words, do the same steps here passing the child node as the current node in the next recursive call).
12-
4. If the child node has the goal node in this sub tree, then, return it.
13-
3. If goal node is not found, then goal node is not in the tree!
14-
15-
16-
Iterative
17-
1. Add root node to the stack.
18-
2. Loop on the stack as long as it's not empty.
19-
1. Get the node at the top of the stack(current), mark it as visited, and remove it.
20-
2. For every non-visited child of the current node, do the following:
21-
1. Check if it's the goal node, If so, then return this child node.
22-
2. Otherwise, push it to the stack.
23-
3. If stack is empty, then goal node was not found!
24-
"""
2+
from .RP import reconstruct_path, check, markup
253

264

275
def dfs(draw, start, end):
@@ -32,25 +10,15 @@ def dfs(draw, start, end):
3210
run = True
3311

3412
while len(stack) and not found and run:
35-
for event in pygame.event.get():
36-
if event.type == pygame.QUIT:
37-
pygame.quit()
38-
if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
39-
run = False
13+
run = check(pygame.event.get(), run)
4014

4115
current = stack.pop()
4216

4317
if current.is_checked():
4418
continue
4519

4620
# Drawing stuff
47-
if current != start:
48-
current.uncheck()
49-
50-
draw()
51-
52-
if current != start:
53-
current.check()
21+
markup(draw, current)
5422

5523
# Find unvisited neighbors and check for end
5624
for neighbor in current.neighbors:

main/algorithms/Dijkstra.py

+3-29
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
import pygame
22
from queue import PriorityQueue, Queue
3-
from .RP import reconstruct_path, get_unvisited_nodes
3+
from .RP import reconstruct_path, get_unvisited_nodes, check, markup
44

5-
"""
6-
Algorithm
7-
8-
for each vertex in graph:
9-
distance[vertex] = inf
10-
if vertex != start, add vertex to Priority Queue (unvisited nodes)
11-
distance[start] = 0
12-
13-
while the queue is not empty:
14-
U = min from queue
15-
for each unvisited neighbor vertex of U
16-
tempDistance = distance[U] + edge_weight(U, vertex)
17-
if tempDistance < distance[vertex]:
18-
distance[vertex] = tempDistance
19-
previous[vertex] = U
20-
21-
"""
225

236
# TODO: Refactor using a priority queue
247
# Make distance a priority queue (node, inf)
@@ -36,11 +19,7 @@ def dijkstra(draw, start, end):
3619
run = True
3720

3821
while unvisited_nodes and run:
39-
for event in pygame.event.get():
40-
if event.type == pygame.QUIT:
41-
pygame.quit()
42-
if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
43-
run = False
22+
run = check(pygame.event.get(), run)
4423

4524
# Choose node with smallest distance
4625
current_min = min(unvisited_nodes, key=distance.get)
@@ -53,19 +32,14 @@ def dijkstra(draw, start, end):
5332
for neighbor in current_min.neighbors:
5433
# Don't recheck for performance
5534
if not neighbor.is_checked():
56-
if not neighbor.is_start() and not neighbor.is_end():
57-
neighbor.uncheck()
5835
# edges between vertecies are not weighted
5936
# (using constant weight of 1)
6037
temp_value = distance[current_min] + 1
6138
if temp_value < distance[neighbor]:
6239
distance[neighbor] = temp_value
6340
previous[neighbor] = current_min
6441

65-
draw()
66-
67-
if not neighbor.is_start() and not neighbor.is_end():
68-
neighbor.check()
42+
markup(draw, neighbor)
6943

7044
unvisited_nodes.remove(current_min)
7145

0 commit comments

Comments
 (0)