Repository navigation
Clarify the meaning of the cutoff parameter in some path-finding functions - #7487
Conversation
|
The docstring updates look good and make sense to me. |
|
Understood, thank you for the detailed feedback! I didn't realize that the adjustment to I suspect that refactoring Thanks again! |
matches all_pairs_shortest_path(), and looks nice in the online docs
| 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. |
There was a problem hiding this comment.
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}
There was a problem hiding this comment.
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:
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 0contains 0 edges,the shortest path to 1 from 0contains 1 edge, andthe shortest path to 2 from 0contains 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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
also small wording change in the all_pairs_shortest_path_length example
rossbar
left a comment
There was a problem hiding this comment.
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?
|
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 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. |
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 |
- 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.
|
Thanks -- I've adjusted the PR accordingly to say "<= |
Peiffap
left a comment
There was a problem hiding this comment.
Nit: Since "cutoff + 1" is an expression, it should use double backticks.
| You can use the ``cutoff`` parameter to only generate paths that are | ||
| shorter than a certain length: |
There was a problem hiding this comment.
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
| 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: |
There was a problem hiding this comment.
I worded it this way to match the corresponding existing sentence in the all_simple_paths() docstring:
networkx/networkx/algorithms/simple_paths.py
Lines 136 to 141 in 6eca932
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:
networkx/networkx/algorithms/planar_drawing.py
Lines 11 to 13 in 6eca932
networkx/networkx/generators/random_graphs.py
Lines 1027 to 1032 in 6eca932
networkx/networkx/algorithms/threshold.py
Lines 946 to 949 in 6eca932
Hopefully updating all of these is out of scope of this PR :)
|
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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!
| Depth to stop the search. Only paths containing <= ``cutoff + 1`` nodes | ||
| are returned. |
There was a problem hiding this comment.
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`.
|
Thank you all! Sorry for letting this slip -- just finished moving and starting a new job... Glad the documentation changes are in now. |

Motivation
While I was working with
all_simple_paths(), I got confused about thecutoffparameter. The current description for this parameter isHow "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 saidkeyed by targetwhere it should, I think, have saidkeyed 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→ update: per discussion below, not addressing in this PR.predecessor().cutoffparameter explicitly check ifcutoff is Noneand adjust their behavior accordingly. However,predecessor()does not do this -- instead, it checks the truth value ofcutoff. This will have (I think) undesirable results ifcutoff == 0, sincecutoff == 0will be treated the same ascutoff == 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.)if cutoffin this function withif cutoff is not Noneto fix this problem.Notes
predecessor()also has acutoffparameter, 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.