Skip to content

56 dijkstra sources - #153

Open
eriknw wants to merge 1 commit into
python-graphblas:30-dijkstrafrom
eriknw:56-dijkstra-sources
Open

eriknw wants to merge 1 commit into
python-graphblas:30-dijkstrafrom
eriknw:56-dijkstra-sources

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

Add single_source_dijkstra_path_length, multi_source_dijkstra_path_length and dijkstra_path_length. Each gives NetworkX's nodes, integer lengths exactly and float lengths within rounding, or NetworkX's error with its message. On the automatic path should_run leaves them to NetworkX, which was faster on most graphs once the conversion is counted; called with backend="graphblas", or on this backend's graphs, they are served. There they are much faster on random graphs once the conversion is cached, and slower on grids: 2 to 4 times NetworkX's time from one source or for a pair, even with the conversion cached.

  • Lengths: one vector of lengths, relaxed one edge a round from every source at once, as 30 dijkstra #133's kernel relaxes each row, with 30 dijkstra #133's cutoff rules. With no negative or NaN weight, the length NetworkX's Dijkstra gives a node is the least over the walks to it from any source, which is what min-plus finds. For dijkstra_path_length, a walk longer than the length found to the target is dropped, so the rounds stop there.
  • Order: each dict starts with the sources as given, at NetworkX's integer 0, and then lists the other nodes by increasing length, equal lengths in node order. A source given as 1.0 for the node 1 stays 1.0, as in NetworkX. NetworkX orders equal lengths by its heap, which is not reproduced.
  • Errors, in NetworkX's order and with its messages: empty sources raise ValueError("sources must not be empty"); a source not in the graph raises NodeNotFound("Node 7 not found in graph"), for the first one given; a missing or unreachable target raises NetworkXNoPath("Node 7 not reachable from 0"). A source that is its target gives 0 before anything is walked, whatever the weights. single_source_dijkstra_path_length passes {source} on, as NetworkX does, so a source that is not hashable raises NetworkX's TypeError. A generator of sources is spent by the check and gives {}, as in NetworkX.
  • NaN and negative weights follow 34 nan, infinite and negative weights #132's rule for the Dijkstra names: declined, with +inf served. 30 dijkstra #133 declines them for all pairs for the same reason: NetworkX's lengths there, and where it raises "Contradictory paths found", follow the order of its walk. The backend raises NotImplementedError after converting, and NetworkX answers them under backend_priority. The check is kept with the converted graph, since NetworkX's own loops, such as its all_pairs_dijkstra_path_length, ask for one source at a time. A self-loop of +inf on an undirected graph is declined too, since NetworkX's matrix holds NaN there; on a directed graph it is served.
  • Declined before converting, as for 30 dijkstra #133: a weight function, a graph class that reports an adjacency other than _adj, and a cutoff that does not compare as a float does. Also a target that is not hashable: NetworkX compares it with each node it reaches before its lookup raises.
  • Cutoff: float, NaN, inf, -inf and negative cutoffs keep what NetworkX keeps. A NaN cutoff keeps every node, and a negative one only the sources.
  • Types, as for 30 dijkstra #133: a graph mixing int and float weights gives float lengths throughout, and float32 weights give Python floats where NetworkX gives np.float32; the values are equal.
  • Speed, Indicative (load 3.3 to 7.0): medians of three interleaved rounds, with every length equal to NetworkX's. Converted for the call, the backend lost on every graph of 1e5 edges, at 1.1 to 16 times NetworkX's time, and of 1e6 edges won clearly only with random float weights on a random graph. From one source on gnm_random_graph(1e5, 1e6) it took 0.55 s against NetworkX's 0.86 s with float weights, 0.65 s against 0.68 s with integer weights 1 to 10, and 0.71 s against 0.39 s with no weight attribute; on a 707 by 707 grid with float weights, 2.4 s against 0.64 s. The conversion is most of that: with it cached, the random graph took 0.055, 0.037 and 0.014 s, 4 to 6 percent of NetworkX's time, but the grid still took 1.85 s, about three times NetworkX's 0.64 s; on a 224 by 224 grid it was about twice NetworkX's time. The backend is slower on grids, cached or not. It takes a round per edge of the paths it relaxes, and on that grid the least path to the far corner has at least 1412 edges. Ten sources at once cost what one does, here as in NetworkX (0.69 s against 1.09 s on the random graph, 1.14 s against 0.91 s on the grid), so serving only multi_source_dijkstra_path_length would not help. A pair stops at its target in both: on the random graph 0.53 to 0.78 s converted and 0.004 to 0.043 s cached, against NetworkX's 0.10 to 0.51 s. A second run, at load 11 to 14, agreed but for random float weights on 1e5 edges, which came out even there (0.87 to 0.97 of NetworkX's time).
  • So should_run leaves the three to NetworkX whenever it would convert a NetworkX graph, which is the only time NetworkX asks it; nothing cheap at that point tells a random graph from a grid. Called with backend="graphblas", on this backend's graphs, or in NetworkX's test mode, they are served.
  • shortest_path_length: its weighted forms with method="dijkstra" call single_source_dijkstra_path_length (from a source, or to a target, on the reversed graph if it is directed) and dijkstra_path_length by their dispatched names, so it goes where they go. On the automatic path that is NetworkX, by should_run; a test sets should_run aside and shows the inner calls served. Called with backend="graphblas" or on this backend's graphs, shortest_path_length itself raises NotImplementedError, as does any name this backend does not serve.
  • Dispatcher.should_run and the nxapi._utils.should_run decorator are line for line the ones 50-spanning-trees adds, and the two branches merge without a conflict.

Stacked on #133.

Serve single_source_dijkstra_path_length,
multi_source_dijkstra_path_length and dijkstra_path_length with one
vector of lengths relaxed from every source at once, as the all-pairs
kernel relaxes a row; a walk to a target goes no farther than its
length. Each dict holds the sources as given, at the integer 0, and
then the other nodes by increasing length.

NetworkX's checks come in its order and with its messages. Negative
and NaN weights are left to NetworkX, as for all pairs, and that
check is kept with the converted graph, since NetworkX's own loops ask
for one source at a time. A target that is not hashable is declined.

On the automatic path should_run leaves the three to NetworkX. Its
walk reads each edge about once, as a conversion does, and with the
conversion counted NetworkX was faster on graphs of 1e5 edges and on
most of 1e6. Called with this backend, or on its graphs, they are
served.
When NetworkX runs shortest_path_length, its weighted forms call them
by name and so go where they go; shortest_path_length itself is not
served here.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.21875% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.27%. Comparing base (1313069) to head (d266a1e).

Additional details and impacted files
@@               Coverage Diff               @@
##           30-dijkstra     #153      +/-   ##
===============================================
+ Coverage        93.08%   93.27%   +0.18%     
===============================================
  Files              183      183              
  Lines            11525    11892     +367     
  Branches          1310     1354      +44     
===============================================
+ Hits             10728    11092     +364     
- Misses             545      547       +2     
- Partials           252      253       +1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@eriknw eriknw mentioned this pull request Dec 3, 2026
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