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