Repository navigation
Conversation
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 Report❌ Patch coverage is 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:
|
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.
Add
dag_longest_path, and servedag_longest_path_lengthfor 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 peelingdag_longest_path_lengthalready uses. Each generation gathers its in-edges and reduces them withnp.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.
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.topo_orderthat 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, atopo_orderraises 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_weighton 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:
[]and 0; one node gives[node]and 0;weight=Noneweighs every edgedefault_weight, and an edge without the weight weighsdefault_weight. The conversion still leaves to NetworkX a default other than 1 where only some edges carry the weight.Left to NetworkX:
weight=None, a default that is not a finite Python int or float;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.
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:
Also added.
algorithms.generations,longest_path_lengthsandlongest_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: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.pyand the doctests ofalgorithms/dag.pywere run through the backend in dev, 3.7 and 3.6.1. The same tests pass before and after (96, 96 and 84).dag_longest_pathand 6 ofdag_longest_path_lengthin each environment. Before, it served none of the first and 4 of the second.topo_orderdoctest ([0, 2, 1]gives[0, 2]) is served, and gives NetworkX's path.Stacked on #125.