Skip to content

Commit

Permalink
sagemathgh-37181: Fix subtour elimination constraints in longest_cycle
Browse files Browse the repository at this point in the history
    
An issue has been raised (see
sagemath#37028 (comment)) on
the formulation used to find the longest (induced) cycle. This was due
to the subtour elimination constraints that were not correct. We change
these constraints to fix this issue. The new constraints force to use
edges from the boundary of a subtour only when a vertex of that subtour
is selected.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->
<!-- If your change requires a documentation PR, please link it
appropriately -->
<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
<!-- Feel free to remove irrelevant items. -->

- [x] The title is concise, informative, and self-explanatory.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [ ] I have updated the documentation accordingly.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on
- sagemath#12345: short description why this is a dependency
- sagemath#34567: ...
-->

<!-- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
    
URL: sagemath#37181
Reported by: David Coudert
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Feb 1, 2024
2 parents b5f69b0 + ebb7584 commit 5a561ab
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -7976,6 +7976,17 @@ def longest_cycle(self, induced=False, use_edge_labels=False,

TESTS:

Check that the example from :issue:`37028` is fixed::

sage: d = {0: [4, 6, 10, 11], 1: [5, 7, 10, 11], 2: [8, 9, 10, 11],
....: 3: [8, 9, 11], 4: [6, 10, 11], 5: [7, 10, 11],
....: 6: [10, 11], 7: [10], 8: [10], 9: [11]}
sage: Z = Graph(d)
sage: Z.longest_cycle()
longest cycle: Graph on 9 vertices
sage: Z.longest_cycle(induced=True)
longest induced cycle: Graph on 5 vertices

Small cases::

sage: Graph().longest_cycle()
Expand Down Expand Up @@ -8085,8 +8096,8 @@ def F(e):
constraint_generation=True)

# We need one binary variable per vertex and per edge
vertex = p.new_variable(binary=True)
edge = p.new_variable(binary=True)
vertex = p.new_variable(binary=True, name='vertex')
edge = p.new_variable(binary=True, name='edge')

# Objective function: maximize the size of the cycle
p.set_objective(p.sum(weight(e) * edge[F(e)] for e in G.edge_iterator()))
Expand Down Expand Up @@ -8165,12 +8176,14 @@ def F(e):

# Add subtour elimination constraints
if directed:
p.add_constraint(p.sum(edge[F(e)] for e in G.edge_boundary(c)), min=1)
c = set(c)
cbar = (v for v in G if v not in c)
p.add_constraint(p.sum(edge[F(e)] for e in G.edge_boundary(cbar, c)), min=1)
for u in c:
p.add_constraint(vertex[u] <= p.sum(edge[F(e)] for e in G.edge_boundary(c)))
p.add_constraint(vertex[u] <= p.sum(edge[F(e)] for e in G.edge_boundary(cbar, c)))
else:
p.add_constraint(p.sum(edge[F(e)] for e in G.edge_boundary(c)), min=2)
for u in c:
p.add_constraint(2*vertex[u] <= p.sum(edge[F(e)] for e in G.edge_boundary(c)))

if induced:
# We eliminate this cycle
Expand Down

0 comments on commit 5a561ab

Please sign in to comment.