@@ -852,6 +852,22 @@ def make_nice_tree_decomposition(graph, tree_decomp):
852
852
sage: bip_one_four_TD = bip_one_four. treewidth( certificate=True)
853
853
sage: make_nice_tree_decomposition( bip_one_four, bip_one_four_TD)
854
854
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
855
871
"""
856
872
if not is_valid_tree_decomposition(graph, tree_decomp):
857
873
raise ValueError (" input must be a valid tree decomposition for this graph" )
@@ -863,11 +879,19 @@ def make_nice_tree_decomposition(graph, tree_decomp):
863
879
864
880
# Step 1: Ensure the tree is directed and has a root
865
881
# 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
+
868
886
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' )
871
895
872
896
# Relabel the graph in range (0, |tree_decomp| - 1)
873
897
bags_to_int = directed_tree.relabel(inplace = True , return_map = True )
@@ -904,7 +928,7 @@ def make_nice_tree_decomposition(graph, tree_decomp):
904
928
children = directed_tree.neighbors_out(ui)
905
929
children.pop() # one vertex remains a child of ui
906
930
907
- directed_tree.delete_edges((ui, vi) for v in children)
931
+ directed_tree.delete_edges((ui, vi) for vi in children)
908
932
909
933
new_nodes = [directed_tree.add_vertex() for _ in range (len (children) - 1 )]
910
934
@@ -1011,7 +1035,7 @@ def make_nice_tree_decomposition(graph, tree_decomp):
1011
1035
1012
1036
return nice_tree_decomp
1013
1037
1014
- def label_nice_tree_decomposition (nice_TD , root ):
1038
+ def label_nice_tree_decomposition (nice_TD , root , directed = False ):
1015
1039
r """
1016
1040
Return a nice tree decomposition with nodes labelled accordingly.
1017
1041
@@ -1021,6 +1045,8 @@ def label_nice_tree_decomposition(nice_TD, root):
1021
1045
1022
1046
- ``root`` -- the root of the nice tree decomposition
1023
1047
1048
+ - ``directed`` -- boolean ( default: ``False``) ; whether to return the directed graph
1049
+
1024
1050
OUTPUT:
1025
1051
1026
1052
A nice tree decomposition with nodes labelled.
@@ -1035,7 +1061,7 @@ def label_nice_tree_decomposition(nice_TD, root):
1035
1061
sage: label_TD = label_nice_tree_decomposition( nice_TD, root)
1036
1062
sage: for node in sorted( label_TD) :
1037
1063
.... : print( node, label_TD. get_vertex( node))
1038
- ( 0, {}) root
1064
+ ( 0, {}) forget
1039
1065
( 1, {0} ) forget
1040
1066
( 2, {0, 1}) intro
1041
1067
( 3, {0} ) forget
@@ -1064,9 +1090,7 @@ def label_nice_tree_decomposition(nice_TD, root):
1064
1090
in_deg = directed_TD.in_degree(node)
1065
1091
out_deg = directed_TD.out_degree(node)
1066
1092
1067
- if in_deg == 0 :
1068
- directed_TD.set_vertex(node, ' root' )
1069
- elif out_deg == 2 :
1093
+ if out_deg == 2 :
1070
1094
directed_TD.set_vertex(node, ' join' )
1071
1095
elif out_deg == 1 :
1072
1096
current_bag = node[1 ]
@@ -1079,6 +1103,8 @@ def label_nice_tree_decomposition(nice_TD, root):
1079
1103
else :
1080
1104
directed_TD.set_vertex(node, ' leaf' )
1081
1105
1106
+ if directed:
1107
+ return directed_TD
1082
1108
return Graph(directed_TD, name = nice_TD.name())
1083
1109
1084
1110
0 commit comments