Skip to content

Clarify the meaning of the cutoff parameter in some path-finding functions - #7487

Merged
dschult merged 18 commits into
networkx:mainfrom
fedarko:pathlen-docs
Oct 27, 2025
Merged

dschult merged 18 commits into
networkx:mainfrom
fedarko:pathlen-docs

Conversation

@fedarko

@fedarko fedarko commented Jun 5, 2024 •

Copy link
Copy Markdown
Contributor

Motivation

While I was working with all_simple_paths(), I got confused about the cutoff parameter. The current description for this parameter is

cutoff : integer, optional
    Depth to stop the search. Only paths of length <= cutoff are returned.

How "length" is defined here is ambiguous; my initial guess was that it was in terms of the number of nodes on the path, but it turns out that it's in terms of edges. (So the path [0, 1] has length 1, the path [0, 1, 2] has length 2, etc.) I imagine other people will also be confused and make the same mistake.

Changes

  • I updated the docstrings for the following eight functions:

    • all_simple_paths()
    • all_simple_edge_paths()
    • all_pairs_shortest_path_length()
    • single_source_shortest_path_length()
    • single_target_shortest_path_length()
    • all_pairs_shortest_path()
    • single_source_shortest_path()
    • single_target_shortest_path()

    to clarify the meaning of cutoff. I think the adjusted documentation is easier to understand.

  • I fixed a small error in the documentation of single_target_shortest_path() -- it said keyed by target where it should, I think, have said keyed by source.

  • Some of the above functions' docstrings (e.g. all_pairs_shortest_path()) say "cutoff" (escaping the parameter name, when referenced, in backticks) while some don't (just saying "cutoff"—e.g. single_source_shortest_path()). To make this consistent, I changed the above functions' docstrings—where needed—to say "cutoff" instead of "cutoff".

  • I fixed a small bug that I noticed in predecessor(). → update: per discussion below, not addressing in this PR.

    • Description: The functions that have a cutoff parameter explicitly check if cutoff is None and adjust their behavior accordingly. However, predecessor() does not do this -- instead, it checks the truth value of cutoff. This will have (I think) undesirable results if cutoff == 0, since cutoff == 0 will be treated the same as cutoff == None. (Not that I imagine a lot of people will be calling this function with a cutoff of 0, but I suppose consistency here is nice.)
    • Fix: I've replaced the use of if cutoff in this function with if cutoff is not None to fix this problem.

Notes

predecessor() also has a cutoff parameter, but I haven't updated its documentation (even though it has the same issue of not explaining what the cutoff "length" means). I'm not completely sure what this function is doing, and I don't want to add incorrect documentation to it by accident.

@Peiffap

Peiffap commented Jun 6, 2024

Copy link
Copy Markdown
Member

The docstring updates look good and make sense to me.
It looks like edge_load_centrality uses cutoff=False as the default, which obviously gets impacted by your change to predecessor (and explains why CI does not like the PR). I think it makes sense to change functions using predecessor to default to None instead of False from a semantic point of view (although the default value of None and handling of the break precedes (pun intended) the latest commits affecting edge_load_centrality using False by at least 6 years). Note that this would be a breaking change in some scenarios (as evidenced by the test failures), requiring a deprecation.

@fedarko

fedarko commented Jun 6, 2024

Copy link
Copy Markdown
Contributor Author

Understood, thank you for the detailed feedback! I didn't realize that the adjustment to predecessor() would break the tests; apologies for the trouble.

I suspect that refactoring predecessor() and everywhere it's used in the codebase might be out-of-scope for this PR (and I don't think I know enough at the moment to confidently make those changes), so for now I've reverted the code change within predecessor(). If you'd like I can open up an issue to document the situation, and/or add some text to the predecessor() docstring in this PR that explains that (at least for now) cutoff == 0 will be treated the same in this one function as cutoff == None.

Thanks again!

Comment on lines +32 to +34
Depth to stop the search. Only target nodes where the shortest path to
this node from the source node contains <= `cutoff` edges will be
included in the returned results.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the proposed wording is necessarily an improvement. For one thing, the verbosity makes it harder to grok (though that's the price for greater precision).

The bigger issue IMO is the fact that introducing "edges" to the explanation is not strictly correct... For example, in the case of multigraphs, it's the existence of an edge between nodes (rather than a specific edge) that dictates whether there is a path between them. Therefore, the number of edges is ambiguous in this context.

I have an alternative proposal: rather than modify the wording in the parameter description, what would you think about instead adding a short example to the Examples section of the docstrings that illustrates how cutoff works? e.g. something like (for this function):

>>> G = nx.path_graph(3)
>>> nx.single_source_shortest_path_length(G, 2)
{0: 0, 1: 1, 2: 2}
>>> nx.single_source_shortest_path_length(G, 2, cutoff=1)
{0: 0, 1: 1}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the comments!

I don't think the proposed wording is necessarily an improvement. For one thing, the verbosity makes it harder to grok (though that's the price for greater precision).

I wouldn't mind shortening the proposed wording, but I believe we should at least include the word "edges" in these descriptions. When I was reading the docs for all_simple_paths(), I assumed that path length was given in terms of nodes, not edges, and I imagine other people will make similar mistakes if these descriptions are left ambiguous.

For this particular function (single_source_shortest_path_length()) the meaning of "path length" is a bit clearer than for all_simple_paths() (since the output of the function actually contains path lengths, unlike the output of all_simple_paths()), but I still think more precision about the meaning of "path length" would be helpful in both cases.

The bigger issue IMO is the fact that introducing "edges" to the explanation is not strictly correct... For example, in the case of multigraphs, it's the existence of an edge between nodes (rather than a specific edge) that dictates whether there is a path between them. Therefore, the number of edges is ambiguous in this context.

I apologize if I have misunderstood something, but I do not agree with this logic. Consider the following multidigraph:

image

Although there are four edges in this graph, and although any of these four edges could be included on a shortest path from 0 to 2, the lengths of these shortest paths (in terms of edges, per single_source_shortest_path_length()) are all 2. The function on which we are currently commenting agrees with this:

import networkx as nx
g = nx.MultiDiGraph()
g.add_edge(0, 1, key="A")
g.add_edge(0, 1, key="B")
g.add_edge(0, 1, key="C")
g.add_edge(1, 2, key="D")
assert nx.single_source_shortest_path_length(g, 0) == {0: 0, 1: 1, 2: 2}

I do not think it is conventional to define the number of edges on a path relative to the number of "possible" edges on this path -- instead, this would be expressed as the number of such paths. In this example:

  • the shortest path to 0 from 0 contains 0 edges,
  • the shortest path to 1 from 0 contains 1 edge, and
  • the shortest path to 2 from 0 contains 2 edges,

even though there are 3 possible shortest paths from 0 to 1 and from 0 to 2.

As far as I am aware, the proposed wordings in this PR are correct, but please let me know if I've missed something.

I have an alternative proposal: rather than modify the wording in the parameter description, what would you think about instead adding a short example to the Examples section of the docstrings that illustrates how cutoff works?

Sure! Of the eight functions whose docstrings were updated in this PR, only one of them (all_simple_paths()) contains an example that uses the cutoff parameter. I just added examples for the other seven functions to this PR.

Although I hope that these examples will be helpful, I do not think that users should be required to read through them to understand how exactly a parameter works -- particularly when the ambiguity about path length discussed above can be fixed by adding/adjusting a few words. So, my preference is to both (1) keep these additional examples and (2) keep the proposed clarifications that "path length" is given in terms of edges. I would be happy to pare down the proposed wordings if you feel that they are currently too difficult to read.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think it is conventional to define the number of edges on a path relative to the number of "possible" edges on this path -- instead, this would be expressed as the number of such paths. In this example:

I agree, though this to me lends itself towards leaving mention of explicit edges out of the description. FWIW, NetworkX generally treats paths (in the context of the shortest/simple path finding functions, anyways) as a sequence of nodes. This choice is intentional in that it decouples the path (i.e. the traversal between the nodes) from the edges themselves. This allows for paths to be treated in the same regardless of the specifics of the edges on the graph instance itself (multiedges, weighted edges, etc.) In other words, IMV it's more "networkx-y" to consider paths from the perspective of a sequence of nodes rather than making explicit mention to edges.

That being said this is ultimately subjective - if others feel the wording is an improvement I'd certainly acquiesce!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other words, IMV it's more "networkx-y" to consider paths from the perspective of a sequence of nodes rather than making explicit mention to edges.

I agree with this sentiment, but unfortunately all of these functions already implicitly define "path length" to be in terms of edges :)

FWIW, NetworkX generally treats paths (in the context of the shortest/simple path finding functions, anyways) as a sequence of nodes.

I suspect that what you are describing here is part of the reason why the current descriptions of cutoff are prone to misinterpretation. Since all_simple_paths() (and single_source_shortest_path(), etc.) return paths as lists of nodes, it is surprising to users that these functions' cutoff parameters are in terms of edges, not nodes.

As an example -- I would guess that many people (if given just the current main branch's single_source_shortest_path() documentation, and no ability to e.g. try this function out in a REPL) would assume that cutoff would be in terms of nodes, since nothing in this function's documentation indicates anything to the contrary. To prevent users from writing subtly buggy code, I believe we should be very clear that this is not the case. Adding more examples (as done in this PR, now) will help, but this is not a substitute for explicit documentation.

It sounds like we agree that the ideal solution would be going back in time and changing all of these functions' cutoff parameters to be in terms of nodes instead of edges -- unfortunately, I don't think that is feasible any more (at the very least, such a change would probably be out of scope of this PR). For now, my opinion (on this admittedly subjective situation) is that we should be clear about this, and admit that cutoff is in terms of edges instead of nodes.

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for this one getting stalled @fedarko . I still am not convinced that the updated parameter descriptions are an improvement, but the added docstring examples are unambiguous improvements. Would you be amenable to getting the examples in and continuing to iterate on the parameter descriptions (as necessary) separately?

@fedarko

fedarko commented Jul 24, 2025

Copy link
Copy Markdown
Contributor Author

Thanks, @rossbar!

If needed, I am willing to separate the updates adding docstring examples from the updates to the parameter descriptions. However, I am strongly in favor of updating these parameter descriptions as soon as we can -- the reason I made this PR in the first place is that the current descriptions are actively confusing, and can lead to the development of subtly incorrect code. Other users have shared this perspective (#7676, #2762). We could change only the examples and leave the rest of the text as is, but I do not think this fully fixes the problem.

If you would prefer, I am happy to reword the proposed parameter descriptions. If you don't like mentioning the word edges, maybe we could just say something like (number of nodes) - 1 (as mentioned in #7676)?

In any case, I strongly believe these descriptions should be updated one way or another, even if it makes the documentation a little bit more complicated. The longer this issue remains unresolved, the more people will misinterpret the current documentation and write buggy code.

@rossbar

rossbar commented Jul 25, 2025

Copy link
Copy Markdown
Contributor

If you would prefer, I am happy to reword the proposed parameter descriptions. If you don't like mentioning the word edges, maybe we could just say something like (number of nodes) - 1

Yes - it's the term "edges" that's still the sticking point for me. Your point about the typical interpretation is well-taken, but "edges" means something specific in practice in NetworkX so for the sake of precision and consistency I would like to avoid that term specifically. Re-wording in terms of number of nodes - 1 seems fine to me!

fedarko added 3 commits July 25, 2025 22:09
- don't say "edges", as discussed
- use a better example for the all_simple_edge_paths() cutoff desc
Also adds an example of `cutoff` to the `predecessor()` docstring,
while we're at it. There are some complications w/r/t the cutoff
parameter of that function (see comments on the PR from last year),
so this PR is not modifying anything in the docs there beyond
cosmetic / example changes.
@fedarko

fedarko commented Jul 26, 2025

Copy link
Copy Markdown
Contributor Author

Thanks -- I've adjusted the PR accordingly to say "<= cutoff + 1 nodes" instead of "<= cutoff edges". I also improved some of the relevant docstring examples. Commit 8b36b84 updates the all_simple_paths() and all_simple_edge_paths() docstrings, and commit 9099b5a updates the other functions' docstrings. The changes should be ready for review.

@Peiffap Peiffap left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Since "cutoff + 1" is an expression, it should use double backticks.

Comment thread networkx/algorithms/simple_paths.py Outdated
Comment on lines +329 to +330
You can use the ``cutoff`` parameter to only generate paths that are
shorter than a certain length:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have an opinion w.r.t. either way of saying it being "more natural", but why doesn't this sentence also use something like

Suggested change
You can use the ``cutoff`` parameter to only generate paths that are
shorter than a certain length:
You can use the `cutoff` parameter to only generate paths that contain <= `cutoff + 1` nodes:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worded it this way to match the corresponding existing sentence in the all_simple_paths() docstring:

You can generate only those paths that are shorter than a certain
length by using the `cutoff` keyword argument::
>>> paths = nx.all_simple_paths(G, source=0, target=3, cutoff=2)
>>> print(list(paths))
[[0, 1, 3], [0, 2, 3], [0, 3]]

I suppose one (small) benefit of this wording is that, if the behavior of the cutoff parameter is changed in the future, then this is one less thing to update. However, I don't have strong feelings about it either way -- if it is a blocking issue on this PR I am happy to adjust the wording here.


More generally, looking through the NetworkX codebase for the word certain, it looks like many functions use this word in a similar mildly ambiguous way:

The algorithm iteratively inserts nodes of the input graph in a certain
order and rearranges previously inserted nodes so that the planar drawing
stays valid. This is done efficiently by only maintaining relative

Notes
-----
The average clustering has a hard time getting above a certain
cutoff that depends on `m`. This cutoff is often quite low. The
transitivity (fraction of triangles to possible triangles) seems to
decrease with network size.

For certain small cases, both left and right dominated versions produce
the same sequence. However, for larger values of `m`, the difference in
flipping order becomes evident. For instance, compare the sequences for
``n=6, m=8``:

Hopefully updating all of these is out of scope of this PR :)

@fedarko

fedarko commented Jul 27, 2025

Copy link
Copy Markdown
Contributor Author

Thanks! Adjusted the changes to use double backticks around "cutoff + 1" (but not around just "cutoff", which I think is the desired style looking at the rest of the file). Please let me know if you'd like any additional changes.

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @fedarko for sticking with this through the back-and-forth!

This LGTM! I took the liberty of pushing up a minor formatting change for the new docstring examples, adhering more closely to the numpydoc examples style as opposed to the indented rst-style code blocks. To be clear, both are rendered correctly, but numpydoc-style better conforms with the rest of the library. There are of course notable exceptions, such as the simple_paths docstrings, so it's completely understandable to have used that pattern. Either way, just a formatting nit and both are fine. Thanks again @fedarko !

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Graph Theory literature establishes the definition of the length of a path -- not the NetworkX software and API. Also, many of the functions you are documenting have the word length in the name -- so there is no confusion with the use of the term length <= cutoff.

The all_simple_paths function is the only function that really needs more clarity of text IMO. The others are placed pretty closely with the corresponding length functions. So length is unlikely to be misinterpreted.
Perhaps we can add a phrase to define the length for all_simple_paths; something like:

Depth to stop the search. Only paths of length <= `cutoff` are returned.
Note that the length of an unweighted path is ``len(path) - 1``.

I think examples with cutoffs are helpful in all these functions.
Thanks!

Comment thread networkx/algorithms/simple_paths.py Outdated
Comment on lines +111 to +112
Depth to stop the search. Only paths containing <= ``cutoff + 1`` nodes
are returned.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make this simpler, how about something like:

        Depth to stop the search. Only paths of length <= `cutoff` are returned.
        where the mathematical "length of a path" is `len(path) -1`. 

Comment thread networkx/algorithms/simple_paths.py Resolved

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made some tweaks in wording (suggested above) that had been thumbed-up.
I'm approving the PR and merging. Thanks @fedarko !!

@dschult
dschult merged commit c6782d8 into networkx:main Oct 27, 2025
@dschult dschult added this to the 3.6 milestone Oct 27, 2025
@fedarko

fedarko commented Oct 27, 2025 •

Copy link
Copy Markdown
Contributor Author

Thank you all! Sorry for letting this slip -- just finished moving and starting a new job... Glad the documentation changes are in now.

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

Development

Successfully merging this pull request may close these issues.

4 participants