1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_MIN_COST_ARBORESCENCE_H
20 #define LEMON_MIN_COST_ARBORESCENCE_H
24 ///\brief Minimum Cost Arborescence algorithm.
28 #include <lemon/list_graph.h>
29 #include <lemon/bin_heap.h>
30 #include <lemon/assert.h>
35 /// \brief Default traits class for MinCostArborescence class.
37 /// Default traits class for MinCostArborescence class.
38 /// \param GR Digraph type.
39 /// \param CM Type of the cost map.
40 template <class GR, class CM>
41 struct MinCostArborescenceDefaultTraits{
43 /// \brief The digraph type the algorithm runs on.
46 /// \brief The type of the map that stores the arc costs.
48 /// The type of the map that stores the arc costs.
49 /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
52 /// \brief The value type of the costs.
54 /// The value type of the costs.
55 typedef typename CostMap::Value Value;
57 /// \brief The type of the map that stores which arcs are in the
60 /// The type of the map that stores which arcs are in the
61 /// arborescence. It must conform to the \ref concepts::WriteMap
62 /// "WriteMap" concept, and its value type must be \c bool
63 /// (or convertible). Initially it will be set to \c false on each
64 /// arc, then it will be set on each arborescence arc once.
65 typedef typename Digraph::template ArcMap<bool> ArborescenceMap;
67 /// \brief Instantiates a \c ArborescenceMap.
69 /// This function instantiates a \c ArborescenceMap.
70 /// \param digraph The digraph to which we would like to calculate
71 /// the \c ArborescenceMap.
72 static ArborescenceMap *createArborescenceMap(const Digraph &digraph){
73 return new ArborescenceMap(digraph);
76 /// \brief The type of the \c PredMap
78 /// The type of the \c PredMap. It must confrom to the
79 /// \ref concepts::WriteMap "WriteMap" concept, and its value type
80 /// must be the \c Arc type of the digraph.
81 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
83 /// \brief Instantiates a \c PredMap.
85 /// This function instantiates a \c PredMap.
86 /// \param digraph The digraph to which we would like to define the
88 static PredMap *createPredMap(const Digraph &digraph){
89 return new PredMap(digraph);
96 /// \brief Minimum Cost Arborescence algorithm class.
98 /// This class provides an efficient implementation of the
99 /// Minimum Cost Arborescence algorithm. The arborescence is a tree
100 /// which is directed from a given source node of the digraph. One or
101 /// more sources should be given to the algorithm and it will calculate
102 /// the minimum cost subgraph that is the union of arborescences with the
103 /// given sources and spans all the nodes which are reachable from the
104 /// sources. The time complexity of the algorithm is O(n<sup>2</sup>+e).
106 /// The algorithm also provides an optimal dual solution, therefore
107 /// the optimality of the solution can be checked.
109 /// \param GR The digraph type the algorithm runs on.
110 /// \param CM A read-only arc map storing the costs of the
111 /// arcs. It is read once for each arc, so the map may involve in
112 /// relatively time consuming process to compute the arc costs if
113 /// it is necessary. The default map type is \ref
114 /// concepts::Digraph::ArcMap "Digraph::ArcMap<int>".
115 /// \tparam TR The traits class that defines various types used by the
116 /// algorithm. By default, it is \ref MinCostArborescenceDefaultTraits
117 /// "MinCostArborescenceDefaultTraits<GR, CM>".
118 /// In most cases, this parameter should not be set directly,
119 /// consider to use the named template parameters instead.
121 template <typename GR,
122 typename CM = typename GR::template ArcMap<int>,
124 MinCostArborescenceDefaultTraits<GR, CM> >
126 template <typename GR, typename CM, typename TR>
128 class MinCostArborescence {
131 /// \brief The \ref MinCostArborescenceDefaultTraits "traits class"
132 /// of the algorithm.
134 /// The type of the underlying digraph.
135 typedef typename Traits::Digraph Digraph;
136 /// The type of the map that stores the arc costs.
137 typedef typename Traits::CostMap CostMap;
138 ///The type of the costs of the arcs.
139 typedef typename Traits::Value Value;
140 ///The type of the predecessor map.
141 typedef typename Traits::PredMap PredMap;
142 ///The type of the map that stores which arcs are in the arborescence.
143 typedef typename Traits::ArborescenceMap ArborescenceMap;
145 typedef MinCostArborescence Create;
149 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
157 CostArc(Arc _arc, Value _value) : arc(_arc), value(_value) {}
161 const Digraph *_digraph;
162 const CostMap *_cost;
167 ArborescenceMap *_arborescence;
168 bool local_arborescence;
170 typedef typename Digraph::template ArcMap<int> ArcOrder;
171 ArcOrder *_arc_order;
173 typedef typename Digraph::template NodeMap<int> NodeOrder;
174 NodeOrder *_node_order;
176 typedef typename Digraph::template NodeMap<CostArc> CostArcMap;
177 CostArcMap *_cost_arcs;
181 std::vector<CostArc> arcs;
186 std::vector<StackLevel> level_stack;
187 std::vector<Node> queue;
189 typedef std::vector<typename Digraph::Node> DualNodeList;
191 DualNodeList _dual_node_list;
193 struct DualVariable {
197 DualVariable(int _begin, int _end, Value _value)
198 : begin(_begin), end(_end), value(_value) {}
202 typedef std::vector<DualVariable> DualVariables;
204 DualVariables _dual_variables;
206 typedef typename Digraph::template NodeMap<int> HeapCrossRef;
208 HeapCrossRef *_heap_cross_ref;
210 typedef BinHeap<int, HeapCrossRef> Heap;
216 MinCostArborescence() {}
220 void createStructures() {
223 _pred = Traits::createPredMap(*_digraph);
225 if (!_arborescence) {
226 local_arborescence = true;
227 _arborescence = Traits::createArborescenceMap(*_digraph);
230 _arc_order = new ArcOrder(*_digraph);
233 _node_order = new NodeOrder(*_digraph);
236 _cost_arcs = new CostArcMap(*_digraph);
238 if (!_heap_cross_ref) {
239 _heap_cross_ref = new HeapCrossRef(*_digraph, -1);
242 _heap = new Heap(*_heap_cross_ref);
246 void destroyStructures() {
247 if (local_arborescence) {
248 delete _arborescence;
265 if (_heap_cross_ref) {
266 delete _heap_cross_ref;
270 Arc prepare(Node node) {
271 std::vector<Node> nodes;
272 (*_node_order)[node] = _dual_node_list.size();
274 level.node_level = _dual_node_list.size();
275 _dual_node_list.push_back(node);
276 for (InArcIt it(*_digraph, node); it != INVALID; ++it) {
278 Node source = _digraph->source(arc);
279 Value value = (*_cost)[it];
280 if (source == node || (*_node_order)[source] == -3) continue;
281 if ((*_cost_arcs)[source].arc == INVALID) {
282 (*_cost_arcs)[source].arc = arc;
283 (*_cost_arcs)[source].value = value;
284 nodes.push_back(source);
286 if ((*_cost_arcs)[source].value > value) {
287 (*_cost_arcs)[source].arc = arc;
288 (*_cost_arcs)[source].value = value;
292 CostArc minimum = (*_cost_arcs)[nodes[0]];
293 for (int i = 1; i < int(nodes.size()); ++i) {
294 if ((*_cost_arcs)[nodes[i]].value < minimum.value) {
295 minimum = (*_cost_arcs)[nodes[i]];
298 (*_arc_order)[minimum.arc] = _dual_variables.size();
299 DualVariable var(_dual_node_list.size() - 1,
300 _dual_node_list.size(), minimum.value);
301 _dual_variables.push_back(var);
302 for (int i = 0; i < int(nodes.size()); ++i) {
303 (*_cost_arcs)[nodes[i]].value -= minimum.value;
304 level.arcs.push_back((*_cost_arcs)[nodes[i]]);
305 (*_cost_arcs)[nodes[i]].arc = INVALID;
307 level_stack.push_back(level);
311 Arc contract(Node node) {
312 int node_bottom = bottom(node);
313 std::vector<Node> nodes;
314 while (!level_stack.empty() &&
315 level_stack.back().node_level >= node_bottom) {
316 for (int i = 0; i < int(level_stack.back().arcs.size()); ++i) {
317 Arc arc = level_stack.back().arcs[i].arc;
318 Node source = _digraph->source(arc);
319 Value value = level_stack.back().arcs[i].value;
320 if ((*_node_order)[source] >= node_bottom) continue;
321 if ((*_cost_arcs)[source].arc == INVALID) {
322 (*_cost_arcs)[source].arc = arc;
323 (*_cost_arcs)[source].value = value;
324 nodes.push_back(source);
326 if ((*_cost_arcs)[source].value > value) {
327 (*_cost_arcs)[source].arc = arc;
328 (*_cost_arcs)[source].value = value;
332 level_stack.pop_back();
334 CostArc minimum = (*_cost_arcs)[nodes[0]];
335 for (int i = 1; i < int(nodes.size()); ++i) {
336 if ((*_cost_arcs)[nodes[i]].value < minimum.value) {
337 minimum = (*_cost_arcs)[nodes[i]];
340 (*_arc_order)[minimum.arc] = _dual_variables.size();
341 DualVariable var(node_bottom, _dual_node_list.size(), minimum.value);
342 _dual_variables.push_back(var);
344 level.node_level = node_bottom;
345 for (int i = 0; i < int(nodes.size()); ++i) {
346 (*_cost_arcs)[nodes[i]].value -= minimum.value;
347 level.arcs.push_back((*_cost_arcs)[nodes[i]]);
348 (*_cost_arcs)[nodes[i]].arc = INVALID;
350 level_stack.push_back(level);
354 int bottom(Node node) {
355 int k = level_stack.size() - 1;
356 while (level_stack[k].node_level > (*_node_order)[node]) {
359 return level_stack[k].node_level;
362 void finalize(Arc arc) {
363 Node node = _digraph->target(arc);
364 _heap->push(node, (*_arc_order)[arc]);
365 _pred->set(node, arc);
366 while (!_heap->empty()) {
367 Node source = _heap->top();
369 (*_node_order)[source] = -1;
370 for (OutArcIt it(*_digraph, source); it != INVALID; ++it) {
371 if ((*_arc_order)[it] < 0) continue;
372 Node target = _digraph->target(it);
373 switch(_heap->state(target)) {
375 _heap->push(target, (*_arc_order)[it]);
376 _pred->set(target, it);
379 if ((*_arc_order)[it] < (*_heap)[target]) {
380 _heap->decrease(target, (*_arc_order)[it]);
381 _pred->set(target, it);
384 case Heap::POST_HEAP:
388 _arborescence->set((*_pred)[source], true);
395 /// \name Named Template Parameters
400 struct SetArborescenceMapTraits : public Traits {
401 typedef T ArborescenceMap;
402 static ArborescenceMap *createArborescenceMap(const Digraph &)
404 LEMON_ASSERT(false, "ArborescenceMap is not initialized");
405 return 0; // ignore warnings
409 /// \brief \ref named-templ-param "Named parameter" for
410 /// setting \c ArborescenceMap type
412 /// \ref named-templ-param "Named parameter" for setting
413 /// \c ArborescenceMap type.
414 /// It must conform to the \ref concepts::WriteMap "WriteMap" concept,
415 /// and its value type must be \c bool (or convertible).
416 /// Initially it will be set to \c false on each arc,
417 /// then it will be set on each arborescence arc once.
419 struct SetArborescenceMap
420 : public MinCostArborescence<Digraph, CostMap,
421 SetArborescenceMapTraits<T> > {
425 struct SetPredMapTraits : public Traits {
427 static PredMap *createPredMap(const Digraph &)
429 LEMON_ASSERT(false, "PredMap is not initialized");
430 return 0; // ignore warnings
434 /// \brief \ref named-templ-param "Named parameter" for
435 /// setting \c PredMap type
437 /// \ref named-templ-param "Named parameter" for setting
439 /// It must meet the \ref concepts::WriteMap "WriteMap" concept,
440 /// and its value type must be the \c Arc type of the digraph.
443 : public MinCostArborescence<Digraph, CostMap, SetPredMapTraits<T> > {
448 /// \brief Constructor.
450 /// \param digraph The digraph the algorithm will run on.
451 /// \param cost The cost map used by the algorithm.
452 MinCostArborescence(const Digraph& digraph, const CostMap& cost)
453 : _digraph(&digraph), _cost(&cost), _pred(0), local_pred(false),
454 _arborescence(0), local_arborescence(false),
455 _arc_order(0), _node_order(0), _cost_arcs(0),
456 _heap_cross_ref(0), _heap(0) {}
458 /// \brief Destructor.
459 ~MinCostArborescence() {
463 /// \brief Sets the arborescence map.
465 /// Sets the arborescence map.
466 /// \return <tt>(*this)</tt>
467 MinCostArborescence& arborescenceMap(ArborescenceMap& m) {
468 if (local_arborescence) {
469 delete _arborescence;
471 local_arborescence = false;
476 /// \brief Sets the predecessor map.
478 /// Sets the predecessor map.
479 /// \return <tt>(*this)</tt>
480 MinCostArborescence& predMap(PredMap& m) {
489 /// \name Execution Control
490 /// The simplest way to execute the algorithm is to use
491 /// one of the member functions called \c run(...). \n
492 /// If you need better control on the execution,
493 /// you have to call \ref init() first, then you can add several
494 /// source nodes with \ref addSource().
495 /// Finally \ref start() will perform the arborescence
500 /// \brief Initializes the internal data structures.
502 /// Initializes the internal data structures.
507 for (NodeIt it(*_digraph); it != INVALID; ++it) {
508 (*_cost_arcs)[it].arc = INVALID;
509 (*_node_order)[it] = -3;
510 (*_heap_cross_ref)[it] = Heap::PRE_HEAP;
511 _pred->set(it, INVALID);
513 for (ArcIt it(*_digraph); it != INVALID; ++it) {
514 _arborescence->set(it, false);
515 (*_arc_order)[it] = -1;
517 _dual_node_list.clear();
518 _dual_variables.clear();
521 /// \brief Adds a new source node.
523 /// Adds a new source node to the algorithm.
524 void addSource(Node source) {
525 std::vector<Node> nodes;
526 nodes.push_back(source);
527 while (!nodes.empty()) {
528 Node node = nodes.back();
530 for (OutArcIt it(*_digraph, node); it != INVALID; ++it) {
531 Node target = _digraph->target(it);
532 if ((*_node_order)[target] == -3) {
533 (*_node_order)[target] = -2;
534 nodes.push_back(target);
535 queue.push_back(target);
539 (*_node_order)[source] = -1;
542 /// \brief Processes the next node in the priority queue.
544 /// Processes the next node in the priority queue.
546 /// \return The processed node.
548 /// \warning The queue must not be empty.
549 Node processNextNode() {
550 Node node = queue.back();
552 if ((*_node_order)[node] == -2) {
553 Arc arc = prepare(node);
554 Node source = _digraph->source(arc);
555 while ((*_node_order)[source] != -1) {
556 if ((*_node_order)[source] >= 0) {
557 arc = contract(source);
559 arc = prepare(source);
561 source = _digraph->source(arc);
569 /// \brief Returns the number of the nodes to be processed.
571 /// Returns the number of the nodes to be processed in the priority
573 int queueSize() const {
577 /// \brief Returns \c false if there are nodes to be processed.
579 /// Returns \c false if there are nodes to be processed.
580 bool emptyQueue() const {
581 return queue.empty();
584 /// \brief Executes the algorithm.
586 /// Executes the algorithm.
588 /// \pre init() must be called and at least one node should be added
589 /// with addSource() before using this function.
591 ///\note mca.start() is just a shortcut of the following code.
593 ///while (!mca.emptyQueue()) {
594 /// mca.processNextNode();
598 while (!emptyQueue()) {
603 /// \brief Runs %MinCostArborescence algorithm from node \c s.
605 /// This method runs the %MinCostArborescence algorithm from
606 /// a root node \c s.
608 /// \note mca.run(s) is just a shortcut of the following code.
611 /// mca.addSource(s);
622 /// \name Query Functions
623 /// The result of the %MinCostArborescence algorithm can be obtained
624 /// using these functions.\n
625 /// Either run() or start() must be called before using them.
629 /// \brief Returns the cost of the arborescence.
631 /// Returns the cost of the arborescence.
632 Value arborescenceCost() const {
634 for (ArcIt it(*_digraph); it != INVALID; ++it) {
635 if (arborescence(it)) {
642 /// \brief Returns \c true if the arc is in the arborescence.
644 /// Returns \c true if the given arc is in the arborescence.
645 /// \param arc An arc of the digraph.
646 /// \pre \ref run() must be called before using this function.
647 bool arborescence(Arc arc) const {
648 return (*_pred)[_digraph->target(arc)] == arc;
651 /// \brief Returns a const reference to the arborescence map.
653 /// Returns a const reference to the arborescence map.
654 /// \pre \ref run() must be called before using this function.
655 const ArborescenceMap& arborescenceMap() const {
656 return *_arborescence;
659 /// \brief Returns the predecessor arc of the given node.
661 /// Returns the predecessor arc of the given node.
662 /// \pre \ref run() must be called before using this function.
663 Arc pred(Node node) const {
664 return (*_pred)[node];
667 /// \brief Returns a const reference to the pred map.
669 /// Returns a const reference to the pred map.
670 /// \pre \ref run() must be called before using this function.
671 const PredMap& predMap() const {
675 /// \brief Indicates that a node is reachable from the sources.
677 /// Indicates that a node is reachable from the sources.
678 bool reached(Node node) const {
679 return (*_node_order)[node] != -3;
682 /// \brief Indicates that a node is processed.
684 /// Indicates that a node is processed. The arborescence path exists
685 /// from the source to the given node.
686 bool processed(Node node) const {
687 return (*_node_order)[node] == -1;
690 /// \brief Returns the number of the dual variables in basis.
692 /// Returns the number of the dual variables in basis.
693 int dualNum() const {
694 return _dual_variables.size();
697 /// \brief Returns the value of the dual solution.
699 /// Returns the value of the dual solution. It should be
700 /// equal to the arborescence value.
701 Value dualValue() const {
703 for (int i = 0; i < int(_dual_variables.size()); ++i) {
704 sum += _dual_variables[i].value;
709 /// \brief Returns the number of the nodes in the dual variable.
711 /// Returns the number of the nodes in the dual variable.
712 int dualSize(int k) const {
713 return _dual_variables[k].end - _dual_variables[k].begin;
716 /// \brief Returns the value of the dual variable.
718 /// Returns the the value of the dual variable.
719 Value dualValue(int k) const {
720 return _dual_variables[k].value;
723 /// \brief LEMON iterator for getting a dual variable.
725 /// This class provides a common style LEMON iterator for getting a
726 /// dual variable of \ref MinCostArborescence algorithm.
727 /// It iterates over a subset of the nodes.
731 /// \brief Constructor.
733 /// Constructor for getting the nodeset of the dual variable
734 /// of \ref MinCostArborescence algorithm.
735 DualIt(const MinCostArborescence& algorithm, int variable)
736 : _algorithm(&algorithm)
738 _index = _algorithm->_dual_variables[variable].begin;
739 _last = _algorithm->_dual_variables[variable].end;
742 /// \brief Conversion to \c Node.
744 /// Conversion to \c Node.
745 operator Node() const {
746 return _algorithm->_dual_node_list[_index];
749 /// \brief Increment operator.
751 /// Increment operator.
752 DualIt& operator++() {
757 /// \brief Validity checking
759 /// Checks whether the iterator is invalid.
760 bool operator==(Invalid) const {
761 return _index == _last;
764 /// \brief Validity checking
766 /// Checks whether the iterator is valid.
767 bool operator!=(Invalid) const {
768 return _index != _last;
772 const MinCostArborescence* _algorithm;
780 /// \ingroup spantree
782 /// \brief Function type interface for MinCostArborescence algorithm.
784 /// Function type interface for MinCostArborescence algorithm.
785 /// \param digraph The digraph the algorithm runs on.
786 /// \param cost An arc map storing the costs.
787 /// \param source The source node of the arborescence.
788 /// \retval arborescence An arc map with \c bool (or convertible) value
789 /// type that stores the arborescence.
790 /// \return The total cost of the arborescence.
792 /// \sa MinCostArborescence
793 template <typename Digraph, typename CostMap, typename ArborescenceMap>
794 typename CostMap::Value minCostArborescence(const Digraph& digraph,
796 typename Digraph::Node source,
797 ArborescenceMap& arborescence) {
798 typename MinCostArborescence<Digraph, CostMap>
799 ::template SetArborescenceMap<ArborescenceMap>
800 ::Create mca(digraph, cost);
801 mca.arborescenceMap(arborescence);
803 return mca.arborescenceCost();