Repository navigation
Add is_perfect_graph function and tests using Strong Perfect Graph Theorem - #8111
sourabh-sudesh-paradeshi wants to merge 1 commit into
Conversation
|
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! |
|
Thanks! Can you clear the unrelated files you committed? |
336ba58 to
630411d
Compare
Sure! I've removed the unrelated files from the PR. Please let me know if any other changes are needed. |
rossbar
left a comment
There was a problem hiding this comment.
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.
e547f2f to
cbf38d0
Compare
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
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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!
| 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 |
There was a problem hiding this comment.
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:
- 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)
- It allows the function to be independently tested
- It makes it easier to replace the function in the future if a better implementation is found.
| if has_induced_odd_hole(G): | ||
| return False | ||
| if has_induced_odd_hole(nx.complement(G)): | ||
| return False |
There was a problem hiding this comment.
Minor style nit, but might as well make this a single statement with an or
rossbar
left a comment
There was a problem hiding this comment.
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
…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).
|
Continuing the work in #8318 - thanks for getting the conversation started @sourabh-sudesh-paradeshi ! |
* 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]>
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.