Decision variables
In the symmetric TSP, the cost of traveling from city i to city j is the same as the cost of traveling from j to i. So the model can use one binary variable for each undirected segment:
The objective is linear because each selected segment contributes its cost exactly once.
Every city has degree 2
A valid tour enters and leaves every city. In the undirected model, this becomes exactly two selected segments incident to each city:
Cut constraints rule out disconnected cycles
For any nonempty strict subset S of cities, a real tour must leave S and later return. Therefore at least two selected segments cross between S and its complement.
If the current 2-factor has a component C with no selected edges leaving it, then the left-hand side is 0. Adding the constraint for C cuts off exactly that subtour solution.
Subtour-elimination ILP
Solve the relaxation, find subtours, add cuts
The solver is exact for small instances: it enumerates all degree-2 binary edge sets and keeps only those satisfying the active subtour cuts. This is meant for teaching, not large TSP instances.
Current relaxation
Selected subset S
Active subtour cuts
Cost matrix and current ILP
Symmetric cost matrix
Edit an upper-triangular entry to change the cost of segment {i,j}. Editing costs turns off “costs follow positions.”
Model generated from this instance
Why not write every subtour constraint?
For n cities, there are n(n−1)/2 binary edge variables and n degree constraints. But the raw number of nonempty strict subsets is 2n−2, so the subtour constraints grow exponentially.
Repeatedly adding constraints
The current model is always a relaxation of the full subtour-elimination ILP. Therefore its optimum is a lower bound on the optimal tour length. When the relaxation optimum itself is a tour, the lower bound is achieved by a feasible TSP solution, so the tour is optimal.
Cut off the current wrong optimum
Ignoring constraints enlarges the feasible region. If the relaxed optimum violates a missing constraint, adding that constraint removes the current solution while preserving every true tour.
