Skip to content

Commit 451b325

Browse files
committed
graphs/modular_decomposition: improve docstrings and fix coding style
1 parent 90c6e20 commit 451b325

File tree

4 files changed

+52
-22
lines changed

4 files changed

+52
-22
lines changed

src/sage/graphs/graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7264,7 +7264,7 @@ def is_module(self, vertices):
72647264
if v not in self:
72657265
raise LookupError(f"vertex ({v}) is not a vertex of the graph")
72667266

7267-
if len(M) == 0 or len(M) == 1 or len(M) == self.order():
7267+
if len(M) <= 1 or len(M) == self.order():
72687268
return True
72697269

72707270
N = None # will contains the neighborhood of M

src/sage/graphs/graph_decompositions/modular_decomposition.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*
22
* Copyright (C) 2024 Cyril Bouvier <[email protected]>
33
*
4-
*This program is free software: you can redistribute it and/or modify
5-
*it under the terms of the GNU General Public License as published by
6-
*the Free Software Foundation, either version 2 of the License, or
7-
*(at your option) any later version.
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 2 of the License, or
7+
* (at your option) any later version.
88
* https://www.gnu.org/licenses/
99
*/
1010

src/sage/graphs/graph_decompositions/modular_decomposition.pxd

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ cdef extern from "modular_decomposition.hpp":
1414
bool is_parallel() const
1515
bool is_series() const
1616
cpplist[md_tree_node *] children
17-
# For a leaf, the corresponding vertex, for a internal node, any vertex
18-
# corresponding to a any leaf below the node
17+
# If is_leaf() is true, the attribute 'vertex' contains the id of the
18+
# corresponding vertex. If is_leaf() is false, the attribute 'vertex'
19+
# contains the id of a vertex corresponding to any leaf below
20+
# the node (i.e., 'vertex' contains the id of a vertex belonging to the
21+
# module corresponding to the node).
1922
int vertex
2023

2124
void dealloc_md_tree_nodes_recursively(md_tree_node *)

src/sage/graphs/graph_decompositions/modular_decomposition.pyx

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def corneil_habib_paul_tedder_algorithm(G):
5555
OUTPUT: an object of type Node representing the modular decomposition tree
5656
of the graph G
5757
58-
This function compute the modular decomposition of the given graph by the
58+
This function computes the modular decomposition of the given graph by the
5959
algorithm of Corneil, Habib, Paul and Tedder [TCHP2008]_. It is a recursive,
6060
linear-time algorithm that first computes the slice decomposition of the
6161
graph (via the extended lexBFS algorithm) and then computes the modular
@@ -112,17 +112,16 @@ cdef object _md_tree_node_to_md_tree_inner_rec(const md_tree_node *n,
112112
cdef md_tree_node *c
113113
if deref(n).is_leaf():
114114
return Node.create_leaf(Gb.vertex_label(deref(n).vertex))
115-
else:
116-
if deref(n).is_series():
117-
node = Node(NodeType.SERIES)
118-
elif deref(n).is_parallel():
119-
node = Node(NodeType.PARALLEL)
120-
else: # is_prime
121-
node = Node(NodeType.PRIME)
122-
node.children.extend(
123-
_md_tree_node_to_md_tree_inner_rec(c, Gb)
124-
for c in deref(n).children)
125-
return node
115+
116+
if deref(n).is_series():
117+
node = Node(NodeType.SERIES)
118+
elif deref(n).is_parallel():
119+
node = Node(NodeType.PARALLEL)
120+
else: # is_prime
121+
node = Node(NodeType.PRIME)
122+
node.children.extend(_md_tree_node_to_md_tree_inner_rec(c, Gb)
123+
for c in deref(n).children)
124+
return node
126125

127126

128127
cdef object md_tree_node_to_md_tree(const md_tree_node *n, CGraphBackend Gb):
@@ -151,8 +150,7 @@ cdef object md_tree_node_to_md_tree(const md_tree_node *n, CGraphBackend Gb):
151150
"""
152151
if n == NULL:
153152
return Node(NodeType.EMPTY)
154-
else:
155-
return _md_tree_node_to_md_tree_inner_rec(n, Gb)
153+
return _md_tree_node_to_md_tree_inner_rec(n, Gb)
156154

157155

158156
################################################################################
@@ -321,7 +319,7 @@ class Node:
321319
@classmethod
322320
def create_leaf(cls, v):
323321
"""
324-
Return Node object that is a leaf corresponding to the vertex ``v``
322+
Return Node object that is a leaf corresponding to the vertex ``v``.
325323
326324
INPUT:
327325
@@ -621,6 +619,21 @@ def modular_decomposition(G, algorithm=None):
621619
This function should not be used directly, it should be called via the
622620
``modular_decomposition`` method of ``Graph``.
623621
622+
INPUT:
623+
624+
- ``G`` -- graph whose modular decomposition tree is to be computed
625+
626+
- ``algorithm`` -- string (default: ``None``); the algorithm to use among:
627+
628+
- ``None`` or ``'corneil_habib_paul_tedder'`` -- will use the
629+
Corneil-Habib-Paul-Tedder algorithm from [TCHP2008]_, its complexity
630+
is linear in the number of vertices and edges.
631+
632+
- ``'habib_maurer'`` -- will use the Habib-Maurer algorithm from
633+
[HM1979]_, its complexity is cubic in the number of vertices.
634+
635+
OUTPUT: The modular decomposition tree, as an object of type ``Node``.
636+
624637
TESTS::
625638
626639
sage: from sage.graphs.graph_decompositions.modular_decomposition import *
@@ -1472,10 +1485,24 @@ def check_algos_are_equivalent(trials, graph_gen, verbose=False):
14721485
Verify that both algorithms compute the same tree (up to equivalence) for
14731486
random graphs.
14741487
1488+
INPUT:
1489+
1490+
- ``trials`` -- integer; the number of tests the function will run.
1491+
1492+
- ``graph_gen`` -- function; a function that can be called without argument
1493+
and returns a random graph.
1494+
1495+
- ``verbose`` -- boolean (defaul: ``False``); enable printing debug
1496+
information.
1497+
1498+
OUTPUT: ``None``. Raises an ``AssertionError`` on failure.
1499+
14751500
EXAMPLES::
14761501
14771502
sage: from sage.graphs.graph_decompositions.modular_decomposition import *
1503+
sage: check_algos_are_equivalent(3, lambda : graphs.RandomGNP(10, 0.1))
14781504
sage: check_algos_are_equivalent(3, lambda : graphs.RandomGNP(10, 0.5))
1505+
sage: check_algos_are_equivalent(3, lambda : graphs.RandomGNP(10, 0.9))
14791506
"""
14801507
for _ in range(trials):
14811508
graph = graph_gen()

0 commit comments

Comments
 (0)