Return neighbors in a deterministic order#1638
Merged
IvanIsCoding merged 2 commits intoJul 15, 2026
Merged
Conversation
PyGraph.neighbors, PyDiGraph.neighbors and PyDiGraph.neighbors_undirected deduplicated their results through a HashSet and returned them via `drain()`. The HashSet iteration order is randomized, so the order of the returned node indices could change between runs and even between calls. Deduplicate while preserving the graph's edge iteration order by filtering through a `seen` set instead of draining a HashSet. This keeps the multigraph deduplication behaviour but makes the ordering stable. Also update the now-deterministic neighbors_undirected docstring example and add regression tests asserting a stable order and correct dedup. Fixes Qiskit#1501
IvanIsCoding
approved these changes
Jul 15, 2026
Contributor
|
Tick the box to add this pull request to the merge queue (same as
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PyGraph.neighbors,PyDiGraph.neighborsandPyDiGraph.neighbors_undirectedreturned neighbor node indices in a non-deterministic order that could change between runs. This makes them return a stable, deterministic order while keeping their multigraph de-duplication behaviour.Fixes #1501.
Root cause
All three methods deduplicated neighbors by collecting into a
HashSet<usize>and then draining it:HashSet's iteration order is randomized, so the order of the returned indices was arbitrary and unstable — it could differ between runs and even between successive calls on the same graph.Fix
We swapped
HashSetwithIndexSetTests
Added regression tests in
tests/graph/test_neighbors.pyandtests/digraph/test_neighbors.pythat assert a stable order across repeated calls, the exact expected order, and correct de-duplication of parallel edges.Verified locally against a fresh build (rustworkx
main): the new tests pass, the fulltests/graph(820) andtests/digraph(918) suites pass, andcargo fmt/cargo clippy -- -D warningsare clean.