From 835efff324dca488d195b941161110988bcc3a2f Mon Sep 17 00:00:00 2001 From: Gonzalo Casas Date: Sun, 9 Mar 2025 01:39:20 +0100 Subject: [PATCH] Fix shortest_path --- CHANGELOG.md | 2 ++ src/compas/datastructures/graph/graph.py | 2 +- tests/compas/datastructures/test_graph.py | 32 +++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97821b390106..8f18b41ca405 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +* Fixed call to `astar_shortest_path` in `Graph.shortest_path`. + ### Removed diff --git a/src/compas/datastructures/graph/graph.py b/src/compas/datastructures/graph/graph.py index 951ffd85de39..23a4e7b5e861 100644 --- a/src/compas/datastructures/graph/graph.py +++ b/src/compas/datastructures/graph/graph.py @@ -1204,7 +1204,7 @@ def shortest_path(self, u, v): :meth:`compas.topology.astar_shortest_path` """ - return astar_shortest_path(self.adjacency, u, v) + return astar_shortest_path(self, u, v) # -------------------------------------------------------------------------- # Default attributes diff --git a/tests/compas/datastructures/test_graph.py b/tests/compas/datastructures/test_graph.py index 98987ae962d1..4c43bb9c2f49 100644 --- a/tests/compas/datastructures/test_graph.py +++ b/tests/compas/datastructures/test_graph.py @@ -117,6 +117,38 @@ def test_graph_data2(): assert Graph.validate_data(other.__data__) +def test_shortest_path(): + graph = Graph() + graph.add_edge(1, 2) + graph.add_edge(2, 3) + graph.add_edge(3, 4) + graph.add_edge(5, 6) + + # Test shortest path from node 1 to node 4 + path = graph.shortest_path(1, 4) + assert path == [1, 2, 3, 4] + + # Test shortest path from node 1 to node 3 + path = graph.shortest_path(1, 3) + assert path == [1, 2, 3] + + # Test shortest path from node 2 to node 4 + path = graph.shortest_path(2, 4) + assert path == [2, 3, 4] + + # Test shortest path from node 4 to node 1 + path = graph.shortest_path(4, 1) + assert path == [4, 3, 2, 1] + + # Test shortest path from node 5 to node 6 + path = graph.shortest_path(5, 6) + assert path == [5, 6] + + # Test shortest path from node 3 to node 5 (should be None) + path = graph.shortest_path(3, 5) + assert path is None + + # ============================================================================== # Properties # ==============================================================================