Skip to content

Fix docstring example of nx.generate_random_paths(index_map=...) - #7832

Merged
rossbar merged 2 commits into
networkx:mainfrom
eriknw:fix_random_paths_example
Feb 9, 2025
Merged

rossbar merged 2 commits into
networkx:mainfrom
eriknw:fix_random_paths_example

Conversation

@eriknw

@eriknw eriknw commented Feb 5, 2025

Copy link
Copy Markdown
Contributor

As discovered in #7817 (comment), the docstring example with using index_map is incorrect, because it was trying to index into a generator.

This argument is a little awkward to use since this function is a generator and index_map will be updated incrementally, but I believe the updated docstring example is what was intended.

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

This looks good to me.
Why use a dict(enumerate(... instead of a list? Both work in this case (I think). The paths will always be indexed by contiguous ints in index_map right? So would it be better to suggest using list(?
Thanks for this!

@eriknw

eriknw commented Feb 8, 2025

Copy link
Copy Markdown
Contributor Author

Ha, yeah, I'll change to list. Good catch, my bad!

>>> G = nx.star_graph(3)
>>> index_map = {}
>>> random_path = nx.generate_random_paths(G, 3, index_map=index_map)
>>> random_paths = list(nx.generate_random_paths(G, 3, index_map=index_map))

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.

Suggested change
>>> random_paths = list(nx.generate_random_paths(G, 3, index_map=index_map))
>>> random_paths = list(nx.generate_random_paths(G, 3, index_map=index_map, seed=42))

... random_path[path_idx] for path_idx in index_map.get(0, [])
... random_paths[path_idx] for path_idx in index_map.get(0, [])
... ]

@Schefflera-Arboricola Schefflera-Arboricola Feb 8, 2025 •

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.

Suggested change
>>> paths_containing_node_0
[[2, 0, 1, 0, 2, 0], [2, 0, 3, 0, 3, 0], [1, 0, 3, 0, 3, 0]]

or we can also display all the random paths.

@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 @eriknw - let's get this in!

+1 for @Schefflera-Arboricola 's suggestions too, but let's move to a follow-up PR so as not to confuse the approvals!

@rossbar
rossbar merged commit 1526acc into networkx:main Feb 9, 2025
@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.

5 participants