1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/nearest_neighbor_tsp.h Wed Oct 17 19:14:07 2018 +0200
1.3 @@ -0,0 +1,238 @@
1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library.
1.7 + *
1.8 + * Copyright (C) 2003-2013
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef LEMON_NEAREST_NEIGHBOUR_TSP_H
1.23 +#define LEMON_NEAREST_NEIGHBOUR_TSP_H
1.24 +
1.25 +/// \ingroup tsp
1.26 +/// \file
1.27 +/// \brief Nearest neighbor algorithm for symmetric TSP
1.28 +
1.29 +#include <deque>
1.30 +#include <vector>
1.31 +#include <limits>
1.32 +#include <lemon/full_graph.h>
1.33 +#include <lemon/maps.h>
1.34 +
1.35 +namespace lemon {
1.36 +
1.37 + /// \ingroup tsp
1.38 + ///
1.39 + /// \brief Nearest neighbor algorithm for symmetric TSP.
1.40 + ///
1.41 + /// NearestNeighborTsp implements the nearest neighbor heuristic for solving
1.42 + /// symmetric \ref tsp "TSP".
1.43 + ///
1.44 + /// This is probably the simplest TSP heuristic.
1.45 + /// It starts with a minimum cost edge and at each step, it connects the
1.46 + /// nearest unvisited node to the current path.
1.47 + /// Finally, it connects the two end points of the path to form a tour.
1.48 + ///
1.49 + /// This method runs in O(n<sup>2</sup>) time.
1.50 + /// It quickly finds a relatively short tour for most TSP instances,
1.51 + /// but it could also yield a really bad (or even the worst) solution
1.52 + /// in special cases.
1.53 + ///
1.54 + /// \tparam CM Type of the cost map.
1.55 + template <typename CM>
1.56 + class NearestNeighborTsp
1.57 + {
1.58 + public:
1.59 +
1.60 + /// Type of the cost map
1.61 + typedef CM CostMap;
1.62 + /// Type of the edge costs
1.63 + typedef typename CM::Value Cost;
1.64 +
1.65 + private:
1.66 +
1.67 + GRAPH_TYPEDEFS(FullGraph);
1.68 +
1.69 + const FullGraph &_gr;
1.70 + const CostMap &_cost;
1.71 + Cost _sum;
1.72 + std::vector<Node> _path;
1.73 +
1.74 + public:
1.75 +
1.76 + /// \brief Constructor
1.77 + ///
1.78 + /// Constructor.
1.79 + /// \param gr The \ref FullGraph "full graph" the algorithm runs on.
1.80 + /// \param cost The cost map.
1.81 + NearestNeighborTsp(const FullGraph &gr, const CostMap &cost)
1.82 + : _gr(gr), _cost(cost) {}
1.83 +
1.84 + /// \name Execution Control
1.85 + /// @{
1.86 +
1.87 + /// \brief Runs the algorithm.
1.88 + ///
1.89 + /// This function runs the algorithm.
1.90 + ///
1.91 + /// \return The total cost of the found tour.
1.92 + Cost run() {
1.93 + _path.clear();
1.94 + if (_gr.nodeNum() == 0) {
1.95 + return _sum = 0;
1.96 + }
1.97 + else if (_gr.nodeNum() == 1) {
1.98 + _path.push_back(_gr(0));
1.99 + return _sum = 0;
1.100 + }
1.101 +
1.102 + std::deque<Node> path_dq;
1.103 + Edge min_edge1 = INVALID,
1.104 + min_edge2 = INVALID;
1.105 +
1.106 + min_edge1 = mapMin(_gr, _cost);
1.107 + Node n1 = _gr.u(min_edge1),
1.108 + n2 = _gr.v(min_edge1);
1.109 + path_dq.push_back(n1);
1.110 + path_dq.push_back(n2);
1.111 +
1.112 + FullGraph::NodeMap<bool> used(_gr, false);
1.113 + used[n1] = true;
1.114 + used[n2] = true;
1.115 +
1.116 + min_edge1 = INVALID;
1.117 + while (int(path_dq.size()) != _gr.nodeNum()) {
1.118 + if (min_edge1 == INVALID) {
1.119 + for (IncEdgeIt e(_gr, n1); e != INVALID; ++e) {
1.120 + if (!used[_gr.runningNode(e)] &&
1.121 + (min_edge1 == INVALID || _cost[e] < _cost[min_edge1])) {
1.122 + min_edge1 = e;
1.123 + }
1.124 + }
1.125 + }
1.126 +
1.127 + if (min_edge2 == INVALID) {
1.128 + for (IncEdgeIt e(_gr, n2); e != INVALID; ++e) {
1.129 + if (!used[_gr.runningNode(e)] &&
1.130 + (min_edge2 == INVALID||_cost[e] < _cost[min_edge2])) {
1.131 + min_edge2 = e;
1.132 + }
1.133 + }
1.134 + }
1.135 +
1.136 + if (_cost[min_edge1] < _cost[min_edge2]) {
1.137 + n1 = _gr.oppositeNode(n1, min_edge1);
1.138 + path_dq.push_front(n1);
1.139 +
1.140 + used[n1] = true;
1.141 + min_edge1 = INVALID;
1.142 +
1.143 + if (_gr.u(min_edge2) == n1 || _gr.v(min_edge2) == n1)
1.144 + min_edge2 = INVALID;
1.145 + } else {
1.146 + n2 = _gr.oppositeNode(n2, min_edge2);
1.147 + path_dq.push_back(n2);
1.148 +
1.149 + used[n2] = true;
1.150 + min_edge2 = INVALID;
1.151 +
1.152 + if (_gr.u(min_edge1) == n2 || _gr.v(min_edge1) == n2)
1.153 + min_edge1 = INVALID;
1.154 + }
1.155 + }
1.156 +
1.157 + n1 = path_dq.back();
1.158 + n2 = path_dq.front();
1.159 + _path.push_back(n2);
1.160 + _sum = _cost[_gr.edge(n1, n2)];
1.161 + for (int i = 1; i < int(path_dq.size()); ++i) {
1.162 + n1 = n2;
1.163 + n2 = path_dq[i];
1.164 + _path.push_back(n2);
1.165 + _sum += _cost[_gr.edge(n1, n2)];
1.166 + }
1.167 +
1.168 + return _sum;
1.169 + }
1.170 +
1.171 + /// @}
1.172 +
1.173 + /// \name Query Functions
1.174 + /// @{
1.175 +
1.176 + /// \brief The total cost of the found tour.
1.177 + ///
1.178 + /// This function returns the total cost of the found tour.
1.179 + ///
1.180 + /// \pre run() must be called before using this function.
1.181 + Cost tourCost() const {
1.182 + return _sum;
1.183 + }
1.184 +
1.185 + /// \brief Returns a const reference to the node sequence of the
1.186 + /// found tour.
1.187 + ///
1.188 + /// This function returns a const reference to a vector
1.189 + /// that stores the node sequence of the found tour.
1.190 + ///
1.191 + /// \pre run() must be called before using this function.
1.192 + const std::vector<Node>& tourNodes() const {
1.193 + return _path;
1.194 + }
1.195 +
1.196 + /// \brief Gives back the node sequence of the found tour.
1.197 + ///
1.198 + /// This function copies the node sequence of the found tour into
1.199 + /// an STL container through the given output iterator. The
1.200 + /// <tt>value_type</tt> of the container must be <tt>FullGraph::Node</tt>.
1.201 + /// For example,
1.202 + /// \code
1.203 + /// std::vector<FullGraph::Node> nodes(countNodes(graph));
1.204 + /// tsp.tourNodes(nodes.begin());
1.205 + /// \endcode
1.206 + /// or
1.207 + /// \code
1.208 + /// std::list<FullGraph::Node> nodes;
1.209 + /// tsp.tourNodes(std::back_inserter(nodes));
1.210 + /// \endcode
1.211 + ///
1.212 + /// \pre run() must be called before using this function.
1.213 + template <typename Iterator>
1.214 + void tourNodes(Iterator out) const {
1.215 + std::copy(_path.begin(), _path.end(), out);
1.216 + }
1.217 +
1.218 + /// \brief Gives back the found tour as a path.
1.219 + ///
1.220 + /// This function copies the found tour as a list of arcs/edges into
1.221 + /// the given \ref lemon::concepts::Path "path structure".
1.222 + ///
1.223 + /// \pre run() must be called before using this function.
1.224 + template <typename Path>
1.225 + void tour(Path &path) const {
1.226 + path.clear();
1.227 + for (int i = 0; i < int(_path.size()) - 1; ++i) {
1.228 + path.addBack(_gr.arc(_path[i], _path[i+1]));
1.229 + }
1.230 + if (int(_path.size()) >= 2) {
1.231 + path.addBack(_gr.arc(_path.back(), _path.front()));
1.232 + }
1.233 + }
1.234 +
1.235 + /// @}
1.236 +
1.237 + };
1.238 +
1.239 +}; // namespace lemon
1.240 +
1.241 +#endif