Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
trac #17893: Incorrect decomposition returned by Graph.treewidth
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanncohen committed Mar 6, 2015
1 parent 319abe2 commit b8464cb
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/sage/graphs/graph.py
Expand Up @@ -2739,6 +2739,18 @@ def treewidth(self,k=None,certificate=False):
False
sage: graphs.PetersenGraph().treewidth(k=4,certificate=True)
Graph on 7 vertices
TESTS:
All edges do appear (:trac:`17893`)::
sage: from itertools import combinations
sage: g = graphs.PathGraph(10)
sage: td = g.treewidth(certificate=True)
sage: for bag in td:
....: g.delete_edges(list(combinations(bag,2)))
sage: g.size()
0
"""
from sage.misc.cachefunc import cached_function
from sage.sets.set import Set
Expand Down Expand Up @@ -2810,7 +2822,8 @@ def rec(cut,cc):

if certificate:
sons.extend(son)
sons.append((cut,reduced_cut))
sons.append((cut,cutv))
sons.append((cutv,reduced_cut))

# Weird Python syntax which is useful once in a lifetime : if break
# was never called in the loop above, we return "sons".
Expand All @@ -2836,26 +2849,18 @@ def rec(cut,cc):
G = Graph()
G.add_edges([(Set(x),Set(y)) for x,y in TD])

# The Tree-Decomposition contains a lot of useless nodes that we now remove.
prune_list = G.vertices()

for v in prune_list:
# We remove any vertex of degree 1 whose corresponding set is contained
# in the set represented by its unique neighbor.
if G.degree(v)==1:
v1 = G.neighbors(v)[0]
if v.issubset(v1):
G.delete_vertex(v)
prune_list.append(v1)

# We remove a vertex v with two neighbors v1,v2 if v1 is a subset of v
# and v is a subset of v2
elif G.degree(v) == 2:
v1,v2 = G.neighbors(v)
if ((v1.issubset(v) and v.issubset(v2)) or
(v1.issuperset(v) and v.issuperset(v2))):
G.add_edge(v1,v2)
G.delete_vertex(v)
# The Tree-Decomposition contains a lot of useless nodes.
#
# We merge all edges between two sets S,S' where S is a subset of S'
changed = True
while changed:
changed=False
for v in G.vertices():
for u in G.neighbors(v):
if u.issuperset(v):
G.merge_vertices([u,v]) # the new vertex is named 'u'
changed = True
break

return G

Expand Down

0 comments on commit b8464cb

Please sign in to comment.