Skip to content

70 unweighted shortest paths - #167

Open
eriknw wants to merge 1 commit into
python-graphblas:46-bfs-own-orderfrom
eriknw:70-unweighted-paths
Open

eriknw wants to merge 1 commit into
python-graphblas:46-bfs-own-orderfrom
eriknw:70-unweighted-paths

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

Add single_source_shortest_path, single_target_shortest_path, all_pairs_shortest_path, shortest_path without weight, and predecessor. Each walks the graph a level at a time, as bfs_edges does: the frontier times the adjacency matrix, each newly reached node taking as its parent its neighbor on the level above that is first in node order. NetworkX's dicts and lists are then built from the levels in Python, a level at a time: a level of 4096 nodes or more in an array with a row per path, a smaller one a path at a time, which costs less where the levels are many and short.

  • Which path. Where two nodes have several shortest paths, NetworkX gives the one its walk meets first, which follows the order each node's edges were added; a matrix keeps no such order. Each path here is a shortest path in the graph, between the nodes NetworkX gives and of NetworkX's length, but it may be another one: where several answers are correct, NetworkX's need not be matched. predecessor gives each node's predecessors as NetworkX does, all of its neighbors one level nearer the source (its in-neighbors, on a directed graph), but in node order rather than in the order NetworkX's walk reaches them; the levels (return_seen) are NetworkX's. The dicts come by level, each level in node order. The source is kept as passed (1.0 for node 1) in the keys and the paths, as NetworkX keeps it.
  • As NetworkX: NodeNotFound with each name's message, checked before the cutoff: "Source 9 not in G", "Target 9 not in G", and for shortest_path to a target "Source 9 not in G", since NetworkX walks out of the target on the reversed graph. predecessor of a target the walk does not reach, or that is not in the graph, is [] (([], -1) with return_seen); an unhashable target raises NetworkX's TypeError, after the cutoff is read. An unknown method raises NetworkX's ValueError. Cutoffs: the paths go on while cutoff > level, so 1.5 reaches level 2 and NaN, 0 or a negative cutoff only the source; predecessor stops after the first level at least cutoff, so a cutoff below 1 still walks one level, and a falsy cutoff or NaN sets no bound. A cutoff that does not compare with an int raises NetworkX's TypeError, from the same first comparison.
  • all_pairs_shortest_path is a generator, as NetworkX's is: nothing is read until the first item is asked for, so a cutoff NetworkX cannot compare raises there, and on a graph without nodes never. The walks are made for a batch of sources at once, a row of a matrix each, so that a level costs one product for the batch; a batch holds at most about 2**20 reached ids (16 MiB), and a source's paths are built when it is yielded, so one source's dict is held at a time. shortest_path with neither end gives this generator.
  • Left to NetworkX: shortest_path with weight, and a path between two nodes of a NetworkX graph: NetworkX's search from both ends reaches a few nodes, where a conversion costs every edge (see the speed below). On a graph of this backend's own, a pair is walked from both ends, widening the smaller frontier, until one walk reaches a node the other has reached, which lies on a shortest path; where there is none, NetworkXNoPath with NetworkX's message.
  • Speed (Indicative, load 9 to 12; interleaved with NetworkX, best of 3, one process at a time, conversion included unless "cached").
    • all_pairs_shortest_path, every item taken and dropped (the conversion is a small share): G(3000, 15000) 1.83 s where NetworkX takes 3.09 (1.68x), directed 1.89 where 2.84 (1.50x), G(5000, 25000) 5.65 where 9.46 (1.68x), G(2000, 40000) 0.45 where 0.92 (2.05x), a 60 by 60 grid 4.75 where 5.26 (1.11x). In a grid the paths are long and NetworkX's walk is cheap, so building the paths costs about as much as NetworkX's whole call. A review measured 1.24x to 1.4x on a sparser G(2000, 6000) (1.6x cached).
    • From one node of G(1e6, 2e6), NetworkX is faster: single_source_shortest_path 2.98 s where NetworkX takes 1.20 (0.40x), single_target_shortest_path 0.43x, predecessor 0.49x, and on the directed graph 0.41x to 0.43x; the conversion takes 1.6 to 2.5 s of each. On a cached conversion they are 2.35x, 2.54x and 2.10x (directed 1.37x to 1.69x). From a node beside a corner of a 300 by 300 grid NetworkX is faster on a cached conversion too: 0.91x, 0.94x and 0.81x. should_run is not on this base, so a single walk on a graph not yet converted is answered here, and is slower.
    • A pair on G(1e6, 2e6): NetworkX takes 1 ms; here 2.4 s with the conversion and 2.6 ms on a cached one (0.54x). Directed, 3.0 ms against 3.2 ms cached. From there to the far corner of the grid, 38 ms against 114 ms with the conversion and 25 ms cached (1.51x). So a pair of a NetworkX graph is left to NetworkX.
    • Memory: the largest of these runs peaked at 2.1 GiB.
  • Notes. The additional_docs notes of the five names say which path is given, the order, and what is left to NetworkX.
  • Tests. 320 random graphs, directed and undirected, in pieces, with self-loops and isolated nodes, labelled by ints, strings, tuples or a mix of ints and strings, in a shuffled node order, with cutoffs of None, 0 to 3, 1.5, 2.5, -1, -0.5, NaN, inf, True, False and 100, each name from up to three sources: each path is a path of the graph from NetworkX's start to its end, for each node NetworkX gives, of NetworkX's length; predecessor's lists equal NetworkX's as sets and hold no node twice, and its levels are NetworkX's, as Python ints; a pair with no path raises NetworkX's NetworkXNoPath. The same with the walks in batches of 1, 7 and 30 ids, and with levels of 1, 2 or 5 nodes and more built in arrays. Each name's errors for absent, unhashable and None nodes, by type and message; each cutoff above and "a", [], [1], 7.0 and -inf; when the generator reads its cutoff, and that a batch is made only when its first node is asked for; the source as passed; the declines; the backend's own graph. 43 versions of the kernels, the wrappers and the dispatch table, in NetworkX dev, 3.7 and 3.6.1: the unchanged code and four other valid choices pass (each node's parent last in node order rather than first, the array and list builds swapped, a pair walked from one end only, from either end); 37 mutants fail the tests; one passes and is equivalent, meeting against the other walk's newest level only, since a node that walk had reached on an older level would give a path shorter than the levels already walked allow.
  • NetworkX's tests through the backend in dev, 3.7 and 3.6.1: test_unweighted.py passes before and after (17 tests), with the five names served 16 times; the whole suite passes the same before and after (9569, 9430 and 6815 passed), with 265 calls of the five names served in each, most of them predecessor from algorithms that call it. None needed an xfail: NetworkX's tests check a grid path with validate_grid_path and a node's predecessors in either order. With each node's parent taken last in node order rather than first, the whole dev suite passes the same.

Stacked on #146.

single_source_shortest_path, single_target_shortest_path,
all_pairs_shortest_path, shortest_path without a weight, and
predecessor walk the graph a level at a time, each node taking as its
parent its neighbor on the level above that is first in node order,
and build NetworkX's dicts and lists from the levels. Where there are
several shortest paths the one given may not be NetworkX's, which
follows the order the edges were added; predecessor gives every
predecessor, as NetworkX does. Errors, cutoffs and when the all-pairs
generator raises are NetworkX's. A path between two nodes of a
NetworkX graph is left to NetworkX, whose search from both ends costs
less than converting the graph.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.65217% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.22%. Comparing base (9f5e7dc) to head (95310fe).

Additional details and impacted files
@@                 Coverage Diff                  @@
##           46-bfs-own-order     #167      +/-   ##
====================================================
+ Coverage             92.89%   93.22%   +0.32%     
====================================================
  Files                   182      183       +1     
  Lines                 11305    11876     +571     
  Branches               1290     1366      +76     
====================================================
+ Hits                  10502    11071     +569     
- Misses                  548      549       +1     
- Partials                255      256       +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