Skip to content

Commit 05760ad

Browse files
committed
add dfs
1 parent acc5909 commit 05760ad

File tree

6 files changed

+100
-2
lines changed

6 files changed

+100
-2
lines changed

gifs/astar.gif

428 KB
Loading
File renamed without changes.

src/a_star.py

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,85 @@
1-
def a_star():
1+
import sys
2+
import numpy as np
3+
from config import config
4+
from collections import deque
5+
import time
6+
import pygame
7+
from queue import PriorityQueue
8+
import copy
9+
10+
11+
def is_valid(mat, row, col):
12+
M, N = mat.shape
13+
return (row >= 0) and (row < M) and (col >= 0) and (col < N) \
14+
and mat[row][col] == 0
15+
16+
17+
def manhattan_distance(x1, y1, x2, y2):
18+
return abs(x1 - x2) + abs(y1 - y2)
19+
20+
21+
def a_star(win, startEnd, walls):
22+
start, end = startEnd
23+
mat = np.zeros([config['board']['w'], config['board']['h']])
24+
for i in walls:
25+
mat[i] = 1
26+
27+
# explore 4 neighbors
28+
row = [-1, 0, 0, 1]
29+
col = [0, -1, 1, 0]
30+
31+
q = PriorityQueue()
32+
count = 0
33+
q.put((0, count, start))
34+
g_score = {}
35+
for i in range(mat.shape[0]):
36+
for j in range(mat.shape[1]):
37+
g_score[(i, j)] = float('inf')
38+
39+
f_score = copy.deepcopy(g_score)
40+
g_score[start] = 0
41+
f_score[start] = manhattan_distance(*start, *end)
42+
q_hash = {start}
43+
came_from = {}
44+
while not q.empty():
45+
for event in pygame.event.get():
46+
if event.type == pygame.QUIT:
47+
pygame.quit()
48+
current = q.get()[2]
49+
q_hash.remove(current)
50+
51+
for k in range(4):
52+
coordinate = (current[0] + row[k], current[1] + col[k])
53+
54+
if is_valid(mat, *coordinate):
55+
temp_g_score = g_score[current] + 1
56+
if temp_g_score < g_score[coordinate]:
57+
came_from[coordinate] = current
58+
g_score[coordinate] = temp_g_score
59+
f_score[coordinate] = temp_g_score + manhattan_distance(*coordinate, *end)
60+
if coordinate not in q_hash:
61+
count += 1
62+
q_hash.add(coordinate)
63+
q.put((f_score[coordinate], count, coordinate))
64+
win.write('*', *coordinate, fgcolor='green')
65+
pygame.time.wait(5)
66+
if current == end:
67+
count = 0
68+
win.write('@', *end)
69+
while current in came_from:
70+
for event in pygame.event.get():
71+
if event.type == pygame.QUIT:
72+
pygame.quit()
73+
current = came_from[current]
74+
win.write('+', *current, fgcolor='red')
75+
count += 1
76+
pygame.time.wait(20)
77+
win.write('@', *start)
78+
win.write(f"The shortest path from source to destination has length {count}", 1, 1)
79+
break
80+
win.write("Destination can't be reached from a given source", 1, 1)
81+
82+
83+
84+
85+

src/bfs.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from config import config
44
from collections import deque
55
import time
6+
import pygame
67

78

89
def is_valid(mat, visited, row, col):
@@ -33,6 +34,10 @@ def bfs(win, startEnd, walls):
3334
min_dist = sys.maxsize
3435

3536
while q:
37+
for event in pygame.event.get():
38+
# Quit if the user closes the window.
39+
if event.type == pygame.QUIT:
40+
return
3641
(start_x, start_y, dist, add) = q.popleft()
3742
if start_x == end_x and start_y == end_y:
3843
min_dist = dist
@@ -50,10 +55,15 @@ def bfs(win, startEnd, walls):
5055
# find path
5156
start_x, start_y = startEnd[0]
5257
for i in range(len(add)):
58+
for event in pygame.event.get():
59+
# Quit if the user closes the window.
60+
if event.type == pygame.QUIT:
61+
return
5362
index = move.index(add[i])
5463
start_x, start_y = start_x + row[index], start_y + col[index]
5564
win.write('+', start_x, start_y, fgcolor='red')
5665
time.sleep(0.02)
66+
5767
win.write('@', end_x, end_y)
5868
win.write(f"The shortest path from source to destination has length {min_dist}", 1, 1)
5969
else:

src/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
'w': 50,
44
'h': 50
55
},
6-
"algo": 'dfs'
6+
"algo": 'astar'
77
}

src/drawer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import time
99
from bfs import bfs
1010
from dfs import dfs
11+
from a_star import a_star
1112

1213

1314
def drawer():
@@ -45,6 +46,9 @@ def drawer():
4546
bfs(win, startEnd, walls)
4647
if config['algo'] == 'dfs':
4748
dfs(win, startEnd, walls)
49+
if config['algo'] == 'astar':
50+
a_star(win, startEnd, walls)
51+
4852

4953

5054
if __name__ == "__main__":

0 commit comments

Comments
 (0)