org.jgrapht.alg.tour

## Class TwoOptHeuristicTSP<V,E>

• Type Parameters:
V - the graph vertex type
E - the graph edge type
All Implemented Interfaces:
HamiltonianCycleAlgorithm<V,E>, TSPAlgorithm<V,E>

public class TwoOptHeuristicTSP<V,E>
extends Object
implements TSPAlgorithm<V,E>
The 2-opt heuristic algorithm for the TSP problem.

The travelling salesman problem (TSP) asks the following question: "Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city exactly once and returns to the origin city?".

This is an implementation of the 2-opt improvement heuristic algorithm. The algorithm generates k random initial tours and then iteratively improves the tours until a local minimum is reached. In each iteration it applies the best possible 2-opt move which means to find the best pair of edges $(i,i+1)$ and $(j,j+1)$ such that replacing them with $(i,j)$ and $(i+1,j+1)$ minimizes the tour length.

See wikipedia for more details.

This implementation can also be used in order to try to improve an existing tour. See method improveTour(GraphPath).

Author:
Dimitrios Michail
• ### Constructor Summary

Constructors
Constructor and Description
TwoOptHeuristicTSP()
Constructor.
TwoOptHeuristicTSP(int k)
Constructor
TwoOptHeuristicTSP(int k, long seed)
Constructor
TwoOptHeuristicTSP(int k, Random rng)
Constructor
• ### Method Summary

All Methods
Modifier and Type Method and Description
GraphPath<V,E> getTour(Graph<V,E> graph)
Computes a 2-approximate tour.
GraphPath<V,E> improveTour(GraphPath<V,E> tour)
Try to improve a tour by running the 2-opt heuristic.
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
• ### Constructor Detail

• #### TwoOptHeuristicTSP

public TwoOptHeuristicTSP()
Constructor. By default one initial random tour is used.
• #### TwoOptHeuristicTSP

public TwoOptHeuristicTSP(int k)
Constructor
Parameters:
k - how many initial random tours to check
• #### TwoOptHeuristicTSP

public TwoOptHeuristicTSP(int k,
long seed)
Constructor
Parameters:
k - how many initial random tours to check
seed - seed for the random number generator
• #### TwoOptHeuristicTSP

public TwoOptHeuristicTSP(int k,
Random rng)
Constructor
Parameters:
k - how many initial random tours to check
rng - random number generator
• ### Method Detail

• #### getTour

public GraphPath<V,E> getTour(Graph<V,E> graph)
Computes a 2-approximate tour.
Specified by:
getTour in interface HamiltonianCycleAlgorithm<V,E>
Specified by:
getTour in interface TSPAlgorithm<V,E>
Parameters:
graph - the input graph
Returns:
a tour
Throws:
IllegalArgumentException - if the graph is not undirected
IllegalArgumentException - if the graph is not complete
IllegalArgumentException - if the graph contains no vertices
• #### improveTour

public GraphPath<V,E> improveTour(GraphPath<V,E> tour)
Try to improve a tour by running the 2-opt heuristic.
Parameters:
tour - a tour
Returns:
a possibly improved tour