deba@512
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
deba@512
|
2 |
*
|
deba@512
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
deba@512
|
4 |
*
|
deba@512
|
5 |
* Copyright (C) 2003-2008
|
deba@512
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@512
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@512
|
8 |
*
|
deba@512
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@512
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@512
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@512
|
12 |
*
|
deba@512
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@512
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@512
|
15 |
* purpose.
|
deba@512
|
16 |
*
|
deba@512
|
17 |
*/
|
deba@512
|
18 |
|
deba@512
|
19 |
#ifndef LEMON_MIN_COST_ARBORESCENCE_H
|
deba@512
|
20 |
#define LEMON_MIN_COST_ARBORESCENCE_H
|
deba@512
|
21 |
|
deba@512
|
22 |
///\ingroup spantree
|
deba@512
|
23 |
///\file
|
deba@512
|
24 |
///\brief Minimum Cost Arborescence algorithm.
|
deba@512
|
25 |
|
deba@512
|
26 |
#include <vector>
|
deba@512
|
27 |
|
deba@512
|
28 |
#include <lemon/list_graph.h>
|
deba@512
|
29 |
#include <lemon/bin_heap.h>
|
deba@512
|
30 |
#include <lemon/assert.h>
|
deba@512
|
31 |
|
deba@512
|
32 |
namespace lemon {
|
deba@512
|
33 |
|
deba@512
|
34 |
|
deba@512
|
35 |
/// \brief Default traits class for MinCostArborescence class.
|
deba@512
|
36 |
///
|
deba@512
|
37 |
/// Default traits class for MinCostArborescence class.
|
kpeter@550
|
38 |
/// \param GR Digraph type.
|
kpeter@550
|
39 |
/// \param CM Type of cost map.
|
kpeter@550
|
40 |
template <class GR, class CM>
|
deba@512
|
41 |
struct MinCostArborescenceDefaultTraits{
|
deba@512
|
42 |
|
deba@512
|
43 |
/// \brief The digraph type the algorithm runs on.
|
kpeter@550
|
44 |
typedef GR Digraph;
|
deba@512
|
45 |
|
deba@512
|
46 |
/// \brief The type of the map that stores the arc costs.
|
deba@512
|
47 |
///
|
deba@512
|
48 |
/// The type of the map that stores the arc costs.
|
deba@512
|
49 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept.
|
kpeter@550
|
50 |
typedef CM CostMap;
|
deba@512
|
51 |
|
deba@512
|
52 |
/// \brief The value type of the costs.
|
deba@512
|
53 |
///
|
deba@512
|
54 |
/// The value type of the costs.
|
deba@512
|
55 |
typedef typename CostMap::Value Value;
|
deba@512
|
56 |
|
deba@512
|
57 |
/// \brief The type of the map that stores which arcs are in the
|
deba@512
|
58 |
/// arborescence.
|
deba@512
|
59 |
///
|
deba@512
|
60 |
/// The type of the map that stores which arcs are in the
|
deba@512
|
61 |
/// arborescence. It must meet the \ref concepts::WriteMap
|
deba@512
|
62 |
/// "WriteMap" concept. Initially it will be set to false on each
|
deba@512
|
63 |
/// arc. After it will set all arborescence arcs once.
|
deba@512
|
64 |
typedef typename Digraph::template ArcMap<bool> ArborescenceMap;
|
deba@512
|
65 |
|
kpeter@550
|
66 |
/// \brief Instantiates a \c ArborescenceMap.
|
deba@512
|
67 |
///
|
kpeter@550
|
68 |
/// This function instantiates a \c ArborescenceMap.
|
deba@512
|
69 |
/// \param digraph is the graph, to which we would like to
|
kpeter@550
|
70 |
/// calculate the \c ArborescenceMap.
|
deba@512
|
71 |
static ArborescenceMap *createArborescenceMap(const Digraph &digraph){
|
deba@512
|
72 |
return new ArborescenceMap(digraph);
|
deba@512
|
73 |
}
|
deba@512
|
74 |
|
kpeter@550
|
75 |
/// \brief The type of the \c PredMap
|
deba@512
|
76 |
///
|
kpeter@550
|
77 |
/// The type of the \c PredMap. It is a node map with an arc value type.
|
deba@512
|
78 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
|
deba@512
|
79 |
|
kpeter@550
|
80 |
/// \brief Instantiates a \c PredMap.
|
deba@512
|
81 |
///
|
kpeter@550
|
82 |
/// This function instantiates a \c PredMap.
|
kpeter@550
|
83 |
/// \param digraph The digraph to which we would like to define the
|
kpeter@550
|
84 |
/// \c PredMap.
|
deba@512
|
85 |
static PredMap *createPredMap(const Digraph &digraph){
|
deba@512
|
86 |
return new PredMap(digraph);
|
deba@512
|
87 |
}
|
deba@512
|
88 |
|
deba@512
|
89 |
};
|
deba@512
|
90 |
|
deba@512
|
91 |
/// \ingroup spantree
|
deba@512
|
92 |
///
|
kpeter@576
|
93 |
/// \brief Minimum Cost Arborescence algorithm class.
|
deba@512
|
94 |
///
|
deba@512
|
95 |
/// This class provides an efficient implementation of
|
kpeter@576
|
96 |
/// Minimum Cost Arborescence algorithm. The arborescence is a tree
|
deba@512
|
97 |
/// which is directed from a given source node of the digraph. One or
|
deba@512
|
98 |
/// more sources should be given for the algorithm and it will calculate
|
deba@512
|
99 |
/// the minimum cost subgraph which are union of arborescences with the
|
deba@512
|
100 |
/// given sources and spans all the nodes which are reachable from the
|
kpeter@550
|
101 |
/// sources. The time complexity of the algorithm is O(n<sup>2</sup>+e).
|
deba@512
|
102 |
///
|
deba@512
|
103 |
/// The algorithm provides also an optimal dual solution, therefore
|
deba@512
|
104 |
/// the optimality of the solution can be checked.
|
deba@512
|
105 |
///
|
kpeter@550
|
106 |
/// \param GR The digraph type the algorithm runs on. The default value
|
deba@512
|
107 |
/// is \ref ListDigraph.
|
kpeter@550
|
108 |
/// \param CM This read-only ArcMap determines the costs of the
|
deba@512
|
109 |
/// arcs. It is read once for each arc, so the map may involve in
|
deba@512
|
110 |
/// relatively time consuming process to compute the arc cost if
|
deba@512
|
111 |
/// it is necessary. The default map type is \ref
|
deba@512
|
112 |
/// concepts::Digraph::ArcMap "Digraph::ArcMap<int>".
|
kpeter@550
|
113 |
/// \param TR Traits class to set various data types used
|
deba@512
|
114 |
/// by the algorithm. The default traits class is
|
deba@512
|
115 |
/// \ref MinCostArborescenceDefaultTraits
|
kpeter@550
|
116 |
/// "MinCostArborescenceDefaultTraits<GR, CM>". See \ref
|
deba@512
|
117 |
/// MinCostArborescenceDefaultTraits for the documentation of a
|
deba@512
|
118 |
/// MinCostArborescence traits class.
|
deba@512
|
119 |
#ifndef DOXYGEN
|
kpeter@550
|
120 |
template <typename GR = ListDigraph,
|
kpeter@550
|
121 |
typename CM = typename GR::template ArcMap<int>,
|
kpeter@550
|
122 |
typename TR =
|
kpeter@550
|
123 |
MinCostArborescenceDefaultTraits<GR, CM> >
|
deba@512
|
124 |
#else
|
kpeter@550
|
125 |
template <typename GR, typename CM, typedef TR>
|
deba@512
|
126 |
#endif
|
deba@512
|
127 |
class MinCostArborescence {
|
deba@512
|
128 |
public:
|
deba@512
|
129 |
|
deba@512
|
130 |
/// The traits.
|
kpeter@550
|
131 |
typedef TR Traits;
|
deba@512
|
132 |
/// The type of the underlying digraph.
|
deba@512
|
133 |
typedef typename Traits::Digraph Digraph;
|
deba@512
|
134 |
/// The type of the map that stores the arc costs.
|
deba@512
|
135 |
typedef typename Traits::CostMap CostMap;
|
deba@512
|
136 |
///The type of the costs of the arcs.
|
deba@512
|
137 |
typedef typename Traits::Value Value;
|
deba@512
|
138 |
///The type of the predecessor map.
|
deba@512
|
139 |
typedef typename Traits::PredMap PredMap;
|
deba@512
|
140 |
///The type of the map that stores which arcs are in the arborescence.
|
deba@512
|
141 |
typedef typename Traits::ArborescenceMap ArborescenceMap;
|
deba@512
|
142 |
|
deba@512
|
143 |
typedef MinCostArborescence Create;
|
deba@512
|
144 |
|
deba@512
|
145 |
private:
|
deba@512
|
146 |
|
deba@512
|
147 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
|
deba@512
|
148 |
|
deba@512
|
149 |
struct CostArc {
|
deba@512
|
150 |
|
deba@512
|
151 |
Arc arc;
|
deba@512
|
152 |
Value value;
|
deba@512
|
153 |
|
deba@512
|
154 |
CostArc() {}
|
deba@512
|
155 |
CostArc(Arc _arc, Value _value) : arc(_arc), value(_value) {}
|
deba@512
|
156 |
|
deba@512
|
157 |
};
|
deba@512
|
158 |
|
deba@512
|
159 |
const Digraph *_digraph;
|
deba@512
|
160 |
const CostMap *_cost;
|
deba@512
|
161 |
|
deba@512
|
162 |
PredMap *_pred;
|
deba@512
|
163 |
bool local_pred;
|
deba@512
|
164 |
|
deba@512
|
165 |
ArborescenceMap *_arborescence;
|
deba@512
|
166 |
bool local_arborescence;
|
deba@512
|
167 |
|
deba@512
|
168 |
typedef typename Digraph::template ArcMap<int> ArcOrder;
|
deba@512
|
169 |
ArcOrder *_arc_order;
|
deba@512
|
170 |
|
deba@512
|
171 |
typedef typename Digraph::template NodeMap<int> NodeOrder;
|
deba@512
|
172 |
NodeOrder *_node_order;
|
deba@512
|
173 |
|
deba@512
|
174 |
typedef typename Digraph::template NodeMap<CostArc> CostArcMap;
|
deba@512
|
175 |
CostArcMap *_cost_arcs;
|
deba@512
|
176 |
|
deba@512
|
177 |
struct StackLevel {
|
deba@512
|
178 |
|
deba@512
|
179 |
std::vector<CostArc> arcs;
|
deba@512
|
180 |
int node_level;
|
deba@512
|
181 |
|
deba@512
|
182 |
};
|
deba@512
|
183 |
|
deba@512
|
184 |
std::vector<StackLevel> level_stack;
|
deba@512
|
185 |
std::vector<Node> queue;
|
deba@512
|
186 |
|
deba@512
|
187 |
typedef std::vector<typename Digraph::Node> DualNodeList;
|
deba@512
|
188 |
|
deba@512
|
189 |
DualNodeList _dual_node_list;
|
deba@512
|
190 |
|
deba@512
|
191 |
struct DualVariable {
|
deba@512
|
192 |
int begin, end;
|
deba@512
|
193 |
Value value;
|
deba@512
|
194 |
|
deba@512
|
195 |
DualVariable(int _begin, int _end, Value _value)
|
deba@512
|
196 |
: begin(_begin), end(_end), value(_value) {}
|
deba@512
|
197 |
|
deba@512
|
198 |
};
|
deba@512
|
199 |
|
deba@512
|
200 |
typedef std::vector<DualVariable> DualVariables;
|
deba@512
|
201 |
|
deba@512
|
202 |
DualVariables _dual_variables;
|
deba@512
|
203 |
|
deba@512
|
204 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef;
|
deba@512
|
205 |
|
deba@512
|
206 |
HeapCrossRef *_heap_cross_ref;
|
deba@512
|
207 |
|
deba@512
|
208 |
typedef BinHeap<int, HeapCrossRef> Heap;
|
deba@512
|
209 |
|
deba@512
|
210 |
Heap *_heap;
|
deba@512
|
211 |
|
deba@512
|
212 |
protected:
|
deba@512
|
213 |
|
deba@512
|
214 |
MinCostArborescence() {}
|
deba@512
|
215 |
|
deba@512
|
216 |
private:
|
deba@512
|
217 |
|
deba@512
|
218 |
void createStructures() {
|
deba@512
|
219 |
if (!_pred) {
|
deba@512
|
220 |
local_pred = true;
|
deba@512
|
221 |
_pred = Traits::createPredMap(*_digraph);
|
deba@512
|
222 |
}
|
deba@512
|
223 |
if (!_arborescence) {
|
deba@512
|
224 |
local_arborescence = true;
|
deba@512
|
225 |
_arborescence = Traits::createArborescenceMap(*_digraph);
|
deba@512
|
226 |
}
|
deba@512
|
227 |
if (!_arc_order) {
|
deba@512
|
228 |
_arc_order = new ArcOrder(*_digraph);
|
deba@512
|
229 |
}
|
deba@512
|
230 |
if (!_node_order) {
|
deba@512
|
231 |
_node_order = new NodeOrder(*_digraph);
|
deba@512
|
232 |
}
|
deba@512
|
233 |
if (!_cost_arcs) {
|
deba@512
|
234 |
_cost_arcs = new CostArcMap(*_digraph);
|
deba@512
|
235 |
}
|
deba@512
|
236 |
if (!_heap_cross_ref) {
|
deba@512
|
237 |
_heap_cross_ref = new HeapCrossRef(*_digraph, -1);
|
deba@512
|
238 |
}
|
deba@512
|
239 |
if (!_heap) {
|
deba@512
|
240 |
_heap = new Heap(*_heap_cross_ref);
|
deba@512
|
241 |
}
|
deba@512
|
242 |
}
|
deba@512
|
243 |
|
deba@512
|
244 |
void destroyStructures() {
|
deba@512
|
245 |
if (local_arborescence) {
|
deba@512
|
246 |
delete _arborescence;
|
deba@512
|
247 |
}
|
deba@512
|
248 |
if (local_pred) {
|
deba@512
|
249 |
delete _pred;
|
deba@512
|
250 |
}
|
deba@512
|
251 |
if (_arc_order) {
|
deba@512
|
252 |
delete _arc_order;
|
deba@512
|
253 |
}
|
deba@512
|
254 |
if (_node_order) {
|
deba@512
|
255 |
delete _node_order;
|
deba@512
|
256 |
}
|
deba@512
|
257 |
if (_cost_arcs) {
|
deba@512
|
258 |
delete _cost_arcs;
|
deba@512
|
259 |
}
|
deba@512
|
260 |
if (_heap) {
|
deba@512
|
261 |
delete _heap;
|
deba@512
|
262 |
}
|
deba@512
|
263 |
if (_heap_cross_ref) {
|
deba@512
|
264 |
delete _heap_cross_ref;
|
deba@512
|
265 |
}
|
deba@512
|
266 |
}
|
deba@512
|
267 |
|
deba@512
|
268 |
Arc prepare(Node node) {
|
deba@512
|
269 |
std::vector<Node> nodes;
|
deba@512
|
270 |
(*_node_order)[node] = _dual_node_list.size();
|
deba@512
|
271 |
StackLevel level;
|
deba@512
|
272 |
level.node_level = _dual_node_list.size();
|
deba@512
|
273 |
_dual_node_list.push_back(node);
|
deba@512
|
274 |
for (InArcIt it(*_digraph, node); it != INVALID; ++it) {
|
deba@512
|
275 |
Arc arc = it;
|
deba@512
|
276 |
Node source = _digraph->source(arc);
|
deba@512
|
277 |
Value value = (*_cost)[it];
|
deba@512
|
278 |
if (source == node || (*_node_order)[source] == -3) continue;
|
deba@512
|
279 |
if ((*_cost_arcs)[source].arc == INVALID) {
|
deba@512
|
280 |
(*_cost_arcs)[source].arc = arc;
|
deba@512
|
281 |
(*_cost_arcs)[source].value = value;
|
deba@512
|
282 |
nodes.push_back(source);
|
deba@512
|
283 |
} else {
|
deba@512
|
284 |
if ((*_cost_arcs)[source].value > value) {
|
deba@512
|
285 |
(*_cost_arcs)[source].arc = arc;
|
deba@512
|
286 |
(*_cost_arcs)[source].value = value;
|
deba@512
|
287 |
}
|
deba@512
|
288 |
}
|
deba@512
|
289 |
}
|
deba@512
|
290 |
CostArc minimum = (*_cost_arcs)[nodes[0]];
|
deba@512
|
291 |
for (int i = 1; i < int(nodes.size()); ++i) {
|
deba@512
|
292 |
if ((*_cost_arcs)[nodes[i]].value < minimum.value) {
|
deba@512
|
293 |
minimum = (*_cost_arcs)[nodes[i]];
|
deba@512
|
294 |
}
|
deba@512
|
295 |
}
|
kpeter@573
|
296 |
(*_arc_order)[minimum.arc] = _dual_variables.size();
|
deba@512
|
297 |
DualVariable var(_dual_node_list.size() - 1,
|
deba@512
|
298 |
_dual_node_list.size(), minimum.value);
|
deba@512
|
299 |
_dual_variables.push_back(var);
|
deba@512
|
300 |
for (int i = 0; i < int(nodes.size()); ++i) {
|
deba@512
|
301 |
(*_cost_arcs)[nodes[i]].value -= minimum.value;
|
deba@512
|
302 |
level.arcs.push_back((*_cost_arcs)[nodes[i]]);
|
deba@512
|
303 |
(*_cost_arcs)[nodes[i]].arc = INVALID;
|
deba@512
|
304 |
}
|
deba@512
|
305 |
level_stack.push_back(level);
|
deba@512
|
306 |
return minimum.arc;
|
deba@512
|
307 |
}
|
deba@512
|
308 |
|
deba@512
|
309 |
Arc contract(Node node) {
|
deba@512
|
310 |
int node_bottom = bottom(node);
|
deba@512
|
311 |
std::vector<Node> nodes;
|
deba@512
|
312 |
while (!level_stack.empty() &&
|
deba@512
|
313 |
level_stack.back().node_level >= node_bottom) {
|
deba@512
|
314 |
for (int i = 0; i < int(level_stack.back().arcs.size()); ++i) {
|
deba@512
|
315 |
Arc arc = level_stack.back().arcs[i].arc;
|
deba@512
|
316 |
Node source = _digraph->source(arc);
|
deba@512
|
317 |
Value value = level_stack.back().arcs[i].value;
|
deba@512
|
318 |
if ((*_node_order)[source] >= node_bottom) continue;
|
deba@512
|
319 |
if ((*_cost_arcs)[source].arc == INVALID) {
|
deba@512
|
320 |
(*_cost_arcs)[source].arc = arc;
|
deba@512
|
321 |
(*_cost_arcs)[source].value = value;
|
deba@512
|
322 |
nodes.push_back(source);
|
deba@512
|
323 |
} else {
|
deba@512
|
324 |
if ((*_cost_arcs)[source].value > value) {
|
deba@512
|
325 |
(*_cost_arcs)[source].arc = arc;
|
deba@512
|
326 |
(*_cost_arcs)[source].value = value;
|
deba@512
|
327 |
}
|
deba@512
|
328 |
}
|
deba@512
|
329 |
}
|
deba@512
|
330 |
level_stack.pop_back();
|
deba@512
|
331 |
}
|
deba@512
|
332 |
CostArc minimum = (*_cost_arcs)[nodes[0]];
|
deba@512
|
333 |
for (int i = 1; i < int(nodes.size()); ++i) {
|
deba@512
|
334 |
if ((*_cost_arcs)[nodes[i]].value < minimum.value) {
|
deba@512
|
335 |
minimum = (*_cost_arcs)[nodes[i]];
|
deba@512
|
336 |
}
|
deba@512
|
337 |
}
|
kpeter@573
|
338 |
(*_arc_order)[minimum.arc] = _dual_variables.size();
|
deba@512
|
339 |
DualVariable var(node_bottom, _dual_node_list.size(), minimum.value);
|
deba@512
|
340 |
_dual_variables.push_back(var);
|
deba@512
|
341 |
StackLevel level;
|
deba@512
|
342 |
level.node_level = node_bottom;
|
deba@512
|
343 |
for (int i = 0; i < int(nodes.size()); ++i) {
|
deba@512
|
344 |
(*_cost_arcs)[nodes[i]].value -= minimum.value;
|
deba@512
|
345 |
level.arcs.push_back((*_cost_arcs)[nodes[i]]);
|
deba@512
|
346 |
(*_cost_arcs)[nodes[i]].arc = INVALID;
|
deba@512
|
347 |
}
|
deba@512
|
348 |
level_stack.push_back(level);
|
deba@512
|
349 |
return minimum.arc;
|
deba@512
|
350 |
}
|
deba@512
|
351 |
|
deba@512
|
352 |
int bottom(Node node) {
|
deba@512
|
353 |
int k = level_stack.size() - 1;
|
deba@512
|
354 |
while (level_stack[k].node_level > (*_node_order)[node]) {
|
deba@512
|
355 |
--k;
|
deba@512
|
356 |
}
|
deba@512
|
357 |
return level_stack[k].node_level;
|
deba@512
|
358 |
}
|
deba@512
|
359 |
|
deba@512
|
360 |
void finalize(Arc arc) {
|
deba@512
|
361 |
Node node = _digraph->target(arc);
|
deba@512
|
362 |
_heap->push(node, (*_arc_order)[arc]);
|
deba@512
|
363 |
_pred->set(node, arc);
|
deba@512
|
364 |
while (!_heap->empty()) {
|
deba@512
|
365 |
Node source = _heap->top();
|
deba@512
|
366 |
_heap->pop();
|
kpeter@573
|
367 |
(*_node_order)[source] = -1;
|
deba@512
|
368 |
for (OutArcIt it(*_digraph, source); it != INVALID; ++it) {
|
deba@512
|
369 |
if ((*_arc_order)[it] < 0) continue;
|
deba@512
|
370 |
Node target = _digraph->target(it);
|
deba@512
|
371 |
switch(_heap->state(target)) {
|
deba@512
|
372 |
case Heap::PRE_HEAP:
|
deba@512
|
373 |
_heap->push(target, (*_arc_order)[it]);
|
deba@512
|
374 |
_pred->set(target, it);
|
deba@512
|
375 |
break;
|
deba@512
|
376 |
case Heap::IN_HEAP:
|
deba@512
|
377 |
if ((*_arc_order)[it] < (*_heap)[target]) {
|
deba@512
|
378 |
_heap->decrease(target, (*_arc_order)[it]);
|
deba@512
|
379 |
_pred->set(target, it);
|
deba@512
|
380 |
}
|
deba@512
|
381 |
break;
|
deba@512
|
382 |
case Heap::POST_HEAP:
|
deba@512
|
383 |
break;
|
deba@512
|
384 |
}
|
deba@512
|
385 |
}
|
deba@512
|
386 |
_arborescence->set((*_pred)[source], true);
|
deba@512
|
387 |
}
|
deba@512
|
388 |
}
|
deba@512
|
389 |
|
deba@512
|
390 |
|
deba@512
|
391 |
public:
|
deba@512
|
392 |
|
kpeter@576
|
393 |
/// \name Named Template Parameters
|
deba@512
|
394 |
|
deba@512
|
395 |
/// @{
|
deba@512
|
396 |
|
deba@512
|
397 |
template <class T>
|
deba@512
|
398 |
struct DefArborescenceMapTraits : public Traits {
|
deba@512
|
399 |
typedef T ArborescenceMap;
|
deba@512
|
400 |
static ArborescenceMap *createArborescenceMap(const Digraph &)
|
deba@512
|
401 |
{
|
deba@512
|
402 |
LEMON_ASSERT(false, "ArborescenceMap is not initialized");
|
deba@512
|
403 |
return 0; // ignore warnings
|
deba@512
|
404 |
}
|
deba@512
|
405 |
};
|
deba@512
|
406 |
|
deba@512
|
407 |
/// \brief \ref named-templ-param "Named parameter" for
|
deba@512
|
408 |
/// setting ArborescenceMap type
|
deba@512
|
409 |
///
|
deba@512
|
410 |
/// \ref named-templ-param "Named parameter" for setting
|
deba@512
|
411 |
/// ArborescenceMap type
|
deba@512
|
412 |
template <class T>
|
deba@512
|
413 |
struct DefArborescenceMap
|
deba@512
|
414 |
: public MinCostArborescence<Digraph, CostMap,
|
deba@512
|
415 |
DefArborescenceMapTraits<T> > {
|
deba@512
|
416 |
};
|
deba@512
|
417 |
|
deba@512
|
418 |
template <class T>
|
deba@512
|
419 |
struct DefPredMapTraits : public Traits {
|
deba@512
|
420 |
typedef T PredMap;
|
deba@512
|
421 |
static PredMap *createPredMap(const Digraph &)
|
deba@512
|
422 |
{
|
deba@512
|
423 |
LEMON_ASSERT(false, "PredMap is not initialized");
|
deba@512
|
424 |
}
|
deba@512
|
425 |
};
|
deba@512
|
426 |
|
deba@512
|
427 |
/// \brief \ref named-templ-param "Named parameter" for
|
deba@512
|
428 |
/// setting PredMap type
|
deba@512
|
429 |
///
|
deba@512
|
430 |
/// \ref named-templ-param "Named parameter" for setting
|
deba@512
|
431 |
/// PredMap type
|
deba@512
|
432 |
template <class T>
|
deba@512
|
433 |
struct DefPredMap
|
deba@512
|
434 |
: public MinCostArborescence<Digraph, CostMap, DefPredMapTraits<T> > {
|
deba@512
|
435 |
};
|
deba@512
|
436 |
|
deba@512
|
437 |
/// @}
|
deba@512
|
438 |
|
deba@512
|
439 |
/// \brief Constructor.
|
deba@512
|
440 |
///
|
kpeter@550
|
441 |
/// \param digraph The digraph the algorithm will run on.
|
kpeter@550
|
442 |
/// \param cost The cost map used by the algorithm.
|
deba@512
|
443 |
MinCostArborescence(const Digraph& digraph, const CostMap& cost)
|
deba@512
|
444 |
: _digraph(&digraph), _cost(&cost), _pred(0), local_pred(false),
|
deba@512
|
445 |
_arborescence(0), local_arborescence(false),
|
deba@512
|
446 |
_arc_order(0), _node_order(0), _cost_arcs(0),
|
deba@512
|
447 |
_heap_cross_ref(0), _heap(0) {}
|
deba@512
|
448 |
|
deba@512
|
449 |
/// \brief Destructor.
|
deba@512
|
450 |
~MinCostArborescence() {
|
deba@512
|
451 |
destroyStructures();
|
deba@512
|
452 |
}
|
deba@512
|
453 |
|
deba@512
|
454 |
/// \brief Sets the arborescence map.
|
deba@512
|
455 |
///
|
deba@512
|
456 |
/// Sets the arborescence map.
|
kpeter@550
|
457 |
/// \return <tt>(*this)</tt>
|
deba@512
|
458 |
MinCostArborescence& arborescenceMap(ArborescenceMap& m) {
|
deba@512
|
459 |
if (local_arborescence) {
|
deba@512
|
460 |
delete _arborescence;
|
deba@512
|
461 |
}
|
deba@512
|
462 |
local_arborescence = false;
|
deba@512
|
463 |
_arborescence = &m;
|
deba@512
|
464 |
return *this;
|
deba@512
|
465 |
}
|
deba@512
|
466 |
|
deba@512
|
467 |
/// \brief Sets the arborescence map.
|
deba@512
|
468 |
///
|
deba@512
|
469 |
/// Sets the arborescence map.
|
kpeter@550
|
470 |
/// \return <tt>(*this)</tt>
|
deba@512
|
471 |
MinCostArborescence& predMap(PredMap& m) {
|
deba@512
|
472 |
if (local_pred) {
|
deba@512
|
473 |
delete _pred;
|
deba@512
|
474 |
}
|
deba@512
|
475 |
local_pred = false;
|
deba@512
|
476 |
_pred = &m;
|
deba@512
|
477 |
return *this;
|
deba@512
|
478 |
}
|
deba@512
|
479 |
|
deba@512
|
480 |
/// \name Query Functions
|
deba@512
|
481 |
/// The result of the %MinCostArborescence algorithm can be obtained
|
deba@512
|
482 |
/// using these functions.\n
|
deba@512
|
483 |
/// Before the use of these functions,
|
deba@512
|
484 |
/// either run() or start() must be called.
|
deba@512
|
485 |
|
deba@512
|
486 |
/// @{
|
deba@512
|
487 |
|
deba@512
|
488 |
/// \brief Returns a reference to the arborescence map.
|
deba@512
|
489 |
///
|
deba@512
|
490 |
/// Returns a reference to the arborescence map.
|
deba@512
|
491 |
const ArborescenceMap& arborescenceMap() const {
|
deba@512
|
492 |
return *_arborescence;
|
deba@512
|
493 |
}
|
deba@512
|
494 |
|
deba@512
|
495 |
/// \brief Returns true if the arc is in the arborescence.
|
deba@512
|
496 |
///
|
deba@512
|
497 |
/// Returns true if the arc is in the arborescence.
|
deba@512
|
498 |
/// \param arc The arc of the digraph.
|
deba@512
|
499 |
/// \pre \ref run() must be called before using this function.
|
deba@512
|
500 |
bool arborescence(Arc arc) const {
|
deba@512
|
501 |
return (*_pred)[_digraph->target(arc)] == arc;
|
deba@512
|
502 |
}
|
deba@512
|
503 |
|
deba@512
|
504 |
/// \brief Returns a reference to the pred map.
|
deba@512
|
505 |
///
|
deba@512
|
506 |
/// Returns a reference to the pred map.
|
deba@512
|
507 |
const PredMap& predMap() const {
|
deba@512
|
508 |
return *_pred;
|
deba@512
|
509 |
}
|
deba@512
|
510 |
|
deba@512
|
511 |
/// \brief Returns the predecessor arc of the given node.
|
deba@512
|
512 |
///
|
deba@512
|
513 |
/// Returns the predecessor arc of the given node.
|
deba@512
|
514 |
Arc pred(Node node) const {
|
deba@512
|
515 |
return (*_pred)[node];
|
deba@512
|
516 |
}
|
deba@512
|
517 |
|
deba@512
|
518 |
/// \brief Returns the cost of the arborescence.
|
deba@512
|
519 |
///
|
deba@512
|
520 |
/// Returns the cost of the arborescence.
|
deba@512
|
521 |
Value arborescenceValue() const {
|
deba@512
|
522 |
Value sum = 0;
|
deba@512
|
523 |
for (ArcIt it(*_digraph); it != INVALID; ++it) {
|
deba@512
|
524 |
if (arborescence(it)) {
|
deba@512
|
525 |
sum += (*_cost)[it];
|
deba@512
|
526 |
}
|
deba@512
|
527 |
}
|
deba@512
|
528 |
return sum;
|
deba@512
|
529 |
}
|
deba@512
|
530 |
|
deba@512
|
531 |
/// \brief Indicates that a node is reachable from the sources.
|
deba@512
|
532 |
///
|
deba@512
|
533 |
/// Indicates that a node is reachable from the sources.
|
deba@512
|
534 |
bool reached(Node node) const {
|
deba@512
|
535 |
return (*_node_order)[node] != -3;
|
deba@512
|
536 |
}
|
deba@512
|
537 |
|
deba@512
|
538 |
/// \brief Indicates that a node is processed.
|
deba@512
|
539 |
///
|
deba@512
|
540 |
/// Indicates that a node is processed. The arborescence path exists
|
deba@512
|
541 |
/// from the source to the given node.
|
deba@512
|
542 |
bool processed(Node node) const {
|
deba@512
|
543 |
return (*_node_order)[node] == -1;
|
deba@512
|
544 |
}
|
deba@512
|
545 |
|
deba@512
|
546 |
/// \brief Returns the number of the dual variables in basis.
|
deba@512
|
547 |
///
|
deba@512
|
548 |
/// Returns the number of the dual variables in basis.
|
deba@512
|
549 |
int dualNum() const {
|
deba@512
|
550 |
return _dual_variables.size();
|
deba@512
|
551 |
}
|
deba@512
|
552 |
|
deba@512
|
553 |
/// \brief Returns the value of the dual solution.
|
deba@512
|
554 |
///
|
deba@512
|
555 |
/// Returns the value of the dual solution. It should be
|
deba@512
|
556 |
/// equal to the arborescence value.
|
deba@512
|
557 |
Value dualValue() const {
|
deba@512
|
558 |
Value sum = 0;
|
deba@512
|
559 |
for (int i = 0; i < int(_dual_variables.size()); ++i) {
|
deba@512
|
560 |
sum += _dual_variables[i].value;
|
deba@512
|
561 |
}
|
deba@512
|
562 |
return sum;
|
deba@512
|
563 |
}
|
deba@512
|
564 |
|
deba@512
|
565 |
/// \brief Returns the number of the nodes in the dual variable.
|
deba@512
|
566 |
///
|
deba@512
|
567 |
/// Returns the number of the nodes in the dual variable.
|
deba@512
|
568 |
int dualSize(int k) const {
|
deba@512
|
569 |
return _dual_variables[k].end - _dual_variables[k].begin;
|
deba@512
|
570 |
}
|
deba@512
|
571 |
|
deba@512
|
572 |
/// \brief Returns the value of the dual variable.
|
deba@512
|
573 |
///
|
deba@512
|
574 |
/// Returns the the value of the dual variable.
|
deba@512
|
575 |
const Value& dualValue(int k) const {
|
deba@512
|
576 |
return _dual_variables[k].value;
|
deba@512
|
577 |
}
|
deba@512
|
578 |
|
deba@512
|
579 |
/// \brief Lemon iterator for get a dual variable.
|
deba@512
|
580 |
///
|
deba@512
|
581 |
/// Lemon iterator for get a dual variable. This class provides
|
deba@512
|
582 |
/// a common style lemon iterator which gives back a subset of
|
deba@512
|
583 |
/// the nodes.
|
deba@512
|
584 |
class DualIt {
|
deba@512
|
585 |
public:
|
deba@512
|
586 |
|
deba@512
|
587 |
/// \brief Constructor.
|
deba@512
|
588 |
///
|
deba@512
|
589 |
/// Constructor for get the nodeset of the variable.
|
deba@512
|
590 |
DualIt(const MinCostArborescence& algorithm, int variable)
|
deba@512
|
591 |
: _algorithm(&algorithm)
|
deba@512
|
592 |
{
|
deba@512
|
593 |
_index = _algorithm->_dual_variables[variable].begin;
|
deba@512
|
594 |
_last = _algorithm->_dual_variables[variable].end;
|
deba@512
|
595 |
}
|
deba@512
|
596 |
|
deba@512
|
597 |
/// \brief Conversion to node.
|
deba@512
|
598 |
///
|
deba@512
|
599 |
/// Conversion to node.
|
deba@512
|
600 |
operator Node() const {
|
deba@512
|
601 |
return _algorithm->_dual_node_list[_index];
|
deba@512
|
602 |
}
|
deba@512
|
603 |
|
deba@512
|
604 |
/// \brief Increment operator.
|
deba@512
|
605 |
///
|
deba@512
|
606 |
/// Increment operator.
|
deba@512
|
607 |
DualIt& operator++() {
|
deba@512
|
608 |
++_index;
|
deba@512
|
609 |
return *this;
|
deba@512
|
610 |
}
|
deba@512
|
611 |
|
deba@512
|
612 |
/// \brief Validity checking
|
deba@512
|
613 |
///
|
deba@512
|
614 |
/// Checks whether the iterator is invalid.
|
deba@512
|
615 |
bool operator==(Invalid) const {
|
deba@512
|
616 |
return _index == _last;
|
deba@512
|
617 |
}
|
deba@512
|
618 |
|
deba@512
|
619 |
/// \brief Validity checking
|
deba@512
|
620 |
///
|
deba@512
|
621 |
/// Checks whether the iterator is valid.
|
deba@512
|
622 |
bool operator!=(Invalid) const {
|
deba@512
|
623 |
return _index != _last;
|
deba@512
|
624 |
}
|
deba@512
|
625 |
|
deba@512
|
626 |
private:
|
deba@512
|
627 |
const MinCostArborescence* _algorithm;
|
deba@512
|
628 |
int _index, _last;
|
deba@512
|
629 |
};
|
deba@512
|
630 |
|
deba@512
|
631 |
/// @}
|
deba@512
|
632 |
|
kpeter@576
|
633 |
/// \name Execution Control
|
deba@512
|
634 |
/// The simplest way to execute the algorithm is to use
|
deba@512
|
635 |
/// one of the member functions called \c run(...). \n
|
deba@512
|
636 |
/// If you need more control on the execution,
|
deba@512
|
637 |
/// first you must call \ref init(), then you can add several
|
deba@512
|
638 |
/// source nodes with \ref addSource().
|
deba@512
|
639 |
/// Finally \ref start() will perform the arborescence
|
deba@512
|
640 |
/// computation.
|
deba@512
|
641 |
|
deba@512
|
642 |
///@{
|
deba@512
|
643 |
|
deba@512
|
644 |
/// \brief Initializes the internal data structures.
|
deba@512
|
645 |
///
|
deba@512
|
646 |
/// Initializes the internal data structures.
|
deba@512
|
647 |
///
|
deba@512
|
648 |
void init() {
|
deba@512
|
649 |
createStructures();
|
deba@512
|
650 |
_heap->clear();
|
deba@512
|
651 |
for (NodeIt it(*_digraph); it != INVALID; ++it) {
|
deba@512
|
652 |
(*_cost_arcs)[it].arc = INVALID;
|
kpeter@573
|
653 |
(*_node_order)[it] = -3;
|
kpeter@573
|
654 |
(*_heap_cross_ref)[it] = Heap::PRE_HEAP;
|
deba@512
|
655 |
_pred->set(it, INVALID);
|
deba@512
|
656 |
}
|
deba@512
|
657 |
for (ArcIt it(*_digraph); it != INVALID; ++it) {
|
deba@512
|
658 |
_arborescence->set(it, false);
|
kpeter@573
|
659 |
(*_arc_order)[it] = -1;
|
deba@512
|
660 |
}
|
deba@512
|
661 |
_dual_node_list.clear();
|
deba@512
|
662 |
_dual_variables.clear();
|
deba@512
|
663 |
}
|
deba@512
|
664 |
|
deba@512
|
665 |
/// \brief Adds a new source node.
|
deba@512
|
666 |
///
|
deba@512
|
667 |
/// Adds a new source node to the algorithm.
|
deba@512
|
668 |
void addSource(Node source) {
|
deba@512
|
669 |
std::vector<Node> nodes;
|
deba@512
|
670 |
nodes.push_back(source);
|
deba@512
|
671 |
while (!nodes.empty()) {
|
deba@512
|
672 |
Node node = nodes.back();
|
deba@512
|
673 |
nodes.pop_back();
|
deba@512
|
674 |
for (OutArcIt it(*_digraph, node); it != INVALID; ++it) {
|
deba@512
|
675 |
Node target = _digraph->target(it);
|
deba@512
|
676 |
if ((*_node_order)[target] == -3) {
|
deba@512
|
677 |
(*_node_order)[target] = -2;
|
deba@512
|
678 |
nodes.push_back(target);
|
deba@512
|
679 |
queue.push_back(target);
|
deba@512
|
680 |
}
|
deba@512
|
681 |
}
|
deba@512
|
682 |
}
|
deba@512
|
683 |
(*_node_order)[source] = -1;
|
deba@512
|
684 |
}
|
deba@512
|
685 |
|
deba@512
|
686 |
/// \brief Processes the next node in the priority queue.
|
deba@512
|
687 |
///
|
deba@512
|
688 |
/// Processes the next node in the priority queue.
|
deba@512
|
689 |
///
|
deba@512
|
690 |
/// \return The processed node.
|
deba@512
|
691 |
///
|
deba@512
|
692 |
/// \warning The queue must not be empty!
|
deba@512
|
693 |
Node processNextNode() {
|
deba@512
|
694 |
Node node = queue.back();
|
deba@512
|
695 |
queue.pop_back();
|
deba@512
|
696 |
if ((*_node_order)[node] == -2) {
|
deba@512
|
697 |
Arc arc = prepare(node);
|
deba@512
|
698 |
Node source = _digraph->source(arc);
|
deba@512
|
699 |
while ((*_node_order)[source] != -1) {
|
deba@512
|
700 |
if ((*_node_order)[source] >= 0) {
|
deba@512
|
701 |
arc = contract(source);
|
deba@512
|
702 |
} else {
|
deba@512
|
703 |
arc = prepare(source);
|
deba@512
|
704 |
}
|
deba@512
|
705 |
source = _digraph->source(arc);
|
deba@512
|
706 |
}
|
deba@512
|
707 |
finalize(arc);
|
deba@512
|
708 |
level_stack.clear();
|
deba@512
|
709 |
}
|
deba@512
|
710 |
return node;
|
deba@512
|
711 |
}
|
deba@512
|
712 |
|
deba@512
|
713 |
/// \brief Returns the number of the nodes to be processed.
|
deba@512
|
714 |
///
|
deba@512
|
715 |
/// Returns the number of the nodes to be processed.
|
deba@512
|
716 |
int queueSize() const {
|
deba@512
|
717 |
return queue.size();
|
deba@512
|
718 |
}
|
deba@512
|
719 |
|
deba@512
|
720 |
/// \brief Returns \c false if there are nodes to be processed.
|
deba@512
|
721 |
///
|
deba@512
|
722 |
/// Returns \c false if there are nodes to be processed.
|
deba@512
|
723 |
bool emptyQueue() const {
|
deba@512
|
724 |
return queue.empty();
|
deba@512
|
725 |
}
|
deba@512
|
726 |
|
deba@512
|
727 |
/// \brief Executes the algorithm.
|
deba@512
|
728 |
///
|
deba@512
|
729 |
/// Executes the algorithm.
|
deba@512
|
730 |
///
|
deba@512
|
731 |
/// \pre init() must be called and at least one node should be added
|
deba@512
|
732 |
/// with addSource() before using this function.
|
deba@512
|
733 |
///
|
deba@512
|
734 |
///\note mca.start() is just a shortcut of the following code.
|
deba@512
|
735 |
///\code
|
deba@512
|
736 |
///while (!mca.emptyQueue()) {
|
deba@512
|
737 |
/// mca.processNextNode();
|
deba@512
|
738 |
///}
|
deba@512
|
739 |
///\endcode
|
deba@512
|
740 |
void start() {
|
deba@512
|
741 |
while (!emptyQueue()) {
|
deba@512
|
742 |
processNextNode();
|
deba@512
|
743 |
}
|
deba@512
|
744 |
}
|
deba@512
|
745 |
|
deba@512
|
746 |
/// \brief Runs %MinCostArborescence algorithm from node \c s.
|
deba@512
|
747 |
///
|
deba@512
|
748 |
/// This method runs the %MinCostArborescence algorithm from
|
deba@512
|
749 |
/// a root node \c s.
|
deba@512
|
750 |
///
|
deba@512
|
751 |
/// \note mca.run(s) is just a shortcut of the following code.
|
deba@512
|
752 |
/// \code
|
deba@512
|
753 |
/// mca.init();
|
deba@512
|
754 |
/// mca.addSource(s);
|
deba@512
|
755 |
/// mca.start();
|
deba@512
|
756 |
/// \endcode
|
deba@512
|
757 |
void run(Node node) {
|
deba@512
|
758 |
init();
|
deba@512
|
759 |
addSource(node);
|
deba@512
|
760 |
start();
|
deba@512
|
761 |
}
|
deba@512
|
762 |
|
deba@512
|
763 |
///@}
|
deba@512
|
764 |
|
deba@512
|
765 |
};
|
deba@512
|
766 |
|
deba@512
|
767 |
/// \ingroup spantree
|
deba@512
|
768 |
///
|
deba@512
|
769 |
/// \brief Function type interface for MinCostArborescence algorithm.
|
deba@512
|
770 |
///
|
deba@512
|
771 |
/// Function type interface for MinCostArborescence algorithm.
|
deba@512
|
772 |
/// \param digraph The Digraph that the algorithm runs on.
|
deba@512
|
773 |
/// \param cost The CostMap of the arcs.
|
deba@512
|
774 |
/// \param source The source of the arborescence.
|
deba@512
|
775 |
/// \retval arborescence The bool ArcMap which stores the arborescence.
|
deba@512
|
776 |
/// \return The cost of the arborescence.
|
deba@512
|
777 |
///
|
deba@512
|
778 |
/// \sa MinCostArborescence
|
deba@512
|
779 |
template <typename Digraph, typename CostMap, typename ArborescenceMap>
|
deba@512
|
780 |
typename CostMap::Value minCostArborescence(const Digraph& digraph,
|
deba@512
|
781 |
const CostMap& cost,
|
deba@512
|
782 |
typename Digraph::Node source,
|
deba@512
|
783 |
ArborescenceMap& arborescence) {
|
deba@512
|
784 |
typename MinCostArborescence<Digraph, CostMap>
|
deba@512
|
785 |
::template DefArborescenceMap<ArborescenceMap>
|
deba@512
|
786 |
::Create mca(digraph, cost);
|
deba@512
|
787 |
mca.arborescenceMap(arborescence);
|
deba@512
|
788 |
mca.run(source);
|
deba@512
|
789 |
return mca.arborescenceValue();
|
deba@512
|
790 |
}
|
deba@512
|
791 |
|
deba@512
|
792 |
}
|
deba@512
|
793 |
|
deba@512
|
794 |
#endif
|