Skip to content

69 dag longest path - #168

Open
eriknw wants to merge 1 commit into
python-graphblas:24-dagfrom
eriknw:69-dag-longest-path
Open

eriknw wants to merge 1 commit into
python-graphblas:24-dagfrom
eriknw:69-dag-longest-path

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

Add dag_longest_path, and serve dag_longest_path_length for weighted graphs (it served unit weights only). In NetworkX, a node's length is the most of its predecessors' lengths plus the edge's weight, or 0 when it has no predecessor or that most is negative, and the path ends at a node of the greatest length and steps back. Here the lengths are taken one topological generation at a time, from the peeling dag_longest_path_length already uses. Each generation gathers its in-edges and reduces them with np.maximum.reduceat: in NumPy when it has many, and on Python lists when it has few, as the peeling walks its small rounds, so a deep, narrow graph stays linear. NetworkX sorts the graph topologically and walks every node's predecessors in Python.

  • Lengths to the bit. Each length is one sum that NetworkX also makes, and the most of equal numbers is that number, so the lengths, floats too, are NetworkX's to the bit. Random DAGs were checked in dev, 3.7 and 3.6.1: 3178 calls in each, 0 different, and the largest relative difference among 735 float lengths is 0.

  • The path. It ends at the first node, by id, of the greatest length, and steps back along in-edges whose sums give the lengths. Its weights add up to NetworkX's length, but where several paths are longest it may be another than NetworkX's, which the contract allows.

    • With topo_order, which is how NetworkX picks among them, the path is NetworkX's. It ends at the order's first node of the greatest length, and each step back takes the first predecessor in the order the converted NetworkX graph keeps them, while the sum is not negative.
    • Left to NetworkX: a topo_order that is not a topological order of every node once (NetworkX's KeyError, ValueError or answer), and one that can be read only once (an iterator would reach NetworkX empty after a decline). With a graph of this backend, which keeps no order of predecessors, a topo_order raises NotImplementedError.
  • The length's type. NetworkX adds the graph's own numbers along its path from the int 0, and so does this backend, along its own path. Where the weights are ints on some edges and floats on others (a float default_weight on edges without a weight counts), and several paths are longest, the length may be an int where NetworkX's is a float of the same value, or the reverse. This happened 2 times in the 3178 calls of each environment. Where one path is longest, the type is NetworkX's.

  • As in NetworkX:

    • an empty graph gives [] and 0; one node gives [node] and 0;
    • where every edge is negative, a path of one node and 0;
    • an undirected graph raises NetworkXNotImplemented;
    • a cycle, a self-loop included, raises NetworkXUnfeasible with NetworkX's message, before anything is asked of the weights, as NetworkX's topological sort raises first;
    • weight=None weighs every edge default_weight, and an edge without the weight weighs default_weight. The conversion still leaves to NetworkX a default other than 1 where only some edges carry the weight.
  • Left to NetworkX:

    • multigraphs, as everywhere in this backend;
    • NaN or infinite weights;
    • float weights of 2**53 or more, which may stand for ints that NetworkX adds exactly;
    • complex and other weights;
    • with weight=None, a default that is not a finite Python int or float;
    • graph classes that read their own edges.
  • Overflow. Int weights raise OverflowError after the conversion where the number of nodes times the heaviest absolute weight reaches 2**62 (checked_sum_type(G, "path")), and so does a uniform default as large. NetworkX's Python ints answer there. A cycle still raises NetworkXUnfeasible first.

  • Speed. Indicative: dev, best of 3 interleaved in one process, conversion on every call unless cached, load 4.7 to 5.7. On graphs where NetworkX takes 1.4 to 3.7 s, with conversion this is about parity, 1.06x to 1.60x, because the conversion is most of the time; on a cached conversion it is 4.6x to 12.8x. The path on a chain of 2e6 nodes is slower than NetworkX with conversion (0.75x) and about even cached (1.2x): each generation there is one node, walked in Python.

    graph edges path, NetworkX with conversion cached length, NetworkX with conversion cached
    random DAG, float weights, 1.5e5 nodes 1.5e6 1.37 s 1.17x 9.1x 1.36 s 1.17x 9.2x
    random DAG, int weights, 5e5 nodes 4.0e6 3.74 s 1.51x 10.3x 3.71 s 1.50x 10.2x
    random DAG, unweighted, 1e5 nodes 3.0e6 1.43 s 1.06x 7.1x 1.43 s 1.14x 12.8x
    layered, 200 x 4000, int weights 4.0e6 1.81 s 1.25x 9.2x 2.31 s 1.39x 11.9x
    path of 2e6 nodes, unweighted 2.0e6 1.83 s 0.75x 1.2x 2.18 s 1.60x 4.6x

    On small graphs NetworkX is faster: 0.13x to 0.15x at 20 edges (under 0.2 ms either way), 0.47x at 300, 0.85x at 3000, then 1.35x to 1.44x at 3e4 and 3e5 edges.

  • Memory. Peak RSS of one process running both names with conversion:

    • random DAG of 4e6 edges: 2882 MB (2035 MB after building it);
    • chain of 2e6 nodes: 2659 MB (1776 MB).
  • Also added. algorithms.generations, longest_path_lengths and longest_path. The peeling's small rounds can now hand over their generations. The dispatch-table notes for both names were updated.

  • Tests. 308 seeded DAGs: random ones (added and ranked in two random orders, some with string labels and isolated nodes), layered ones, paths up to 600 nodes, random trees, the empty graph, isolated nodes and one node. Their weights are ints, tied ints, floats, tied floats, negatives, zeros (0, 0.0, -0.0), mixed ints and floats, missing (with default 1, 1.0, and a weight no edge has), or none (weight=None, defaults 1 and 3). They check:

    • the path is a path of the graph with NetworkX's length;
    • the length is NetworkX's: floats within a relative 1e-12 and, in a separate test, to the bit; ints exactly and of their type, except in the mixed case, where the type is checked against this backend's own path;
    • with three topological orders of each graph, the path is NetworkX's;
    • every error and decline above, three ways: NetworkX, graphblas by name, and graphblas first in the priority with the fallback off;
    • the overflow bound;
    • the kernel's Python and NumPy modes, each forced and mixed;
    • NetworkX's doc examples.

    33 mutants of the kernel and wrappers each fail them, in dev, 3.7 and 3.6.1.

  • NetworkX's tests. algorithms/tests/test_dag.py and the doctests of algorithms/dag.py were run through the backend in dev, 3.7 and 3.6.1. The same tests pass before and after (96, 96 and 84).

    • After the change, the backend serves 9 calls of dag_longest_path and 6 of dag_longest_path_length in each environment. Before, it served none of the first and 4 of the second.
    • The calls left to NetworkX are its multigraph tests.
    • No test pins a path among ties, so none is marked xfail. The topo_order doctest ([0, 2, 1] gives [0, 2]) is served, and gives NetworkX's path.

Stacked on #125.

A node's length is NetworkX's: the most of its predecessors' lengths
plus the edge's weight, or 0 when it has no predecessor or that is
negative. The topological generations give the order. Each generation
takes the most over its in-edges at once, in NumPy when it has many and
in Python on lists when it has few, as the peeling walks its small
rounds, so a deep graph stays linear; the peeling now hands over the
generations of its small rounds in one loop. Each length is one sum
NetworkX also makes, and the most of equal numbers is that number, so
the lengths, floats too, are NetworkX's to the bit.

The path ends at a node of the greatest length and steps back along
in-edges whose sums give the lengths. Where several paths are longest it
may be another than NetworkX's. Given topo_order, NetworkX's way to pick
among them, the path is NetworkX's: it ends at the order's first node of
the greatest length and takes the first predecessor in the order the
NetworkX graph converted keeps them. A topo_order that is not a
topological order of every node once, or that can be read only once, is
left to NetworkX.

dag_longest_path_length now serves weighted graphs, adding the graph's
own numbers along the path as NetworkX adds them; unit weights keep the
count of generations. Where the weights are ints on some edges and
floats on others, NetworkX's type follows its path, so of several
longest paths this one may give an int where NetworkX's gives a float of
the same value, or the reverse. A cycle raises NetworkXUnfeasible before
anything is asked of the weights. NaN or infinite weights, and floats of
2**53 or more, which may stand for ints NetworkX adds exactly, are left
to NetworkX; int weights raise OverflowError where a sum could pass
int64. With weight=None every edge weighs default_weight.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.60159% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.42%. Comparing base (6915bcf) to head (8e4cbc9).

Additional details and impacted files
@@            Coverage Diff             @@
##           24-dag     #168      +/-   ##
==========================================
+ Coverage   93.19%   93.42%   +0.22%     
==========================================
  Files         183      184       +1     
  Lines       11863    12317     +454     
  Branches     1345     1413      +68     
==========================================
+ Hits        11056    11507     +451     
- Misses        551      553       +2     
- Partials      256      257       +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