... |
... |
@@ -765,131 +765,150 @@
|
765 |
765 |
IntNodeMap *_node_index;
|
766 |
766 |
IntArcMap *_node_heap_index;
|
767 |
767 |
|
768 |
768 |
struct NodeData {
|
769 |
769 |
|
770 |
770 |
NodeData(IntArcMap& node_heap_index)
|
771 |
771 |
: heap(node_heap_index) {}
|
772 |
772 |
|
773 |
773 |
int blossom;
|
774 |
774 |
Value pot;
|
775 |
775 |
BinHeap<Value, IntArcMap> heap;
|
776 |
776 |
std::map<int, Arc> heap_index;
|
777 |
777 |
|
778 |
778 |
int tree;
|
779 |
779 |
};
|
780 |
780 |
|
781 |
781 |
RangeMap<NodeData>* _node_data;
|
782 |
782 |
|
783 |
783 |
typedef ExtendFindEnum<IntIntMap> TreeSet;
|
784 |
784 |
|
785 |
785 |
IntIntMap *_tree_set_index;
|
786 |
786 |
TreeSet *_tree_set;
|
787 |
787 |
|
788 |
788 |
IntNodeMap *_delta1_index;
|
789 |
789 |
BinHeap<Value, IntNodeMap> *_delta1;
|
790 |
790 |
|
791 |
791 |
IntIntMap *_delta2_index;
|
792 |
792 |
BinHeap<Value, IntIntMap> *_delta2;
|
793 |
793 |
|
794 |
794 |
IntEdgeMap *_delta3_index;
|
795 |
795 |
BinHeap<Value, IntEdgeMap> *_delta3;
|
796 |
796 |
|
797 |
797 |
IntIntMap *_delta4_index;
|
798 |
798 |
BinHeap<Value, IntIntMap> *_delta4;
|
799 |
799 |
|
800 |
800 |
Value _delta_sum;
|
801 |
801 |
int _unmatched;
|
802 |
802 |
|
803 |
803 |
typedef MaxWeightedFractionalMatching<Graph, WeightMap> FractionalMatching;
|
804 |
804 |
FractionalMatching *_fractional;
|
805 |
805 |
|
806 |
806 |
void createStructures() {
|
807 |
807 |
_node_num = countNodes(_graph);
|
808 |
808 |
_blossom_num = _node_num * 3 / 2;
|
809 |
809 |
|
810 |
810 |
if (!_matching) {
|
811 |
811 |
_matching = new MatchingMap(_graph);
|
812 |
812 |
}
|
|
813 |
|
813 |
814 |
if (!_node_potential) {
|
814 |
815 |
_node_potential = new NodePotential(_graph);
|
815 |
816 |
}
|
|
817 |
|
816 |
818 |
if (!_blossom_set) {
|
817 |
819 |
_blossom_index = new IntNodeMap(_graph);
|
818 |
820 |
_blossom_set = new BlossomSet(*_blossom_index);
|
819 |
821 |
_blossom_data = new RangeMap<BlossomData>(_blossom_num);
|
|
822 |
} else if (_blossom_data->size() != _blossom_num) {
|
|
823 |
delete _blossom_data;
|
|
824 |
_blossom_data = new RangeMap<BlossomData>(_blossom_num);
|
820 |
825 |
}
|
821 |
826 |
|
822 |
827 |
if (!_node_index) {
|
823 |
828 |
_node_index = new IntNodeMap(_graph);
|
824 |
829 |
_node_heap_index = new IntArcMap(_graph);
|
825 |
830 |
_node_data = new RangeMap<NodeData>(_node_num,
|
826 |
|
NodeData(*_node_heap_index));
|
|
831 |
NodeData(*_node_heap_index));
|
|
832 |
} else {
|
|
833 |
delete _node_data;
|
|
834 |
_node_data = new RangeMap<NodeData>(_node_num,
|
|
835 |
NodeData(*_node_heap_index));
|
827 |
836 |
}
|
828 |
837 |
|
829 |
838 |
if (!_tree_set) {
|
830 |
839 |
_tree_set_index = new IntIntMap(_blossom_num);
|
831 |
840 |
_tree_set = new TreeSet(*_tree_set_index);
|
|
841 |
} else {
|
|
842 |
_tree_set_index->resize(_blossom_num);
|
832 |
843 |
}
|
|
844 |
|
833 |
845 |
if (!_delta1) {
|
834 |
846 |
_delta1_index = new IntNodeMap(_graph);
|
835 |
847 |
_delta1 = new BinHeap<Value, IntNodeMap>(*_delta1_index);
|
836 |
848 |
}
|
|
849 |
|
837 |
850 |
if (!_delta2) {
|
838 |
851 |
_delta2_index = new IntIntMap(_blossom_num);
|
839 |
852 |
_delta2 = new BinHeap<Value, IntIntMap>(*_delta2_index);
|
|
853 |
} else {
|
|
854 |
_delta2_index->resize(_blossom_num);
|
840 |
855 |
}
|
|
856 |
|
841 |
857 |
if (!_delta3) {
|
842 |
858 |
_delta3_index = new IntEdgeMap(_graph);
|
843 |
859 |
_delta3 = new BinHeap<Value, IntEdgeMap>(*_delta3_index);
|
844 |
860 |
}
|
|
861 |
|
845 |
862 |
if (!_delta4) {
|
846 |
863 |
_delta4_index = new IntIntMap(_blossom_num);
|
847 |
864 |
_delta4 = new BinHeap<Value, IntIntMap>(*_delta4_index);
|
|
865 |
} else {
|
|
866 |
_delta4_index->resize(_blossom_num);
|
848 |
867 |
}
|
849 |
868 |
}
|
850 |
869 |
|
851 |
870 |
void destroyStructures() {
|
852 |
871 |
if (_matching) {
|
853 |
872 |
delete _matching;
|
854 |
873 |
}
|
855 |
874 |
if (_node_potential) {
|
856 |
875 |
delete _node_potential;
|
857 |
876 |
}
|
858 |
877 |
if (_blossom_set) {
|
859 |
878 |
delete _blossom_index;
|
860 |
879 |
delete _blossom_set;
|
861 |
880 |
delete _blossom_data;
|
862 |
881 |
}
|
863 |
882 |
|
864 |
883 |
if (_node_index) {
|
865 |
884 |
delete _node_index;
|
866 |
885 |
delete _node_heap_index;
|
867 |
886 |
delete _node_data;
|
868 |
887 |
}
|
869 |
888 |
|
870 |
889 |
if (_tree_set) {
|
871 |
890 |
delete _tree_set_index;
|
872 |
891 |
delete _tree_set;
|
873 |
892 |
}
|
874 |
893 |
if (_delta1) {
|
875 |
894 |
delete _delta1_index;
|
876 |
895 |
delete _delta1;
|
877 |
896 |
}
|
878 |
897 |
if (_delta2) {
|
879 |
898 |
delete _delta2_index;
|
880 |
899 |
delete _delta2;
|
881 |
900 |
}
|
882 |
901 |
if (_delta3) {
|
883 |
902 |
delete _delta3_index;
|
884 |
903 |
delete _delta3;
|
885 |
904 |
}
|
886 |
905 |
if (_delta4) {
|
887 |
906 |
delete _delta4_index;
|
888 |
907 |
delete _delta4;
|
889 |
908 |
}
|
890 |
909 |
}
|
891 |
910 |
|
892 |
911 |
void matchedToEven(int blossom, int tree) {
|
893 |
912 |
if (_delta2->state(blossom) == _delta2->IN_HEAP) {
|
894 |
913 |
_delta2->erase(blossom);
|
895 |
914 |
}
|
... |
... |
@@ -1543,122 +1562,134 @@
|
1543 |
1562 |
extractBlossom(blossoms[i], base, INVALID);
|
1544 |
1563 |
}
|
1545 |
1564 |
}
|
1546 |
1565 |
}
|
1547 |
1566 |
|
1548 |
1567 |
public:
|
1549 |
1568 |
|
1550 |
1569 |
/// \brief Constructor
|
1551 |
1570 |
///
|
1552 |
1571 |
/// Constructor.
|
1553 |
1572 |
MaxWeightedMatching(const Graph& graph, const WeightMap& weight)
|
1554 |
1573 |
: _graph(graph), _weight(weight), _matching(0),
|
1555 |
1574 |
_node_potential(0), _blossom_potential(), _blossom_node_list(),
|
1556 |
1575 |
_node_num(0), _blossom_num(0),
|
1557 |
1576 |
|
1558 |
1577 |
_blossom_index(0), _blossom_set(0), _blossom_data(0),
|
1559 |
1578 |
_node_index(0), _node_heap_index(0), _node_data(0),
|
1560 |
1579 |
_tree_set_index(0), _tree_set(0),
|
1561 |
1580 |
|
1562 |
1581 |
_delta1_index(0), _delta1(0),
|
1563 |
1582 |
_delta2_index(0), _delta2(0),
|
1564 |
1583 |
_delta3_index(0), _delta3(0),
|
1565 |
1584 |
_delta4_index(0), _delta4(0),
|
1566 |
1585 |
|
1567 |
1586 |
_delta_sum(), _unmatched(0),
|
1568 |
1587 |
|
1569 |
1588 |
_fractional(0)
|
1570 |
1589 |
{}
|
1571 |
1590 |
|
1572 |
1591 |
~MaxWeightedMatching() {
|
1573 |
1592 |
destroyStructures();
|
1574 |
1593 |
if (_fractional) {
|
1575 |
1594 |
delete _fractional;
|
1576 |
1595 |
}
|
1577 |
1596 |
}
|
1578 |
1597 |
|
1579 |
1598 |
/// \name Execution Control
|
1580 |
1599 |
/// The simplest way to execute the algorithm is to use the
|
1581 |
1600 |
/// \ref run() member function.
|
1582 |
1601 |
|
1583 |
1602 |
///@{
|
1584 |
1603 |
|
1585 |
1604 |
/// \brief Initialize the algorithm
|
1586 |
1605 |
///
|
1587 |
1606 |
/// This function initializes the algorithm.
|
1588 |
1607 |
void init() {
|
1589 |
1608 |
createStructures();
|
1590 |
1609 |
|
|
1610 |
_blossom_node_list.clear();
|
|
1611 |
_blossom_potential.clear();
|
|
1612 |
|
1591 |
1613 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
1592 |
1614 |
(*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP;
|
1593 |
1615 |
}
|
1594 |
1616 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
1595 |
1617 |
(*_delta1_index)[n] = _delta1->PRE_HEAP;
|
1596 |
1618 |
}
|
1597 |
1619 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
1598 |
1620 |
(*_delta3_index)[e] = _delta3->PRE_HEAP;
|
1599 |
1621 |
}
|
1600 |
1622 |
for (int i = 0; i < _blossom_num; ++i) {
|
1601 |
1623 |
(*_delta2_index)[i] = _delta2->PRE_HEAP;
|
1602 |
1624 |
(*_delta4_index)[i] = _delta4->PRE_HEAP;
|
1603 |
1625 |
}
|
1604 |
|
|
|
1626 |
|
1605 |
1627 |
_unmatched = _node_num;
|
1606 |
1628 |
|
|
1629 |
_delta1->clear();
|
|
1630 |
_delta2->clear();
|
|
1631 |
_delta3->clear();
|
|
1632 |
_delta4->clear();
|
|
1633 |
_blossom_set->clear();
|
|
1634 |
_tree_set->clear();
|
|
1635 |
|
1607 |
1636 |
int index = 0;
|
1608 |
1637 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
1609 |
1638 |
Value max = 0;
|
1610 |
1639 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
1611 |
1640 |
if (_graph.target(e) == n) continue;
|
1612 |
1641 |
if ((dualScale * _weight[e]) / 2 > max) {
|
1613 |
1642 |
max = (dualScale * _weight[e]) / 2;
|
1614 |
1643 |
}
|
1615 |
1644 |
}
|
1616 |
1645 |
(*_node_index)[n] = index;
|
|
1646 |
(*_node_data)[index].heap_index.clear();
|
|
1647 |
(*_node_data)[index].heap.clear();
|
1617 |
1648 |
(*_node_data)[index].pot = max;
|
1618 |
1649 |
_delta1->push(n, max);
|
1619 |
1650 |
int blossom =
|
1620 |
1651 |
_blossom_set->insert(n, std::numeric_limits<Value>::max());
|
1621 |
1652 |
|
1622 |
1653 |
_tree_set->insert(blossom);
|
1623 |
1654 |
|
1624 |
1655 |
(*_blossom_data)[blossom].status = EVEN;
|
1625 |
1656 |
(*_blossom_data)[blossom].pred = INVALID;
|
1626 |
1657 |
(*_blossom_data)[blossom].next = INVALID;
|
1627 |
1658 |
(*_blossom_data)[blossom].pot = 0;
|
1628 |
1659 |
(*_blossom_data)[blossom].offset = 0;
|
1629 |
1660 |
++index;
|
1630 |
1661 |
}
|
1631 |
1662 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
1632 |
1663 |
int si = (*_node_index)[_graph.u(e)];
|
1633 |
1664 |
int ti = (*_node_index)[_graph.v(e)];
|
1634 |
1665 |
if (_graph.u(e) != _graph.v(e)) {
|
1635 |
1666 |
_delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot -
|
1636 |
1667 |
dualScale * _weight[e]) / 2);
|
1637 |
1668 |
}
|
1638 |
1669 |
}
|
1639 |
1670 |
}
|
1640 |
1671 |
|
1641 |
1672 |
/// \brief Initialize the algorithm with fractional matching
|
1642 |
1673 |
///
|
1643 |
1674 |
/// This function initializes the algorithm with a fractional
|
1644 |
1675 |
/// matching. This initialization is also called jumpstart heuristic.
|
1645 |
1676 |
void fractionalInit() {
|
1646 |
1677 |
createStructures();
|
1647 |
1678 |
|
1648 |
1679 |
if (_fractional == 0) {
|
1649 |
1680 |
_fractional = new FractionalMatching(_graph, _weight, false);
|
1650 |
1681 |
}
|
1651 |
1682 |
_fractional->run();
|
1652 |
1683 |
|
1653 |
1684 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
1654 |
1685 |
(*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP;
|
1655 |
1686 |
}
|
1656 |
1687 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
1657 |
1688 |
(*_delta1_index)[n] = _delta1->PRE_HEAP;
|
1658 |
1689 |
}
|
1659 |
1690 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
1660 |
1691 |
(*_delta3_index)[e] = _delta3->PRE_HEAP;
|
1661 |
1692 |
}
|
1662 |
1693 |
for (int i = 0; i < _blossom_num; ++i) {
|
1663 |
1694 |
(*_delta2_index)[i] = _delta2->PRE_HEAP;
|
1664 |
1695 |
(*_delta4_index)[i] = _delta4->PRE_HEAP;
|
... |
... |
@@ -2192,127 +2223,145 @@
|
2192 |
2223 |
RangeMap<BlossomData>* _blossom_data;
|
2193 |
2224 |
|
2194 |
2225 |
IntNodeMap *_node_index;
|
2195 |
2226 |
IntArcMap *_node_heap_index;
|
2196 |
2227 |
|
2197 |
2228 |
struct NodeData {
|
2198 |
2229 |
|
2199 |
2230 |
NodeData(IntArcMap& node_heap_index)
|
2200 |
2231 |
: heap(node_heap_index) {}
|
2201 |
2232 |
|
2202 |
2233 |
int blossom;
|
2203 |
2234 |
Value pot;
|
2204 |
2235 |
BinHeap<Value, IntArcMap> heap;
|
2205 |
2236 |
std::map<int, Arc> heap_index;
|
2206 |
2237 |
|
2207 |
2238 |
int tree;
|
2208 |
2239 |
};
|
2209 |
2240 |
|
2210 |
2241 |
RangeMap<NodeData>* _node_data;
|
2211 |
2242 |
|
2212 |
2243 |
typedef ExtendFindEnum<IntIntMap> TreeSet;
|
2213 |
2244 |
|
2214 |
2245 |
IntIntMap *_tree_set_index;
|
2215 |
2246 |
TreeSet *_tree_set;
|
2216 |
2247 |
|
2217 |
2248 |
IntIntMap *_delta2_index;
|
2218 |
2249 |
BinHeap<Value, IntIntMap> *_delta2;
|
2219 |
2250 |
|
2220 |
2251 |
IntEdgeMap *_delta3_index;
|
2221 |
2252 |
BinHeap<Value, IntEdgeMap> *_delta3;
|
2222 |
2253 |
|
2223 |
2254 |
IntIntMap *_delta4_index;
|
2224 |
2255 |
BinHeap<Value, IntIntMap> *_delta4;
|
2225 |
2256 |
|
2226 |
2257 |
Value _delta_sum;
|
2227 |
2258 |
int _unmatched;
|
2228 |
2259 |
|
2229 |
2260 |
typedef MaxWeightedPerfectFractionalMatching<Graph, WeightMap>
|
2230 |
2261 |
FractionalMatching;
|
2231 |
2262 |
FractionalMatching *_fractional;
|
2232 |
2263 |
|
2233 |
2264 |
void createStructures() {
|
2234 |
2265 |
_node_num = countNodes(_graph);
|
2235 |
2266 |
_blossom_num = _node_num * 3 / 2;
|
2236 |
2267 |
|
2237 |
2268 |
if (!_matching) {
|
2238 |
2269 |
_matching = new MatchingMap(_graph);
|
2239 |
2270 |
}
|
|
2271 |
|
2240 |
2272 |
if (!_node_potential) {
|
2241 |
2273 |
_node_potential = new NodePotential(_graph);
|
2242 |
2274 |
}
|
|
2275 |
|
2243 |
2276 |
if (!_blossom_set) {
|
2244 |
2277 |
_blossom_index = new IntNodeMap(_graph);
|
2245 |
2278 |
_blossom_set = new BlossomSet(*_blossom_index);
|
2246 |
2279 |
_blossom_data = new RangeMap<BlossomData>(_blossom_num);
|
|
2280 |
} else if (_blossom_data->size() != _blossom_num) {
|
|
2281 |
delete _blossom_data;
|
|
2282 |
_blossom_data = new RangeMap<BlossomData>(_blossom_num);
|
2247 |
2283 |
}
|
2248 |
2284 |
|
2249 |
2285 |
if (!_node_index) {
|
2250 |
2286 |
_node_index = new IntNodeMap(_graph);
|
2251 |
2287 |
_node_heap_index = new IntArcMap(_graph);
|
2252 |
2288 |
_node_data = new RangeMap<NodeData>(_node_num,
|
2253 |
2289 |
NodeData(*_node_heap_index));
|
|
2290 |
} else if (_node_data->size() != _node_num) {
|
|
2291 |
delete _node_data;
|
|
2292 |
_node_data = new RangeMap<NodeData>(_node_num,
|
|
2293 |
NodeData(*_node_heap_index));
|
2254 |
2294 |
}
|
2255 |
2295 |
|
2256 |
2296 |
if (!_tree_set) {
|
2257 |
2297 |
_tree_set_index = new IntIntMap(_blossom_num);
|
2258 |
2298 |
_tree_set = new TreeSet(*_tree_set_index);
|
|
2299 |
} else {
|
|
2300 |
_tree_set_index->resize(_blossom_num);
|
2259 |
2301 |
}
|
|
2302 |
|
2260 |
2303 |
if (!_delta2) {
|
2261 |
2304 |
_delta2_index = new IntIntMap(_blossom_num);
|
2262 |
2305 |
_delta2 = new BinHeap<Value, IntIntMap>(*_delta2_index);
|
|
2306 |
} else {
|
|
2307 |
_delta2_index->resize(_blossom_num);
|
2263 |
2308 |
}
|
|
2309 |
|
2264 |
2310 |
if (!_delta3) {
|
2265 |
2311 |
_delta3_index = new IntEdgeMap(_graph);
|
2266 |
2312 |
_delta3 = new BinHeap<Value, IntEdgeMap>(*_delta3_index);
|
2267 |
2313 |
}
|
|
2314 |
|
2268 |
2315 |
if (!_delta4) {
|
2269 |
2316 |
_delta4_index = new IntIntMap(_blossom_num);
|
2270 |
2317 |
_delta4 = new BinHeap<Value, IntIntMap>(*_delta4_index);
|
|
2318 |
} else {
|
|
2319 |
_delta4_index->resize(_blossom_num);
|
2271 |
2320 |
}
|
2272 |
2321 |
}
|
2273 |
2322 |
|
2274 |
2323 |
void destroyStructures() {
|
2275 |
2324 |
if (_matching) {
|
2276 |
2325 |
delete _matching;
|
2277 |
2326 |
}
|
2278 |
2327 |
if (_node_potential) {
|
2279 |
2328 |
delete _node_potential;
|
2280 |
2329 |
}
|
2281 |
2330 |
if (_blossom_set) {
|
2282 |
2331 |
delete _blossom_index;
|
2283 |
2332 |
delete _blossom_set;
|
2284 |
2333 |
delete _blossom_data;
|
2285 |
2334 |
}
|
2286 |
2335 |
|
2287 |
2336 |
if (_node_index) {
|
2288 |
2337 |
delete _node_index;
|
2289 |
2338 |
delete _node_heap_index;
|
2290 |
2339 |
delete _node_data;
|
2291 |
2340 |
}
|
2292 |
2341 |
|
2293 |
2342 |
if (_tree_set) {
|
2294 |
2343 |
delete _tree_set_index;
|
2295 |
2344 |
delete _tree_set;
|
2296 |
2345 |
}
|
2297 |
2346 |
if (_delta2) {
|
2298 |
2347 |
delete _delta2_index;
|
2299 |
2348 |
delete _delta2;
|
2300 |
2349 |
}
|
2301 |
2350 |
if (_delta3) {
|
2302 |
2351 |
delete _delta3_index;
|
2303 |
2352 |
delete _delta3;
|
2304 |
2353 |
}
|
2305 |
2354 |
if (_delta4) {
|
2306 |
2355 |
delete _delta4_index;
|
2307 |
2356 |
delete _delta4;
|
2308 |
2357 |
}
|
2309 |
2358 |
}
|
2310 |
2359 |
|
2311 |
2360 |
void matchedToEven(int blossom, int tree) {
|
2312 |
2361 |
if (_delta2->state(blossom) == _delta2->IN_HEAP) {
|
2313 |
2362 |
_delta2->erase(blossom);
|
2314 |
2363 |
}
|
2315 |
2364 |
|
2316 |
2365 |
if (!_blossom_set->trivial(blossom)) {
|
2317 |
2366 |
(*_blossom_data)[blossom].pot -=
|
2318 |
2367 |
2 * (_delta_sum - (*_blossom_data)[blossom].offset);
|
... |
... |
@@ -2923,119 +2972,130 @@
|
2923 |
2972 |
Arc matching = (*_blossom_data)[blossoms[i]].next;
|
2924 |
2973 |
Node base = _graph.source(matching);
|
2925 |
2974 |
extractBlossom(blossoms[i], base, matching);
|
2926 |
2975 |
}
|
2927 |
2976 |
}
|
2928 |
2977 |
|
2929 |
2978 |
public:
|
2930 |
2979 |
|
2931 |
2980 |
/// \brief Constructor
|
2932 |
2981 |
///
|
2933 |
2982 |
/// Constructor.
|
2934 |
2983 |
MaxWeightedPerfectMatching(const Graph& graph, const WeightMap& weight)
|
2935 |
2984 |
: _graph(graph), _weight(weight), _matching(0),
|
2936 |
2985 |
_node_potential(0), _blossom_potential(), _blossom_node_list(),
|
2937 |
2986 |
_node_num(0), _blossom_num(0),
|
2938 |
2987 |
|
2939 |
2988 |
_blossom_index(0), _blossom_set(0), _blossom_data(0),
|
2940 |
2989 |
_node_index(0), _node_heap_index(0), _node_data(0),
|
2941 |
2990 |
_tree_set_index(0), _tree_set(0),
|
2942 |
2991 |
|
2943 |
2992 |
_delta2_index(0), _delta2(0),
|
2944 |
2993 |
_delta3_index(0), _delta3(0),
|
2945 |
2994 |
_delta4_index(0), _delta4(0),
|
2946 |
2995 |
|
2947 |
2996 |
_delta_sum(), _unmatched(0),
|
2948 |
2997 |
|
2949 |
2998 |
_fractional(0)
|
2950 |
2999 |
{}
|
2951 |
3000 |
|
2952 |
3001 |
~MaxWeightedPerfectMatching() {
|
2953 |
3002 |
destroyStructures();
|
2954 |
3003 |
if (_fractional) {
|
2955 |
3004 |
delete _fractional;
|
2956 |
3005 |
}
|
2957 |
3006 |
}
|
2958 |
3007 |
|
2959 |
3008 |
/// \name Execution Control
|
2960 |
3009 |
/// The simplest way to execute the algorithm is to use the
|
2961 |
3010 |
/// \ref run() member function.
|
2962 |
3011 |
|
2963 |
3012 |
///@{
|
2964 |
3013 |
|
2965 |
3014 |
/// \brief Initialize the algorithm
|
2966 |
3015 |
///
|
2967 |
3016 |
/// This function initializes the algorithm.
|
2968 |
3017 |
void init() {
|
2969 |
3018 |
createStructures();
|
2970 |
3019 |
|
|
3020 |
_blossom_node_list.clear();
|
|
3021 |
_blossom_potential.clear();
|
|
3022 |
|
2971 |
3023 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
2972 |
3024 |
(*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP;
|
2973 |
3025 |
}
|
2974 |
3026 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
2975 |
3027 |
(*_delta3_index)[e] = _delta3->PRE_HEAP;
|
2976 |
3028 |
}
|
2977 |
3029 |
for (int i = 0; i < _blossom_num; ++i) {
|
2978 |
3030 |
(*_delta2_index)[i] = _delta2->PRE_HEAP;
|
2979 |
3031 |
(*_delta4_index)[i] = _delta4->PRE_HEAP;
|
2980 |
3032 |
}
|
2981 |
3033 |
|
2982 |
3034 |
_unmatched = _node_num;
|
2983 |
3035 |
|
|
3036 |
_delta2->clear();
|
|
3037 |
_delta3->clear();
|
|
3038 |
_delta4->clear();
|
|
3039 |
_blossom_set->clear();
|
|
3040 |
_tree_set->clear();
|
|
3041 |
|
2984 |
3042 |
int index = 0;
|
2985 |
3043 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
2986 |
3044 |
Value max = - std::numeric_limits<Value>::max();
|
2987 |
3045 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
2988 |
3046 |
if (_graph.target(e) == n) continue;
|
2989 |
3047 |
if ((dualScale * _weight[e]) / 2 > max) {
|
2990 |
3048 |
max = (dualScale * _weight[e]) / 2;
|
2991 |
3049 |
}
|
2992 |
3050 |
}
|
2993 |
3051 |
(*_node_index)[n] = index;
|
|
3052 |
(*_node_data)[index].heap_index.clear();
|
|
3053 |
(*_node_data)[index].heap.clear();
|
2994 |
3054 |
(*_node_data)[index].pot = max;
|
2995 |
3055 |
int blossom =
|
2996 |
3056 |
_blossom_set->insert(n, std::numeric_limits<Value>::max());
|
2997 |
3057 |
|
2998 |
3058 |
_tree_set->insert(blossom);
|
2999 |
3059 |
|
3000 |
3060 |
(*_blossom_data)[blossom].status = EVEN;
|
3001 |
3061 |
(*_blossom_data)[blossom].pred = INVALID;
|
3002 |
3062 |
(*_blossom_data)[blossom].next = INVALID;
|
3003 |
3063 |
(*_blossom_data)[blossom].pot = 0;
|
3004 |
3064 |
(*_blossom_data)[blossom].offset = 0;
|
3005 |
3065 |
++index;
|
3006 |
3066 |
}
|
3007 |
3067 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
3008 |
3068 |
int si = (*_node_index)[_graph.u(e)];
|
3009 |
3069 |
int ti = (*_node_index)[_graph.v(e)];
|
3010 |
3070 |
if (_graph.u(e) != _graph.v(e)) {
|
3011 |
3071 |
_delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot -
|
3012 |
3072 |
dualScale * _weight[e]) / 2);
|
3013 |
3073 |
}
|
3014 |
3074 |
}
|
3015 |
3075 |
}
|
3016 |
3076 |
|
3017 |
3077 |
/// \brief Initialize the algorithm with fractional matching
|
3018 |
3078 |
///
|
3019 |
3079 |
/// This function initializes the algorithm with a fractional
|
3020 |
3080 |
/// matching. This initialization is also called jumpstart heuristic.
|
3021 |
3081 |
void fractionalInit() {
|
3022 |
3082 |
createStructures();
|
3023 |
3083 |
|
3024 |
3084 |
if (_fractional == 0) {
|
3025 |
3085 |
_fractional = new FractionalMatching(_graph, _weight, false);
|
3026 |
3086 |
}
|
3027 |
3087 |
if (!_fractional->run()) {
|
3028 |
3088 |
_unmatched = -1;
|
3029 |
3089 |
return;
|
3030 |
3090 |
}
|
3031 |
3091 |
|
3032 |
3092 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
3033 |
3093 |
(*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP;
|
3034 |
3094 |
}
|
3035 |
3095 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
3036 |
3096 |
(*_delta3_index)[e] = _delta3->PRE_HEAP;
|
3037 |
3097 |
}
|
3038 |
3098 |
for (int i = 0; i < _blossom_num; ++i) {
|
3039 |
3099 |
(*_delta2_index)[i] = _delta2->PRE_HEAP;
|
3040 |
3100 |
(*_delta4_index)[i] = _delta4->PRE_HEAP;
|
3041 |
3101 |
}
|