Skip to content

Commit

Permalink
gh-38301: graph: modular decomposition of a single vertex should be a…
Browse files Browse the repository at this point in the history
… single tree node

    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes #12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes #12345". -->

The modular decomposition tree computed by Sage for a graph with a
single vertex is wrong (tested in Sage from 9.8-10.4.rc0)

```py
sage: Graph(1).modular_decomposition()
(PRIME, [0])
```

Sage returns a tree with a `PRIME` root with a single child representing
the vertex `0`, but according to the recursive definition of a modular
decomposition tree, it should be a single node tree ([see Wikipedia for 
example](https://en.wikipedia.org/wiki/Modular_decomposition#:~:text=As%
20a%20base%20case%2C%20if,is%20a%20single%20tree%20node.))

This PR fixes this issue. It is an easy fix, as the case of single
vertex graphs was treated separately in the `modular_decomposition`
method of `Graph`.
Two doctests were modified to reflect this changes.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [ ] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - #12345: short description why this is a dependency -->
<!-- - #34567: ... -->
    
URL: #38301
Reported by: cyrilbouvier
Reviewer(s): cyrilbouvier, David Coudert
  • Loading branch information
Release Manager committed Jul 20, 2024
2 parents 4efc355 + 420692b commit 22dbf40
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/sage/graphs/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -8159,9 +8159,9 @@ def modular_decomposition(self, algorithm=None, style='tuple'):
Singleton Vertex::
sage: Graph(1).modular_decomposition()
(PRIME, [0])
0
sage: Graph(1).modular_decomposition(style='tree')
PRIME[0[]]
0[]
Vertices may be arbitrary --- check that :issue:`24898` is fixed::
Expand Down Expand Up @@ -8209,8 +8209,7 @@ def modular_decomposition(self, algorithm=None, style='tuple'):
if not self.order():
D = None
elif self.order() == 1:
D = create_prime_node()
D.children.append(create_normal_node(self.vertices(sort=False)[0]))
D = create_normal_node(next(self.vertex_iterator()))
else:
D = habib_maurer_algorithm(self)

Expand Down

0 comments on commit 22dbf40

Please sign in to comment.