Skip to content

Commit 7893722

Browse files
A* Searching Algorithm Added with README file issue Resolved
1 parent 851975e commit 7893722

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

A_star_searching_algorithm/A_star_search.py

+24-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Python program for A* Search Algorithm
22
import math
33
import heapq
4+
from typing import List, Tuple
45

56
# Define the Cell class
67

78

89
class Cell:
9-
def __init__(self):
10+
def __init__(self)->None:
1011
# Parent cell's row index
1112
self.parent_i = 0
1213
# Parent cell's column index
@@ -26,31 +27,31 @@ def __init__(self):
2627
# Check if a cell is valid (within the grid)
2728

2829

29-
def is_valid(row, col):
30+
def is_valid(row: int, col: int) -> bool:
3031
return (row >= 0) and (row < ROW) and (col >= 0) and (col < COL)
3132

3233
# Check if a cell is unblocked
3334

3435

35-
def is_unblocked(grid, row, col):
36+
def is_unblocked(grid: List[List[int]], row: int, col: int) -> bool:
3637
return grid[row][col] == 1
3738

3839
# Check if a cell is the destination
3940

4041

41-
def is_destination(row, col, dest):
42+
def is_destination(row: int, col: int, dest: Tuple[int, int]) -> bool:
4243
return row == dest[0] and col == dest[1]
4344

4445
# Calculate the heuristic value of a cell (Euclidean distance to destination)
4546

4647

47-
def calculate_h_value(row, col, dest):
48+
def calculate_h_value(row: int, col: int, dest: Tuple[int, int]) -> float:
4849
return ((row - dest[0]) ** 2 + (col - dest[1]) ** 2) ** 0.5
4950

5051
# Trace the path from source to destination
5152

5253

53-
def trace_path(cell_details, dest):
54+
def trace_path(cell_details: List[List[Cell]], dest: Tuple[int, int]) -> None:
5455
print("The Path is ")
5556
path = []
5657
row = dest[0]
@@ -77,7 +78,7 @@ def trace_path(cell_details, dest):
7778
# Implement the A* search algorithm
7879

7980

80-
def a_star_search(grid, src, dest):
81+
def a_star_search(grid: List[List[int]], src: Tuple[int, int], dest: Tuple[int, int]) -> None:
8182
# Check if the source and destination are valid
8283
if not is_valid(src[0], src[1]) or not is_valid(dest[0], dest[1]):
8384
print("Source or destination is invalid")
@@ -167,7 +168,19 @@ def a_star_search(grid, src, dest):
167168
# Driver Code
168169

169170

170-
def main():
171+
def main() -> None:
172+
"""
173+
Run the A* search algorithm on a predefined grid.
174+
175+
Returns:
176+
None
177+
178+
Examples:
179+
>>> main()
180+
The destination cell is found
181+
The Path is
182+
-> (8, 0) -> (7, 1) -> (6, 0) -> (5, 1) -> (4, 0) -> (3, 1) -> (2, 0) -> (1, 1) -> (0, 0)
183+
"""
171184
# Define the grid (1 for unblocked, 0 for blocked)
172185
grid = [
173186
[1, 0, 1, 1, 1, 1, 0, 1, 1, 1],
@@ -182,12 +195,12 @@ def main():
182195
]
183196

184197
# Define the source and destination
185-
src = [8, 0]
186-
dest = [0, 0]
198+
src = (8, 0)
199+
dest = (0, 0)
187200

188201
# Run the A* search algorithm
189202
a_star_search(grid, src, dest)
190203

191-
192204
if __name__ == "__main__":
193205
main()
206+

A_star_searching_algorithm/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)