Skip to content

Clarify that bfs_predecessors() does not actually search through predecessors - #8445

Merged
dschult merged 5 commits into
networkx:mainfrom
fedarko:bfs-pred-docs-fix-8442
Jan 8, 2026
Merged

dschult merged 5 commits into
networkx:mainfrom
fedarko:bfs-pred-docs-fix-8442

Conversation

@fedarko

@fedarko fedarko commented Jan 3, 2026

Copy link
Copy Markdown
Contributor

Adds a paragraph to the docs of bfs_predecessors() addressing the confusion I had in #8442. This confusion has also been brought up in #710.

I tried to keep the wording fairly casual here; I think being explicit about this is best. Please let me know if you'd like any edits.

make it closer to @Swaritz's comment in networkx#8442, which explains the
situation well
@amcandio

amcandio commented Jan 3, 2026

Copy link
Copy Markdown
Contributor

Question: would this https://networkx.org/documentation/stable/reference/classes/generated/networkx.DiGraph.reverse.html have solved your original problem? Might be worth mentioning it in the docs update.

@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.

Since there isn't currently a paragraph description of what this function does, the warning pragraph seems out of place. Can we resolve most of the confusion be starting with a paragraph on what this actually does return, so the warning can be an add-on sentence at the end?
Perhaps something like:

The yielded node pairs are each node along with its predecessor in the
breadth-first-search. That is, the predecessor is the node from which the
BFS moved directly to the node indicated. This should not be confused
with the more general term predecessors of nodes in directed graphs.

@fedarko

fedarko commented Jan 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Thank you!

Question: would this https://networkx.org/documentation/stable/reference/classes/generated/networkx.DiGraph.reverse.html have solved your original problem? Might be worth mentioning it in the docs update.

It would have helped a bit; however, the main thing I was trying to achieve was finding all nodes within some distance of a given set of source nodes in a directed graph (ignoring edge directionality). So, I wanted to search both backwards and forwards.

I ended up doing this by using DiGraph.to_undirected() to get an undirected view of the graph, then using nx.bfs_layers() to traverse this undirected graph view. I was hesitant to take this approach due to worries about performance (calling .to_undirected() seemed wasteful to me, since it would not actually be creating any "new information"?) but this was the most elegant solution I could find for my particular problem.

Since there isn't currently a paragraph description of what this function does, the warning pragraph seems out of place. Can we resolve most of the confusion be starting with a paragraph on what this actually does return, so the warning can be an add-on sentence at the end?

Understood. I added a paragraph explaining the return value of the function, and adjusted the warning accordingly. The warning is currently included as a second paragraph below the return-value explanation paragraph; I think being explicit in the warning will make this documentation easier to interpret than having it be a single add-on sentence. However, if you would prefer the clarification be shortened, I can change that.

@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.

IMO the added wording is quite dense and doesn't necessarily relate to the function itself, but rather higher-level terminology. IMO the info in the first sentence is adequately captured in the return type description.

If anything, I'd vote for simply noting that bfs_predecessors is equivalent to yield (t, s) for s, t in bfs_edges(G, source)

@fedarko

fedarko commented Jan 6, 2026 •

Copy link
Copy Markdown
Contributor Author

IMO the added wording is quite dense and doesn't necessarily relate to the function itself, but rather higher-level terminology.

Happy to rewrite things as needed. Maybe trimming down the second paragraph to something like

Note that, despite its use of the term "predecessor," this function does
not perform BFS "backwards" (i.e. traversing nodes' incoming edges) in
directed graphs. It instead performs an ordinary "forwards" BFS,
traversing nodes' outgoing edges.

... if that is acceptable?

IMO the info in the first sentence is adequately captured in the return type description.

If you would like I can remove the first sentence or paragraph, although I believe this would contradict @dschult's request. Or maybe rewriting the first paragraph to be more "casual" would make sense?

If anything, I'd vote for simply noting that bfs_predecessors is equivalent to yield (t, s) for s, t in bfs_edges(G, source)

This is a very elegant way of expressing this, but I suspect it is impenetrable to anyone who doesn't already know the NetworkX API -- it is not immediately obvious what bfs_edges() does. In my opinion, such a note is more dense than the currently proposed changes (which are my attempt at a plain english description).

So far multiple people (at least me and #710) have seen this function's documentation, skimmed it, and assumed that it searched backwards -- this assumption matches the majority of other uses of the term predecessor in NetworkX's API. Thinking back on it, I doubt that seeing a note saying "This function is equivalent to yield (t, s) for s, t in bfs_edges(G, source)" would have done anything to dispel this confusion. (Of course, if I had seen this note and then gone to the trouble of looking up bfs_edges() then I would have understood things, but I think function documentation should ideally be self-contained :)

For this reason, I am in favor of adding some sort of warning to avoid other people going down similar rabbit holes.

@fedarko

fedarko commented Jan 7, 2026

Copy link
Copy Markdown
Contributor Author

Thank you, @dschult! I added your suggestion, plus a small grammar edit.

@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.

This level of detail seems appropriate to me, thanks @fedarko and @dschult !

@dschult
dschult merged commit e577796 into networkx:main Jan 8, 2026
46 checks passed
@dschult dschult added this to the 3.7 milestone Jan 8, 2026
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