Skip to content

Commit 62411ed

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

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

graphs/bidirectional_breadth_first_search.py

+15-10
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],
@@ -20,7 +23,9 @@
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)
@@ -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,8 +70,9 @@ 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
"""
@@ -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)