Skip to content

Commit a472dad

Browse files
A* Searching Algorithm Added with README file issuse resolved
2 parents 7893722 + 04d1c19 commit a472dad

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

A_star_searching_algorithm/A_star_search.py

+44-13
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@
77

88

99
class Cell:
10+
<<<<<<< HEAD
1011
def __init__(self)->None:
1112
# Parent cell's row index
13+
=======
14+
def __init__(self):
15+
# Parent cell's row index
16+
>>>>>>> 04d1c193f3be0847c2a2d643aff6107aa52146ff
1217
self.parent_i = 0
13-
# Parent cell's column index
18+
# Parent cell's column index
1419
self.parent_j = 0
15-
# Total cost of the cell (g + h)
16-
self.f = float('inf')
17-
# Cost from start to this cell
18-
self.g = float('inf')
19-
# Heuristic cost from this cell to destination
20+
# Total cost of the cell (g + h)
21+
self.f = float("inf")
22+
# Cost from start to this cell
23+
self.g = float("inf")
24+
# Heuristic cost from this cell to destination
2025
self.h = 0
2126

2227

@@ -30,24 +35,28 @@ def __init__(self)->None:
3035
def is_valid(row: int, col: int) -> bool:
3136
return (row >= 0) and (row < ROW) and (col >= 0) and (col < COL)
3237

38+
3339
# Check if a cell is unblocked
3440

3541

3642
def is_unblocked(grid: List[List[int]], row: int, col: int) -> bool:
3743
return grid[row][col] == 1
3844

45+
3946
# Check if a cell is the destination
4047

4148

4249
def is_destination(row: int, col: int, dest: Tuple[int, int]) -> bool:
4350
return row == dest[0] and col == dest[1]
4451

52+
4553
# Calculate the heuristic value of a cell (Euclidean distance to destination)
4654

4755

4856
def calculate_h_value(row: int, col: int, dest: Tuple[int, int]) -> float:
4957
return ((row - dest[0]) ** 2 + (col - dest[1]) ** 2) ** 0.5
5058

59+
5160
# Trace the path from source to destination
5261

5362

@@ -58,7 +67,10 @@ def trace_path(cell_details: List[List[Cell]], dest: Tuple[int, int]) -> None:
5867
col = dest[1]
5968

6069
# Trace the path from destination to source using parent cells
61-
while not (cell_details[row][col].parent_i == row and cell_details[row][col].parent_j == col):
70+
while not (
71+
cell_details[row][col].parent_i == row
72+
and cell_details[row][col].parent_j == col
73+
):
6274
path.append((row, col))
6375
temp_row = cell_details[row][col].parent_i
6476
temp_col = cell_details[row][col].parent_j
@@ -75,6 +87,7 @@ def trace_path(cell_details: List[List[Cell]], dest: Tuple[int, int]) -> None:
7587
print("->", i, end=" ")
7688
print()
7789

90+
7891
# Implement the A* search algorithm
7992

8093

@@ -85,7 +98,9 @@ def a_star_search(grid: List[List[int]], src: Tuple[int, int], dest: Tuple[int,
8598
return
8699

87100
# Check if the source and destination are unblocked
88-
if not is_unblocked(grid, src[0], src[1]) or not is_unblocked(grid, dest[0], dest[1]):
101+
if not is_unblocked(grid, src[0], src[1]) or not is_unblocked(
102+
grid, dest[0], dest[1]
103+
):
89104
print("Source or the destination is blocked")
90105
return
91106

@@ -126,14 +141,26 @@ def a_star_search(grid: List[List[int]], src: Tuple[int, int], dest: Tuple[int,
126141
closed_list[i][j] = True
127142

128143
# For each direction, check the successors
129-
directions = [(0, 1), (0, -1), (1, 0), (-1, 0),
130-
(1, 1), (1, -1), (-1, 1), (-1, -1)]
144+
directions = [
145+
(0, 1),
146+
(0, -1),
147+
(1, 0),
148+
(-1, 0),
149+
(1, 1),
150+
(1, -1),
151+
(-1, 1),
152+
(-1, -1),
153+
]
131154
for dir in directions:
132155
new_i = i + dir[0]
133156
new_j = j + dir[1]
134157

135158
# If the successor is valid, unblocked, and not visited
136-
if is_valid(new_i, new_j) and is_unblocked(grid, new_i, new_j) and not closed_list[new_i][new_j]:
159+
if (
160+
is_valid(new_i, new_j)
161+
and is_unblocked(grid, new_i, new_j)
162+
and not closed_list[new_i][new_j]
163+
):
137164
# If the successor is the destination
138165
if is_destination(new_i, new_j, dest):
139166
# Set the parent of the destination cell
@@ -151,7 +178,10 @@ def a_star_search(grid: List[List[int]], src: Tuple[int, int], dest: Tuple[int,
151178
f_new = g_new + h_new
152179

153180
# If the cell is not in the open list or the new f value is smaller
154-
if cell_details[new_i][new_j].f == float('inf') or cell_details[new_i][new_j].f > f_new:
181+
if (
182+
cell_details[new_i][new_j].f == float("inf")
183+
or cell_details[new_i][new_j].f > f_new
184+
):
155185
# Add the cell to the open list
156186
heapq.heappush(open_list, (f_new, new_i, new_j))
157187
# Update the cell details
@@ -165,6 +195,7 @@ def a_star_search(grid: List[List[int]], src: Tuple[int, int], dest: Tuple[int,
165195
if not found_dest:
166196
print("Failed to find the destination cell")
167197

198+
168199
# Driver Code
169200

170201

@@ -191,7 +222,7 @@ def main() -> None:
191222
[1, 0, 1, 1, 1, 1, 0, 1, 0, 0],
192223
[1, 0, 0, 0, 0, 1, 0, 0, 0, 1],
193224
[1, 0, 1, 1, 1, 1, 0, 1, 1, 1],
194-
[1, 1, 1, 0, 0, 0, 1, 0, 0, 1]
225+
[1, 1, 1, 0, 0, 0, 1, 0, 0, 1],
195226
]
196227

197228
# 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)