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.
|
deba@522
|
38 |
/// \param _Digraph Digraph type.
|
deba@522
|
39 |
/// \param _CostMap Type of cost map.
|
deba@522
|
40 |
template <class _Digraph, class _CostMap>
|
deba@522
|
41 |
struct MinCostArborescenceDefaultTraits{
|
deba@522
|
42 |
|
deba@522
|
43 |
/// \brief The digraph type the algorithm runs on.
|
deba@522
|
44 |
typedef _Digraph 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.
|
deba@522
|
50 |
typedef _CostMap 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 |
|
deba@522
|
66 |
/// \brief Instantiates a ArborescenceMap.
|
deba@522
|
67 |
///
|
deba@522
|
68 |
/// This function instantiates a \ref ArborescenceMap.
|
deba@522
|
69 |
/// \param digraph is the graph, to which we would like to
|
deba@522
|
70 |
/// calculate the ArborescenceMap.
|
deba@522
|
71 |
static ArborescenceMap *createArborescenceMap(const Digraph &digraph){
|
deba@522
|
72 |
return new ArborescenceMap(digraph);
|
deba@522
|
73 |
}
|
deba@522
|
74 |
|
deba@522
|
75 |
/// \brief The type of the PredMap
|
deba@522
|
76 |
///
|
deba@522
|
77 |
/// The type of the 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 |
|
deba@522
|
80 |
/// \brief Instantiates a PredMap.
|
deba@522
|
81 |
///
|
deba@522
|
82 |
/// This function instantiates a \ref PredMap.
|
deba@522
|
83 |
/// \param _digraph is the digraph, to which we would like to define the
|
deba@522
|
84 |
/// 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 |
///
|
deba@522
|
93 |
/// \brief %MinCostArborescence algorithm class.
|
deba@522
|
94 |
///
|
deba@522
|
95 |
/// This class provides an efficient implementation of
|
deba@522
|
96 |
/// %MinCostArborescence 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
|
deba@522
|
101 |
/// sources. The time complexity of the algorithm is \f$ O(n^2+e) \f$.
|
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 |
///
|
deba@522
|
106 |
/// \param _Digraph The digraph type the algorithm runs on. The default value
|
deba@522
|
107 |
/// is \ref ListDigraph.
|
deba@522
|
108 |
/// \param _CostMap 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>".
|
deba@522
|
113 |
/// \param _Traits Traits class to set various data types used
|
deba@522
|
114 |
/// by the algorithm. The default traits class is
|
deba@522
|
115 |
/// \ref MinCostArborescenceDefaultTraits
|
deba@522
|
116 |
/// "MinCostArborescenceDefaultTraits<_Digraph, _CostMap>". See \ref
|
deba@522
|
117 |
/// MinCostArborescenceDefaultTraits for the documentation of a
|
deba@522
|
118 |
/// MinCostArborescence traits class.
|
deba@522
|
119 |
///
|
deba@522
|
120 |
/// \author Balazs Dezso
|
deba@522
|
121 |
#ifndef DOXYGEN
|
deba@522
|
122 |
template <typename _Digraph = ListDigraph,
|
deba@522
|
123 |
typename _CostMap = typename _Digraph::template ArcMap<int>,
|
deba@522
|
124 |
typename _Traits =
|
deba@522
|
125 |
MinCostArborescenceDefaultTraits<_Digraph, _CostMap> >
|
deba@522
|
126 |
#else
|
deba@522
|
127 |
template <typename _Digraph, typename _CostMap, typedef _Traits>
|
deba@522
|
128 |
#endif
|
deba@522
|
129 |
class MinCostArborescence {
|
deba@522
|
130 |
public:
|
deba@522
|
131 |
|
deba@522
|
132 |
/// The traits.
|
deba@522
|
133 |
typedef _Traits 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 |
}
|
deba@522
|
298 |
_arc_order->set(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 |
}
|
deba@522
|
340 |
_arc_order->set(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();
|
deba@522
|
369 |
_node_order->set(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 |
|
deba@522
|
395 |
/// \name Named template parameters
|
deba@522
|
396 |
|
deba@522
|
397 |
/// @{
|
deba@522
|
398 |
|
deba@522
|
399 |
template <class T>
|
deba@522
|
400 |
struct DefArborescenceMapTraits : 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
|
deba@522
|
410 |
/// setting ArborescenceMap type
|
deba@522
|
411 |
///
|
deba@522
|
412 |
/// \ref named-templ-param "Named parameter" for setting
|
deba@522
|
413 |
/// ArborescenceMap type
|
deba@522
|
414 |
template <class T>
|
deba@522
|
415 |
struct DefArborescenceMap
|
deba@522
|
416 |
: public MinCostArborescence<Digraph, CostMap,
|
deba@522
|
417 |
DefArborescenceMapTraits<T> > {
|
deba@522
|
418 |
};
|
deba@522
|
419 |
|
deba@522
|
420 |
template <class T>
|
deba@522
|
421 |
struct DefPredMapTraits : public Traits {
|
deba@522
|
422 |
typedef T PredMap;
|
deba@522
|
423 |
static PredMap *createPredMap(const Digraph &)
|
deba@522
|
424 |
{
|
deba@522
|
425 |
LEMON_ASSERT(false, "PredMap is not initialized");
|
deba@522
|
426 |
}
|
deba@522
|
427 |
};
|
deba@522
|
428 |
|
deba@522
|
429 |
/// \brief \ref named-templ-param "Named parameter" for
|
deba@522
|
430 |
/// setting PredMap type
|
deba@522
|
431 |
///
|
deba@522
|
432 |
/// \ref named-templ-param "Named parameter" for setting
|
deba@522
|
433 |
/// PredMap type
|
deba@522
|
434 |
template <class T>
|
deba@522
|
435 |
struct DefPredMap
|
deba@522
|
436 |
: public MinCostArborescence<Digraph, CostMap, DefPredMapTraits<T> > {
|
deba@522
|
437 |
};
|
deba@522
|
438 |
|
deba@522
|
439 |
/// @}
|
deba@522
|
440 |
|
deba@522
|
441 |
/// \brief Constructor.
|
deba@522
|
442 |
///
|
deba@522
|
443 |
/// \param _digraph The digraph the algorithm will run on.
|
deba@522
|
444 |
/// \param _cost The cost map used by the algorithm.
|
deba@522
|
445 |
MinCostArborescence(const Digraph& digraph, const CostMap& cost)
|
deba@522
|
446 |
: _digraph(&digraph), _cost(&cost), _pred(0), local_pred(false),
|
deba@522
|
447 |
_arborescence(0), local_arborescence(false),
|
deba@522
|
448 |
_arc_order(0), _node_order(0), _cost_arcs(0),
|
deba@522
|
449 |
_heap_cross_ref(0), _heap(0) {}
|
deba@522
|
450 |
|
deba@522
|
451 |
/// \brief Destructor.
|
deba@522
|
452 |
~MinCostArborescence() {
|
deba@522
|
453 |
destroyStructures();
|
deba@522
|
454 |
}
|
deba@522
|
455 |
|
deba@522
|
456 |
/// \brief Sets the arborescence map.
|
deba@522
|
457 |
///
|
deba@522
|
458 |
/// Sets the arborescence map.
|
deba@522
|
459 |
/// \return \c (*this)
|
deba@522
|
460 |
MinCostArborescence& arborescenceMap(ArborescenceMap& m) {
|
deba@522
|
461 |
if (local_arborescence) {
|
deba@522
|
462 |
delete _arborescence;
|
deba@522
|
463 |
}
|
deba@522
|
464 |
local_arborescence = false;
|
deba@522
|
465 |
_arborescence = &m;
|
deba@522
|
466 |
return *this;
|
deba@522
|
467 |
}
|
deba@522
|
468 |
|
deba@522
|
469 |
/// \brief Sets the arborescence map.
|
deba@522
|
470 |
///
|
deba@522
|
471 |
/// Sets the arborescence map.
|
deba@522
|
472 |
/// \return \c (*this)
|
deba@522
|
473 |
MinCostArborescence& predMap(PredMap& m) {
|
deba@522
|
474 |
if (local_pred) {
|
deba@522
|
475 |
delete _pred;
|
deba@522
|
476 |
}
|
deba@522
|
477 |
local_pred = false;
|
deba@522
|
478 |
_pred = &m;
|
deba@522
|
479 |
return *this;
|
deba@522
|
480 |
}
|
deba@522
|
481 |
|
deba@522
|
482 |
/// \name Query Functions
|
deba@522
|
483 |
/// The result of the %MinCostArborescence algorithm can be obtained
|
deba@522
|
484 |
/// using these functions.\n
|
deba@522
|
485 |
/// Before the use of these functions,
|
deba@522
|
486 |
/// either run() or start() must be called.
|
deba@522
|
487 |
|
deba@522
|
488 |
/// @{
|
deba@522
|
489 |
|
deba@522
|
490 |
/// \brief Returns a reference to the arborescence map.
|
deba@522
|
491 |
///
|
deba@522
|
492 |
/// Returns a reference to the arborescence map.
|
deba@522
|
493 |
const ArborescenceMap& arborescenceMap() const {
|
deba@522
|
494 |
return *_arborescence;
|
deba@522
|
495 |
}
|
deba@522
|
496 |
|
deba@522
|
497 |
/// \brief Returns true if the arc is in the arborescence.
|
deba@522
|
498 |
///
|
deba@522
|
499 |
/// Returns true if the arc is in the arborescence.
|
deba@522
|
500 |
/// \param arc The arc of the digraph.
|
deba@522
|
501 |
/// \pre \ref run() must be called before using this function.
|
deba@522
|
502 |
bool arborescence(Arc arc) const {
|
deba@522
|
503 |
return (*_pred)[_digraph->target(arc)] == arc;
|
deba@522
|
504 |
}
|
deba@522
|
505 |
|
deba@522
|
506 |
/// \brief Returns a reference to the pred map.
|
deba@522
|
507 |
///
|
deba@522
|
508 |
/// Returns a reference to the pred map.
|
deba@522
|
509 |
const PredMap& predMap() const {
|
deba@522
|
510 |
return *_pred;
|
deba@522
|
511 |
}
|
deba@522
|
512 |
|
deba@522
|
513 |
/// \brief Returns the predecessor arc of the given node.
|
deba@522
|
514 |
///
|
deba@522
|
515 |
/// Returns the predecessor arc of the given node.
|
deba@522
|
516 |
Arc pred(Node node) const {
|
deba@522
|
517 |
return (*_pred)[node];
|
deba@522
|
518 |
}
|
deba@522
|
519 |
|
deba@522
|
520 |
/// \brief Returns the cost of the arborescence.
|
deba@522
|
521 |
///
|
deba@522
|
522 |
/// Returns the cost of the arborescence.
|
deba@522
|
523 |
Value arborescenceValue() const {
|
deba@522
|
524 |
Value sum = 0;
|
deba@522
|
525 |
for (ArcIt it(*_digraph); it != INVALID; ++it) {
|
deba@522
|
526 |
if (arborescence(it)) {
|
deba@522
|
527 |
sum += (*_cost)[it];
|
deba@522
|
528 |
}
|
deba@522
|
529 |
}
|
deba@522
|
530 |
return sum;
|
deba@522
|
531 |
}
|
deba@522
|
532 |
|
deba@522
|
533 |
/// \brief Indicates that a node is reachable from the sources.
|
deba@522
|
534 |
///
|
deba@522
|
535 |
/// Indicates that a node is reachable from the sources.
|
deba@522
|
536 |
bool reached(Node node) const {
|
deba@522
|
537 |
return (*_node_order)[node] != -3;
|
deba@522
|
538 |
}
|
deba@522
|
539 |
|
deba@522
|
540 |
/// \brief Indicates that a node is processed.
|
deba@522
|
541 |
///
|
deba@522
|
542 |
/// Indicates that a node is processed. The arborescence path exists
|
deba@522
|
543 |
/// from the source to the given node.
|
deba@522
|
544 |
bool processed(Node node) const {
|
deba@522
|
545 |
return (*_node_order)[node] == -1;
|
deba@522
|
546 |
}
|
deba@522
|
547 |
|
deba@522
|
548 |
/// \brief Returns the number of the dual variables in basis.
|
deba@522
|
549 |
///
|
deba@522
|
550 |
/// Returns the number of the dual variables in basis.
|
deba@522
|
551 |
int dualNum() const {
|
deba@522
|
552 |
return _dual_variables.size();
|
deba@522
|
553 |
}
|
deba@522
|
554 |
|
deba@522
|
555 |
/// \brief Returns the value of the dual solution.
|
deba@522
|
556 |
///
|
deba@522
|
557 |
/// Returns the value of the dual solution. It should be
|
deba@522
|
558 |
/// equal to the arborescence value.
|
deba@522
|
559 |
Value dualValue() const {
|
deba@522
|
560 |
Value sum = 0;
|
deba@522
|
561 |
for (int i = 0; i < int(_dual_variables.size()); ++i) {
|
deba@522
|
562 |
sum += _dual_variables[i].value;
|
deba@522
|
563 |
}
|
deba@522
|
564 |
return sum;
|
deba@522
|
565 |
}
|
deba@522
|
566 |
|
deba@522
|
567 |
/// \brief Returns the number of the nodes in the dual variable.
|
deba@522
|
568 |
///
|
deba@522
|
569 |
/// Returns the number of the nodes in the dual variable.
|
deba@522
|
570 |
int dualSize(int k) const {
|
deba@522
|
571 |
return _dual_variables[k].end - _dual_variables[k].begin;
|
deba@522
|
572 |
}
|
deba@522
|
573 |
|
deba@522
|
574 |
/// \brief Returns the value of the dual variable.
|
deba@522
|
575 |
///
|
deba@522
|
576 |
/// Returns the the value of the dual variable.
|
deba@522
|
577 |
const Value& dualValue(int k) const {
|
deba@522
|
578 |
return _dual_variables[k].value;
|
deba@522
|
579 |
}
|
deba@522
|
580 |
|
deba@522
|
581 |
/// \brief Lemon iterator for get a dual variable.
|
deba@522
|
582 |
///
|
deba@522
|
583 |
/// Lemon iterator for get a dual variable. This class provides
|
deba@522
|
584 |
/// a common style lemon iterator which gives back a subset of
|
deba@522
|
585 |
/// the nodes.
|
deba@522
|
586 |
class DualIt {
|
deba@522
|
587 |
public:
|
deba@522
|
588 |
|
deba@522
|
589 |
/// \brief Constructor.
|
deba@522
|
590 |
///
|
deba@522
|
591 |
/// Constructor for get the nodeset of the variable.
|
deba@522
|
592 |
DualIt(const MinCostArborescence& algorithm, int variable)
|
deba@522
|
593 |
: _algorithm(&algorithm)
|
deba@522
|
594 |
{
|
deba@522
|
595 |
_index = _algorithm->_dual_variables[variable].begin;
|
deba@522
|
596 |
_last = _algorithm->_dual_variables[variable].end;
|
deba@522
|
597 |
}
|
deba@522
|
598 |
|
deba@522
|
599 |
/// \brief Conversion to node.
|
deba@522
|
600 |
///
|
deba@522
|
601 |
/// Conversion to node.
|
deba@522
|
602 |
operator Node() const {
|
deba@522
|
603 |
return _algorithm->_dual_node_list[_index];
|
deba@522
|
604 |
}
|
deba@522
|
605 |
|
deba@522
|
606 |
/// \brief Increment operator.
|
deba@522
|
607 |
///
|
deba@522
|
608 |
/// Increment operator.
|
deba@522
|
609 |
DualIt& operator++() {
|
deba@522
|
610 |
++_index;
|
deba@522
|
611 |
return *this;
|
deba@522
|
612 |
}
|
deba@522
|
613 |
|
deba@522
|
614 |
/// \brief Validity checking
|
deba@522
|
615 |
///
|
deba@522
|
616 |
/// Checks whether the iterator is invalid.
|
deba@522
|
617 |
bool operator==(Invalid) const {
|
deba@522
|
618 |
return _index == _last;
|
deba@522
|
619 |
}
|
deba@522
|
620 |
|
deba@522
|
621 |
/// \brief Validity checking
|
deba@522
|
622 |
///
|
deba@522
|
623 |
/// Checks whether the iterator is valid.
|
deba@522
|
624 |
bool operator!=(Invalid) const {
|
deba@522
|
625 |
return _index != _last;
|
deba@522
|
626 |
}
|
deba@522
|
627 |
|
deba@522
|
628 |
private:
|
deba@522
|
629 |
const MinCostArborescence* _algorithm;
|
deba@522
|
630 |
int _index, _last;
|
deba@522
|
631 |
};
|
deba@522
|
632 |
|
deba@522
|
633 |
/// @}
|
deba@522
|
634 |
|
deba@522
|
635 |
/// \name Execution control
|
deba@522
|
636 |
/// The simplest way to execute the algorithm is to use
|
deba@522
|
637 |
/// one of the member functions called \c run(...). \n
|
deba@522
|
638 |
/// If you need more control on the execution,
|
deba@522
|
639 |
/// first you must call \ref init(), then you can add several
|
deba@522
|
640 |
/// source nodes with \ref addSource().
|
deba@522
|
641 |
/// Finally \ref start() will perform the arborescence
|
deba@522
|
642 |
/// computation.
|
deba@522
|
643 |
|
deba@522
|
644 |
///@{
|
deba@522
|
645 |
|
deba@522
|
646 |
/// \brief Initializes the internal data structures.
|
deba@522
|
647 |
///
|
deba@522
|
648 |
/// Initializes the internal data structures.
|
deba@522
|
649 |
///
|
deba@522
|
650 |
void init() {
|
deba@522
|
651 |
createStructures();
|
deba@522
|
652 |
_heap->clear();
|
deba@522
|
653 |
for (NodeIt it(*_digraph); it != INVALID; ++it) {
|
deba@522
|
654 |
(*_cost_arcs)[it].arc = INVALID;
|
deba@522
|
655 |
_node_order->set(it, -3);
|
deba@522
|
656 |
_heap_cross_ref->set(it, Heap::PRE_HEAP);
|
deba@522
|
657 |
_pred->set(it, INVALID);
|
deba@522
|
658 |
}
|
deba@522
|
659 |
for (ArcIt it(*_digraph); it != INVALID; ++it) {
|
deba@522
|
660 |
_arborescence->set(it, false);
|
deba@522
|
661 |
_arc_order->set(it, -1);
|
deba@522
|
662 |
}
|
deba@522
|
663 |
_dual_node_list.clear();
|
deba@522
|
664 |
_dual_variables.clear();
|
deba@522
|
665 |
}
|
deba@522
|
666 |
|
deba@522
|
667 |
/// \brief Adds a new source node.
|
deba@522
|
668 |
///
|
deba@522
|
669 |
/// Adds a new source node to the algorithm.
|
deba@522
|
670 |
void addSource(Node source) {
|
deba@522
|
671 |
std::vector<Node> nodes;
|
deba@522
|
672 |
nodes.push_back(source);
|
deba@522
|
673 |
while (!nodes.empty()) {
|
deba@522
|
674 |
Node node = nodes.back();
|
deba@522
|
675 |
nodes.pop_back();
|
deba@522
|
676 |
for (OutArcIt it(*_digraph, node); it != INVALID; ++it) {
|
deba@522
|
677 |
Node target = _digraph->target(it);
|
deba@522
|
678 |
if ((*_node_order)[target] == -3) {
|
deba@522
|
679 |
(*_node_order)[target] = -2;
|
deba@522
|
680 |
nodes.push_back(target);
|
deba@522
|
681 |
queue.push_back(target);
|
deba@522
|
682 |
}
|
deba@522
|
683 |
}
|
deba@522
|
684 |
}
|
deba@522
|
685 |
(*_node_order)[source] = -1;
|
deba@522
|
686 |
}
|
deba@522
|
687 |
|
deba@522
|
688 |
/// \brief Processes the next node in the priority queue.
|
deba@522
|
689 |
///
|
deba@522
|
690 |
/// Processes the next node in the priority queue.
|
deba@522
|
691 |
///
|
deba@522
|
692 |
/// \return The processed node.
|
deba@522
|
693 |
///
|
deba@522
|
694 |
/// \warning The queue must not be empty!
|
deba@522
|
695 |
Node processNextNode() {
|
deba@522
|
696 |
Node node = queue.back();
|
deba@522
|
697 |
queue.pop_back();
|
deba@522
|
698 |
if ((*_node_order)[node] == -2) {
|
deba@522
|
699 |
Arc arc = prepare(node);
|
deba@522
|
700 |
Node source = _digraph->source(arc);
|
deba@522
|
701 |
while ((*_node_order)[source] != -1) {
|
deba@522
|
702 |
if ((*_node_order)[source] >= 0) {
|
deba@522
|
703 |
arc = contract(source);
|
deba@522
|
704 |
} else {
|
deba@522
|
705 |
arc = prepare(source);
|
deba@522
|
706 |
}
|
deba@522
|
707 |
source = _digraph->source(arc);
|
deba@522
|
708 |
}
|
deba@522
|
709 |
finalize(arc);
|
deba@522
|
710 |
level_stack.clear();
|
deba@522
|
711 |
}
|
deba@522
|
712 |
return node;
|
deba@522
|
713 |
}
|
deba@522
|
714 |
|
deba@522
|
715 |
/// \brief Returns the number of the nodes to be processed.
|
deba@522
|
716 |
///
|
deba@522
|
717 |
/// Returns the number of the nodes to be processed.
|
deba@522
|
718 |
int queueSize() const {
|
deba@522
|
719 |
return queue.size();
|
deba@522
|
720 |
}
|
deba@522
|
721 |
|
deba@522
|
722 |
/// \brief Returns \c false if there are nodes to be processed.
|
deba@522
|
723 |
///
|
deba@522
|
724 |
/// Returns \c false if there are nodes to be processed.
|
deba@522
|
725 |
bool emptyQueue() const {
|
deba@522
|
726 |
return queue.empty();
|
deba@522
|
727 |
}
|
deba@522
|
728 |
|
deba@522
|
729 |
/// \brief Executes the algorithm.
|
deba@522
|
730 |
///
|
deba@522
|
731 |
/// Executes the algorithm.
|
deba@522
|
732 |
///
|
deba@522
|
733 |
/// \pre init() must be called and at least one node should be added
|
deba@522
|
734 |
/// with addSource() before using this function.
|
deba@522
|
735 |
///
|
deba@522
|
736 |
///\note mca.start() is just a shortcut of the following code.
|
deba@522
|
737 |
///\code
|
deba@522
|
738 |
///while (!mca.emptyQueue()) {
|
deba@522
|
739 |
/// mca.processNextNode();
|
deba@522
|
740 |
///}
|
deba@522
|
741 |
///\endcode
|
deba@522
|
742 |
void start() {
|
deba@522
|
743 |
while (!emptyQueue()) {
|
deba@522
|
744 |
processNextNode();
|
deba@522
|
745 |
}
|
deba@522
|
746 |
}
|
deba@522
|
747 |
|
deba@522
|
748 |
/// \brief Runs %MinCostArborescence algorithm from node \c s.
|
deba@522
|
749 |
///
|
deba@522
|
750 |
/// This method runs the %MinCostArborescence algorithm from
|
deba@522
|
751 |
/// a root node \c s.
|
deba@522
|
752 |
///
|
deba@522
|
753 |
/// \note mca.run(s) is just a shortcut of the following code.
|
deba@522
|
754 |
/// \code
|
deba@522
|
755 |
/// mca.init();
|
deba@522
|
756 |
/// mca.addSource(s);
|
deba@522
|
757 |
/// mca.start();
|
deba@522
|
758 |
/// \endcode
|
deba@522
|
759 |
void run(Node node) {
|
deba@522
|
760 |
init();
|
deba@522
|
761 |
addSource(node);
|
deba@522
|
762 |
start();
|
deba@522
|
763 |
}
|
deba@522
|
764 |
|
deba@522
|
765 |
///@}
|
deba@522
|
766 |
|
deba@522
|
767 |
};
|
deba@522
|
768 |
|
deba@522
|
769 |
/// \ingroup spantree
|
deba@522
|
770 |
///
|
deba@522
|
771 |
/// \brief Function type interface for MinCostArborescence algorithm.
|
deba@522
|
772 |
///
|
deba@522
|
773 |
/// Function type interface for MinCostArborescence algorithm.
|
deba@522
|
774 |
/// \param digraph The Digraph that the algorithm runs on.
|
deba@522
|
775 |
/// \param cost The CostMap of the arcs.
|
deba@522
|
776 |
/// \param source The source of the arborescence.
|
deba@522
|
777 |
/// \retval arborescence The bool ArcMap which stores the arborescence.
|
deba@522
|
778 |
/// \return The cost of the arborescence.
|
deba@522
|
779 |
///
|
deba@522
|
780 |
/// \sa MinCostArborescence
|
deba@522
|
781 |
template <typename Digraph, typename CostMap, typename ArborescenceMap>
|
deba@522
|
782 |
typename CostMap::Value minCostArborescence(const Digraph& digraph,
|
deba@522
|
783 |
const CostMap& cost,
|
deba@522
|
784 |
typename Digraph::Node source,
|
deba@522
|
785 |
ArborescenceMap& arborescence) {
|
deba@522
|
786 |
typename MinCostArborescence<Digraph, CostMap>
|
deba@522
|
787 |
::template DefArborescenceMap<ArborescenceMap>
|
deba@522
|
788 |
::Create mca(digraph, cost);
|
deba@522
|
789 |
mca.arborescenceMap(arborescence);
|
deba@522
|
790 |
mca.run(source);
|
deba@522
|
791 |
return mca.arborescenceValue();
|
deba@522
|
792 |
}
|
deba@522
|
793 |
|
deba@522
|
794 |
}
|
deba@522
|
795 |
|
deba@522
|
796 |
#endif
|