Skip to content

Commit b3a829d

Browse files
Issue Resolved
2 parents 6135d44 + 65c3821 commit b3a829d

File tree

1 file changed

+128
-4
lines changed

1 file changed

+128
-4
lines changed

searches/A_star_search.py

+128-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88

99
class Cell:
10-
def __init__(self)->None:
11-
# Parent cell's row index
10+
def __init__(self) -> None:
11+
# Parent cell's row index
1212
self.parent_i = 0
1313
# Parent cell's column index
1414
self.parent_j = 0
@@ -28,34 +28,134 @@ def __init__(self)->None:
2828

2929

3030
def is_valid(row: int, col: int) -> bool:
31+
"""
32+
Check if a cell is within the grid bounds.
33+
34+
Args:
35+
row (int): The row index of the cell.
36+
col (int): The column index of the cell.
37+
38+
Returns:
39+
bool: True if the cell is within bounds, False otherwise.
40+
41+
Examples:
42+
>>> is_valid(5, 3)
43+
True
44+
>>> is_valid(9, 5)
45+
False
46+
>>> is_valid(-1, 0)
47+
False
48+
"""
3149
return (row >= 0) and (row < ROW) and (col >= 0) and (col < COL)
3250

3351

3452
# Check if a cell is unblocked
3553

3654

3755
def is_unblocked(grid: List[List[int]], row: int, col: int) -> bool:
56+
"""
57+
Check if a cell is unblocked in the grid.
58+
59+
Args:
60+
grid (List[List[int]]): The grid representing the map.
61+
row (int): The row index of the cell.
62+
col (int): The column index of the cell.
63+
64+
Returns:
65+
bool: True if the cell is unblocked (1), False if blocked (0).
66+
67+
Examples:
68+
>>> grid = [
69+
... [1, 0, 1],
70+
... [1, 1, 0],
71+
... [0, 1, 1]
72+
... ]
73+
>>> is_unblocked(grid, 0, 0)
74+
True
75+
>>> is_unblocked(grid, 0, 1)
76+
False
77+
>>> is_unblocked(grid, 2, 2)
78+
True
79+
"""
3880
return grid[row][col] == 1
3981

4082

4183
# Check if a cell is the destination
4284

4385

4486
def is_destination(row: int, col: int, dest: Tuple[int, int]) -> bool:
87+
"""
88+
Check if a cell is the destination.
89+
90+
Args:
91+
row (int): The row index of the cell.
92+
col (int): The column index of the cell.
93+
dest (Tuple[int, int]): The destination coordinates as (row, col).
94+
95+
Returns:
96+
bool: True if the cell is the destination, False otherwise.
97+
98+
Examples:
99+
>>> is_destination(3, 4, (3, 4))
100+
True
101+
>>> is_destination(2, 1, (3, 4))
102+
False
103+
"""
45104
return row == dest[0] and col == dest[1]
46105

47106

48107
# Calculate the heuristic value of a cell (Euclidean distance to destination)
49108

50109

51110
def calculate_h_value(row: int, col: int, dest: Tuple[int, int]) -> float:
111+
"""
112+
Calculate the heuristic value (Euclidean distance) from a cell to the destination.
113+
114+
Args:
115+
row (int): The row index of the cell.
116+
col (int): The column index of the cell.
117+
dest (Tuple[int, int]): The destination coordinates as (row, col).
118+
119+
Returns:
120+
float: The Euclidean distance from the current cell to the destination.
121+
122+
Examples:
123+
>>> calculate_h_value(0, 0, (3, 4))
124+
5.0
125+
>>> calculate_h_value(2, 1, (2, 1))
126+
0.0
127+
>>> calculate_h_value(1, 1, (4, 5))
128+
5.0
129+
"""
52130
return ((row - dest[0]) ** 2 + (col - dest[1]) ** 2) ** 0.5
53131

54132

55133
# Trace the path from source to destination
56134

57135

58136
def trace_path(cell_details: List[List[Cell]], dest: Tuple[int, int]) -> None:
137+
"""
138+
Trace and print the path from the source to the destination.
139+
140+
Args:
141+
cell_details (List[List[Cell]]): A 2D list containing details of each cell.
142+
dest (Tuple[int, int]): The destination coordinates as (row, col).
143+
144+
Returns:
145+
None
146+
147+
Examples:
148+
>>> class Cell:
149+
... def __init__(self):
150+
... self.parent_i = 0
151+
... self.parent_j = 0
152+
>>> cell_details = [[Cell() for _ in range(2)] for _ in range(2)]
153+
>>> cell_details[1][1].parent_i = 0
154+
>>> cell_details[1][1].parent_j = 0
155+
>>> trace_path(cell_details, (1, 1))
156+
The Path is
157+
-> (0, 0) -> (1, 1)
158+
"""
59159
print("The Path is ")
60160
path = []
61161
row = dest[0]
@@ -87,6 +187,30 @@ def trace_path(cell_details: List[List[Cell]], dest: Tuple[int, int]) -> None:
87187

88188

89189
def a_star_search(grid: List[List[int]], src: Tuple[int, int], dest: Tuple[int, int]) -> None:
190+
"""
191+
Perform the A* search to find the shortest path from source to destination.
192+
193+
Args:
194+
grid (List[List[int]]): The grid representing the map, where 1 is unblocked and 0 is blocked.
195+
src (Tuple[int, int]): The source coordinates as (row, col).
196+
dest (Tuple[int, int]): The destination coordinates as (row, col).
197+
198+
Returns:
199+
None
200+
201+
Examples:
202+
>>> grid = [
203+
... [1, 1, 1],
204+
... [1, 0, 1],
205+
... [1, 1, 1]
206+
... ]
207+
>>> a_star_search(grid, (0, 0), (2, 2))
208+
The destination cell is found
209+
The Path is
210+
-> (0, 0) -> (1, 1) -> (2, 2)
211+
>>> a_star_search(grid, (0, 0), (1, 1))
212+
Source or the destination is blocked
213+
"""
90214
# Check if the source and destination are valid
91215
if not is_valid(src[0], src[1]) or not is_valid(dest[0], dest[1]):
92216
print("Source or destination is invalid")
@@ -203,8 +327,8 @@ def a_star_search(grid: List[List[int]], src: Tuple[int, int], dest: Tuple[int,
203327
Examples:
204328
>>> main()
205329
The destination cell is found
206-
The Path is
207-
-> (8, 0) -> (7, 1) -> (6, 0) -> (5, 1) -> (4, 0) -> (3, 1) -> (2, 0) -> (1, 1) -> (0, 0)
330+
The Path is
331+
-> (8, 0) -> (7, 1) -> (6, 0) -> (5, 1) -> (4, 0) -> (3, 1) -> (2, 0) -> (1, 1) -> (0, 0)
208332
"""
209333
# Define the grid (1 for unblocked, 0 for blocked)
210334
grid = [

0 commit comments

Comments
 (0)