Skip to content

Commit 04d1c19

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 851975e commit 04d1c19

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

A_star_searching_algorithm/A_star_search.py

+40-14
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
class Cell:
99
def __init__(self):
10-
# Parent cell's row index
10+
# Parent cell's row index
1111
self.parent_i = 0
12-
# Parent cell's column index
12+
# Parent cell's column index
1313
self.parent_j = 0
14-
# Total cost of the cell (g + h)
15-
self.f = float('inf')
16-
# Cost from start to this cell
17-
self.g = float('inf')
18-
# Heuristic cost from this cell to destination
14+
# Total cost of the cell (g + h)
15+
self.f = float("inf")
16+
# Cost from start to this cell
17+
self.g = float("inf")
18+
# Heuristic cost from this cell to destination
1919
self.h = 0
2020

2121

@@ -29,24 +29,28 @@ def __init__(self):
2929
def is_valid(row, col):
3030
return (row >= 0) and (row < ROW) and (col >= 0) and (col < COL)
3131

32+
3233
# Check if a cell is unblocked
3334

3435

3536
def is_unblocked(grid, row, col):
3637
return grid[row][col] == 1
3738

39+
3840
# Check if a cell is the destination
3941

4042

4143
def is_destination(row, col, dest):
4244
return row == dest[0] and col == dest[1]
4345

46+
4447
# Calculate the heuristic value of a cell (Euclidean distance to destination)
4548

4649

4750
def calculate_h_value(row, col, dest):
4851
return ((row - dest[0]) ** 2 + (col - dest[1]) ** 2) ** 0.5
4952

53+
5054
# Trace the path from source to destination
5155

5256

@@ -57,7 +61,10 @@ def trace_path(cell_details, dest):
5761
col = dest[1]
5862

5963
# Trace the path from destination to source using parent cells
60-
while not (cell_details[row][col].parent_i == row and cell_details[row][col].parent_j == col):
64+
while not (
65+
cell_details[row][col].parent_i == row
66+
and cell_details[row][col].parent_j == col
67+
):
6168
path.append((row, col))
6269
temp_row = cell_details[row][col].parent_i
6370
temp_col = cell_details[row][col].parent_j
@@ -74,6 +81,7 @@ def trace_path(cell_details, dest):
7481
print("->", i, end=" ")
7582
print()
7683

84+
7785
# Implement the A* search algorithm
7886

7987

@@ -84,7 +92,9 @@ def a_star_search(grid, src, dest):
8492
return
8593

8694
# Check if the source and destination are unblocked
87-
if not is_unblocked(grid, src[0], src[1]) or not is_unblocked(grid, dest[0], dest[1]):
95+
if not is_unblocked(grid, src[0], src[1]) or not is_unblocked(
96+
grid, dest[0], dest[1]
97+
):
8898
print("Source or the destination is blocked")
8999
return
90100

@@ -125,14 +135,26 @@ def a_star_search(grid, src, dest):
125135
closed_list[i][j] = True
126136

127137
# For each direction, check the successors
128-
directions = [(0, 1), (0, -1), (1, 0), (-1, 0),
129-
(1, 1), (1, -1), (-1, 1), (-1, -1)]
138+
directions = [
139+
(0, 1),
140+
(0, -1),
141+
(1, 0),
142+
(-1, 0),
143+
(1, 1),
144+
(1, -1),
145+
(-1, 1),
146+
(-1, -1),
147+
]
130148
for dir in directions:
131149
new_i = i + dir[0]
132150
new_j = j + dir[1]
133151

134152
# If the successor is valid, unblocked, and not visited
135-
if is_valid(new_i, new_j) and is_unblocked(grid, new_i, new_j) and not closed_list[new_i][new_j]:
153+
if (
154+
is_valid(new_i, new_j)
155+
and is_unblocked(grid, new_i, new_j)
156+
and not closed_list[new_i][new_j]
157+
):
136158
# If the successor is the destination
137159
if is_destination(new_i, new_j, dest):
138160
# Set the parent of the destination cell
@@ -150,7 +172,10 @@ def a_star_search(grid, src, dest):
150172
f_new = g_new + h_new
151173

152174
# If the cell is not in the open list or the new f value is smaller
153-
if cell_details[new_i][new_j].f == float('inf') or cell_details[new_i][new_j].f > f_new:
175+
if (
176+
cell_details[new_i][new_j].f == float("inf")
177+
or cell_details[new_i][new_j].f > f_new
178+
):
154179
# Add the cell to the open list
155180
heapq.heappush(open_list, (f_new, new_i, new_j))
156181
# Update the cell details
@@ -164,6 +189,7 @@ def a_star_search(grid, src, dest):
164189
if not found_dest:
165190
print("Failed to find the destination cell")
166191

192+
167193
# Driver Code
168194

169195

@@ -178,7 +204,7 @@ def main():
178204
[1, 0, 1, 1, 1, 1, 0, 1, 0, 0],
179205
[1, 0, 0, 0, 0, 1, 0, 0, 0, 1],
180206
[1, 0, 1, 1, 1, 1, 0, 1, 1, 1],
181-
[1, 1, 1, 0, 0, 0, 1, 0, 0, 1]
207+
[1, 1, 1, 0, 0, 0, 1, 0, 0, 1],
182208
]
183209

184210
# Define the source and destination

A_star_searching_algorithm/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ if __name__ == "__main__":
255255
***`output:`***
256256
```bash
257257
The destination cell is found
258-
The Path is
259-
-> (8, 0) -> (7, 0) -> (6, 0) -> (5, 0) -> (4, 1) -> (3, 2) -> (2, 1) -> (1, 0) -> (0, 0)
258+
The Path is
259+
-> (8, 0) -> (7, 0) -> (6, 0) -> (5, 0) -> (4, 1) -> (3, 2) -> (2, 1) -> (1, 0) -> (0, 0)
260260
```
261261
262262
- ***Time Complexity:*** O(E log V), where E is the number of edges and V is the number of vertices, due to the use of the priority queue.

0 commit comments

Comments
 (0)