Skip to content

Conversation

@rudyarthur
Copy link
Contributor

@rudyarthur rudyarthur commented Jul 12, 2025

This PR adds Generalized Petersen Graph as discussed in #8133

@dschult is going to take this on and hopefully get it merged.

Copy link
Member

@dschult dschult left a comment

Choose a reason for hiding this comment

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

Thanks for this. I have some comments below.

@dschult
Copy link
Member

dschult commented Nov 6, 2025

I have updated this Generalized Petersen Graph pull request.

I added tests in the test_small.py module. I added cross-links in docs for the graphs that are isomorphic to a generalized petersen graph. I updated the function to allow the case when k == n/2 because wikipedia says (and has citations) "some people allow k == n/2. " Made the function check for directed versions of create_using and raise if it is. The decorator in small.py only works with functions that have create_using as the only input parameter. And the @not_implemented_for decorator doesn't work because G is not an argument of the function. I added a doc_string citation to the wikipedia page, and connected the doc_string to the docs.

:) Should be ready for review.

raise NetworkXError(f" Got {n=} {k=}. Need 1 <= k <= n/2")

G = nx.cycle_graph(range(n), create_using=create_using) # u-nodes
if G.is_directed():
Copy link
Contributor

Choose a reason for hiding this comment

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

Is multi graph possible? I think there is only one class that makes sense here. Should we drop create_using?

Copy link
Contributor

Choose a reason for hiding this comment

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

We keep the create_using so that folks can pass in their own duck-type-compatible graph classes if they want to. It looks like an antipattern in NX itself, but it's a nice feature for downstream folks!

Copy link
Member

Choose a reason for hiding this comment

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

We also allow a multigraph in all the small.py generators because the result is the desired graph even if it does not have minimal possible graph class representation. I mean the nodes and edges are the same for both Graph and MultiGraph versions. So petersen_graph(create_using=nx.MultiGraph) is the Petersen Graph -- it's just a multigraph representation without any multiedges. It doesn't contain any multiedges. But it could be changed to contain multiedges. Anyway, someone might want to do that -- and it is correct. So we allow MultiGraph (and any undirected subclass of Graph that a user creates).

Copy link
Contributor

@rossbar rossbar left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @rudyarthur and @dschult , and @amcandio for the insightful comments!

assert [d for n, d in G.degree()] == 46 * [3]

# Test create_using with directed or multigraphs on small graphs
pytest.raises(nx.NetworkXError, nx.tutte_graph, create_using=nx.DiGraph)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to add this one back below? Or perhaps it's duplicated elsewhere and I just missed it since I'm looking only at the diff... if so, ignore!

Copy link
Member

Choose a reason for hiding this comment

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

This test was duplicated in the next test function "tests_raises_with_directed_create_using" so I removed it here. Thanks for noticing that and asking -- I should have mentioned it in the PR description.

@amcandio
Copy link
Contributor

amcandio commented Nov 7, 2025

LGTM too!

Co-authored-by: Ross Barnowski <rossbar@caltech.edu>
@dschult dschult merged commit 79e113b into networkx:main Nov 8, 2025
50 checks passed
@dschult dschult added this to the 3.6 milestone Nov 8, 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.

4 participants