Skip to content

Add is_perfect_graph function and tests using Strong Perfect Graph Theorem - #8111

Closed
sourabh-sudesh-paradeshi wants to merge 1 commit into
networkx:mainfrom
sourabh-sudesh-paradeshi:is-perfect-graph
Closed

sourabh-sudesh-paradeshi wants to merge 1 commit into
networkx:mainfrom
sourabh-sudesh-paradeshi:is-perfect-graph

Conversation

@sourabh-sudesh-paradeshi

@sourabh-sudesh-paradeshi sourabh-sudesh-paradeshi commented Jun 12, 2025 •

Copy link
Copy Markdown
Contributor

This PR adds the is_perfect_graph function to check whether an undirected graph is perfect, based on the Strong Perfect Graph Theorem (SPGT).

Returns True if neither the graph nor its complement contains an induced odd cycle of length ≥ 5.

Uses cycle_basis to detect induced odd holes and antiholes.

Includes a fast check using chordality and an optional max_cycle_length parameter.

Adds tests for complete graphs, odd/even cycles, and complements.

This complements NetworkX’s structural graph tools by enabling efficient perfectness checks.

@sourabh-sudesh-paradeshi

Copy link
Copy Markdown
Contributor Author

Hi! This PR is ready for review. Kindly consider adding the type: enhancement label so the CI passes. Let me know if any changes are needed. Thank you!

@amcandio

Copy link
Copy Markdown
Contributor

Thanks! Can you clear the unrelated files you committed?

@sourabh-sudesh-paradeshi
sourabh-sudesh-paradeshi force-pushed the is-perfect-graph branch 3 times, most recently from 336ba58 to 630411d Compare June 13, 2025 19:20
@sourabh-sudesh-paradeshi

sourabh-sudesh-paradeshi commented Jun 13, 2025 •

Copy link
Copy Markdown
Contributor Author

Thanks! Can you clear the unrelated files you committed?

Sure! I've removed the unrelated files from the PR. Please let me know if any other changes are needed.

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

An algorithm for checking whether graphs are perfect graphs strikes me as a good fit for NetworkX. Thanks for the proposal!

There are some high-level considerations to hammer out (see inline comment) and then some tweaks to the code to improve things (e.g. get rid of the is True 's, etc.) but overall I'm in favor of this approach.

Comment thread networkx/algorithms/perfect_graph.py Resolved
@sourabh-sudesh-paradeshi
sourabh-sudesh-paradeshi force-pushed the is-perfect-graph branch 2 times, most recently from e547f2f to cbf38d0 Compare June 25, 2025 08:26
@sourabh-sudesh-paradeshi

Copy link
Copy Markdown
Contributor Author

An algorithm for checking whether graphs are perfect graphs strikes me as a good fit for NetworkX. Thanks for the proposal!

There are some high-level considerations to hammer out (see inline comment) and then some tweaks to the code to improve things (e.g. get rid of the is True 's, etc.) but overall I'm in favor of this approach.

Thanks! I've removed max_cycle_length, cleaned up the logic, and squashed the commit. All related tests pass — the one failure seems unrelated. Let me know if anything else’s needed.

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

It's not a blocker at all, but I'm curious if you've any insight on the path-based hole finding versus other "hole-finder" approaches. For example, what about using simple_cycles to enumerate all cycles, filtering on odd ones with len > 4, then checking the degree of all nodes in these filtered cycles is 2? Again not a blocker, but I'm curious about any additional research you may have done for the implementation!

Finally we'll have to make sure that this function is properly exported to the nx namespace (and think about the subpackage name) and included in the reference docs.

def is_perfect_graph(G):
"""Return True if G is a perfect graph, else False.

According to the Strong Perfect Graph Theorem, a graph is perfect

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.

A reference should be included here, my vote would be to the wiki article.


According to the Strong Perfect Graph Theorem, a graph is perfect
if and only if neither it nor its complement contains an induced
odd cycle of length at least 5.

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.

Similarly this docstring could include some information about the time complexity of the algorithm, since this is one of the big payoffs of the strong perfect graph theorem (i.e. the existence of a polynomial-time algorithm for checking whether a graph if perfect).

Finally, the docstring should be formatted according to the numpydoc style guide - i.e. there should be a Parameters section, etc. See other docstrings in the library for inspiration!

Comment on lines +14 to +31
def has_induced_odd_hole(graph):
nodes = list(graph.nodes)
n = len(nodes)
for i in range(n):
for j in range(i + 1, n):
try:
paths = nx.all_simple_paths(graph, nodes[i], nodes[j], cutoff=n)
except nx.NetworkXNoPath:
continue
for path in paths:
if len(path) < 5 or len(path) % 2 == 0:
continue
if graph.has_edge(path[-1], path[0]):
cycle_nodes = path
subG = graph.subgraph(cycle_nodes)
if subG.number_of_edges() == len(cycle_nodes):
return True
return False

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 this would be better as an external "private" function (i.e. def _has_induced_odd_hole) rather than a nested function. Doing so would have several advantages:

  1. Users don't have to worry about multiple scopes when reading the code (though arguably that's trivial in this case as there are no variables that cross scope boundaries)
  2. It allows the function to be independently tested
  3. It makes it easier to replace the function in the future if a better implementation is found.

Comment on lines +33 to +36
if has_induced_odd_hole(G):
return False
if has_induced_odd_hole(nx.complement(G)):
return False

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.

Minor style nit, but might as well make this a single statement with an or

@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 one's been stale for a while - are you still intending on working on this @sourabh-sudesh-paradeshi ? If so, I think an implementation based on chordless_cycles would generally be much more efficient than the proposed has_induced_odd_hole

Aka2210 added a commit to Aka2210/networkx that referenced this pull request Oct 9, 2025
…string

Refactors the `is_perfect_graph` function (originally proposed in PR networkx#8111)
to use `nx.chordless_cycles` for induced odd-hole detection instead of the
path-based enumeration approach. This improves clarity and efficiency while
keeping the implementation faithful to the Strong Perfect Graph Theorem (SPGT).
@rossbar

rossbar commented Oct 9, 2025

Copy link
Copy Markdown
Contributor

Continuing the work in #8318 - thanks for getting the conversation started @sourabh-sudesh-paradeshi !

@rossbar rossbar closed this Oct 9, 2025
dschult pushed a commit that referenced this pull request Oct 16, 2025
* Add is_perfect_graph function and tests using Strong Perfect Graph Theorem

* REF: Refactor is_perfect_graph to use chordless_cycles and expand docstring

Refactors the `is_perfect_graph` function (originally proposed in PR #8111)
to use `nx.chordless_cycles` for induced odd-hole detection instead of the
path-based enumeration approach. This improves clarity and efficiency while
keeping the implementation faithful to the Strong Perfect Graph Theorem (SPGT).

* DOC: Expand Notes section in is_perfect_graph with subclass references

Adds a third "Related Specialized Algorithms" section to the docstring of
`is_perfect_graph`, mentioning existing recognition functions for chordal and
bipartite. This clarifies the relationship between these subclasses and the
general SPGT-based check.

* Update networkx/algorithms/perfect_graph.py

Co-authored-by: Ross Barnowski <[email protected]>

* Update networkx/algorithms/perfect_graph.py

Co-authored-by: Ross Barnowski <[email protected]>

* Update networkx/algorithms/tests/test_perfect_graph.py

Co-authored-by: Ross Barnowski <[email protected]>

* Update networkx/algorithms/perfect_graph.py

Co-authored-by: Ross Barnowski <[email protected]>

* TST: Use nx.is_perfect_graph instead of direct call in test

* STY: Fix docstring spacing in See Also section

* DOC: Add reference page for perfect graph module

* DOC: Add perfect_graph to algorithms index.rst

---------

Co-authored-by: Sourabh Sudesh Paradeshi <[email protected]>
Co-authored-by: Ross Barnowski <[email protected]>
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.

3 participants