1 | /* -*- C++ -*- |
---|
2 | * |
---|
3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
4 | * |
---|
5 | * Copyright (C) 2003-2008 |
---|
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_MIN_MEAN_CYCLE_H |
---|
20 | #define LEMON_MIN_MEAN_CYCLE_H |
---|
21 | |
---|
22 | /// \ingroup shortest_path |
---|
23 | /// |
---|
24 | /// \file |
---|
25 | /// \brief Howard's algorithm for finding a minimum mean cycle. |
---|
26 | |
---|
27 | #include <vector> |
---|
28 | #include <lemon/core.h> |
---|
29 | #include <lemon/path.h> |
---|
30 | #include <lemon/tolerance.h> |
---|
31 | #include <lemon/connectivity.h> |
---|
32 | |
---|
33 | namespace lemon { |
---|
34 | |
---|
35 | /// \addtogroup shortest_path |
---|
36 | /// @{ |
---|
37 | |
---|
38 | /// \brief Implementation of Howard's algorithm for finding a minimum |
---|
39 | /// mean cycle. |
---|
40 | /// |
---|
41 | /// \ref MinMeanCycle implements Howard's algorithm for finding a |
---|
42 | /// directed cycle of minimum mean length (cost) in a digraph. |
---|
43 | /// |
---|
44 | /// \tparam GR The type of the digraph the algorithm runs on. |
---|
45 | /// \tparam LEN The type of the length map. The default |
---|
46 | /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
---|
47 | /// |
---|
48 | /// \warning \c LEN::Value must be convertible to \c double. |
---|
49 | #ifdef DOXYGEN |
---|
50 | template <typename GR, typename LEN> |
---|
51 | #else |
---|
52 | template < typename GR, |
---|
53 | typename LEN = typename GR::template ArcMap<int> > |
---|
54 | #endif |
---|
55 | class MinMeanCycle |
---|
56 | { |
---|
57 | public: |
---|
58 | |
---|
59 | /// The type of the digraph the algorithm runs on |
---|
60 | typedef GR Digraph; |
---|
61 | /// The type of the length map |
---|
62 | typedef LEN LengthMap; |
---|
63 | /// The type of the arc lengths |
---|
64 | typedef typename LengthMap::Value Value; |
---|
65 | /// The type of the paths |
---|
66 | typedef lemon::Path<Digraph> Path; |
---|
67 | |
---|
68 | private: |
---|
69 | |
---|
70 | TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
---|
71 | |
---|
72 | // The digraph the algorithm runs on |
---|
73 | const Digraph &_gr; |
---|
74 | // The length of the arcs |
---|
75 | const LengthMap &_length; |
---|
76 | |
---|
77 | // The total length of the found cycle |
---|
78 | Value _cycle_length; |
---|
79 | // The number of arcs on the found cycle |
---|
80 | int _cycle_size; |
---|
81 | // The found cycle |
---|
82 | Path *_cycle_path; |
---|
83 | |
---|
84 | bool _local_path; |
---|
85 | bool _cycle_found; |
---|
86 | Node _cycle_node; |
---|
87 | |
---|
88 | typename Digraph::template NodeMap<bool> _reached; |
---|
89 | typename Digraph::template NodeMap<double> _dist; |
---|
90 | typename Digraph::template NodeMap<Arc> _policy; |
---|
91 | |
---|
92 | typename Digraph::template NodeMap<int> _comp; |
---|
93 | int _comp_num; |
---|
94 | |
---|
95 | std::vector<Node> _nodes; |
---|
96 | std::vector<Arc> _arcs; |
---|
97 | Tolerance<double> _tol; |
---|
98 | |
---|
99 | public: |
---|
100 | |
---|
101 | /// \brief Constructor. |
---|
102 | /// |
---|
103 | /// The constructor of the class. |
---|
104 | /// |
---|
105 | /// \param digraph The digraph the algorithm runs on. |
---|
106 | /// \param length The lengths (costs) of the arcs. |
---|
107 | MinMeanCycle( const Digraph &digraph, |
---|
108 | const LengthMap &length ) : |
---|
109 | _gr(digraph), _length(length), _cycle_length(0), _cycle_size(-1), |
---|
110 | _cycle_path(NULL), _local_path(false), _reached(digraph), |
---|
111 | _dist(digraph), _policy(digraph), _comp(digraph) |
---|
112 | {} |
---|
113 | |
---|
114 | /// Destructor. |
---|
115 | ~MinMeanCycle() { |
---|
116 | if (_local_path) delete _cycle_path; |
---|
117 | } |
---|
118 | |
---|
119 | /// \brief Set the path structure for storing the found cycle. |
---|
120 | /// |
---|
121 | /// This function sets an external path structure for storing the |
---|
122 | /// found cycle. |
---|
123 | /// |
---|
124 | /// If you don't call this function before calling \ref run() or |
---|
125 | /// \ref findMinMean(), it will allocate a local \ref Path "path" |
---|
126 | /// structure. The destuctor deallocates this automatically |
---|
127 | /// allocated object, of course. |
---|
128 | /// |
---|
129 | /// \note The algorithm calls only the \ref lemon::Path::addBack() |
---|
130 | /// "addBack()" function of the given path structure. |
---|
131 | /// |
---|
132 | /// \return <tt>(*this)</tt> |
---|
133 | /// |
---|
134 | /// \sa cycle() |
---|
135 | MinMeanCycle& cyclePath(Path &path) { |
---|
136 | if (_local_path) { |
---|
137 | delete _cycle_path; |
---|
138 | _local_path = false; |
---|
139 | } |
---|
140 | _cycle_path = &path; |
---|
141 | return *this; |
---|
142 | } |
---|
143 | |
---|
144 | /// \name Execution control |
---|
145 | /// The simplest way to execute the algorithm is to call the \ref run() |
---|
146 | /// function.\n |
---|
147 | /// If you only need the minimum mean length, you may call |
---|
148 | /// \ref findMinMean(). |
---|
149 | |
---|
150 | /// @{ |
---|
151 | |
---|
152 | /// \brief Run the algorithm. |
---|
153 | /// |
---|
154 | /// This function runs the algorithm. |
---|
155 | /// It can be called more than once (e.g. if the underlying digraph |
---|
156 | /// and/or the arc lengths have been modified). |
---|
157 | /// |
---|
158 | /// \return \c true if a directed cycle exists in the digraph. |
---|
159 | /// |
---|
160 | /// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
---|
161 | /// \code |
---|
162 | /// return mmc.findMinMean() && mmc.findCycle(); |
---|
163 | /// \endcode |
---|
164 | bool run() { |
---|
165 | return findMinMean() && findCycle(); |
---|
166 | } |
---|
167 | |
---|
168 | /// \brief Find the minimum cycle mean. |
---|
169 | /// |
---|
170 | /// This function finds the minimum mean length of the directed |
---|
171 | /// cycles in the digraph. |
---|
172 | /// |
---|
173 | /// \return \c true if a directed cycle exists in the digraph. |
---|
174 | bool findMinMean() { |
---|
175 | // Initialize |
---|
176 | _tol.epsilon(1e-6); |
---|
177 | if (!_cycle_path) { |
---|
178 | _local_path = true; |
---|
179 | _cycle_path = new Path; |
---|
180 | } |
---|
181 | _cycle_path->clear(); |
---|
182 | _cycle_found = false; |
---|
183 | |
---|
184 | // Find the minimum cycle mean in the components |
---|
185 | _comp_num = stronglyConnectedComponents(_gr, _comp); |
---|
186 | for (int comp = 0; comp < _comp_num; ++comp) { |
---|
187 | if (!initCurrentComponent(comp)) continue; |
---|
188 | while (true) { |
---|
189 | if (!findPolicyCycles()) break; |
---|
190 | contractPolicyGraph(comp); |
---|
191 | if (!computeNodeDistances()) break; |
---|
192 | } |
---|
193 | } |
---|
194 | return _cycle_found; |
---|
195 | } |
---|
196 | |
---|
197 | /// \brief Find a minimum mean directed cycle. |
---|
198 | /// |
---|
199 | /// This function finds a directed cycle of minimum mean length |
---|
200 | /// in the digraph using the data computed by findMinMean(). |
---|
201 | /// |
---|
202 | /// \return \c true if a directed cycle exists in the digraph. |
---|
203 | /// |
---|
204 | /// \pre \ref findMinMean() must be called before using this function. |
---|
205 | bool findCycle() { |
---|
206 | if (!_cycle_found) return false; |
---|
207 | _cycle_path->addBack(_policy[_cycle_node]); |
---|
208 | for ( Node v = _cycle_node; |
---|
209 | (v = _gr.target(_policy[v])) != _cycle_node; ) { |
---|
210 | _cycle_path->addBack(_policy[v]); |
---|
211 | } |
---|
212 | return true; |
---|
213 | } |
---|
214 | |
---|
215 | /// @} |
---|
216 | |
---|
217 | /// \name Query Functions |
---|
218 | /// The results of the algorithm can be obtained using these |
---|
219 | /// functions.\n |
---|
220 | /// The algorithm should be executed before using them. |
---|
221 | |
---|
222 | /// @{ |
---|
223 | |
---|
224 | /// \brief Return the total length of the found cycle. |
---|
225 | /// |
---|
226 | /// This function returns the total length of the found cycle. |
---|
227 | /// |
---|
228 | /// \pre \ref run() or \ref findCycle() must be called before |
---|
229 | /// using this function. |
---|
230 | Value cycleLength() const { |
---|
231 | return _cycle_length; |
---|
232 | } |
---|
233 | |
---|
234 | /// \brief Return the number of arcs on the found cycle. |
---|
235 | /// |
---|
236 | /// This function returns the number of arcs on the found cycle. |
---|
237 | /// |
---|
238 | /// \pre \ref run() or \ref findCycle() must be called before |
---|
239 | /// using this function. |
---|
240 | int cycleArcNum() const { |
---|
241 | return _cycle_size; |
---|
242 | } |
---|
243 | |
---|
244 | /// \brief Return the mean length of the found cycle. |
---|
245 | /// |
---|
246 | /// This function returns the mean length of the found cycle. |
---|
247 | /// |
---|
248 | /// \note <tt>mmc.cycleMean()</tt> is just a shortcut of the |
---|
249 | /// following code. |
---|
250 | /// \code |
---|
251 | /// return double(mmc.cycleLength()) / mmc.cycleArcNum(); |
---|
252 | /// \endcode |
---|
253 | /// |
---|
254 | /// \pre \ref run() or \ref findMinMean() must be called before |
---|
255 | /// using this function. |
---|
256 | double cycleMean() const { |
---|
257 | return double(_cycle_length) / _cycle_size; |
---|
258 | } |
---|
259 | |
---|
260 | /// \brief Return the found cycle. |
---|
261 | /// |
---|
262 | /// This function returns a const reference to the path structure |
---|
263 | /// storing the found cycle. |
---|
264 | /// |
---|
265 | /// \pre \ref run() or \ref findCycle() must be called before using |
---|
266 | /// this function. |
---|
267 | /// |
---|
268 | /// \sa cyclePath() |
---|
269 | const Path& cycle() const { |
---|
270 | return *_cycle_path; |
---|
271 | } |
---|
272 | |
---|
273 | ///@} |
---|
274 | |
---|
275 | private: |
---|
276 | |
---|
277 | // Initialize the internal data structures for the current strongly |
---|
278 | // connected component and create the policy graph. |
---|
279 | // The policy graph can be represented by the _policy map because |
---|
280 | // the out-degree of every node is 1. |
---|
281 | bool initCurrentComponent(int comp) { |
---|
282 | // Find the nodes of the current component |
---|
283 | _nodes.clear(); |
---|
284 | for (NodeIt n(_gr); n != INVALID; ++n) { |
---|
285 | if (_comp[n] == comp) _nodes.push_back(n); |
---|
286 | } |
---|
287 | if (_nodes.size() <= 1) return false; |
---|
288 | // Find the arcs of the current component |
---|
289 | _arcs.clear(); |
---|
290 | for (ArcIt e(_gr); e != INVALID; ++e) { |
---|
291 | if ( _comp[_gr.source(e)] == comp && |
---|
292 | _comp[_gr.target(e)] == comp ) |
---|
293 | _arcs.push_back(e); |
---|
294 | } |
---|
295 | // Initialize _reached, _dist, _policy maps |
---|
296 | for (int i = 0; i < int(_nodes.size()); ++i) { |
---|
297 | _reached[_nodes[i]] = false; |
---|
298 | _policy[_nodes[i]] = INVALID; |
---|
299 | } |
---|
300 | Node u; Arc e; |
---|
301 | for (int j = 0; j < int(_arcs.size()); ++j) { |
---|
302 | e = _arcs[j]; |
---|
303 | u = _gr.source(e); |
---|
304 | if (!_reached[u] || _length[e] < _dist[u]) { |
---|
305 | _dist[u] = _length[e]; |
---|
306 | _policy[u] = e; |
---|
307 | _reached[u] = true; |
---|
308 | } |
---|
309 | } |
---|
310 | return true; |
---|
311 | } |
---|
312 | |
---|
313 | // Find all cycles in the policy graph. |
---|
314 | // Set _cycle_found to true if a cycle is found and set |
---|
315 | // _cycle_length, _cycle_size, _cycle_node to represent the minimum |
---|
316 | // mean cycle in the policy graph. |
---|
317 | bool findPolicyCycles() { |
---|
318 | typename Digraph::template NodeMap<int> level(_gr, -1); |
---|
319 | bool curr_cycle_found = false; |
---|
320 | Value clength; |
---|
321 | int csize; |
---|
322 | int path_cnt = 0; |
---|
323 | Node u, v; |
---|
324 | // Searching for cycles |
---|
325 | for (int i = 0; i < int(_nodes.size()); ++i) { |
---|
326 | if (level[_nodes[i]] < 0) { |
---|
327 | u = _nodes[i]; |
---|
328 | level[u] = path_cnt; |
---|
329 | while (level[u = _gr.target(_policy[u])] < 0) |
---|
330 | level[u] = path_cnt; |
---|
331 | if (level[u] == path_cnt) { |
---|
332 | // A cycle is found |
---|
333 | curr_cycle_found = true; |
---|
334 | clength = _length[_policy[u]]; |
---|
335 | csize = 1; |
---|
336 | for (v = u; (v = _gr.target(_policy[v])) != u; ) { |
---|
337 | clength += _length[_policy[v]]; |
---|
338 | ++csize; |
---|
339 | } |
---|
340 | if ( !_cycle_found || |
---|
341 | clength * _cycle_size < _cycle_length * csize ) { |
---|
342 | _cycle_found = true; |
---|
343 | _cycle_length = clength; |
---|
344 | _cycle_size = csize; |
---|
345 | _cycle_node = u; |
---|
346 | } |
---|
347 | } |
---|
348 | ++path_cnt; |
---|
349 | } |
---|
350 | } |
---|
351 | return curr_cycle_found; |
---|
352 | } |
---|
353 | |
---|
354 | // Contract the policy graph to be connected by cutting all cycles |
---|
355 | // except for the main cycle (i.e. the minimum mean cycle). |
---|
356 | void contractPolicyGraph(int comp) { |
---|
357 | // Find the component of the main cycle using reverse BFS search |
---|
358 | typename Digraph::template NodeMap<int> found(_gr, false); |
---|
359 | std::deque<Node> queue; |
---|
360 | queue.push_back(_cycle_node); |
---|
361 | found[_cycle_node] = true; |
---|
362 | Node u, v; |
---|
363 | while (!queue.empty()) { |
---|
364 | v = queue.front(); queue.pop_front(); |
---|
365 | for (InArcIt e(_gr, v); e != INVALID; ++e) { |
---|
366 | u = _gr.source(e); |
---|
367 | if (_policy[u] == e && !found[u]) { |
---|
368 | found[u] = true; |
---|
369 | queue.push_back(u); |
---|
370 | } |
---|
371 | } |
---|
372 | } |
---|
373 | // Connect all other nodes to this component using reverse BFS search |
---|
374 | queue.clear(); |
---|
375 | for (int i = 0; i < int(_nodes.size()); ++i) |
---|
376 | if (found[_nodes[i]]) queue.push_back(_nodes[i]); |
---|
377 | int found_cnt = queue.size(); |
---|
378 | while (found_cnt < int(_nodes.size())) { |
---|
379 | v = queue.front(); queue.pop_front(); |
---|
380 | for (InArcIt e(_gr, v); e != INVALID; ++e) { |
---|
381 | u = _gr.source(e); |
---|
382 | if (_comp[u] == comp && !found[u]) { |
---|
383 | found[u] = true; |
---|
384 | ++found_cnt; |
---|
385 | _policy[u] = e; |
---|
386 | queue.push_back(u); |
---|
387 | } |
---|
388 | } |
---|
389 | } |
---|
390 | } |
---|
391 | |
---|
392 | // Compute node distances in the policy graph and update the |
---|
393 | // policy graph if the node distances can be improved. |
---|
394 | bool computeNodeDistances() { |
---|
395 | // Compute node distances using reverse BFS search |
---|
396 | double cycle_mean = double(_cycle_length) / _cycle_size; |
---|
397 | typename Digraph::template NodeMap<int> found(_gr, false); |
---|
398 | std::deque<Node> queue; |
---|
399 | queue.push_back(_cycle_node); |
---|
400 | found[_cycle_node] = true; |
---|
401 | _dist[_cycle_node] = 0; |
---|
402 | Node u, v; |
---|
403 | while (!queue.empty()) { |
---|
404 | v = queue.front(); queue.pop_front(); |
---|
405 | for (InArcIt e(_gr, v); e != INVALID; ++e) { |
---|
406 | u = _gr.source(e); |
---|
407 | if (_policy[u] == e && !found[u]) { |
---|
408 | found[u] = true; |
---|
409 | _dist[u] = _dist[v] + _length[e] - cycle_mean; |
---|
410 | queue.push_back(u); |
---|
411 | } |
---|
412 | } |
---|
413 | } |
---|
414 | // Improving node distances |
---|
415 | bool improved = false; |
---|
416 | for (int j = 0; j < int(_arcs.size()); ++j) { |
---|
417 | Arc e = _arcs[j]; |
---|
418 | u = _gr.source(e); v = _gr.target(e); |
---|
419 | double delta = _dist[v] + _length[e] - cycle_mean; |
---|
420 | if (_tol.less(delta, _dist[u])) { |
---|
421 | improved = true; |
---|
422 | _dist[u] = delta; |
---|
423 | _policy[u] = e; |
---|
424 | } |
---|
425 | } |
---|
426 | return improved; |
---|
427 | } |
---|
428 | |
---|
429 | }; //class MinMeanCycle |
---|
430 | |
---|
431 | ///@} |
---|
432 | |
---|
433 | } //namespace lemon |
---|
434 | |
---|
435 | #endif //LEMON_MIN_MEAN_CYCLE_H |
---|