Debian Codegraph @

lemon/greedy_tsp.h

raw symbols SCIP
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library.
4 *
5 * Copyright (C) 2003-2013
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 *
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
12 *
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
15 * purpose.
16 *
17 */
18
19 #ifndef LEMON_GREEDY_TSP_H
20 #define LEMON_GREEDY_TSP_Hcxx . . $ `lemon/greedy_tsp.h:20:9`!
21
22 /// \ingroup tsp
23 /// \file
24 /// \brief Greedy algorithm for symmetric TSP
25
26 #include <vector>
27 #include <algorithm>
28 #include <lemon/full_graph.h>cxx . . $ `<file>/lemon/full_graph.h`/
29 #include <lemon/unionfind.h>cxx . . $ `<file>/lemon/unionfind.h`/
30
31 namespace lemoncxx . . $ lemon/ {
32
33 /// \ingroup tsp
34 ///
35 /// \brief Greedy algorithm for symmetric TSP.
36 ///
37 /// GreedyTsp implements the greedy heuristic for solving
38 /// symmetric \ref tsp "TSP".
39 ///
40 /// This algorithm is quite similar to the \ref NearestNeighborTsp
41 /// "nearest neighbor" heuristic, but it maintains a set of disjoint paths.
42 /// At each step, the shortest possible edge is added to these paths
43 /// as long as it does not create a cycle of less than n edges and it does
44 /// not increase the degree of any node above two.
45 ///
46 /// This method runs in O(n<sup>2</sup>) time.
47 /// It quickly finds a relatively short tour for most TSP instances,
48 /// but it could also yield a really bad (or even the worst) solution
49 /// in special cases.
50 ///
51 /// \tparam CM Type of the cost map.
52 template <typename CM>
53 class GreedyTspcxx . . $ lemon/GreedyTsp#
54 {
55 public:
56
57 /// Type of the cost map
58 typedef CM CostMapcxx . . $ lemon/GreedyTsp#CostMap#;
59 /// Type of the edge costs
60 typedef typename CM::Value Costcxx . . $ lemon/GreedyTsp#Cost#;
61
62 private:
63
64 GRAPH_TYPEDEFScxx . . $ `lemon/core.h:138:9`!(FullGraph);
65
66 const FullGraphcxx . . $ lemon/FullGraph# &_grcxx . . $ lemon/GreedyTsp#_gr.;
67 const CostMapcxx . . $ lemon/GreedyTsp#CostMap# &_costcxx . . $ lemon/GreedyTsp#_cost.;
68 Costcxx . . $ lemon/GreedyTsp#Cost# _sumcxx . . $ lemon/GreedyTsp#_sum.;
69 stdcxx . . $ std/::vectorcxx . . $ std/vector#<Nodecxx . . $ lemon/GreedyTsp#Node#> _pathcxx . . $ lemon/GreedyTsp#_path.;
70
71 private:
72
73 // Functor class to compare edges by their costs
74 class EdgeCompcxx . . $ lemon/GreedyTsp#EdgeComp# {
75 private:
76 const CostMapcxx . . $ lemon/GreedyTsp#CostMap# &_costcxx . . $ lemon/GreedyTsp#EdgeComp#_cost.;
77
78 public:
79 EdgeCompcxx . . $ lemon/GreedyTsp#EdgeComp#EdgeComp(77d9f17553126ecb).(const CostMapcxx . . $ lemon/GreedyTsp#CostMap# &cost) : _costcxx . . $ lemon/GreedyTsp#EdgeComp#_cost.(cost) {}
80
81 bool operatorcxx . . $ lemon/GreedyTsp#EdgeComp#`operator()`(4e75b29f384a414c).()(const Edgecxx . . $ lemon/GreedyTsp#Edge# &a, const Edgecxx . . $ lemon/GreedyTsp#Edge# &b) const {
82 return _costcxx . . $ lemon/GreedyTsp#EdgeComp#_cost.[a] < _costcxx . . $ lemon/GreedyTsp#EdgeComp#_cost.[b];
83 }
84 };
85
86 public:
87
88 /// \brief Constructor
89 ///
90 /// Constructor.
91 /// \param gr The \ref FullGraph "full graph" the algorithm runs on.
92 /// \param cost The cost map.
93 GreedyTspcxx . . $ lemon/GreedyTsp#`GreedyTsp<CM>`(81ea2a8cfca42bae).(const FullGraphcxx . . $ lemon/FullGraph# &gr, const CostMapcxx . . $ lemon/GreedyTsp#CostMap# &cost)
94 : _grcxx . . $ lemon/GreedyTsp#_gr.(gr), _costcxx . . $ lemon/GreedyTsp#_cost.(cost) {}
95
96 /// \name Execution Control
97 /// @{
98
99 /// \brief Runs the algorithm.
100 ///
101 /// This function runs the algorithm.
102 ///
103 /// \return The total cost of the found tour.
104 Costcxx . . $ lemon/GreedyTsp#Cost# runcxx . . $ lemon/GreedyTsp#run(80f8fef63b8a4c54).() {
105 _pathcxx . . $ lemon/GreedyTsp#_path..clearcxx . . $ std/vector#clear(ced63f7c635d850d).();
106
107 if (_grcxx . . $ lemon/GreedyTsp#_gr..nodeNumcxx . . $ lemon/FullGraph#nodeNum(455f465bc33b4cdf).() == 0) return _sumcxx . . $ lemon/GreedyTsp#_sum. = 0;
108 else if (_grcxx . . $ lemon/GreedyTsp#_gr..nodeNumcxx . . $ lemon/FullGraph#nodeNum(455f465bc33b4cdf).() == 1) {
109 _pathcxx . . $ lemon/GreedyTsp#_path..push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(_grcxx . . $ lemon/GreedyTsp#_gr.(cxx . . $ lemon/FullGraph#`operator()`(f43f0888873f1bda).0));
110 return _sumcxx . . $ lemon/GreedyTsp#_sum. = 0;
111 }
112
113 stdcxx . . $ std/::vectorcxx . . $ std/vector#<int> plistcxx . . $ std/vector#vector(49f6e7a06ebc5aa8).;
114 plist.resizecxx . . $ std/vector#resize(3804226c8c0acbc3).(_grcxx . . $ lemon/GreedyTsp#_gr..nodeNumcxx . . $ lemon/FullGraph#nodeNum(455f465bc33b4cdf).()*2, -1);
115
116 stdcxx . . $ std/::vectorcxx . . $ std/vector#<Edgecxx . . $ lemon/GreedyTsp#Edge#> sorted_edgescxx . . $ std/vector#vector(49f6e7a06ebc5aa8).;
117 sorted_edges.reservecxx . . $ std/vector#reserve(2c44cd906ae4e829).(_grcxx . . $ lemon/GreedyTsp#_gr..edgeNumcxx . . $ lemon/FullGraph#edgeNum(455f465bc33b4cdf).());
118 for (EdgeItcxx . . $ lemon/GreedyTsp#EdgeIt# ecxx . . $ lemon/GraphExtender#EdgeIt#EdgeIt(f7cbc259d733564d).(_grcxx . . $ lemon/GreedyTsp#_gr.); e !=cxx . . $ lemon/FullGraphBase#Edge#`operator!=`(3449a43a7ad96bc9). INVALIDcxx . . $ lemon/FullGraphBase#Edge#Edge(9941a315a0500fa9).; ++cxx . . $ lemon/GraphExtender#EdgeIt#operator++(4af255ba78cf7b51).e)
119 sorted_edges.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(e);
120 stdcxx . . $ std/::sortcxx . . $ std/sort(249673c20e2b8d5).(sorted_edges.begincxx . . $ std/vector#begin(55a6de06420d4903).(), sorted_edges.endcxx . . $ std/vector#end(55a6de06420d4903).(), EdgeCompcxx . . $ lemon/GreedyTsp#EdgeComp#(_costcxx . . $ lemon/GreedyTsp#_cost.));
121
122 FullGraphcxx . . $ lemon/FullGraph#::NodeMapcxx . . $ lemon/GraphExtender#NodeMap#<int> item_int_mapcxx . . $ lemon/GraphExtender#NodeMap#NodeMap(f7cbc259d733564d).(_grcxx . . $ lemon/GreedyTsp#_gr.);
123 UnionFindcxx . . $ lemon/UnionFind#<FullGraphcxx . . $ lemon/FullGraph#::NodeMapcxx . . $ lemon/GraphExtender#NodeMap#<int> > union_findcxx . . $ lemon/UnionFind#UnionFind(8e935d9c2e576e3a).(item_int_map);
124 for (NodeItcxx . . $ lemon/GreedyTsp#NodeIt# ncxx . . $ lemon/GraphExtender#NodeIt#NodeIt(f7cbc259d733564d).(_grcxx . . $ lemon/GreedyTsp#_gr.); n !=cxx . . $ lemon/FullGraphBase#Node#`operator!=`(ae7cad53b1a60f94). INVALIDcxx . . $ lemon/FullGraphBase#Node#Node(9941a315a0500fa9).; ++cxx . . $ lemon/GraphExtender#NodeIt#operator++(815279054e383796).n)
125 union_find.insertcxx . . $ lemon/UnionFind#insert(ad6f53e7bf7b4b41).(n);
126
127 FullGraphcxx . . $ lemon/FullGraph#::NodeMapcxx . . $ lemon/GraphExtender#NodeMap#<int> degreecxx . . $ lemon/GraphExtender#NodeMap#NodeMap(a904b77160914920).(_grcxx . . $ lemon/GreedyTsp#_gr., 0);
128
129 int nodesNum = 0, i = 0;
130 while (nodesNum != _grcxx . . $ lemon/GreedyTsp#_gr..nodeNumcxx . . $ lemon/FullGraph#nodeNum(455f465bc33b4cdf).()-1) {
131 Edgecxx . . $ lemon/GreedyTsp#Edge# e = sorted_edges[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i++];
132 Nodecxx . . $ lemon/GreedyTsp#Node# u = _grcxx . . $ lemon/GreedyTsp#_gr..ucxx . . $ lemon/FullGraphBase#u(90e9f24214544109).(e),
133 v = _grcxx . . $ lemon/GreedyTsp#_gr..vcxx . . $ lemon/FullGraphBase#v(90e9f24214544109).(e);
134
135 if (degree[cxx . . $ lemon/VectorMap#`operator[]`(26476d667ba1c130).u] <= 1 && degree[cxx . . $ lemon/VectorMap#`operator[]`(26476d667ba1c130).v] <= 1) {
136 if (union_find.joincxx . . $ lemon/UnionFind#join(48999622324ae362).(u, v)) {
137 const int uid = _grcxx . . $ lemon/GreedyTsp#_gr..idcxx . . $ lemon/FullGraphBase#id(75262ae0874cc752).(u),
138 vid = _grcxx . . $ lemon/GreedyTsp#_gr..idcxx . . $ lemon/FullGraphBase#id(75262ae0874cc752).(v);
139
140 plist[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).uid*2 + degree[cxx . . $ lemon/VectorMap#`operator[]`(26476d667ba1c130).u]] = vid;
141 plist[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).vid*2 + degree[cxx . . $ lemon/VectorMap#`operator[]`(26476d667ba1c130).v]] = uid;
142
143 ++degree[cxx . . $ lemon/VectorMap#`operator[]`(26476d667ba1c130).u];
144 ++degree[cxx . . $ lemon/VectorMap#`operator[]`(26476d667ba1c130).v];
145 ++nodesNum;
146 }
147 }
148 }
149
150 for (int i=0, n=-1; i<_grcxx . . $ lemon/GreedyTsp#_gr..nodeNumcxx . . $ lemon/FullGraph#nodeNum(455f465bc33b4cdf).()*2; ++i) {
151 if (plist[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i] == -1) {
152 if (n==-1) {
153 n = i;
154 } else {
155 plist[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).n] = i/2;
156 plist[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i] = n/2;
157 break;
158 }
159 }
160 }
161
162 for (int i=0, next=0, last=-1; i!=_grcxx . . $ lemon/GreedyTsp#_gr..nodeNumcxx . . $ lemon/FullGraph#nodeNum(455f465bc33b4cdf).(); ++i) {
163 _pathcxx . . $ lemon/GreedyTsp#_path..push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(_grcxx . . $ lemon/GreedyTsp#_gr..nodeFromIdcxx . . $ lemon/FullGraphBase#nodeFromId(7afe1083acffa87e).(next));
164 if (plist[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).2*next] != last) {
165 last = next;
166 next = plist[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).2*next];
167 } else {
168 last = next;
169 next = plist[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).2*next+1];
170 }
171 }
172
173 _sumcxx . . $ lemon/GreedyTsp#_sum. = _costcxx . . $ lemon/GreedyTsp#_cost.[_grcxx . . $ lemon/GreedyTsp#_gr..edgecxx . . $ lemon/FullGraph#edge(d05214c95ce1fb2).(_pathcxx . . $ lemon/GreedyTsp#_path..backcxx . . $ std/vector#back(6baabfa6fc7b3a8e).(), _pathcxx . . $ lemon/GreedyTsp#_path..frontcxx . . $ std/vector#front(6baabfa6fc7b3a8e).())];
174 for (int i = 0; i < int(_pathcxx . . $ lemon/GreedyTsp#_path..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).())-1; ++i) {
175 _sumcxx . . $ lemon/GreedyTsp#_sum. += _costcxx . . $ lemon/GreedyTsp#_cost.[_grcxx . . $ lemon/GreedyTsp#_gr..edgecxx . . $ lemon/FullGraph#edge(d05214c95ce1fb2).(_pathcxx . . $ lemon/GreedyTsp#_path.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i], _pathcxx . . $ lemon/GreedyTsp#_path.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i+1])];
176 }
177
178 return _sumcxx . . $ lemon/GreedyTsp#_sum.;
179 }
180
181 /// @}
182
183 /// \name Query Functions
184 /// @{
185
186 /// \brief The total cost of the found tour.
187 ///
188 /// This function returns the total cost of the found tour.
189 ///
190 /// \pre run() must be called before using this function.
191 Costcxx . . $ lemon/GreedyTsp#Cost# tourCostcxx . . $ lemon/GreedyTsp#tourCost(b6c54a5948c5d88d).() const {
192 return _sumcxx . . $ lemon/GreedyTsp#_sum.;
193 }
194
195 /// \brief Returns a const reference to the node sequence of the
196 /// found tour.
197 ///
198 /// This function returns a const reference to a vector
199 /// that stores the node sequence of the found tour.
200 ///
201 /// \pre run() must be called before using this function.
202 const stdcxx . . $ std/::vectorcxx . . $ std/vector#<Nodecxx . . $ lemon/GreedyTsp#Node#>& tourNodescxx . . $ lemon/GreedyTsp#tourNodes(6db751875f28fd97).() const {
203 return _pathcxx . . $ lemon/GreedyTsp#_path.;
204 }
205
206 /// \brief Gives back the node sequence of the found tour.
207 ///
208 /// This function copies the node sequence of the found tour into
209 /// an STL container through the given output iterator. The
210 /// <tt>value_type</tt> of the container must be <tt>FullGraph::Node</tt>.
211 /// For example,
212 /// \code
213 /// std::vector<FullGraph::Node> nodes(countNodes(graph));
214 /// tsp.tourNodes(nodes.begin());
215 /// \endcode
216 /// or
217 /// \code
218 /// std::list<FullGraph::Node> nodes;
219 /// tsp.tourNodes(std::back_inserter(nodes));
220 /// \endcode
221 ///
222 /// \pre run() must be called before using this function.
223 template <typename Iterator>
224 void tourNodescxx . . $ lemon/GreedyTsp#tourNodes(ca25bb472b6ba38f).(Iterator out) const {
225 stdcxx . . $ std/::copycxx . . $ std/copy(88304b602929f6f4).(_pathcxx . . $ lemon/GreedyTsp#_path..begincxx . . $ std/vector#begin(d1e1699016b9d7e6).(), _pathcxx . . $ lemon/GreedyTsp#_path..endcxx . . $ std/vector#end(d1e1699016b9d7e6).(), out);
226 }
227
228 /// \brief Gives back the found tour as a path.
229 ///
230 /// This function copies the found tour as a list of arcs/edges into
231 /// the given \ref lemon::concepts::Path "path structure".
232 ///
233 /// \pre run() must be called before using this function.
234 template <typename Path>
235 void tourcxx . . $ lemon/GreedyTsp#tour(b7b72e2ad5578bfe).(Path &path) const {
236 path.clear();
237 for (int i = 0; i < int(_pathcxx . . $ lemon/GreedyTsp#_path..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) - 1; ++i) {
238 path.addBack(_grcxx . . $ lemon/GreedyTsp#_gr..arccxx . . $ lemon/FullGraph#arc(fe5199824d9892b).(_pathcxx . . $ lemon/GreedyTsp#_path.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).i], _pathcxx . . $ lemon/GreedyTsp#_path.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).i+1]));
239 }
240 if (int(_pathcxx . . $ lemon/GreedyTsp#_path..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) >= 2) {
241 path.addBack(_grcxx . . $ lemon/GreedyTsp#_gr..arccxx . . $ lemon/FullGraph#arc(fe5199824d9892b).(_pathcxx . . $ lemon/GreedyTsp#_path..backcxx . . $ std/vector#back(af0004c3ce7cab93).(), _pathcxx . . $ lemon/GreedyTsp#_path..frontcxx . . $ std/vector#front(af0004c3ce7cab93).()));
242 }
243 }
244
245 /// @}
246
247 };
248
249 }; // namespace lemon
250
251 #endif