Skip to content

Commit b0a6d68

Browse files
committed
Fix mypy errors at bidirectional_bfs
1 parent 256c319 commit b0a6d68

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

graphs/bidirectional_breadth_first_search.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
from __future__ import annotations
66

77
import time
8+
from typing import Optional
9+
10+
Path = list[tuple[int, int]]
811

912
grid = [
1013
[0, 0, 0, 0, 0, 0, 0],
@@ -16,11 +19,13 @@
1619
[0, 0, 0, 0, 1, 0, 0],
1720
]
1821

19-
delta = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right
22+
direction = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right
2023

2124

2225
class Node:
23-
def __init__(self, pos_x, pos_y, goal_x, goal_y, parent):
26+
def __init__(
27+
self, pos_x: int, pos_y: int, goal_x: int, goal_y: int, parent: Optional[Node]
28+
):
2429
self.pos_x = pos_x
2530
self.pos_y = pos_y
2631
self.pos = (pos_y, pos_x)
@@ -32,11 +37,11 @@ def __init__(self, pos_x, pos_y, goal_x, goal_y, parent):
3237
class BreadthFirstSearch:
3338
"""
3439
>>> bfs = BreadthFirstSearch((0, 0), (len(grid) - 1, len(grid[0]) - 1))
35-
>>> (bfs.start.pos_y + delta[3][0], bfs.start.pos_x + delta[3][1])
40+
>>> (bfs.start.pos_y + direction[3][0], bfs.start.pos_x + direction[3][1])
3641
(0, 1)
3742
>>> [x.pos for x in bfs.get_successors(bfs.start)]
3843
[(1, 0), (0, 1)]
39-
>>> (bfs.start.pos_y + delta[2][0], bfs.start.pos_x + delta[2][1])
44+
>>> (bfs.start.pos_y + direction[2][0], bfs.start.pos_x + direction[2][1])
4045
(1, 0)
4146
>>> bfs.retrace_path(bfs.start)
4247
[(0, 0)]
@@ -45,14 +50,14 @@ class BreadthFirstSearch:
4550
(5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (6, 5), (6, 6)]
4651
"""
4752

48-
def __init__(self, start, goal):
53+
def __init__(self, start: tuple[int, int], goal: tuple[int, int]):
4954
self.start = Node(start[1], start[0], goal[1], goal[0], None)
5055
self.target = Node(goal[1], goal[0], goal[1], goal[0], None)
5156

5257
self.node_queue = [self.start]
5358
self.reached = False
5459

55-
def search(self) -> list[tuple[int]]:
60+
def search(self) -> Optional[Path]:
5661
while self.node_queue:
5762
current_node = self.node_queue.pop(0)
5863

@@ -65,15 +70,16 @@ def search(self) -> list[tuple[int]]:
6570
for node in successors:
6671
self.node_queue.append(node)
6772

68-
if not (self.reached):
69-
return [(self.start.pos)]
73+
if not self.reached:
74+
return [self.start.pos]
75+
return None
7076

7177
def get_successors(self, parent: Node) -> list[Node]:
7278
"""
7379
Returns a list of successors (both in the grid and free spaces)
7480
"""
7581
successors = []
76-
for action in delta:
82+
for action in direction:
7783
pos_x = parent.pos_x + action[1]
7884
pos_y = parent.pos_y + action[0]
7985
if not (0 <= pos_x <= len(grid[0]) - 1 and 0 <= pos_y <= len(grid) - 1):
@@ -87,7 +93,7 @@ def get_successors(self, parent: Node) -> list[Node]:
8793
)
8894
return successors
8995

90-
def retrace_path(self, node: Node) -> list[tuple[int]]:
96+
def retrace_path(self, node: Optional[Node]) -> Path:
9197
"""
9298
Retrace the path from parents to parents until start node
9399
"""
@@ -119,7 +125,7 @@ def __init__(self, start, goal):
119125
self.bwd_bfs = BreadthFirstSearch(goal, start)
120126
self.reached = False
121127

122-
def search(self) -> list[tuple[int]]:
128+
def search(self) -> Optional[Path]:
123129
while self.fwd_bfs.node_queue or self.bwd_bfs.node_queue:
124130
current_fwd_node = self.fwd_bfs.node_queue.pop(0)
125131
current_bwd_node = self.bwd_bfs.node_queue.pop(0)
@@ -144,10 +150,9 @@ def search(self) -> list[tuple[int]]:
144150

145151
if not self.reached:
146152
return [self.fwd_bfs.start.pos]
153+
return None
147154

148-
def retrace_bidirectional_path(
149-
self, fwd_node: Node, bwd_node: Node
150-
) -> list[tuple[int]]:
155+
def retrace_bidirectional_path(self, fwd_node: Node, bwd_node: Node) -> Path:
151156
fwd_path = self.fwd_bfs.retrace_path(fwd_node)
152157
bwd_path = self.bwd_bfs.retrace_path(bwd_node)
153158
bwd_path.pop()

0 commit comments

Comments
 (0)