Skip to content

Fix behavior for iterable sources argument in bfs_layers - #8013

Merged
dschult merged 2 commits into
networkx:mainfrom
Peiffap:gh-8009
Apr 30, 2025
Merged

dschult merged 2 commits into
networkx:mainfrom
Peiffap:gh-8009

Conversation

@Peiffap

@Peiffap Peiffap commented Apr 30, 2025

Copy link
Copy Markdown
Member

This addresses the issue reported in #8009.
It makes sense to me to ensure the function works correctly for generators as well; especially since the fix is as simple as this.
I also thought it was more reasonable to gracefully handle duplicate nodes in the sources argument; at every depth other than 0, nodes never appear multiple times (because of the visited set), and I see no convincing reason to have the sources be handled any differently.

List of changes:

  • Avoid silent failure when sources is a generator
  • Only handle duplicate nodes once
  • Add test cases where sources is a generator and list with duplicate elements
  • Minor clarifications in docstring

Fixes gh-8009.

* Avoid silent failure when `sources` is a generator
* Only handle duplicate nodes once
* Add test cases where `sources` is a generator and list with duplicate elements
* Minor clarifications in docstring

Fixes networkxgh-8009
@amcandio

Copy link
Copy Markdown
Contributor

Nice! Can you add an explicit test case passing a generator?

@Peiffap

Peiffap commented Apr 30, 2025

Copy link
Copy Markdown
Member Author

Can you add an explicit test case passing a generator?

I did, but it's not very clear looking at the diff.
The third test case in the for loop uses sources = (i for i in [0]).

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

Got it. LGTM

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

Looks pretty good to me, thanks @Peiffap for the PR and @amcandio for the first review!

I left one minor suggestion

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

Looks good to me!
Thanks @Peiffap for the quick fix. And @RoJamieson for the report!
And a very nice review team response too. :)

@dschult
dschult merged commit aab0380 into networkx:main Apr 30, 2025
@Peiffap
Peiffap deleted the gh-8009 branch May 1, 2025 13:28
@jarrodmillman jarrodmillman added this to the 3.5 milestone May 29, 2025
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.

bfs_layers revisits source nodes in sources if sources an generator

5 participants