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 Jan 30, 2024
2 parents bcf9436 + ebb7584 commit 2fa0467
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
6 changes: 3 additions & 3 deletions build/pkgs/configure/checksums.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tarball=configure-VERSION.tar.gz
sha1=8124eeddb2d440bd40f9656a8f69ce53175a4706
md5=db7e8fb79c8cd9a69e8cc6865e43add9
cksum=719116257
sha1=5c08b6a13e4ea76db215bc1ea12127c0e881820f
md5=2d7a75734386c8349199843d499e1e28
cksum=2132610142
2 changes: 1 addition & 1 deletion build/pkgs/configure/package-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0c16f847a4db786d105c48e79bf3bc3fe07d82fc
6291957381bd29b690316fb18f99fe33c36a798c
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 2fa0467

Please sign in to comment.