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