All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
List of all members | Public Types | Public Member Functions
Opt2Tsp< CM > Class Template Reference

Detailed Description

template<typename CM>
class lemon::Opt2Tsp< CM >

Opt2Tsp implements the 2-opt heuristic for solving symmetric TSP.

This algorithm starts with an initial tour and iteratively improves it. At each step, it removes two edges and the reconnects the created two paths in the other way if the resulting tour is shorter. The algorithm finishes when no such 2-opt move can be applied, and so the tour is 2-optimal.

If no starting tour is given to the run() function, then the algorithm uses the node sequence determined by the node IDs. Oherwise, it starts with the given tour.

This is a rather slow but effective method. Its typical usage is the improvement of the result of a fast tour construction heuristic (e.g. the InsertionTsp algorithm).

Template Parameters
CMType of the cost map.

#include <lemon/opt2_tsp.h>

Public Types

typedef CM CostMap
 Type of the cost map.
 
typedef CM::Value Cost
 Type of the edge costs.
 

Public Member Functions

 Opt2Tsp (const FullGraph &gr, const CostMap &cost)
 Constructor.
 
Execution Control
Cost run ()
 Runs the algorithm from scratch.
 
template<typename Path >
Cost run (const Path &tour)
 Runs the algorithm starting from the given tour.
 
Cost run (const std::vector< Node > &tour)
 Runs the algorithm starting from the given tour.
 
Query Functions
Cost tourCost () const
 The total cost of the found tour.
 
const std::vector< Node > & tourNodes () const
 Returns a const reference to the node sequence of the found tour.
 
template<typename Iterator >
void tourNodes (Iterator out) const
 Gives back the node sequence of the found tour.
 
template<typename Path >
void tour (Path &path) const
 Gives back the found tour as a path.
 

Constructor & Destructor Documentation

Opt2Tsp ( const FullGraph gr,
const CostMap cost 
)
inline

Constructor.

Parameters
grThe full graph the algorithm runs on.
costThe cost map.

Member Function Documentation

Cost run ( )
inline

This function runs the algorithm starting from the tour that is determined by the node ID sequence.

Returns
The total cost of the found tour.
Cost run ( const Path tour)
inline

This function runs the algorithm starting from the given tour.

Parameters
tourThe tour as a path structure. It must be a valid path containing excactly n arcs.
Returns
The total cost of the found tour.
Cost run ( const std::vector< Node > &  tour)
inline

This function runs the algorithm starting from the given tour (node sequence).

Parameters
tourA vector that stores all Nodes of the graph in the desired order.
Returns
The total cost of the found tour.
Cost tourCost ( ) const
inline

This function returns the total cost of the found tour.

Precondition
run() must be called before using this function.
const std::vector<Node>& tourNodes ( ) const
inline

This function returns a const reference to a vector that stores the node sequence of the found tour.

Precondition
run() must be called before using this function.
void tourNodes ( Iterator  out) const
inline

This function copies the node sequence of the found tour into an STL container through the given output iterator. The value_type of the container must be FullGraph::Node. For example,

std::vector<FullGraph::Node> nodes(countNodes(graph));
tsp.tourNodes(nodes.begin());

or

std::list<FullGraph::Node> nodes;
tsp.tourNodes(std::back_inserter(nodes));
Precondition
run() must be called before using this function.
void tour ( Path path) const
inline

This function copies the found tour as a list of arcs/edges into the given path structure.

Precondition
run() must be called before using this function.