Skip to content

Fix mypy errors at bidirectional_bfs #4531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions graphs/bidirectional_breadth_first_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from __future__ import annotations

import time
from typing import Optional

Path = list[tuple[int, int]]

grid = [
[0, 0, 0, 0, 0, 0, 0],
Expand All @@ -20,7 +23,9 @@


class Node:
def __init__(self, pos_x, pos_y, goal_x, goal_y, parent):
def __init__(
self, pos_x: int, pos_y: int, goal_x: int, goal_y: int, parent: Optional[Node]
):
self.pos_x = pos_x
self.pos_y = pos_y
self.pos = (pos_y, pos_x)
Expand All @@ -45,14 +50,14 @@ class BreadthFirstSearch:
(5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (6, 5), (6, 6)]
"""

def __init__(self, start, goal):
def __init__(self, start: tuple[int, int], goal: tuple[int, int]):
self.start = Node(start[1], start[0], goal[1], goal[0], None)
self.target = Node(goal[1], goal[0], goal[1], goal[0], None)

self.node_queue = [self.start]
self.reached = False

def search(self) -> list[tuple[int]]:
def search(self) -> Optional[Path]:
while self.node_queue:
current_node = self.node_queue.pop(0)

Expand All @@ -65,8 +70,9 @@ def search(self) -> list[tuple[int]]:
for node in successors:
self.node_queue.append(node)

if not (self.reached):
return [(self.start.pos)]
if not self.reached:
return [self.start.pos]
return None

def get_successors(self, parent: Node) -> list[Node]:
"""
Expand All @@ -87,7 +93,7 @@ def get_successors(self, parent: Node) -> list[Node]:
)
return successors

def retrace_path(self, node: Node) -> list[tuple[int]]:
def retrace_path(self, node: Optional[Node]) -> Path:
"""
Retrace the path from parents to parents until start node
"""
Expand Down Expand Up @@ -119,7 +125,7 @@ def __init__(self, start, goal):
self.bwd_bfs = BreadthFirstSearch(goal, start)
self.reached = False

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

if not self.reached:
return [self.fwd_bfs.start.pos]
return None

def retrace_bidirectional_path(
self, fwd_node: Node, bwd_node: Node
) -> list[tuple[int]]:
def retrace_bidirectional_path(self, fwd_node: Node, bwd_node: Node) -> Path:
fwd_path = self.fwd_bfs.retrace_path(fwd_node)
bwd_path = self.bwd_bfs.retrace_path(bwd_node)
bwd_path.pop()
Expand Down