Skip to content

Commit 8b2c666

Browse files
committed
tree_decomposition.pyx: Allow returning directed nice tree decomp; fix bugs in #36843
1 parent 272582b commit 8b2c666

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

src/sage/graphs/graph_decompositions/tree_decomposition.pyx

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,22 @@ def make_nice_tree_decomposition(graph, tree_decomp):
852852
sage: bip_one_four_TD = bip_one_four.treewidth(certificate=True)
853853
sage: make_nice_tree_decomposition(bip_one_four, bip_one_four_TD)
854854
Nice tree decomposition of Tree decomposition: Graph on 15 vertices
855+
856+
The following two examples are for testing GitHub Issue 36843::
857+
858+
sage: from sage.graphs.graph_decompositions.tree_decomposition import make_nice_tree_decomposition
859+
sage: triangle = graphs.CompleteGraph(3)
860+
sage: triangle_TD = triangle.treewidth(certificate=True)
861+
sage: make_nice_tree_decomposition(triangle, triangle_TD)
862+
Nice tree decomposition of Tree decomposition: Graph on 7 vertices
863+
864+
::
865+
866+
sage: from sage.graphs.graph_decompositions.tree_decomposition import make_nice_tree_decomposition
867+
sage: graph = graphs.CompleteBipartiteGraph(2, 5)
868+
sage: graph_TD = graph.treewidth(certificate=True)
869+
sage: make_nice_tree_decomposition(graph, graph_TD)
870+
Nice tree decomposition of Tree decomposition: Graph on 25 vertices
855871
"""
856872
if not is_valid_tree_decomposition(graph, tree_decomp):
857873
raise ValueError("input must be a valid tree decomposition for this graph")
@@ -863,11 +879,19 @@ def make_nice_tree_decomposition(graph, tree_decomp):
863879

864880
# Step 1: Ensure the tree is directed and has a root
865881
# Choose a root and orient the edges from root-to-leaves direction
866-
leaves = [u for u in tree_decomp if tree_decomp.degree(u) == 1]
867-
root = leaves.pop()
882+
#
883+
# Testing <= 1 for the special case when one bag containing all vertices
884+
leaves = [u for u in tree_decomp if tree_decomp.degree(u) <= 1]
885+
868886
from sage.graphs.digraph import DiGraph
869-
directed_tree = DiGraph(tree_decomp.breadth_first_search(start=root, edges=True),
870-
format='list_of_edges')
887+
if len(leaves) == 1:
888+
root = leaves[0]
889+
directed_tree = DiGraph(tree_decomp)
890+
else:
891+
root = leaves.pop()
892+
893+
directed_tree = DiGraph(tree_decomp.breadth_first_search(start=root, edges=True),
894+
format='list_of_edges')
871895

872896
# Relabel the graph in range (0, |tree_decomp| - 1)
873897
bags_to_int = directed_tree.relabel(inplace=True, return_map=True)
@@ -904,7 +928,7 @@ def make_nice_tree_decomposition(graph, tree_decomp):
904928
children = directed_tree.neighbors_out(ui)
905929
children.pop() # one vertex remains a child of ui
906930

907-
directed_tree.delete_edges((ui, vi) for v in children)
931+
directed_tree.delete_edges((ui, vi) for vi in children)
908932

909933
new_nodes = [directed_tree.add_vertex() for _ in range(len(children) - 1)]
910934

@@ -1011,7 +1035,7 @@ def make_nice_tree_decomposition(graph, tree_decomp):
10111035

10121036
return nice_tree_decomp
10131037

1014-
def label_nice_tree_decomposition(nice_TD, root):
1038+
def label_nice_tree_decomposition(nice_TD, root, directed=False):
10151039
r"""
10161040
Return a nice tree decomposition with nodes labelled accordingly.
10171041
@@ -1021,6 +1045,8 @@ def label_nice_tree_decomposition(nice_TD, root):
10211045
10221046
- ``root`` -- the root of the nice tree decomposition
10231047
1048+
- ``directed`` -- boolean (default: ``False``); whether to return the directed graph
1049+
10241050
OUTPUT:
10251051
10261052
A nice tree decomposition with nodes labelled.
@@ -1035,7 +1061,7 @@ def label_nice_tree_decomposition(nice_TD, root):
10351061
sage: label_TD = label_nice_tree_decomposition(nice_TD, root)
10361062
sage: for node in sorted(label_TD):
10371063
....: print(node, label_TD.get_vertex(node))
1038-
(0, {}) root
1064+
(0, {}) forget
10391065
(1, {0}) forget
10401066
(2, {0, 1}) intro
10411067
(3, {0}) forget
@@ -1064,9 +1090,7 @@ def label_nice_tree_decomposition(nice_TD, root):
10641090
in_deg = directed_TD.in_degree(node)
10651091
out_deg = directed_TD.out_degree(node)
10661092

1067-
if in_deg == 0:
1068-
directed_TD.set_vertex(node, 'root')
1069-
elif out_deg == 2:
1093+
if out_deg == 2:
10701094
directed_TD.set_vertex(node, 'join')
10711095
elif out_deg == 1:
10721096
current_bag = node[1]
@@ -1079,6 +1103,8 @@ def label_nice_tree_decomposition(nice_TD, root):
10791103
else:
10801104
directed_TD.set_vertex(node, 'leaf')
10811105

1106+
if directed:
1107+
return directed_TD
10821108
return Graph(directed_TD, name=nice_TD.name())
10831109

10841110

0 commit comments

Comments
 (0)