Redesigned CapacityScaling algorithm with almost the same interface.
The new version does not use the ResidualGraphAdaptor for performance reasons.
Scaling can be enabled and disabled with a parameter of the run() function.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
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_ITERABLE_MAPS_H
20 #define LEMON_ITERABLE_MAPS_H
22 #include <lemon/bits/traits.h>
23 #include <lemon/bits/invalid.h>
25 #include <lemon/bits/default_map.h>
26 #include <lemon/bits/map_extender.h>
36 ///\brief Maps that makes it possible to iterate through the keys having
44 ///\ingroup graph_maps
46 /// \brief Dynamic iterable bool map.
48 /// This class provides a special graph map type which can store
49 /// for each graph item(node, edge, etc.) a bool value. For both
50 /// the true and the false it is possible to iterate on the keys which
51 /// mapped to the given value.
53 /// \param _Graph The graph type.
54 /// \param _Item One of the graph's item type, the key of the map.
55 template <typename _Graph, typename _Item>
56 class IterableBoolMap : protected DefaultMap<_Graph, _Item, int> {
60 typedef typename ItemSetTraits<Graph, _Item>::ItemIt KeyIt;
61 typedef DefaultMap<_Graph, _Item, int> Parent;
63 std::vector<_Item> array;
70 /// Indicates that the map if reference map.
71 typedef True ReferenceMapTag;
77 /// The const reference type.
78 typedef const Value& ConstReference;
82 int position(const Key& key) const {
83 return Parent::operator[](key);
88 /// \brief Refernce to the value of the map.
90 /// This class is near to similar to the bool type. It can
91 /// be converted to bool and it has the same operators.
93 friend class IterableBoolMap;
95 Reference(IterableBoolMap& map, const Key& key)
96 : _key(key), _map(map) {}
99 Reference& operator=(const Reference& value) {
100 _map.set(_key, static_cast<bool>(value));
104 operator bool() const {
105 return static_cast<const IterableBoolMap&>(_map)[_key];
108 Reference& operator=(bool value) {
109 _map.set(_key, value);
112 Reference& operator&=(bool value) {
113 _map.set(_key, _map[_key] & value);
116 Reference& operator|=(bool value) {
117 _map.set(_key, _map[_key] | value);
120 Reference& operator^=(bool value) {
121 _map.set(_key, _map[_key] ^ value);
126 IterableBoolMap& _map;
129 /// \brief Constructor of the Map with a default value.
131 /// Constructor of the Map with a default value.
132 explicit IterableBoolMap(const Graph& _graph, bool def = false)
133 : Parent(_graph), graph(_graph) {
134 for (KeyIt it(graph); it != INVALID; ++it) {
135 Parent::set(it, array.size());
138 sep = (def ? array.size() : 0);
141 /// \brief Const subscript operator of the map.
143 /// Const subscript operator of the map.
144 bool operator[](const Key& key) const {
145 return position(key) < sep;
148 /// \brief Subscript operator of the map.
150 /// Subscript operator of the map.
151 Reference operator[](const Key& key) {
152 return Reference(*this, key);
155 /// \brief Set operation of the map.
157 /// Set operation of the map.
158 void set(const Key& key, bool value) {
159 int pos = position(key);
161 if (pos < sep) return;
162 Key tmp = array[sep];
164 Parent::set(key, sep);
166 Parent::set(tmp, pos);
169 if (pos >= sep) return;
171 Key tmp = array[sep];
173 Parent::set(key, sep);
175 Parent::set(tmp, pos);
179 /// \brief Set all items.
181 /// Set all items in the map.
182 /// \note Constant time operation.
183 void setAll(bool value) {
184 sep = (value ? array.size() : 0);
187 /// \brief Returns the number of the keys mapped to true.
189 /// Returns the number of the keys mapped to true.
190 int trueNum() const {
194 /// \brief Returns the number of the keys mapped to false.
196 /// Returns the number of the keys mapped to false.
197 int falseNum() const {
198 return array.size() - sep;
201 /// \brief Iterator for the keys mapped to true.
203 /// Iterator for the keys mapped to true. It works
204 /// like a graph item iterator in the map, it can be converted
205 /// the key type of the map, incremented with \c ++ operator, and
206 /// if the iterator leave the last valid key it will be equal to
208 class TrueIt : public Key {
212 /// \brief Creates an iterator.
214 /// Creates an iterator. It iterates on the
215 /// keys which mapped to true.
216 /// \param _map The IterableIntMap
217 explicit TrueIt(const IterableBoolMap& _map)
218 : Parent(_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID),
221 /// \brief Invalid constructor \& conversion.
223 /// This constructor initializes the key to be invalid.
224 /// \sa Invalid for more details.
225 TrueIt(Invalid) : Parent(INVALID), map(0) {}
227 /// \brief Increment operator.
229 /// Increment Operator.
230 TrueIt& operator++() {
231 int pos = map->position(*this);
232 Parent::operator=(pos > 0 ? map->array[pos - 1] : INVALID);
238 const IterableBoolMap* map;
241 /// \brief Iterator for the keys mapped to false.
243 /// Iterator for the keys mapped to false. It works
244 /// like a graph item iterator in the map, it can be converted
245 /// the key type of the map, incremented with \c ++ operator, and
246 /// if the iterator leave the last valid key it will be equal to
248 class FalseIt : public Key {
252 /// \brief Creates an iterator.
254 /// Creates an iterator. It iterates on the
255 /// keys which mapped to false.
256 /// \param _map The IterableIntMap
257 explicit FalseIt(const IterableBoolMap& _map)
258 : Parent(_map.sep < int(_map.array.size()) ?
259 _map.array.back() : INVALID), map(&_map) {}
261 /// \brief Invalid constructor \& conversion.
263 /// This constructor initializes the key to be invalid.
264 /// \sa Invalid for more details.
265 FalseIt(Invalid) : Parent(INVALID), map(0) {}
267 /// \brief Increment operator.
269 /// Increment Operator.
270 FalseIt& operator++() {
271 int pos = map->position(*this);
272 Parent::operator=(pos > map->sep ? map->array[pos - 1] : INVALID);
277 const IterableBoolMap* map;
280 /// \brief Iterator for the keys mapped to a given value.
282 /// Iterator for the keys mapped to a given value. It works
283 /// like a graph item iterator in the map, it can be converted
284 /// the key type of the map, incremented with \c ++ operator, and
285 /// if the iterator leave the last valid key it will be equal to
287 class ItemIt : public Key {
291 /// \brief Creates an iterator.
293 /// Creates an iterator. It iterates on the
294 /// keys which mapped to false.
295 /// \param _map The IterableIntMap
296 /// \param value Which elements should be iterated.
297 ItemIt(const IterableBoolMap& _map, bool value)
298 : Parent(value ? (_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID) :
299 (_map.sep < int(_map.array.size()) ?
300 _map.array.back() : INVALID)), map(&_map) {}
302 /// \brief Invalid constructor \& conversion.
304 /// This constructor initializes the key to be invalid.
305 /// \sa Invalid for more details.
306 ItemIt(Invalid) : Parent(INVALID), map(0) {}
308 /// \brief Increment operator.
310 /// Increment Operator.
311 ItemIt& operator++() {
312 int pos = map->position(*this);
313 int sep = pos >= map->sep ? map->sep : 0;
314 Parent::operator=(pos > sep ? map->array[pos - 1] : INVALID);
319 const IterableBoolMap* map;
324 virtual void add(const Key& key) {
326 Parent::set(key, array.size());
327 array.push_back(key);
330 virtual void add(const std::vector<Key>& keys) {
332 for (int i = 0; i < int(keys.size()); ++i) {
333 Parent::set(keys[i], array.size());
334 array.push_back(keys[i]);
338 virtual void erase(const Key& key) {
339 int pos = position(key);
342 Parent::set(array[sep], pos);
343 array[pos] = array[sep];
344 Parent::set(array.back(), sep);
345 array[sep] = array.back();
348 Parent::set(array.back(), pos);
349 array[pos] = array.back();
355 virtual void erase(const std::vector<Key>& keys) {
356 for (int i = 0; i < int(keys.size()); ++i) {
357 int pos = position(keys[i]);
360 Parent::set(array[sep], pos);
361 array[pos] = array[sep];
362 Parent::set(array.back(), sep);
363 array[sep] = array.back();
366 Parent::set(array.back(), pos);
367 array[pos] = array.back();
374 virtual void build() {
376 for (KeyIt it(graph); it != INVALID; ++it) {
377 Parent::set(it, array.size());
383 virtual void clear() {
392 namespace _iterable_maps_bits {
393 template <typename Item>
394 struct IterableIntMapNode {
395 IterableIntMapNode() : value(-1) {}
396 IterableIntMapNode(int _value) : value(_value) {}
402 ///\ingroup graph_maps
404 /// \brief Dynamic iterable integer map.
406 /// This class provides a special graph map type which can store
407 /// for each graph item(node, edge, etc.) an integer value. For each
408 /// non negative value it is possible to iterate on the keys which
409 /// mapped to the given value.
411 /// \param _Graph The graph type.
412 /// \param _Item One of the graph's item type, the key of the map.
413 template <typename _Graph, typename _Item>
415 : protected MapExtender<DefaultMap<_Graph, _Item, _iterable_maps_bits::
416 IterableIntMapNode<_Item> > >{
418 typedef MapExtender<DefaultMap<_Graph, _Item, _iterable_maps_bits::
419 IterableIntMapNode<_Item> > > Parent;
426 typedef _Graph Graph;
428 /// \brief Constructor of the Map.
430 /// Constructor of the Map. It set all values -1.
431 explicit IterableIntMap(const Graph& graph)
434 /// \brief Constructor of the Map with a given value.
436 /// Constructor of the Map with a given value.
437 explicit IterableIntMap(const Graph& graph, int value)
438 : Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(value)) {
440 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
448 void unlace(const Key& key) {
449 typename Parent::Value& node = Parent::operator[](key);
450 if (node.value < 0) return;
451 if (node.prev != INVALID) {
452 Parent::operator[](node.prev).next = node.next;
454 first[node.value] = node.next;
456 if (node.next != INVALID) {
457 Parent::operator[](node.next).prev = node.prev;
459 while (!first.empty() && first.back() == INVALID) {
464 void lace(const Key& key) {
465 typename Parent::Value& node = Parent::operator[](key);
466 if (node.value < 0) return;
467 if (node.value >= int(first.size())) {
468 first.resize(node.value + 1, INVALID);
471 node.next = first[node.value];
472 if (node.next != INVALID) {
473 Parent::operator[](node.next).prev = key;
475 first[node.value] = key;
480 /// Indicates that the map if reference map.
481 typedef True ReferenceMapTag;
483 /// \brief Refernce to the value of the map.
485 /// This class is near to similar to the int type. It can
486 /// be converted to int and it has the same operators.
488 friend class IterableIntMap;
490 Reference(IterableIntMap& map, const Key& key)
491 : _key(key), _map(map) {}
494 Reference& operator=(const Reference& value) {
495 _map.set(_key, static_cast<const int&>(value));
499 operator const int&() const {
500 return static_cast<const IterableIntMap&>(_map)[_key];
503 Reference& operator=(int value) {
504 _map.set(_key, value);
507 Reference& operator++() {
508 _map.set(_key, _map[_key] + 1);
511 int operator++(int) {
512 int value = _map[_key];
513 _map.set(_key, value + 1);
516 Reference& operator--() {
517 _map.set(_key, _map[_key] - 1);
520 int operator--(int) {
521 int value = _map[_key];
522 _map.set(_key, value - 1);
525 Reference& operator+=(int value) {
526 _map.set(_key, _map[_key] + value);
529 Reference& operator-=(int value) {
530 _map.set(_key, _map[_key] - value);
533 Reference& operator*=(int value) {
534 _map.set(_key, _map[_key] * value);
537 Reference& operator/=(int value) {
538 _map.set(_key, _map[_key] / value);
541 Reference& operator%=(int value) {
542 _map.set(_key, _map[_key] % value);
545 Reference& operator&=(int value) {
546 _map.set(_key, _map[_key] & value);
549 Reference& operator|=(int value) {
550 _map.set(_key, _map[_key] | value);
553 Reference& operator^=(int value) {
554 _map.set(_key, _map[_key] ^ value);
557 Reference& operator<<=(int value) {
558 _map.set(_key, _map[_key] << value);
561 Reference& operator>>=(int value) {
562 _map.set(_key, _map[_key] >> value);
568 IterableIntMap& _map;
571 /// The const reference type.
572 typedef const Value& ConstReference;
574 /// \brief Gives back the maximal value plus one.
576 /// Gives back the maximal value plus one.
577 unsigned int size() const {
581 /// \brief Set operation of the map.
583 /// Set operation of the map.
584 void set(const Key& key, const Value& value) {
586 Parent::operator[](key).value = value;
590 /// \brief Const subscript operator of the map.
592 /// Const subscript operator of the map.
593 const Value& operator[](const Key& key) const {
594 return Parent::operator[](key).value;
597 /// \brief Subscript operator of the map.
599 /// Subscript operator of the map.
600 Reference operator[](const Key& key) {
601 return Reference(*this, key);
604 /// \brief Iterator for the keys with the same value.
606 /// Iterator for the keys with the same value. It works
607 /// like a graph item iterator in the map, it can be converted
608 /// the item type of the map, incremented with \c ++ operator, and
609 /// if the iterator leave the last valid item it will be equal to
611 class ItemIt : public _Item {
613 typedef _Item Parent;
615 /// \brief Invalid constructor \& conversion.
617 /// This constructor initializes the item to be invalid.
618 /// \sa Invalid for more details.
619 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
621 /// \brief Creates an iterator with a value.
623 /// Creates an iterator with a value. It iterates on the
624 /// keys which have the given value.
625 /// \param map The IterableIntMap
626 /// \param value The value
627 ItemIt(const IterableIntMap& map, int value) : _map(&map) {
628 if (value < 0 || value >= int(_map->first.size())) {
629 Parent::operator=(INVALID);
631 Parent::operator=(_map->first[value]);
635 /// \brief Increment operator.
637 /// Increment Operator.
638 ItemIt& operator++() {
639 Parent::operator=(_map->IterableIntMap::Parent::
640 operator[](static_cast<Parent&>(*this)).next);
646 const IterableIntMap* _map;
651 virtual void erase(const Key& key) {
656 virtual void erase(const std::vector<Key>& keys) {
657 for (int i = 0; i < int(keys.size()); ++i) {
663 virtual void clear() {
669 std::vector<_Item> first;
672 namespace _iterable_maps_bits {
673 template <typename Item, typename Value>
674 struct IterableValueMapNode {
675 IterableValueMapNode(Value _value = Value()) : value(_value) {}
681 ///\ingroup graph_maps
683 /// \brief Dynamic iterable map for comparable values.
685 /// This class provides a special graph map type which can store
686 /// for each graph item(node, edge, etc.) a value. For each
687 /// value it is possible to iterate on the keys which mapped to the
688 /// given value. The type stores for each value a linked list with
689 /// the items which mapped to the value, and the values are stored
690 /// in balanced binary tree. The values of the map can be accessed
691 /// with stl compatible forward iterator.
693 /// This type is not reference map so it cannot be modified with
694 /// the subscription operator.
696 /// \see InvertableMap
698 /// \param _Graph The graph type.
699 /// \param _Item One of the graph's item type, the key of the map.
700 /// \param _Value Any comparable value type.
701 template <typename _Graph, typename _Item, typename _Value>
702 class IterableValueMap
703 : protected MapExtender<DefaultMap<_Graph, _Item, _iterable_maps_bits::
704 IterableValueMapNode<_Item, _Value> > >{
706 typedef MapExtender<DefaultMap<_Graph, _Item, _iterable_maps_bits::
707 IterableValueMapNode<_Item, _Value> > >
713 typedef _Value Value;
715 typedef _Graph Graph;
719 /// \brief Constructor of the Map with a given value.
721 /// Constructor of the Map with a given value.
722 explicit IterableValueMap(const Graph& graph,
723 const Value& value = Value())
724 : Parent(graph, _iterable_maps_bits::
725 IterableValueMapNode<_Item, _Value>(value)) {
726 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
733 void unlace(const Key& key) {
734 typename Parent::Value& node = Parent::operator[](key);
735 if (node.prev != INVALID) {
736 Parent::operator[](node.prev).next = node.next;
738 if (node.next != INVALID) {
739 first[node.value] = node.next;
741 first.erase(node.value);
744 if (node.next != INVALID) {
745 Parent::operator[](node.next).prev = node.prev;
749 void lace(const Key& key) {
750 typename Parent::Value& node = Parent::operator[](key);
751 typename std::map<Value, Key>::iterator it = first.find(node.value);
752 if (it == first.end()) {
753 node.prev = node.next = INVALID;
754 if (node.next != INVALID) {
755 Parent::operator[](node.next).prev = key;
757 first.insert(make_pair(node.value, key));
760 node.next = it->second;
761 if (node.next != INVALID) {
762 Parent::operator[](node.next).prev = key;
770 /// \brief Forward iterator for values.
772 /// This iterator is an stl compatible forward
773 /// iterator on the values of the map. The values can
774 /// be accessed in the [beginValue, endValue) range.
777 : public std::iterator<std::forward_iterator_tag, Value> {
778 friend class IterableValueMap;
780 ValueIterator(typename std::map<Value, Key>::const_iterator _it)
786 ValueIterator& operator++() { ++it; return *this; }
787 ValueIterator operator++(int) {
788 ValueIterator tmp(*this);
793 const Value& operator*() const { return it->first; }
794 const Value* operator->() const { return &(it->first); }
796 bool operator==(ValueIterator jt) const { return it == jt.it; }
797 bool operator!=(ValueIterator jt) const { return it != jt.it; }
800 typename std::map<Value, Key>::const_iterator it;
803 /// \brief Returns an iterator to the first value.
805 /// Returns an stl compatible iterator to the
806 /// first value of the map. The values of the
807 /// map can be accessed in the [beginValue, endValue)
809 ValueIterator beginValue() const {
810 return ValueIterator(first.begin());
813 /// \brief Returns an iterator after the last value.
815 /// Returns an stl compatible iterator after the
816 /// last value of the map. The values of the
817 /// map can be accessed in the [beginValue, endValue)
819 ValueIterator endValue() const {
820 return ValueIterator(first.end());
823 /// \brief Set operation of the map.
825 /// Set operation of the map.
826 void set(const Key& key, const Value& value) {
828 Parent::operator[](key).value = value;
832 /// \brief Const subscript operator of the map.
834 /// Const subscript operator of the map.
835 const Value& operator[](const Key& key) const {
836 return Parent::operator[](key).value;
839 /// \brief Iterator for the keys with the same value.
841 /// Iterator for the keys with the same value. It works
842 /// like a graph item iterator in the map, it can be converted
843 /// the item type of the map, incremented with \c ++ operator, and
844 /// if the iterator leave the last valid item it will be equal to
846 class ItemIt : public _Item {
848 typedef _Item Parent;
850 /// \brief Invalid constructor \& conversion.
852 /// This constructor initializes the item to be invalid.
853 /// \sa Invalid for more details.
854 ItemIt(Invalid) : Parent(INVALID), _map(0) {}
856 /// \brief Creates an iterator with a value.
858 /// Creates an iterator with a value. It iterates on the
859 /// keys which have the given value.
860 /// \param map The IterableValueMap
861 /// \param value The value
862 ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
863 typename std::map<Value, Key>::const_iterator it =
864 map.first.find(value);
865 if (it == map.first.end()) {
866 Parent::operator=(INVALID);
868 Parent::operator=(it->second);
872 /// \brief Increment operator.
874 /// Increment Operator.
875 ItemIt& operator++() {
876 Parent::operator=(_map->IterableValueMap::Parent::
877 operator[](static_cast<Parent&>(*this)).next);
883 const IterableValueMap* _map;
888 virtual void add(const Key& key) {
893 virtual void add(const std::vector<Key>& keys) {
895 for (int i = 0; i < int(keys.size()); ++i) {
900 virtual void erase(const Key& key) {
905 virtual void erase(const std::vector<Key>& keys) {
906 for (int i = 0; i < int(keys.size()); ++i) {
912 virtual void build() {
914 for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
919 virtual void clear() {
925 std::map<Value, Key> first;