GreedyTsp

Declared in lemon/greedy_tsp.h, in the group Traveling Salesman Problem; the same class on the Doxygen site.

template< typename CM >
class GreedyTsp

Greedy algorithm for symmetric TSP.

GreedyTsp implements the greedy heuristic for solving symmetric TSP.

This algorithm is quite similar to the nearest neighbor heuristic, but it maintains a set of disjoint paths. At each step, the shortest possible edge is added to these paths as long as it does not create a cycle of less than n edges and it does not increase the degree of any node above two.

This method runs in O(n2logn) time, the sort of the edges of the complete graph. It quickly finds a relatively short tour for most TSP instances, but it could also yield a really bad (or even the worst) solution in special cases.

Template Parameters:

CM – Type of the cost map.

Execution Control

inline Cost run()

Runs the algorithm.

This function runs the algorithm.

Returns:

The total cost of the found tour.

Query Functions

inline Cost tourCost() const

The total cost of the found tour.

This function returns the total cost of the found tour.

Pre:

run() must be called before using this function.

inline const std::vector< Node > & tourNodes() const

Returns a const reference to the node sequence of the found tour.

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

Pre:

run() must be called before using this function.

template< typename Iterator >
inline void tourNodes( Iterator out ) const

Gives back the node sequence of the found tour.

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 ) );

Pre:

run() must be called before using this function.

template< typename Path >
inline void tour( Path & path ) const

Gives back the found tour as a path.

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

Pre:

run() must be called before using this function.

Public Types

typedef CM CostMap

Type of the cost map.

typedef CM::Value Cost

Type of the edge costs.

Public Functions

inline GreedyTsp( const FullGraph & gr , const CostMap & cost )

Constructor.

Parameters:
  • gr – The full graph the algorithm runs on.

  • cost – The cost map.