Skip to content

Commit 0fd4827

Browse files
committed
graphs/modular_decomposition: add tests to check equivalence of both algos
1 parent 5cc82cc commit 0fd4827

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/sage/graphs/graph_decompositions/modular_decomposition.pyx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,11 @@ def modular_decomposition(G, algorithm=None):
631631
sage: modular_decomposition(Graph(1))
632632
NORMAL [0]
633633
634+
sage: check_algos_are_equivalent(5,\
635+
....: lambda : graphs.RandomProperIntervalGraph(100))
636+
637+
sage: check_algos_are_equivalent(5, lambda : graphs.RandomGNM(75, 1000))
638+
634639
sage: modular_decomposition(DiGraph())
635640
Traceback (most recent call last):
636641
...
@@ -1459,3 +1464,30 @@ def recreate_decomposition(trials, algorithm, max_depth, max_fan_out,
14591464
assert equivalent_trees(rand_tree, reconstruction)
14601465
if verbose:
14611466
print("Passes!")
1467+
1468+
@random_testing
1469+
def check_algos_are_equivalent(trials, graph_gen, verbose=False):
1470+
r"""
1471+
Verify that both algorithms compute the same tree (up to equivalence) for
1472+
random graphs.
1473+
1474+
EXAMPLES::
1475+
1476+
sage: from sage.graphs.graph_decompositions.modular_decomposition import *
1477+
sage: check_algos_are_equivalent(3, lambda : graphs.RandomGNP(10, 0.5))
1478+
"""
1479+
for _ in range(trials):
1480+
graph = graph_gen()
1481+
if verbose:
1482+
print(graph.graph6_string())
1483+
print(graph.to_dictionary())
1484+
MD = []
1485+
for algo in ('habib_maurer', 'corneil_habib_paul_tedder'):
1486+
md = modular_decomposition(graph, algorithm=algo)
1487+
MD.append(md)
1488+
if verbose:
1489+
print(f'Using {algo}:')
1490+
print_md_tree(md)
1491+
assert equivalent_trees(MD[0], MD[1])
1492+
if verbose:
1493+
print("Passes!")

0 commit comments

Comments
 (0)