Skip to content
Merged
Show file tree
Hide file tree
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
24 changes: 17 additions & 7 deletions tests/test_algorithms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from waypoint.algorithms import djikstra, a_star
from waypoint.algorithms import djikstra, bfs
from waypoint.path import Path


Expand Down Expand Up @@ -47,12 +47,8 @@ def multiple_paths_graph():
algorithm_parametrize = pytest.mark.parametrize(
"algorithm",
[
# pytest.param(bfs, marks=pytest.mark.xfail(reason="BFS not implemented")),
pytest.param(
djikstra,
# marks=pytest.mark.xfail(reason="Dijkstra not implemented"),
),
pytest.param(a_star, marks=pytest.mark.xfail(reason="A* not implemented")),
pytest.param(djikstra),
pytest.param(bfs),
],
)

Expand All @@ -61,6 +57,13 @@ def multiple_paths_graph():
@algorithm_parametrize
class TestAlgorithms:
def test_simple_path(self, algorithm, simple_graph):
if algorithm.__name__ == "bfs":
# BFS does not consider weights, so the path may differ
result = algorithm(simple_graph, 100, 400)
assert result.flights is not None
assert result.flights[0] == 100
assert result.flights[-1] == 400
return
result = algorithm(simple_graph, 100, 400)
assert isinstance(result, Path)
assert result.flights == [100, 200, 300, 400]
Expand All @@ -78,6 +81,13 @@ def test_cyclic_path(self, algorithm, cyclic_graph):
assert result.time <= 7 # Maximum possible path length

def test_multiple_paths(self, algorithm, multiple_paths_graph):
if algorithm.__name__ == "bfs":
# BFS does not consider weights, so the path may differ
result = algorithm(multiple_paths_graph, 100, 400)
assert result.flights is not None
assert result.flights[0] == 100
assert result.flights[-1] == 400
return
result = algorithm(multiple_paths_graph, 100, 400)
assert result.flights == [100, 300, 400] # Should find shortest path
assert result.time == 4
Expand Down
4 changes: 2 additions & 2 deletions waypoint/algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"""

from .djikstra import djikstra
from .a_star import a_star
from .bfs import bfs

__all__ = ["djikstra", "a_star"]
__all__ = ["djikstra", "bfs"]
5 changes: 0 additions & 5 deletions waypoint/algorithms/a_star.py

This file was deleted.

24 changes: 24 additions & 0 deletions waypoint/algorithms/bfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from waypoint.path import Path


def bfs(graph: dict[int, dict[int, float]], start: int, destination: int) -> Path:
visited: set[int] = set()
queue: list[tuple[int, float, list[int]]] = [(start, 0, [])]

while queue:
current, time, path = queue.pop(0)
current: int
time: float
path: list[int]

if current == destination:
return Path.from_list(path + [current], time)

if current not in visited:
visited.add(current)
neighbors = graph.get(current, {})
for neighbor, weight in neighbors.items():
if neighbor not in visited:
queue.append((neighbor, time + weight, path + [current]))

return Path.empty()
2 changes: 1 addition & 1 deletion waypoint/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def empty(cls) -> "Path":
return self

@classmethod
def from_list(cls, flights: list[int] | None, time: float = 0.0) -> "Path":
def from_list(cls, flights: list[int] | None, time: float) -> "Path":
"""Creates a path from a list of flights and an optional time."""
self = super().__new__(cls)
self._flights = flights
Expand Down