Skip to content

30 dijkstra - #133

Open
eriknw wants to merge 1 commit into
python-graphblas:32-float-path-lengthsfrom
eriknw:30-dijkstra
Open

eriknw wants to merge 1 commit into
python-graphblas:32-float-path-lengthsfrom
eriknw:30-dijkstra

Conversation

@eriknw

@eriknw eriknw commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Add all_pairs_dijkstra_path_length. For each source it yields the same targets as NetworkX with the same lengths: integer lengths exactly, float lengths within rounding. Each dict is in the backend's own order, not NetworkX's.

  • Lengths: with no negative or NaN weight, adding an edge never shortens a walk, so the length NetworkX's Dijkstra gives a node is the least sum over the walks to it, which is what min-plus finds. The batched Bellman-Ford kernel gives the lengths, in the weights' own type (float32 included), and now takes a cutoff. Min-plus adds along each path from the source as NetworkX does, so on the graphs measured the float lengths were equal too, but that is not promised.
  • Order: each dict starts with the source, at NetworkX's integer 0, and then lists the other nodes by increasing length, equal lengths in node order. NetworkX orders equal lengths by the pushes of its heap; that order is arbitrary and is not reproduced. Sources come in node order.
  • Types: a graph mixing int and float weights gives float lengths throughout, where NetworkX gives an int for a path through int weights only: with edges (0, 1, 2), (1, 2, 0.5) and (0, 3, 1), node 1 from 0 is 2.0 here and 2 in NetworkX. The values are equal. The source's own length is the integer 0 in both.
  • Left to NetworkX: negative and NaN weights. There NetworkX's lengths, and the item at which it raises "Contradictory paths found", follow its walk. The backend raises NotImplementedError after converting, so NetworkX answers these under backend_priority.
  • Declined: a weight function, a graph whose class reports an adjacency other than _adj (such as _AntiGraph), and a cutoff that is not an int below 2**53, a float, or a NumPy integer or float64. Python compares those types with a length differently than a matrix of lengths does.
  • Speed, Indicative (load 3.8 to 4.6, in a benchslot run): on gnm_random_graph(3000, 15000), converted, with every item read into a dict, as the median of three interleaved rounds that each took the best of three. Unit weights take 0.64 s here against NetworkX's 9.7 s; random floats 1.23 s against 14.9 s; weights of 0, 1 or 2 0.75 s against 12.4 s; float32 weights 1.21 s against 17.2 s. The version first pushed here, which replayed NetworkX's order, took 4.3, 1.22, 10.5 and 13.8 s. The conversion adds 0.02 s. Ordering each dict by length is 0.1 to 0.4 s of these times: in node order they would be 0.37, 0.84, 0.64 and 0.78 s. On the first 300 sources of each graph, the lengths equal NetworkX's exactly.
  • Sources are taken in batches of about 2**22 lengths, so memory stays bounded when each item is read and dropped.
  • Not served: dijkstra_path_length, single_source_dijkstra_path_length and multi_source_dijkstra_path_length. With the conversion counted, the same kernel loses on a random graph of 1e5 nodes, and on grids of 1e5 and 1e6 nodes both cold and warm. A grid needs a round per step of its diameter. bidirectional_dijkstra and the path names are not served either.
  • Integer weights whose sums could reach 2**63 are checked when the function is called, not at the first item, so the error comes while NetworkX's dispatcher is still in the call. Here it is an OverflowError; with 26 overflow fallback #126 it becomes IntegerOverflowError, and NetworkX answers such weights itself under backend_priority, as it does for all_pairs_bellman_ford_path_length.
  • Where no edge joins two nodes, each length is a source's integer 0, and a cutoff no longer fails on a self-loop weight that an integer cannot hold, such as inf. This also fixes ego_graph with a distance on such a graph.
  • The graph is converted when the function is called, so a change made after that, even before the first item, is not seen; NetworkX's own generator would see it. The other all-pairs names do the same. A conversion that another name cached on a view, before the graph under the view changed, is read as it is for every other name; 27 stale views #128 handles that for all names.

Stacked on #129.

Serve it with the batched Bellman-Ford kernel, which now takes a
cutoff. With no negative or NaN weight, the length NetworkX's Dijkstra
gives a node is the least sum over the walks to it, which is what
min-plus finds, here in the weights' own type, float32 included.

Each source's dict holds the source, at the integer 0, and then the
other nodes by increasing length, equal lengths in id order. NetworkX
orders equal lengths by its heap, which is arbitrary.

Negative and NaN weights are left to NetworkX, since its lengths and
its "Contradictory paths" error there follow its walk. A weight
function, a graph class that reports another adjacency, and a cutoff
that does not compare as a float does are declined. Integer weights
whose sums could overflow raise when called, not at the first item.

The tests compare targets and integer lengths exactly and float
lengths within rounding, and check that every length is a shortest
path's: no edge shortens one, and a tight edge leads into each.
@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.69565% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.18%. Comparing base (faf0d71) to head (0d26544).

Additional details and impacted files
@@                    Coverage Diff                    @@
##           32-float-path-lengths     #133      +/-   ##
=========================================================
+ Coverage                  92.92%   93.18%   +0.26%     
=========================================================
  Files                        182      183       +1     
  Lines                      11286    11741     +455     
  Branches                    1279     1341      +62     
=========================================================
+ Hits                       10487    10941     +454     
  Misses                       546      546              
- Partials                     253      254       +1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant