Debian Codegraph @

lemon/christofides_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_CHRISTOFIDES_TSP_H
20 #define LEMON_CHRISTOFIDES_TSP_Hcxx . . $ `lemon/christofides_tsp.h:20:9`!
21
22 /// \ingroup tsp
23 /// \file
24 /// \brief Christofides algorithm for symmetric TSP
25
26 #include <lemon/full_graph.h>cxx . . $ `<file>/lemon/full_graph.h`/
27 #include <lemon/smart_graph.h>cxx . . $ `<file>/lemon/smart_graph.h`/
28 #include <lemon/kruskal.h>cxx . . $ `<file>/lemon/kruskal.h`/
29 #include <lemon/matching.h>cxx . . $ `<file>/lemon/matching.h`/
30 #include <lemon/euler.h>cxx . . $ `<file>/lemon/euler.h`/
31
32 namespace lemoncxx . . $ lemon/ {
33
34 /// \ingroup tsp
35 ///
36 /// \brief Christofides algorithm for symmetric TSP.
37 ///
38 /// ChristofidesTsp implements Christofides' heuristic for solving
39 /// symmetric \ref tsp "TSP".
40 ///
41 /// This a well-known approximation method for the TSP problem with
42 /// metric cost function.
43 /// It has a guaranteed approximation factor of 3/2 (i.e. it finds a tour
44 /// whose total cost is at most 3/2 of the optimum), but it usually
45 /// provides better solutions in practice.
46 /// This implementation runs in O(n<sup>3</sup>log(n)) time.
47 ///
48 /// The algorithm starts with a \ref spantree "minimum cost spanning tree" and
49 /// finds a \ref MaxWeightedPerfectMatching "minimum cost perfect matching"
50 /// in the subgraph induced by the nodes that have odd degree in the
51 /// spanning tree.
52 /// Finally, it constructs the tour from the \ref EulerIt "Euler traversal"
53 /// of the union of the spanning tree and the matching.
54 /// During this last step, the algorithm simply skips the visited nodes
55 /// (i.e. creates shortcuts) assuming that the triangle inequality holds
56 /// for the cost function.
57 ///
58 /// \tparam CM Type of the cost map.
59 ///
60 /// \warning CM::Value must be a signed number type.
61 template <typename CM>
62 class ChristofidesTspcxx . . $ lemon/ChristofidesTsp#
63 {
64 public:
65
66 /// Type of the cost map
67 typedef CM CostMapcxx . . $ lemon/ChristofidesTsp#CostMap#;
68 /// Type of the edge costs
69 typedef typename CM::Value Costcxx . . $ lemon/ChristofidesTsp#Cost#;
70
71 private:
72
73 GRAPH_TYPEDEFScxx . . $ `lemon/core.h:138:9`!(FullGraph);
74
75 const FullGraphcxx . . $ lemon/FullGraph# &_grcxx . . $ lemon/ChristofidesTsp#_gr.;
76 const CostMapcxx . . $ lemon/ChristofidesTsp#CostMap# &_costcxx . . $ lemon/ChristofidesTsp#_cost.;
77 stdcxx . . $ std/::vectorcxx . . $ std/vector#<Nodecxx . . $ lemon/ChristofidesTsp#Node#> _pathcxx . . $ lemon/ChristofidesTsp#_path.;
78 Costcxx . . $ lemon/ChristofidesTsp#Cost# _sumcxx . . $ lemon/ChristofidesTsp#_sum.;
79
80 public:
81
82 /// \brief Constructor
83 ///
84 /// Constructor.
85 /// \param gr The \ref FullGraph "full graph" the algorithm runs on.
86 /// \param cost The cost map.
87 ChristofidesTspcxx . . $ lemon/ChristofidesTsp#`ChristofidesTsp<CM>`(81ea2a8cfca42bae).(const FullGraphcxx . . $ lemon/FullGraph# &gr, const CostMapcxx . . $ lemon/ChristofidesTsp#CostMap# &cost)
88 : _grcxx . . $ lemon/ChristofidesTsp#_gr.(gr), _costcxx . . $ lemon/ChristofidesTsp#_cost.(cost) {}
89
90 /// \name Execution Control
91 /// @{
92
93 /// \brief Runs the algorithm.
94 ///
95 /// This function runs the algorithm.
96 ///
97 /// \return The total cost of the found tour.
98 Costcxx . . $ lemon/ChristofidesTsp#Cost# runcxx . . $ lemon/ChristofidesTsp#run(80f8fef63b8a4c54).() {
99 _pathcxx . . $ lemon/ChristofidesTsp#_path..clearcxx . . $ std/vector#clear(ced63f7c635d850d).();
100
101 if (_grcxx . . $ lemon/ChristofidesTsp#_gr..nodeNumcxx . . $ lemon/FullGraph#nodeNum(455f465bc33b4cdf).() == 0) return _sumcxx . . $ lemon/ChristofidesTsp#_sum. = 0;
102 else if (_grcxx . . $ lemon/ChristofidesTsp#_gr..nodeNumcxx . . $ lemon/FullGraph#nodeNum(455f465bc33b4cdf).() == 1) {
103 _pathcxx . . $ lemon/ChristofidesTsp#_path..push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(_grcxx . . $ lemon/ChristofidesTsp#_gr.(cxx . . $ lemon/FullGraph#`operator()`(f43f0888873f1bda).0));
104 return _sumcxx . . $ lemon/ChristofidesTsp#_sum. = 0;
105 }
106 else if (_grcxx . . $ lemon/ChristofidesTsp#_gr..nodeNumcxx . . $ lemon/FullGraph#nodeNum(455f465bc33b4cdf).() == 2) {
107 _pathcxx . . $ lemon/ChristofidesTsp#_path..push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(_grcxx . . $ lemon/ChristofidesTsp#_gr.(cxx . . $ lemon/FullGraph#`operator()`(f43f0888873f1bda).0));
108 _pathcxx . . $ lemon/ChristofidesTsp#_path..push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(_grcxx . . $ lemon/ChristofidesTsp#_gr.(cxx . . $ lemon/FullGraph#`operator()`(f43f0888873f1bda).1));
109 return _sumcxx . . $ lemon/ChristofidesTsp#_sum. = 2 * _costcxx . . $ lemon/ChristofidesTsp#_cost.[_grcxx . . $ lemon/ChristofidesTsp#_gr..edgecxx . . $ lemon/FullGraph#edge(d05214c95ce1fb2).(_grcxx . . $ lemon/ChristofidesTsp#_gr.(cxx . . $ lemon/FullGraph#`operator()`(f43f0888873f1bda).0), _grcxx . . $ lemon/ChristofidesTsp#_gr.(cxx . . $ lemon/FullGraph#`operator()`(f43f0888873f1bda).1))];
110 }
111
112 // Compute min. cost spanning tree
113 stdcxx . . $ std/::vectorcxx . . $ std/vector#<Edgecxx . . $ lemon/ChristofidesTsp#Edge#> treecxx . . $ std/vector#vector(49f6e7a06ebc5aa8).;
114 kruskalcxx . . $ lemon/kruskal(f9c95b406ce1f8b0).(_grcxx . . $ lemon/ChristofidesTsp#_gr., _costcxx . . $ lemon/ChristofidesTsp#_cost., stdcxx . . $ std/::back_insertercxx . . $ std/back_inserter(7b559d6bdd90ff74).(tree));
115
116 FullGraphcxx . . $ lemon/FullGraph#::NodeMapcxx . . $ lemon/GraphExtender#NodeMap#<int> degcxx . . $ lemon/GraphExtender#NodeMap#NodeMap(a904b77160914920).(_grcxx . . $ lemon/ChristofidesTsp#_gr., 0);
117 for (int i = 0; i != int(tree.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()); ++i) {
118 Edgecxx . . $ lemon/ChristofidesTsp#Edge# e = tree[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i];
119 ++deg[cxx . . $ lemon/VectorMap#`operator[]`(26476d667ba1c130)._grcxx . . $ lemon/ChristofidesTsp#_gr..ucxx . . $ lemon/FullGraphBase#u(90e9f24214544109).(e)];
120 ++deg[cxx . . $ lemon/VectorMap#`operator[]`(26476d667ba1c130)._grcxx . . $ lemon/ChristofidesTsp#_gr..vcxx . . $ lemon/FullGraphBase#v(90e9f24214544109).(e)];
121 }
122
123 // Copy the induced subgraph of odd nodes
124 stdcxx . . $ std/::vectorcxx . . $ std/vector#<Nodecxx . . $ lemon/ChristofidesTsp#Node#> odd_nodescxx . . $ std/vector#vector(49f6e7a06ebc5aa8).;
125 for (NodeItcxx . . $ lemon/ChristofidesTsp#NodeIt# ucxx . . $ lemon/GraphExtender#NodeIt#NodeIt(f7cbc259d733564d).(_grcxx . . $ lemon/ChristofidesTsp#_gr.); u !=cxx . . $ lemon/FullGraphBase#Node#`operator!=`(ae7cad53b1a60f94). INVALIDcxx . . $ lemon/FullGraphBase#Node#Node(9941a315a0500fa9).; ++cxx . . $ lemon/GraphExtender#NodeIt#operator++(815279054e383796).u) {
126 if (deg[cxx . . $ lemon/VectorMap#`operator[]`(26476d667ba1c130).u] % 2 == 1) odd_nodes.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(u);
127 }
128
129 SmartGraphcxx . . $ lemon/SmartGraph# sgrcxx . . $ lemon/SmartGraph#SmartGraph(49f6e7a06ebc5aa8).;
130 SmartGraphcxx . . $ lemon/SmartGraph#::EdgeMapcxx . . $ lemon/GraphExtender#EdgeMap#<Costcxx . . $ lemon/ChristofidesTsp#Cost#> scost(sgr);
131 for (int i = 0; i != int(odd_nodes.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()); ++i) {
132 sgr.addNodecxx . . $ lemon/SmartGraph#addNode(377d2348da19766e).();
133 }
134 for (int i = 0; i != int(odd_nodes.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()); ++i) {
135 for (int j = 0; j != int(odd_nodes.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()); ++j) {
136 if (j == i) continue;
137 SmartGraphcxx . . $ lemon/SmartGraph#::Edgecxx . . $ lemon/GraphExtender#Edge# e =
138 sgr.addEdgecxx . . $ lemon/SmartGraph#addEdge(ca3a6a7f6eda9a1d).(sgr.nodeFromIdcxx . . $ lemon/SmartGraphBase#nodeFromId(93dbb998c121545b).(i), sgr.nodeFromIdcxx . . $ lemon/SmartGraphBase#nodeFromId(93dbb998c121545b).(j));
139 scost[e] = -_costcxx . . $ lemon/ChristofidesTsp#_cost.[_grcxx . . $ lemon/ChristofidesTsp#_gr..edgecxx . . $ lemon/FullGraph#edge(d05214c95ce1fb2).(odd_nodes[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i], odd_nodes[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).j])];
140 }
141 }
142
143 // Compute min. cost perfect matching
144 MaxWeightedPerfectMatchingcxx . . $ lemon/MaxWeightedPerfectMatching#<SmartGraphcxx . . $ lemon/SmartGraph#, SmartGraphcxx . . $ lemon/SmartGraph#::EdgeMapcxx . . $ lemon/GraphExtender#EdgeMap#<Costcxx . . $ lemon/ChristofidesTsp#Cost#> >
145 mwpm(sgr, scost);
146 mwpm.runcxx . . $ lemon/MaxWeightedPerfectMatching#run(c9857a73d68e59ba).();
147
148 for (SmartGraphcxx . . $ lemon/SmartGraph#::EdgeItcxx . . $ lemon/GraphExtender#EdgeIt# ecxx . . $ lemon/GraphExtender#EdgeIt#EdgeIt(f7cbc259d733564d).(sgr); e !=cxx . . $ lemon/SmartGraphBase#Edge#`operator!=`(7b5ec7fa42356449). INVALIDcxx . . $ lemon/SmartGraphBase#Edge#Edge(9941a315a0500fa9).; ++cxx . . $ lemon/GraphExtender#EdgeIt#operator++(4af255ba78cf7b51).e) {
149 if (mwpm.matchingcxx . . $ lemon/MaxWeightedPerfectMatching#matching(20d676d682052ee3).(e)) {
150 tree.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).( _grcxx . . $ lemon/ChristofidesTsp#_gr..edgecxx . . $ lemon/FullGraph#edge(d05214c95ce1fb2).(odd_nodes[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).sgr.idcxx . . $ lemon/SmartGraphBase#id(167b11ce76d47f38).(sgr.ucxx . . $ lemon/SmartGraphBase#u(c6aae2566db329b1).(e))],
151 odd_nodes[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).sgr.idcxx . . $ lemon/SmartGraphBase#id(167b11ce76d47f38).(sgr.vcxx . . $ lemon/SmartGraphBase#v(c6aae2566db329b1).(e))]) );
152 }
153 }
154
155 // Join the spanning tree and the matching
156 sgr.clearcxx . . $ lemon/SmartGraph#clear(49f6e7a06ebc5aa8).();
157 for (int i = 0; i != _grcxx . . $ lemon/ChristofidesTsp#_gr..nodeNumcxx . . $ lemon/FullGraph#nodeNum(455f465bc33b4cdf).(); ++i) {
158 sgr.addNodecxx . . $ lemon/SmartGraph#addNode(377d2348da19766e).();
159 }
160 for (int i = 0; i != int(tree.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()); ++i) {
161 int ui = _grcxx . . $ lemon/ChristofidesTsp#_gr..idcxx . . $ lemon/FullGraphBase#id(75262ae0874cc752).(_grcxx . . $ lemon/ChristofidesTsp#_gr..ucxx . . $ lemon/FullGraphBase#u(90e9f24214544109).(tree[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i])),
162 vi = _grcxx . . $ lemon/ChristofidesTsp#_gr..idcxx . . $ lemon/FullGraphBase#id(75262ae0874cc752).(_grcxx . . $ lemon/ChristofidesTsp#_gr..vcxx . . $ lemon/FullGraphBase#v(90e9f24214544109).(tree[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i]));
163 sgr.addEdgecxx . . $ lemon/SmartGraph#addEdge(ca3a6a7f6eda9a1d).(sgr.nodeFromIdcxx . . $ lemon/SmartGraphBase#nodeFromId(93dbb998c121545b).(ui), sgr.nodeFromIdcxx . . $ lemon/SmartGraphBase#nodeFromId(93dbb998c121545b).(vi));
164 }
165
166 // Compute the tour from the Euler traversal
167 SmartGraphcxx . . $ lemon/SmartGraph#::NodeMapcxx . . $ lemon/GraphExtender#NodeMap#<bool> visitedcxx . . $ lemon/GraphExtender#NodeMap#NodeMap(a904b77160914920).(sgr, false);
168 for (EulerItcxx . . $ lemon/EulerIt#<SmartGraphcxx . . $ lemon/SmartGraph#> ecxx . . $ lemon/EulerIt#EulerIt(4594fc70e2e451aa).(sgr); e !=cxx . . $ lemon/EulerIt#`operator!=`(d37eab13c3241f2f). INVALIDcxx . . $ lemon/INVALID.; ++cxx . . $ lemon/EulerIt#operator++(4aef498da5553b8d).e) {
169 SmartGraphcxx . . $ lemon/SmartGraph#::Nodecxx . . $ lemon/GraphExtender#Node# n = sgr.targetcxx . . $ lemon/SmartGraphBase#target(f3880e907b7576dc).(e);
170 if (!visited[cxx . . $ lemon/VectorMap#`operator[]`(26476d667ba1c130).n]) {
171 _pathcxx . . $ lemon/ChristofidesTsp#_path..push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(_grcxx . . $ lemon/ChristofidesTsp#_gr.(cxx . . $ lemon/FullGraph#`operator()`(f43f0888873f1bda).sgr.idcxx . . $ lemon/SmartGraphBase#id(167b11ce76d47f38).(n)));
172 visited[cxx . . $ lemon/VectorMap#`operator[]`(26476d667ba1c130).n] =cxx . . $ std/_Bit_reference#`operator=`(2a6c206c8bbf32aa). true;
173 }
174 }
175
176 _sumcxx . . $ lemon/ChristofidesTsp#_sum. = _costcxx . . $ lemon/ChristofidesTsp#_cost.[_grcxx . . $ lemon/ChristofidesTsp#_gr..edgecxx . . $ lemon/FullGraph#edge(d05214c95ce1fb2).(_pathcxx . . $ lemon/ChristofidesTsp#_path..backcxx . . $ std/vector#back(6baabfa6fc7b3a8e).(), _pathcxx . . $ lemon/ChristofidesTsp#_path..frontcxx . . $ std/vector#front(6baabfa6fc7b3a8e).())];
177 for (int i = 0; i < int(_pathcxx . . $ lemon/ChristofidesTsp#_path..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).())-1; ++i) {
178 _sumcxx . . $ lemon/ChristofidesTsp#_sum. += _costcxx . . $ lemon/ChristofidesTsp#_cost.[_grcxx . . $ lemon/ChristofidesTsp#_gr..edgecxx . . $ lemon/FullGraph#edge(d05214c95ce1fb2).(_pathcxx . . $ lemon/ChristofidesTsp#_path.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i], _pathcxx . . $ lemon/ChristofidesTsp#_path.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i+1])];
179 }
180
181 return _sumcxx . . $ lemon/ChristofidesTsp#_sum.;
182 }
183
184 /// @}
185
186 /// \name Query Functions
187 /// @{
188
189 /// \brief The total cost of the found tour.
190 ///
191 /// This function returns the total cost of the found tour.
192 ///
193 /// \pre run() must be called before using this function.
194 Costcxx . . $ lemon/ChristofidesTsp#Cost# tourCostcxx . . $ lemon/ChristofidesTsp#tourCost(b6c54a5948c5d88d).() const {
195 return _sumcxx . . $ lemon/ChristofidesTsp#_sum.;
196 }
197
198 /// \brief Returns a const reference to the node sequence of the
199 /// found tour.
200 ///
201 /// This function returns a const reference to a vector
202 /// that stores the node sequence of the found tour.
203 ///
204 /// \pre run() must be called before using this function.
205 const stdcxx . . $ std/::vectorcxx . . $ std/vector#<Nodecxx . . $ lemon/ChristofidesTsp#Node#>& tourNodescxx . . $ lemon/ChristofidesTsp#tourNodes(6db751875f28fd97).() const {
206 return _pathcxx . . $ lemon/ChristofidesTsp#_path.;
207 }
208
209 /// \brief Gives back the node sequence of the found tour.
210 ///
211 /// This function copies the node sequence of the found tour into
212 /// an STL container through the given output iterator. The
213 /// <tt>value_type</tt> of the container must be <tt>FullGraph::Node</tt>.
214 /// For example,
215 /// \code
216 /// std::vector<FullGraph::Node> nodes(countNodes(graph));
217 /// tsp.tourNodes(nodes.begin());
218 /// \endcode
219 /// or
220 /// \code
221 /// std::list<FullGraph::Node> nodes;
222 /// tsp.tourNodes(std::back_inserter(nodes));
223 /// \endcode
224 ///
225 /// \pre run() must be called before using this function.
226 template <typename Iterator>
227 void tourNodescxx . . $ lemon/ChristofidesTsp#tourNodes(ca25bb472b6ba38f).(Iterator out) const {
228 stdcxx . . $ std/::copycxx . . $ std/copy(88304b602929f6f4).(_pathcxx . . $ lemon/ChristofidesTsp#_path..begincxx . . $ std/vector#begin(d1e1699016b9d7e6).(), _pathcxx . . $ lemon/ChristofidesTsp#_path..endcxx . . $ std/vector#end(d1e1699016b9d7e6).(), out);
229 }
230
231 /// \brief Gives back the found tour as a path.
232 ///
233 /// This function copies the found tour as a list of arcs/edges into
234 /// the given \ref lemon::concepts::Path "path structure".
235 ///
236 /// \pre run() must be called before using this function.
237 template <typename Path>
238 void tourcxx . . $ lemon/ChristofidesTsp#tour(b7b72e2ad5578bfe).(Path &path) const {
239 path.clear();
240 for (int i = 0; i < int(_pathcxx . . $ lemon/ChristofidesTsp#_path..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) - 1; ++i) {
241 path.addBack(_grcxx . . $ lemon/ChristofidesTsp#_gr..arccxx . . $ lemon/FullGraph#arc(fe5199824d9892b).(_pathcxx . . $ lemon/ChristofidesTsp#_path.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).i], _pathcxx . . $ lemon/ChristofidesTsp#_path.[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).i+1]));
242 }
243 if (int(_pathcxx . . $ lemon/ChristofidesTsp#_path..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) >= 2) {
244 path.addBack(_grcxx . . $ lemon/ChristofidesTsp#_gr..arccxx . . $ lemon/FullGraph#arc(fe5199824d9892b).(_pathcxx . . $ lemon/ChristofidesTsp#_path..backcxx . . $ std/vector#back(af0004c3ce7cab93).(), _pathcxx . . $ lemon/ChristofidesTsp#_path..frontcxx . . $ std/vector#front(af0004c3ce7cab93).()));
245 }
246 }
247
248 /// @}
249
250 };
251
252 }; // namespace lemon
253
254 #endif