Skip to content

Commit 270bd48

Browse files
committed
tree_decomposition.pyx: Add tests for small simple graphs; change docstring
1 parent 8b2c666 commit 270bd48

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/sage/graphs/graph_decompositions/tree_decomposition.pyx

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ def make_nice_tree_decomposition(graph, tree_decomp):
817817
INPUT:
818818
819819
- ``graph`` -- a Sage graph
820+
820821
- ``tree_decomp`` -- a tree decomposition
821822
822823
OUTPUT:
@@ -853,7 +854,7 @@ def make_nice_tree_decomposition(graph, tree_decomp):
853854
sage: make_nice_tree_decomposition(bip_one_four, bip_one_four_TD)
854855
Nice tree decomposition of Tree decomposition: Graph on 15 vertices
855856
856-
The following two examples are for testing GitHub Issue 36843::
857+
Check that :issue:`36843` is fixed::
857858
858859
sage: from sage.graphs.graph_decompositions.tree_decomposition import make_nice_tree_decomposition
859860
sage: triangle = graphs.CompleteGraph(3)
@@ -868,6 +869,30 @@ def make_nice_tree_decomposition(graph, tree_decomp):
868869
sage: graph_TD = graph.treewidth(certificate=True)
869870
sage: make_nice_tree_decomposition(graph, graph_TD)
870871
Nice tree decomposition of Tree decomposition: Graph on 25 vertices
872+
873+
::
874+
875+
sage: from sage.graphs.graph_decompositions.tree_decomposition import make_nice_tree_decomposition
876+
sage: empty_graph = graphs.EmptyGraph()
877+
sage: tree_decomp = empty_graph.treewidth(certificate=True)
878+
sage: len(make_nice_tree_decomposition(empty_graph, tree_decomp))
879+
0
880+
881+
::
882+
883+
sage: from sage.graphs.graph_decompositions.tree_decomposition import make_nice_tree_decomposition
884+
sage: singleton = graphs.CompleteGraph(1)
885+
sage: tree_decomp = singleton.treewidth(certificate=True)
886+
sage: make_nice_tree_decomposition(singleton, tree_decomp)
887+
Nice tree decomposition of Tree decomposition: Graph on 3 vertices
888+
889+
::
890+
891+
sage: from sage.graphs.graph_decompositions.tree_decomposition import make_nice_tree_decomposition
892+
sage: an_edge = graphs.CompleteGraph(2)
893+
sage: tree_decomp = an_edge.treewidth(certificate=True)
894+
sage: make_nice_tree_decomposition(an_edge, tree_decomp)
895+
Nice tree decomposition of Tree decomposition: Graph on 5 vertices
871896
"""
872897
if not is_valid_tree_decomposition(graph, tree_decomp):
873898
raise ValueError("input must be a valid tree decomposition for this graph")
@@ -1035,6 +1060,7 @@ def make_nice_tree_decomposition(graph, tree_decomp):
10351060

10361061
return nice_tree_decomp
10371062

1063+
10381064
def label_nice_tree_decomposition(nice_TD, root, directed=False):
10391065
r"""
10401066
Return a nice tree decomposition with nodes labelled accordingly.
@@ -1045,7 +1071,9 @@ def label_nice_tree_decomposition(nice_TD, root, directed=False):
10451071
10461072
- ``root`` -- the root of the nice tree decomposition
10471073
1048-
- ``directed`` -- boolean (default: ``False``); whether to return the directed graph
1074+
- ``directed`` -- boolean (default: ``False``); whether to return the nice
1075+
tree decomposition as a directed graph rooted at vertex ``root`` or as an
1076+
undirected graph
10491077
10501078
OUTPUT:
10511079
@@ -1058,7 +1086,9 @@ def label_nice_tree_decomposition(nice_TD, root, directed=False):
10581086
sage: bip_one_four_TD = bip_one_four.treewidth(certificate=True)
10591087
sage: nice_TD = make_nice_tree_decomposition(bip_one_four, bip_one_four_TD)
10601088
sage: root = sorted(nice_TD.vertices())[0]
1061-
sage: label_TD = label_nice_tree_decomposition(nice_TD, root)
1089+
sage: label_TD = label_nice_tree_decomposition(nice_TD, root, directed=True)
1090+
sage: print(label_TD.name())
1091+
Labelled Nice tree decomposition of Tree decomposition
10621092
sage: for node in sorted(label_TD):
10631093
....: print(node, label_TD.get_vertex(node))
10641094
(0, {}) forget
@@ -1081,13 +1111,13 @@ def label_nice_tree_decomposition(nice_TD, root, directed=False):
10811111
from sage.graphs.graph import Graph
10821112

10831113
directed_TD = DiGraph(nice_TD.breadth_first_search(start=root, edges=True),
1084-
format='list_of_edges')
1114+
format='list_of_edges',
1115+
name='Labelled {}'.format(nice_TD))
10851116

10861117
# The loop starts from the root node
10871118
# We assume the tree decomposition is valid and nice,
10881119
# hence saving time on checking.
10891120
for node in directed_TD:
1090-
in_deg = directed_TD.in_degree(node)
10911121
out_deg = directed_TD.out_degree(node)
10921122

10931123
if out_deg == 2:

0 commit comments

Comments
 (0)