COIN-OR::LEMON - Graph Library

Ignore:
Timestamp:
01/09/11 00:56:52 (13 years ago)
Author:
Peter Kovacs <kpeter@…>
Branch:
default
Phase:
public
Message:

Unifications and improvements in TSP algorithms (#386)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/nearest_neighbor_tsp.h

    r1201 r1202  
    2525
    2626#include <deque>
     27#include <vector>
    2728#include <limits>
    2829#include <lemon/full_graph.h>
     
    3132namespace lemon {
    3233
     34  /// \ingroup tsp
     35  ///
    3336  /// \brief Nearest neighbor algorithm for symmetric TSP.
    3437  ///
     
    4245  ///
    4346  /// This method runs in O(n<sup>2</sup>) time.
    44   /// It quickly finds an effectively short tour for most TSP
    45   /// instances, but in special cases, it could yield a really bad
    46   /// (or even the worst) solution.
     47  /// It quickly finds a short tour for most TSP instances, but in special
     48  /// cases, it could yield a really bad (or even the worst) solution.
    4749  ///
    4850  /// \tparam CM Type of the cost map.
     
    6466      const CostMap &_cost;
    6567      Cost _sum;
    66       std::deque<Node> _path;
     68      std::vector<Node> _path;
    6769
    6870    public:
     
    8688      Cost run() {
    8789        _path.clear();
    88 
    89         if (_gr.nodeNum() == 0) return _sum = 0;
     90        if (_gr.nodeNum() == 0) {
     91          return _sum = 0;
     92        }
    9093        else if (_gr.nodeNum() == 1) {
    9194          _path.push_back(_gr(0));
     
    9396        }
    9497
     98        std::deque<Node> path_dq;
    9599        Edge min_edge1 = INVALID,
    96100             min_edge2 = INVALID;
     
    99103        Node n1 = _gr.u(min_edge1),
    100104             n2 = _gr.v(min_edge1);
    101         _path.push_back(n1);
    102         _path.push_back(n2);
     105        path_dq.push_back(n1);
     106        path_dq.push_back(n2);
    103107
    104108        FullGraph::NodeMap<bool> used(_gr, false);
     
    107111
    108112        min_edge1 = INVALID;
    109         while (int(_path.size()) != _gr.nodeNum()) {
     113        while (int(path_dq.size()) != _gr.nodeNum()) {
    110114          if (min_edge1 == INVALID) {
    111115            for (IncEdgeIt e(_gr, n1); e != INVALID; ++e) {
     
    128132          if (_cost[min_edge1] < _cost[min_edge2]) {
    129133            n1 = _gr.oppositeNode(n1, min_edge1);
    130             _path.push_front(n1);
     134            path_dq.push_front(n1);
    131135
    132136            used[n1] = true;
     
    137141          } else {
    138142            n2 = _gr.oppositeNode(n2, min_edge2);
    139             _path.push_back(n2);
     143            path_dq.push_back(n2);
    140144
    141145            used[n2] = true;
     
    147151        }
    148152
    149         _sum = _cost[_gr.edge(_path.back(), _path.front())];
    150         for (int i = 0; i < int(_path.size())-1; ++i) {
    151           _sum += _cost[_gr.edge(_path[i], _path[i+1])];
     153        n1 = path_dq.back();
     154        n2 = path_dq.front();
     155        _path.push_back(n2);
     156        _sum = _cost[_gr.edge(n1, n2)];
     157        for (int i = 1; i < int(path_dq.size()); ++i) {
     158          n1 = n2;
     159          n2 = path_dq[i];
     160          _path.push_back(n2);
     161          _sum += _cost[_gr.edge(n1, n2)];
    152162        }
    153163
     
    172182      /// found tour.
    173183      ///
    174       /// This function returns a const reference to the internal structure
     184      /// This function returns a const reference to a vector
    175185      /// that stores the node sequence of the found tour.
    176186      ///
    177187      /// \pre run() must be called before using this function.
    178       const std::deque<Node>& tourNodes() const {
     188      const std::vector<Node>& tourNodes() const {
    179189        return _path;
    180190      }
Note: See TracChangeset for help on using the changeset viewer.