@@ -7976,6 +7976,17 @@ def longest_cycle(self, induced=False, use_edge_labels=False,
7976
7976
7977
7977
TESTS:
7978
7978
7979
+ Check that the example from :issue:`37028` is fixed::
7980
+
7981
+ sage: d = {0: [4, 6, 10, 11], 1: [5, 7, 10, 11], 2: [8, 9, 10, 11],
7982
+ ....: 3: [8, 9, 11], 4: [6, 10, 11], 5: [7, 10, 11],
7983
+ ....: 6: [10, 11], 7: [10], 8: [10], 9: [11]}
7984
+ sage: Z = Graph(d)
7985
+ sage: Z.longest_cycle()
7986
+ longest cycle: Graph on 9 vertices
7987
+ sage: Z.longest_cycle(induced=True)
7988
+ longest induced cycle: Graph on 5 vertices
7989
+
7979
7990
Small cases::
7980
7991
7981
7992
sage: Graph().longest_cycle()
@@ -8085,8 +8096,8 @@ def F(e):
8085
8096
constraint_generation=True)
8086
8097
8087
8098
# We need one binary variable per vertex and per edge
8088
- vertex = p.new_variable(binary=True)
8089
- edge = p.new_variable(binary=True)
8099
+ vertex = p.new_variable(binary=True, name='vertex' )
8100
+ edge = p.new_variable(binary=True, name='edge' )
8090
8101
8091
8102
# Objective function: maximize the size of the cycle
8092
8103
p.set_objective(p.sum(weight(e) * edge[F(e)] for e in G.edge_iterator()))
@@ -8165,12 +8176,14 @@ def F(e):
8165
8176
8166
8177
# Add subtour elimination constraints
8167
8178
if directed:
8168
- p.add_constraint(p.sum(edge[F(e)] for e in G.edge_boundary(c)), min=1)
8169
8179
c = set(c)
8170
8180
cbar = (v for v in G if v not in c)
8171
- p.add_constraint(p.sum(edge[F(e)] for e in G.edge_boundary(cbar, c)), min=1)
8181
+ for u in c:
8182
+ p.add_constraint(vertex[u] <= p.sum(edge[F(e)] for e in G.edge_boundary(c)))
8183
+ p.add_constraint(vertex[u] <= p.sum(edge[F(e)] for e in G.edge_boundary(cbar, c)))
8172
8184
else:
8173
- p.add_constraint(p.sum(edge[F(e)] for e in G.edge_boundary(c)), min=2)
8185
+ for u in c:
8186
+ p.add_constraint(2*vertex[u] <= p.sum(edge[F(e)] for e in G.edge_boundary(c)))
8174
8187
8175
8188
if induced:
8176
8189
# We eliminate this cycle
0 commit comments