Techniques for Subtour Elimination in Traveling Salesman Problem: Theory and Implementation in Python

Aayush Aggarwal
Dec 6, 2020 · 9 min read


In this article, I will explain and implement the well-known Traveling Salesman Problem aka TSP with a special focus on subtour elimination methods. We will use python to implement the MILP formulation. The dataset contains the coordinates of various cities of India. The aim is to find the shortest path that covers all cities. We will cover the following points in this article

  1. Input the data and visualize the problem
  2. Model TSP as ILP formulation w/o Subtour constraints
  3. Implement Subtour Elimination Method 1: MTZ’s Approach
  4. Implement Subtour Elimination Method 2: DFJ’s Approach
  5. Compare MTZ’s formulation and DFJ’s formulation
  6. Conclusion

The GitHub codes for this article can be found on the link:

1 Input the data and problem visualization

The CSV file “tsp_city_data.csv” contains the names of cities in India with their latitude and longitude information. The first city “Delhi” is assumed to be the starting point of the trip (depot). The data input to TSP model is the distance matrix which stores the distance (or travel time or cost) from each city (location) to every other city. Thus, for a traveling salesman problem for N cities (location), the distance matrix is of size N x N. The variable no_of_locs in the code is used to define the first n no. of cities we want to include in our TSP problem data. The value is set at 6 for now. The python pandas library is used to read CSV file and distance matrix “dis_mat”.

The function “plot_fig” is used to plot the data and visualize the problem and the function “get_plan” takes the LP solution and returns all the subtours present in the solution.

The following code below reads data from the CSV file and creates a distance matrix for TSP problem.

2 Model TSP in ILP without Subtour elimination constraints

TSP problem can be modeled as Integer Linear Program. The LP model is explained as follows


N= Number of location including depot (starting point)
Ci,j = Edge cost from node i to node j where i,j= [1…N]

Decision Variable

xi,j = 1 if solution has direct path from node i to j, otherwise 0

The LP model is formulated as follows

The objective (1) minimize the cost of the tour. Constraints (2) and (3) ensures that for each node, there is only one outflow and inflow edge respectively. Thus, each node is visited only once. Constraint (3) restrict outflow to one’s own node.

In this article, python’s PuLP library is used for implementing MILP model in python. PuLP is an LP modeler written in Python. PuLP can call a variety of LP solvers like CBC, CPLEX, GUROBI, SCIP to solve linear problems. It can be installed from the link “”. The CBC solver is preinstalled in the PuLP library while one has to install other solvers like GUROBI, CPLEX separately to use in PuLP. In this implementation, CBC is used as LP solver.

The optimal solution given by the LP model has subtours i.e

  1. Tour 1 : Delhi > Nagpur > Rajkot
  2. Tour 2 : Kolkata > Dispur > Agartala

The solution given by the model has 2 tours but what required is the single tour that starts with the depot (Delhi) and visits all locations one by one and ends at Delhi. To solve this problem and to get the desired single tour, the subtour elimination constraints need to be added in the LP Model.

There are 2 well-known formulations DSF and MTZ (named after their authors). This article covers both the ideas and the implementation in python.

3. MTZ Method for subtour elimination

This formulation was proposed by Miller, Tucker, Zemlin. To eliminate subtours, continuous decision variables representing times at which a location is visited is added. Variable for all locations except depot node is added. ti= time at which location i is visited, i =[2,…N] Finally what is required are the constraint

How does constraint (5) remove subtours ?

Lets takes an previous example and take the subtour Kolkata (k) > Dispur(d) > Agartala(a)

So adding constraint (5) will eliminate the subtour.

The complete Lp model is formulated as follows


N= Number of location including depot (starting point)
Ci,j = Edge cost from node i to node j where i,j= [1…N]

Decision Variable

xi,j = 1 if solution has direct path from node i to j, otherwise 0
ti = time at which location i is visited , i =[2,…N]

The LP model is formulated as follows

The MTZ’s formulation is implemented in python as shown below.

4. DFJ Method for subtour elimination

This formulation was proposed by Dantzig, Fulkerson, Jhonson. To eliminate subtours, for every set S of cities, add a constraint saying that the tour leaves S at least once.

How does this constraint eliminate subtours?

Let's take the same example and take a set Si= {kolkata, Dispur, Agartala} and the rest of the cities be represented by s′i={ Delhi(del), Rajkot(r), Nagpur(n)}
Now as per constraint (15), the new constraint added is as follows

since there is no edge going to any other node in this set (due to subtour), this equation is not satisfied for the set Si= {{kolkata, Dispur, Agartala}. So, by adding constraint (15), this solution becomes infeasible and all subtours will be eliminated.

Modification in DFJ Method

For N cities, the number of possible sets adds up to 2^n i.e the number of constraints grows exponentially. So, instead of adding constraints for all the possible sets, only some constraints are added. Given a solution to LP model(without having subtour elimination constraints) with subtours, one can quickly find the subset for which DFJ subtour constraint is eliminated. In the example above, one needs to add only 2 constraints and not 2^5 constraints.

So, the higher-level algorithm is as follows

Higher-level Algorithm for DFJ

step 1. Solve TSP problem with LP formulation w/o Subtour Constraints

step 2. If no subtour present in the current solution, goto step 6

step 3. Add subtour constraint only for the subtours present in the current solution.

step 4. Solve TSP problem with newly added constraint.

step 5. goto step 2

step 6. Return the final TSP solution

The above-mentioned algorithm is implemented as follows

Compare MTZ’s Formulation vs DFJ’s formulation

Since two approaches for subtour elimination have been discussed in this article, it's time to compare the two. MTZ’s approach introduces n² constraints (one for each pair (i,j) where i, j=[1..n]) while DFJ’s approach introduces subtour constraints for all possible sets of locations i.e 2^n for n locations. Thus, MTZ’s approach adds a polynomial number of constraints while DFJ’s approach introduces an exponential number of constraints.

In terms of decision variables, MTZ approach introduces n new decision variables (titi for i =[1..n]).ON the other hand, DFJ introduces no new decision variable. MTZ’s approach has to be solved only once to get an optimal solution While DFJ is generally implemented as a modified version and it is solved iteratively ( i.e LP model has to be solved multiple times with new subtour constraints added every time).

There is no clear winner among the two. For some problems, DFJ gives solutions faster than MTZ and for some problems, MTZ is faster. But DFJ has an efficient branch and bound approach due to which it becomes more efficient than MTZ. Also, MTZ’s formulation is weaker i.e the feasible region has the same integer points but includes more fractional points.


In this article, MILP formulation of TSP is explained with a special focus on subtour elimination approaches. TSP problem is a special case of Vehicle Routing Problem (VRP) with no. of vehicle equal to 1. But, subtour elimination is a core issue in VRP as well which is solved by using the same techniques. In this article, the TSP problem is solved for only 6 cities to simplify the explanation of subtour elimination. The CSV file uploaded in the Github repository contains data of 27 cities. One can try to solve the problem for more number of cities.

The Startup

Get smarter at building your thing. Join The Startup’s +792K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store