Skip to content

Keep graph searches independent when edge lists are reused - #95

Open
euno102 wants to merge 1 commit into
brean:mainfrom
euno102:fix/independent-graph-search
Open

euno102 wants to merge 1 commit into
brean:mainfrom
euno102:fix/independent-graph-search

Conversation

@euno102

@euno102 euno102 commented Nov 26, 2026

Copy link
Copy Markdown

Keep Dijkstra searches independent when graphs reuse an edge list

Two Graph instances constructed from the same list of edge IDs share their nodes. After the first search visits them, the second graph can return an empty path on its very first search.

from pathfinding.core.graph import Graph
from pathfinding.finder.dijkstra import DijkstraFinder

edges = [["S", "M", 2], ["M", "G", 3]]
first, second = Graph(edges), Graph(edges)
for graph in (first, second):
    path, _ = DijkstraFinder().find_path(graph.node("S"), graph.node("G"), graph)
    print([node.node_id for node in path])

On e271d46 the output is ['S', 'M', 'G'] and then []. Both calls now return ['S', 'M', 'G'].

edge_node_items() replaces endpoint IDs in each input row. Copy each row before this conversion so graphs built from the same IDs create their own nodes. This shallow copy retains explicitly supplied node objects.

The new regression test fails on the original code. With the change, the full pytest suite passes locally (68 tests, Python 3.13.9). Related to #67, which covered repeated searches on one graph.

AI assistance: OpenAI Codex prepared the change and regression test, ran the reproducer and test suite, and drafted this description.

Copy each edge row before converting IDs to nodes and cover searches on two graphs constructed from the same list.

Assisted-by: OpenAI Codex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant