| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2009 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 |
#ifndef LEMON_MAX_MATCHING_H |
|
| 20 |
#define LEMON_MAX_MATCHING_H |
|
| 19 |
#ifndef LEMON_MATCHING_H |
|
| 20 |
#define LEMON_MATCHING_H |
|
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <queue> |
| 24 | 24 |
#include <set> |
| 25 | 25 |
#include <limits> |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/core.h> |
| 28 | 28 |
#include <lemon/unionfind.h> |
| 29 | 29 |
#include <lemon/bin_heap.h> |
| 30 | 30 |
#include <lemon/maps.h> |
| 31 | 31 |
|
| 32 | 32 |
///\ingroup matching |
| 33 | 33 |
///\file |
| 34 | 34 |
///\brief Maximum matching algorithms in general graphs. |
| 35 | 35 |
|
| 36 | 36 |
namespace lemon {
|
| 37 | 37 |
|
| 38 | 38 |
/// \ingroup matching |
| 39 | 39 |
/// |
| 40 | 40 |
/// \brief Maximum cardinality matching in general graphs |
| 41 | 41 |
/// |
| 42 | 42 |
/// This class implements Edmonds' alternating forest matching algorithm |
| 43 | 43 |
/// for finding a maximum cardinality matching in a general undirected graph. |
| 44 | 44 |
/// It can be started from an arbitrary initial matching |
| 45 | 45 |
/// (the default is the empty one). |
| 46 | 46 |
/// |
| 47 | 47 |
/// The dual solution of the problem is a map of the nodes to |
| 48 | 48 |
/// \ref MaxMatching::Status "Status", having values \c EVEN (or \c D), |
| 49 | 49 |
/// \c ODD (or \c A) and \c MATCHED (or \c C) defining the Gallai-Edmonds |
| 50 | 50 |
/// decomposition of the graph. The nodes in \c EVEN/D induce a subgraph |
| 51 | 51 |
/// with factor-critical components, the nodes in \c ODD/A form the |
| 52 | 52 |
/// canonical barrier, and the nodes in \c MATCHED/C induce a graph having |
| 53 | 53 |
/// a perfect matching. The number of the factor-critical components |
| 54 | 54 |
/// minus the number of barrier nodes is a lower bound on the |
| 55 | 55 |
/// unmatched nodes, and the matching is optimal if and only if this bound is |
| 56 | 56 |
/// tight. This decomposition can be obtained using \ref status() or |
| 57 | 57 |
/// \ref statusMap() after running the algorithm. |
| 58 | 58 |
/// |
| 59 | 59 |
/// \tparam GR The undirected graph type the algorithm runs on. |
| 60 | 60 |
template <typename GR> |
| 61 | 61 |
class MaxMatching {
|
| 62 | 62 |
public: |
| 63 | 63 |
|
| 64 | 64 |
/// The graph type of the algorithm |
| 65 | 65 |
typedef GR Graph; |
| 66 | 66 |
/// The type of the matching map |
| 67 | 67 |
typedef typename Graph::template NodeMap<typename Graph::Arc> |
| 68 | 68 |
MatchingMap; |
| ... | ... |
@@ -700,97 +700,97 @@ |
| 700 | 700 |
/// The value type of the edge weights |
| 701 | 701 |
typedef typename WeightMap::Value Value; |
| 702 | 702 |
|
| 703 | 703 |
/// The type of the matching map |
| 704 | 704 |
typedef typename Graph::template NodeMap<typename Graph::Arc> |
| 705 | 705 |
MatchingMap; |
| 706 | 706 |
|
| 707 | 707 |
/// \brief Scaling factor for dual solution |
| 708 | 708 |
/// |
| 709 | 709 |
/// Scaling factor for dual solution. It is equal to 4 or 1 |
| 710 | 710 |
/// according to the value type. |
| 711 | 711 |
static const int dualScale = |
| 712 | 712 |
std::numeric_limits<Value>::is_integer ? 4 : 1; |
| 713 | 713 |
|
| 714 | 714 |
private: |
| 715 | 715 |
|
| 716 | 716 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 717 | 717 |
|
| 718 | 718 |
typedef typename Graph::template NodeMap<Value> NodePotential; |
| 719 | 719 |
typedef std::vector<Node> BlossomNodeList; |
| 720 | 720 |
|
| 721 | 721 |
struct BlossomVariable {
|
| 722 | 722 |
int begin, end; |
| 723 | 723 |
Value value; |
| 724 | 724 |
|
| 725 | 725 |
BlossomVariable(int _begin, int _end, Value _value) |
| 726 | 726 |
: begin(_begin), end(_end), value(_value) {}
|
| 727 | 727 |
|
| 728 | 728 |
}; |
| 729 | 729 |
|
| 730 | 730 |
typedef std::vector<BlossomVariable> BlossomPotential; |
| 731 | 731 |
|
| 732 | 732 |
const Graph& _graph; |
| 733 | 733 |
const WeightMap& _weight; |
| 734 | 734 |
|
| 735 | 735 |
MatchingMap* _matching; |
| 736 | 736 |
|
| 737 | 737 |
NodePotential* _node_potential; |
| 738 | 738 |
|
| 739 | 739 |
BlossomPotential _blossom_potential; |
| 740 | 740 |
BlossomNodeList _blossom_node_list; |
| 741 | 741 |
|
| 742 | 742 |
int _node_num; |
| 743 | 743 |
int _blossom_num; |
| 744 | 744 |
|
| 745 | 745 |
typedef RangeMap<int> IntIntMap; |
| 746 | 746 |
|
| 747 | 747 |
enum Status {
|
| 748 |
EVEN = -1, MATCHED = 0, ODD = 1 |
|
| 748 |
EVEN = -1, MATCHED = 0, ODD = 1 |
|
| 749 | 749 |
}; |
| 750 | 750 |
|
| 751 | 751 |
typedef HeapUnionFind<Value, IntNodeMap> BlossomSet; |
| 752 | 752 |
struct BlossomData {
|
| 753 | 753 |
int tree; |
| 754 | 754 |
Status status; |
| 755 | 755 |
Arc pred, next; |
| 756 | 756 |
Value pot, offset; |
| 757 | 757 |
Node base; |
| 758 | 758 |
}; |
| 759 | 759 |
|
| 760 | 760 |
IntNodeMap *_blossom_index; |
| 761 | 761 |
BlossomSet *_blossom_set; |
| 762 | 762 |
RangeMap<BlossomData>* _blossom_data; |
| 763 | 763 |
|
| 764 | 764 |
IntNodeMap *_node_index; |
| 765 | 765 |
IntArcMap *_node_heap_index; |
| 766 | 766 |
|
| 767 | 767 |
struct NodeData {
|
| 768 | 768 |
|
| 769 | 769 |
NodeData(IntArcMap& node_heap_index) |
| 770 | 770 |
: heap(node_heap_index) {}
|
| 771 | 771 |
|
| 772 | 772 |
int blossom; |
| 773 | 773 |
Value pot; |
| 774 | 774 |
BinHeap<Value, IntArcMap> heap; |
| 775 | 775 |
std::map<int, Arc> heap_index; |
| 776 | 776 |
|
| 777 | 777 |
int tree; |
| 778 | 778 |
}; |
| 779 | 779 |
|
| 780 | 780 |
RangeMap<NodeData>* _node_data; |
| 781 | 781 |
|
| 782 | 782 |
typedef ExtendFindEnum<IntIntMap> TreeSet; |
| 783 | 783 |
|
| 784 | 784 |
IntIntMap *_tree_set_index; |
| 785 | 785 |
TreeSet *_tree_set; |
| 786 | 786 |
|
| 787 | 787 |
IntNodeMap *_delta1_index; |
| 788 | 788 |
BinHeap<Value, IntNodeMap> *_delta1; |
| 789 | 789 |
|
| 790 | 790 |
IntIntMap *_delta2_index; |
| 791 | 791 |
BinHeap<Value, IntIntMap> *_delta2; |
| 792 | 792 |
|
| 793 | 793 |
IntEdgeMap *_delta3_index; |
| 794 | 794 |
BinHeap<Value, IntEdgeMap> *_delta3; |
| 795 | 795 |
|
| 796 | 796 |
IntIntMap *_delta4_index; |
| ... | ... |
@@ -799,178 +799,171 @@ |
| 799 | 799 |
Value _delta_sum; |
| 800 | 800 |
|
| 801 | 801 |
void createStructures() {
|
| 802 | 802 |
_node_num = countNodes(_graph); |
| 803 | 803 |
_blossom_num = _node_num * 3 / 2; |
| 804 | 804 |
|
| 805 | 805 |
if (!_matching) {
|
| 806 | 806 |
_matching = new MatchingMap(_graph); |
| 807 | 807 |
} |
| 808 | 808 |
if (!_node_potential) {
|
| 809 | 809 |
_node_potential = new NodePotential(_graph); |
| 810 | 810 |
} |
| 811 | 811 |
if (!_blossom_set) {
|
| 812 | 812 |
_blossom_index = new IntNodeMap(_graph); |
| 813 | 813 |
_blossom_set = new BlossomSet(*_blossom_index); |
| 814 | 814 |
_blossom_data = new RangeMap<BlossomData>(_blossom_num); |
| 815 | 815 |
} |
| 816 | 816 |
|
| 817 | 817 |
if (!_node_index) {
|
| 818 | 818 |
_node_index = new IntNodeMap(_graph); |
| 819 | 819 |
_node_heap_index = new IntArcMap(_graph); |
| 820 | 820 |
_node_data = new RangeMap<NodeData>(_node_num, |
| 821 | 821 |
NodeData(*_node_heap_index)); |
| 822 | 822 |
} |
| 823 | 823 |
|
| 824 | 824 |
if (!_tree_set) {
|
| 825 | 825 |
_tree_set_index = new IntIntMap(_blossom_num); |
| 826 | 826 |
_tree_set = new TreeSet(*_tree_set_index); |
| 827 | 827 |
} |
| 828 | 828 |
if (!_delta1) {
|
| 829 | 829 |
_delta1_index = new IntNodeMap(_graph); |
| 830 | 830 |
_delta1 = new BinHeap<Value, IntNodeMap>(*_delta1_index); |
| 831 | 831 |
} |
| 832 | 832 |
if (!_delta2) {
|
| 833 | 833 |
_delta2_index = new IntIntMap(_blossom_num); |
| 834 | 834 |
_delta2 = new BinHeap<Value, IntIntMap>(*_delta2_index); |
| 835 | 835 |
} |
| 836 | 836 |
if (!_delta3) {
|
| 837 | 837 |
_delta3_index = new IntEdgeMap(_graph); |
| 838 | 838 |
_delta3 = new BinHeap<Value, IntEdgeMap>(*_delta3_index); |
| 839 | 839 |
} |
| 840 | 840 |
if (!_delta4) {
|
| 841 | 841 |
_delta4_index = new IntIntMap(_blossom_num); |
| 842 | 842 |
_delta4 = new BinHeap<Value, IntIntMap>(*_delta4_index); |
| 843 | 843 |
} |
| 844 | 844 |
} |
| 845 | 845 |
|
| 846 | 846 |
void destroyStructures() {
|
| 847 |
_node_num = countNodes(_graph); |
|
| 848 |
_blossom_num = _node_num * 3 / 2; |
|
| 849 |
|
|
| 850 | 847 |
if (_matching) {
|
| 851 | 848 |
delete _matching; |
| 852 | 849 |
} |
| 853 | 850 |
if (_node_potential) {
|
| 854 | 851 |
delete _node_potential; |
| 855 | 852 |
} |
| 856 | 853 |
if (_blossom_set) {
|
| 857 | 854 |
delete _blossom_index; |
| 858 | 855 |
delete _blossom_set; |
| 859 | 856 |
delete _blossom_data; |
| 860 | 857 |
} |
| 861 | 858 |
|
| 862 | 859 |
if (_node_index) {
|
| 863 | 860 |
delete _node_index; |
| 864 | 861 |
delete _node_heap_index; |
| 865 | 862 |
delete _node_data; |
| 866 | 863 |
} |
| 867 | 864 |
|
| 868 | 865 |
if (_tree_set) {
|
| 869 | 866 |
delete _tree_set_index; |
| 870 | 867 |
delete _tree_set; |
| 871 | 868 |
} |
| 872 | 869 |
if (_delta1) {
|
| 873 | 870 |
delete _delta1_index; |
| 874 | 871 |
delete _delta1; |
| 875 | 872 |
} |
| 876 | 873 |
if (_delta2) {
|
| 877 | 874 |
delete _delta2_index; |
| 878 | 875 |
delete _delta2; |
| 879 | 876 |
} |
| 880 | 877 |
if (_delta3) {
|
| 881 | 878 |
delete _delta3_index; |
| 882 | 879 |
delete _delta3; |
| 883 | 880 |
} |
| 884 | 881 |
if (_delta4) {
|
| 885 | 882 |
delete _delta4_index; |
| 886 | 883 |
delete _delta4; |
| 887 | 884 |
} |
| 888 | 885 |
} |
| 889 | 886 |
|
| 890 | 887 |
void matchedToEven(int blossom, int tree) {
|
| 891 | 888 |
if (_delta2->state(blossom) == _delta2->IN_HEAP) {
|
| 892 | 889 |
_delta2->erase(blossom); |
| 893 | 890 |
} |
| 894 | 891 |
|
| 895 | 892 |
if (!_blossom_set->trivial(blossom)) {
|
| 896 | 893 |
(*_blossom_data)[blossom].pot -= |
| 897 | 894 |
2 * (_delta_sum - (*_blossom_data)[blossom].offset); |
| 898 | 895 |
} |
| 899 | 896 |
|
| 900 | 897 |
for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); |
| 901 | 898 |
n != INVALID; ++n) {
|
| 902 | 899 |
|
| 903 | 900 |
_blossom_set->increase(n, std::numeric_limits<Value>::max()); |
| 904 | 901 |
int ni = (*_node_index)[n]; |
| 905 | 902 |
|
| 906 | 903 |
(*_node_data)[ni].heap.clear(); |
| 907 | 904 |
(*_node_data)[ni].heap_index.clear(); |
| 908 | 905 |
|
| 909 | 906 |
(*_node_data)[ni].pot += _delta_sum - (*_blossom_data)[blossom].offset; |
| 910 | 907 |
|
| 911 | 908 |
_delta1->push(n, (*_node_data)[ni].pot); |
| 912 | 909 |
|
| 913 | 910 |
for (InArcIt e(_graph, n); e != INVALID; ++e) {
|
| 914 | 911 |
Node v = _graph.source(e); |
| 915 | 912 |
int vb = _blossom_set->find(v); |
| 916 | 913 |
int vi = (*_node_index)[v]; |
| 917 | 914 |
|
| 918 | 915 |
Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - |
| 919 | 916 |
dualScale * _weight[e]; |
| 920 | 917 |
|
| 921 | 918 |
if ((*_blossom_data)[vb].status == EVEN) {
|
| 922 | 919 |
if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) {
|
| 923 | 920 |
_delta3->push(e, rw / 2); |
| 924 | 921 |
} |
| 925 |
} else if ((*_blossom_data)[vb].status == UNMATCHED) {
|
|
| 926 |
if (_delta3->state(e) != _delta3->IN_HEAP) {
|
|
| 927 |
_delta3->push(e, rw); |
|
| 928 |
} |
|
| 929 | 922 |
} else {
|
| 930 | 923 |
typename std::map<int, Arc>::iterator it = |
| 931 | 924 |
(*_node_data)[vi].heap_index.find(tree); |
| 932 | 925 |
|
| 933 | 926 |
if (it != (*_node_data)[vi].heap_index.end()) {
|
| 934 | 927 |
if ((*_node_data)[vi].heap[it->second] > rw) {
|
| 935 | 928 |
(*_node_data)[vi].heap.replace(it->second, e); |
| 936 | 929 |
(*_node_data)[vi].heap.decrease(e, rw); |
| 937 | 930 |
it->second = e; |
| 938 | 931 |
} |
| 939 | 932 |
} else {
|
| 940 | 933 |
(*_node_data)[vi].heap.push(e, rw); |
| 941 | 934 |
(*_node_data)[vi].heap_index.insert(std::make_pair(tree, e)); |
| 942 | 935 |
} |
| 943 | 936 |
|
| 944 | 937 |
if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) {
|
| 945 | 938 |
_blossom_set->decrease(v, (*_node_data)[vi].heap.prio()); |
| 946 | 939 |
|
| 947 | 940 |
if ((*_blossom_data)[vb].status == MATCHED) {
|
| 948 | 941 |
if (_delta2->state(vb) != _delta2->IN_HEAP) {
|
| 949 | 942 |
_delta2->push(vb, _blossom_set->classPrio(vb) - |
| 950 | 943 |
(*_blossom_data)[vb].offset); |
| 951 | 944 |
} else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) - |
| 952 | 945 |
(*_blossom_data)[vb].offset){
|
| 953 | 946 |
_delta2->decrease(vb, _blossom_set->classPrio(vb) - |
| 954 | 947 |
(*_blossom_data)[vb].offset); |
| 955 | 948 |
} |
| 956 | 949 |
} |
| 957 | 950 |
} |
| 958 | 951 |
} |
| 959 | 952 |
} |
| 960 | 953 |
} |
| 961 | 954 |
(*_blossom_data)[blossom].offset = 0; |
| 962 | 955 |
} |
| 963 | 956 |
|
| 964 | 957 |
void matchedToOdd(int blossom) {
|
| 965 | 958 |
if (_delta2->state(blossom) == _delta2->IN_HEAP) {
|
| 966 | 959 |
_delta2->erase(blossom); |
| 967 | 960 |
} |
| 968 | 961 |
(*_blossom_data)[blossom].offset += _delta_sum; |
| 969 | 962 |
if (!_blossom_set->trivial(blossom)) {
|
| 970 | 963 |
_delta4->push(blossom, (*_blossom_data)[blossom].pot / 2 + |
| 971 | 964 |
(*_blossom_data)[blossom].offset); |
| 972 | 965 |
} |
| 973 | 966 |
} |
| 974 | 967 |
|
| 975 | 968 |
void evenToMatched(int blossom, int tree) {
|
| 976 | 969 |
if (!_blossom_set->trivial(blossom)) {
|
| ... | ... |
@@ -991,387 +984,286 @@ |
| 991 | 984 |
|
| 992 | 985 |
Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - |
| 993 | 986 |
dualScale * _weight[e]; |
| 994 | 987 |
|
| 995 | 988 |
if (vb == blossom) {
|
| 996 | 989 |
if (_delta3->state(e) == _delta3->IN_HEAP) {
|
| 997 | 990 |
_delta3->erase(e); |
| 998 | 991 |
} |
| 999 | 992 |
} else if ((*_blossom_data)[vb].status == EVEN) {
|
| 1000 | 993 |
|
| 1001 | 994 |
if (_delta3->state(e) == _delta3->IN_HEAP) {
|
| 1002 | 995 |
_delta3->erase(e); |
| 1003 | 996 |
} |
| 1004 | 997 |
|
| 1005 | 998 |
int vt = _tree_set->find(vb); |
| 1006 | 999 |
|
| 1007 | 1000 |
if (vt != tree) {
|
| 1008 | 1001 |
|
| 1009 | 1002 |
Arc r = _graph.oppositeArc(e); |
| 1010 | 1003 |
|
| 1011 | 1004 |
typename std::map<int, Arc>::iterator it = |
| 1012 | 1005 |
(*_node_data)[ni].heap_index.find(vt); |
| 1013 | 1006 |
|
| 1014 | 1007 |
if (it != (*_node_data)[ni].heap_index.end()) {
|
| 1015 | 1008 |
if ((*_node_data)[ni].heap[it->second] > rw) {
|
| 1016 | 1009 |
(*_node_data)[ni].heap.replace(it->second, r); |
| 1017 | 1010 |
(*_node_data)[ni].heap.decrease(r, rw); |
| 1018 | 1011 |
it->second = r; |
| 1019 | 1012 |
} |
| 1020 | 1013 |
} else {
|
| 1021 | 1014 |
(*_node_data)[ni].heap.push(r, rw); |
| 1022 | 1015 |
(*_node_data)[ni].heap_index.insert(std::make_pair(vt, r)); |
| 1023 | 1016 |
} |
| 1024 | 1017 |
|
| 1025 | 1018 |
if ((*_blossom_set)[n] > (*_node_data)[ni].heap.prio()) {
|
| 1026 | 1019 |
_blossom_set->decrease(n, (*_node_data)[ni].heap.prio()); |
| 1027 | 1020 |
|
| 1028 | 1021 |
if (_delta2->state(blossom) != _delta2->IN_HEAP) {
|
| 1029 | 1022 |
_delta2->push(blossom, _blossom_set->classPrio(blossom) - |
| 1030 | 1023 |
(*_blossom_data)[blossom].offset); |
| 1031 | 1024 |
} else if ((*_delta2)[blossom] > |
| 1032 | 1025 |
_blossom_set->classPrio(blossom) - |
| 1033 | 1026 |
(*_blossom_data)[blossom].offset){
|
| 1034 | 1027 |
_delta2->decrease(blossom, _blossom_set->classPrio(blossom) - |
| 1035 | 1028 |
(*_blossom_data)[blossom].offset); |
| 1036 | 1029 |
} |
| 1037 | 1030 |
} |
| 1038 | 1031 |
} |
| 1039 |
|
|
| 1040 |
} else if ((*_blossom_data)[vb].status == UNMATCHED) {
|
|
| 1041 |
if (_delta3->state(e) == _delta3->IN_HEAP) {
|
|
| 1042 |
_delta3->erase(e); |
|
| 1043 |
} |
|
| 1044 | 1032 |
} else {
|
| 1045 | 1033 |
|
| 1046 | 1034 |
typename std::map<int, Arc>::iterator it = |
| 1047 | 1035 |
(*_node_data)[vi].heap_index.find(tree); |
| 1048 | 1036 |
|
| 1049 | 1037 |
if (it != (*_node_data)[vi].heap_index.end()) {
|
| 1050 | 1038 |
(*_node_data)[vi].heap.erase(it->second); |
| 1051 | 1039 |
(*_node_data)[vi].heap_index.erase(it); |
| 1052 | 1040 |
if ((*_node_data)[vi].heap.empty()) {
|
| 1053 | 1041 |
_blossom_set->increase(v, std::numeric_limits<Value>::max()); |
| 1054 | 1042 |
} else if ((*_blossom_set)[v] < (*_node_data)[vi].heap.prio()) {
|
| 1055 | 1043 |
_blossom_set->increase(v, (*_node_data)[vi].heap.prio()); |
| 1056 | 1044 |
} |
| 1057 | 1045 |
|
| 1058 | 1046 |
if ((*_blossom_data)[vb].status == MATCHED) {
|
| 1059 | 1047 |
if (_blossom_set->classPrio(vb) == |
| 1060 | 1048 |
std::numeric_limits<Value>::max()) {
|
| 1061 | 1049 |
_delta2->erase(vb); |
| 1062 | 1050 |
} else if ((*_delta2)[vb] < _blossom_set->classPrio(vb) - |
| 1063 | 1051 |
(*_blossom_data)[vb].offset) {
|
| 1064 | 1052 |
_delta2->increase(vb, _blossom_set->classPrio(vb) - |
| 1065 | 1053 |
(*_blossom_data)[vb].offset); |
| 1066 | 1054 |
} |
| 1067 | 1055 |
} |
| 1068 | 1056 |
} |
| 1069 | 1057 |
} |
| 1070 | 1058 |
} |
| 1071 | 1059 |
} |
| 1072 | 1060 |
} |
| 1073 | 1061 |
|
| 1074 | 1062 |
void oddToMatched(int blossom) {
|
| 1075 | 1063 |
(*_blossom_data)[blossom].offset -= _delta_sum; |
| 1076 | 1064 |
|
| 1077 | 1065 |
if (_blossom_set->classPrio(blossom) != |
| 1078 | 1066 |
std::numeric_limits<Value>::max()) {
|
| 1079 | 1067 |
_delta2->push(blossom, _blossom_set->classPrio(blossom) - |
| 1080 | 1068 |
(*_blossom_data)[blossom].offset); |
| 1081 | 1069 |
} |
| 1082 | 1070 |
|
| 1083 | 1071 |
if (!_blossom_set->trivial(blossom)) {
|
| 1084 | 1072 |
_delta4->erase(blossom); |
| 1085 | 1073 |
} |
| 1086 | 1074 |
} |
| 1087 | 1075 |
|
| 1088 | 1076 |
void oddToEven(int blossom, int tree) {
|
| 1089 | 1077 |
if (!_blossom_set->trivial(blossom)) {
|
| 1090 | 1078 |
_delta4->erase(blossom); |
| 1091 | 1079 |
(*_blossom_data)[blossom].pot -= |
| 1092 | 1080 |
2 * (2 * _delta_sum - (*_blossom_data)[blossom].offset); |
| 1093 | 1081 |
} |
| 1094 | 1082 |
|
| 1095 | 1083 |
for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); |
| 1096 | 1084 |
n != INVALID; ++n) {
|
| 1097 | 1085 |
int ni = (*_node_index)[n]; |
| 1098 | 1086 |
|
| 1099 | 1087 |
_blossom_set->increase(n, std::numeric_limits<Value>::max()); |
| 1100 | 1088 |
|
| 1101 | 1089 |
(*_node_data)[ni].heap.clear(); |
| 1102 | 1090 |
(*_node_data)[ni].heap_index.clear(); |
| 1103 | 1091 |
(*_node_data)[ni].pot += |
| 1104 | 1092 |
2 * _delta_sum - (*_blossom_data)[blossom].offset; |
| 1105 | 1093 |
|
| 1106 | 1094 |
_delta1->push(n, (*_node_data)[ni].pot); |
| 1107 | 1095 |
|
| 1108 | 1096 |
for (InArcIt e(_graph, n); e != INVALID; ++e) {
|
| 1109 | 1097 |
Node v = _graph.source(e); |
| 1110 | 1098 |
int vb = _blossom_set->find(v); |
| 1111 | 1099 |
int vi = (*_node_index)[v]; |
| 1112 | 1100 |
|
| 1113 | 1101 |
Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - |
| 1114 | 1102 |
dualScale * _weight[e]; |
| 1115 | 1103 |
|
| 1116 | 1104 |
if ((*_blossom_data)[vb].status == EVEN) {
|
| 1117 | 1105 |
if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) {
|
| 1118 | 1106 |
_delta3->push(e, rw / 2); |
| 1119 | 1107 |
} |
| 1120 |
} else if ((*_blossom_data)[vb].status == UNMATCHED) {
|
|
| 1121 |
if (_delta3->state(e) != _delta3->IN_HEAP) {
|
|
| 1122 |
_delta3->push(e, rw); |
|
| 1123 |
} |
|
| 1124 | 1108 |
} else {
|
| 1125 | 1109 |
|
| 1126 | 1110 |
typename std::map<int, Arc>::iterator it = |
| 1127 | 1111 |
(*_node_data)[vi].heap_index.find(tree); |
| 1128 | 1112 |
|
| 1129 | 1113 |
if (it != (*_node_data)[vi].heap_index.end()) {
|
| 1130 | 1114 |
if ((*_node_data)[vi].heap[it->second] > rw) {
|
| 1131 | 1115 |
(*_node_data)[vi].heap.replace(it->second, e); |
| 1132 | 1116 |
(*_node_data)[vi].heap.decrease(e, rw); |
| 1133 | 1117 |
it->second = e; |
| 1134 | 1118 |
} |
| 1135 | 1119 |
} else {
|
| 1136 | 1120 |
(*_node_data)[vi].heap.push(e, rw); |
| 1137 | 1121 |
(*_node_data)[vi].heap_index.insert(std::make_pair(tree, e)); |
| 1138 | 1122 |
} |
| 1139 | 1123 |
|
| 1140 | 1124 |
if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) {
|
| 1141 | 1125 |
_blossom_set->decrease(v, (*_node_data)[vi].heap.prio()); |
| 1142 | 1126 |
|
| 1143 | 1127 |
if ((*_blossom_data)[vb].status == MATCHED) {
|
| 1144 | 1128 |
if (_delta2->state(vb) != _delta2->IN_HEAP) {
|
| 1145 | 1129 |
_delta2->push(vb, _blossom_set->classPrio(vb) - |
| 1146 | 1130 |
(*_blossom_data)[vb].offset); |
| 1147 | 1131 |
} else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) - |
| 1148 | 1132 |
(*_blossom_data)[vb].offset) {
|
| 1149 | 1133 |
_delta2->decrease(vb, _blossom_set->classPrio(vb) - |
| 1150 | 1134 |
(*_blossom_data)[vb].offset); |
| 1151 | 1135 |
} |
| 1152 | 1136 |
} |
| 1153 | 1137 |
} |
| 1154 | 1138 |
} |
| 1155 | 1139 |
} |
| 1156 | 1140 |
} |
| 1157 | 1141 |
(*_blossom_data)[blossom].offset = 0; |
| 1158 | 1142 |
} |
| 1159 | 1143 |
|
| 1160 |
|
|
| 1161 |
void matchedToUnmatched(int blossom) {
|
|
| 1162 |
if (_delta2->state(blossom) == _delta2->IN_HEAP) {
|
|
| 1163 |
_delta2->erase(blossom); |
|
| 1164 |
} |
|
| 1165 |
|
|
| 1166 |
for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); |
|
| 1167 |
n != INVALID; ++n) {
|
|
| 1168 |
int ni = (*_node_index)[n]; |
|
| 1169 |
|
|
| 1170 |
_blossom_set->increase(n, std::numeric_limits<Value>::max()); |
|
| 1171 |
|
|
| 1172 |
(*_node_data)[ni].heap.clear(); |
|
| 1173 |
(*_node_data)[ni].heap_index.clear(); |
|
| 1174 |
|
|
| 1175 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
|
| 1176 |
Node v = _graph.target(e); |
|
| 1177 |
int vb = _blossom_set->find(v); |
|
| 1178 |
int vi = (*_node_index)[v]; |
|
| 1179 |
|
|
| 1180 |
Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - |
|
| 1181 |
dualScale * _weight[e]; |
|
| 1182 |
|
|
| 1183 |
if ((*_blossom_data)[vb].status == EVEN) {
|
|
| 1184 |
if (_delta3->state(e) != _delta3->IN_HEAP) {
|
|
| 1185 |
_delta3->push(e, rw); |
|
| 1186 |
} |
|
| 1187 |
} |
|
| 1188 |
} |
|
| 1189 |
} |
|
| 1190 |
} |
|
| 1191 |
|
|
| 1192 |
void unmatchedToMatched(int blossom) {
|
|
| 1193 |
for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); |
|
| 1194 |
n != INVALID; ++n) {
|
|
| 1195 |
int ni = (*_node_index)[n]; |
|
| 1196 |
|
|
| 1197 |
for (InArcIt e(_graph, n); e != INVALID; ++e) {
|
|
| 1198 |
Node v = _graph.source(e); |
|
| 1199 |
int vb = _blossom_set->find(v); |
|
| 1200 |
int vi = (*_node_index)[v]; |
|
| 1201 |
|
|
| 1202 |
Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - |
|
| 1203 |
dualScale * _weight[e]; |
|
| 1204 |
|
|
| 1205 |
if (vb == blossom) {
|
|
| 1206 |
if (_delta3->state(e) == _delta3->IN_HEAP) {
|
|
| 1207 |
_delta3->erase(e); |
|
| 1208 |
} |
|
| 1209 |
} else if ((*_blossom_data)[vb].status == EVEN) {
|
|
| 1210 |
|
|
| 1211 |
if (_delta3->state(e) == _delta3->IN_HEAP) {
|
|
| 1212 |
_delta3->erase(e); |
|
| 1213 |
} |
|
| 1214 |
|
|
| 1215 |
int vt = _tree_set->find(vb); |
|
| 1216 |
|
|
| 1217 |
Arc r = _graph.oppositeArc(e); |
|
| 1218 |
|
|
| 1219 |
typename std::map<int, Arc>::iterator it = |
|
| 1220 |
(*_node_data)[ni].heap_index.find(vt); |
|
| 1221 |
|
|
| 1222 |
if (it != (*_node_data)[ni].heap_index.end()) {
|
|
| 1223 |
if ((*_node_data)[ni].heap[it->second] > rw) {
|
|
| 1224 |
(*_node_data)[ni].heap.replace(it->second, r); |
|
| 1225 |
(*_node_data)[ni].heap.decrease(r, rw); |
|
| 1226 |
it->second = r; |
|
| 1227 |
} |
|
| 1228 |
} else {
|
|
| 1229 |
(*_node_data)[ni].heap.push(r, rw); |
|
| 1230 |
(*_node_data)[ni].heap_index.insert(std::make_pair(vt, r)); |
|
| 1231 |
} |
|
| 1232 |
|
|
| 1233 |
if ((*_blossom_set)[n] > (*_node_data)[ni].heap.prio()) {
|
|
| 1234 |
_blossom_set->decrease(n, (*_node_data)[ni].heap.prio()); |
|
| 1235 |
|
|
| 1236 |
if (_delta2->state(blossom) != _delta2->IN_HEAP) {
|
|
| 1237 |
_delta2->push(blossom, _blossom_set->classPrio(blossom) - |
|
| 1238 |
(*_blossom_data)[blossom].offset); |
|
| 1239 |
} else if ((*_delta2)[blossom] > _blossom_set->classPrio(blossom)- |
|
| 1240 |
(*_blossom_data)[blossom].offset){
|
|
| 1241 |
_delta2->decrease(blossom, _blossom_set->classPrio(blossom) - |
|
| 1242 |
(*_blossom_data)[blossom].offset); |
|
| 1243 |
} |
|
| 1244 |
} |
|
| 1245 |
|
|
| 1246 |
} else if ((*_blossom_data)[vb].status == UNMATCHED) {
|
|
| 1247 |
if (_delta3->state(e) == _delta3->IN_HEAP) {
|
|
| 1248 |
_delta3->erase(e); |
|
| 1249 |
} |
|
| 1250 |
} |
|
| 1251 |
} |
|
| 1252 |
} |
|
| 1253 |
} |
|
| 1254 |
|
|
| 1255 | 1144 |
void alternatePath(int even, int tree) {
|
| 1256 | 1145 |
int odd; |
| 1257 | 1146 |
|
| 1258 | 1147 |
evenToMatched(even, tree); |
| 1259 | 1148 |
(*_blossom_data)[even].status = MATCHED; |
| 1260 | 1149 |
|
| 1261 | 1150 |
while ((*_blossom_data)[even].pred != INVALID) {
|
| 1262 | 1151 |
odd = _blossom_set->find(_graph.target((*_blossom_data)[even].pred)); |
| 1263 | 1152 |
(*_blossom_data)[odd].status = MATCHED; |
| 1264 | 1153 |
oddToMatched(odd); |
| 1265 | 1154 |
(*_blossom_data)[odd].next = (*_blossom_data)[odd].pred; |
| 1266 | 1155 |
|
| 1267 | 1156 |
even = _blossom_set->find(_graph.target((*_blossom_data)[odd].pred)); |
| 1268 | 1157 |
(*_blossom_data)[even].status = MATCHED; |
| 1269 | 1158 |
evenToMatched(even, tree); |
| 1270 | 1159 |
(*_blossom_data)[even].next = |
| 1271 | 1160 |
_graph.oppositeArc((*_blossom_data)[odd].pred); |
| 1272 | 1161 |
} |
| 1273 | 1162 |
|
| 1274 | 1163 |
} |
| 1275 | 1164 |
|
| 1276 | 1165 |
void destroyTree(int tree) {
|
| 1277 | 1166 |
for (TreeSet::ItemIt b(*_tree_set, tree); b != INVALID; ++b) {
|
| 1278 | 1167 |
if ((*_blossom_data)[b].status == EVEN) {
|
| 1279 | 1168 |
(*_blossom_data)[b].status = MATCHED; |
| 1280 | 1169 |
evenToMatched(b, tree); |
| 1281 | 1170 |
} else if ((*_blossom_data)[b].status == ODD) {
|
| 1282 | 1171 |
(*_blossom_data)[b].status = MATCHED; |
| 1283 | 1172 |
oddToMatched(b); |
| 1284 | 1173 |
} |
| 1285 | 1174 |
} |
| 1286 | 1175 |
_tree_set->eraseClass(tree); |
| 1287 | 1176 |
} |
| 1288 | 1177 |
|
| 1289 | 1178 |
|
| 1290 | 1179 |
void unmatchNode(const Node& node) {
|
| 1291 | 1180 |
int blossom = _blossom_set->find(node); |
| 1292 | 1181 |
int tree = _tree_set->find(blossom); |
| 1293 | 1182 |
|
| 1294 | 1183 |
alternatePath(blossom, tree); |
| 1295 | 1184 |
destroyTree(tree); |
| 1296 | 1185 |
|
| 1297 |
(*_blossom_data)[blossom].status = UNMATCHED; |
|
| 1298 | 1186 |
(*_blossom_data)[blossom].base = node; |
| 1299 |
matchedToUnmatched(blossom); |
|
| 1300 |
} |
|
| 1301 |
|
|
| 1187 |
(*_blossom_data)[blossom].next = INVALID; |
|
| 1188 |
} |
|
| 1302 | 1189 |
|
| 1303 | 1190 |
void augmentOnEdge(const Edge& edge) {
|
| 1304 | 1191 |
|
| 1305 | 1192 |
int left = _blossom_set->find(_graph.u(edge)); |
| 1306 | 1193 |
int right = _blossom_set->find(_graph.v(edge)); |
| 1307 | 1194 |
|
| 1308 |
if ((*_blossom_data)[left].status == EVEN) {
|
|
| 1309 | 1195 |
int left_tree = _tree_set->find(left); |
| 1310 | 1196 |
alternatePath(left, left_tree); |
| 1311 | 1197 |
destroyTree(left_tree); |
| 1312 |
} else {
|
|
| 1313 |
(*_blossom_data)[left].status = MATCHED; |
|
| 1314 |
unmatchedToMatched(left); |
|
| 1315 |
} |
|
| 1316 |
|
|
| 1317 |
if ((*_blossom_data)[right].status == EVEN) {
|
|
| 1198 |
|
|
| 1318 | 1199 |
int right_tree = _tree_set->find(right); |
| 1319 | 1200 |
alternatePath(right, right_tree); |
| 1320 | 1201 |
destroyTree(right_tree); |
| 1321 |
} else {
|
|
| 1322 |
(*_blossom_data)[right].status = MATCHED; |
|
| 1323 |
unmatchedToMatched(right); |
|
| 1324 |
} |
|
| 1325 | 1202 |
|
| 1326 | 1203 |
(*_blossom_data)[left].next = _graph.direct(edge, true); |
| 1327 | 1204 |
(*_blossom_data)[right].next = _graph.direct(edge, false); |
| 1328 | 1205 |
} |
| 1329 | 1206 |
|
| 1207 |
void augmentOnArc(const Arc& arc) {
|
|
| 1208 |
|
|
| 1209 |
int left = _blossom_set->find(_graph.source(arc)); |
|
| 1210 |
int right = _blossom_set->find(_graph.target(arc)); |
|
| 1211 |
|
|
| 1212 |
(*_blossom_data)[left].status = MATCHED; |
|
| 1213 |
|
|
| 1214 |
int right_tree = _tree_set->find(right); |
|
| 1215 |
alternatePath(right, right_tree); |
|
| 1216 |
destroyTree(right_tree); |
|
| 1217 |
|
|
| 1218 |
(*_blossom_data)[left].next = arc; |
|
| 1219 |
(*_blossom_data)[right].next = _graph.oppositeArc(arc); |
|
| 1220 |
} |
|
| 1221 |
|
|
| 1330 | 1222 |
void extendOnArc(const Arc& arc) {
|
| 1331 | 1223 |
int base = _blossom_set->find(_graph.target(arc)); |
| 1332 | 1224 |
int tree = _tree_set->find(base); |
| 1333 | 1225 |
|
| 1334 | 1226 |
int odd = _blossom_set->find(_graph.source(arc)); |
| 1335 | 1227 |
_tree_set->insert(odd, tree); |
| 1336 | 1228 |
(*_blossom_data)[odd].status = ODD; |
| 1337 | 1229 |
matchedToOdd(odd); |
| 1338 | 1230 |
(*_blossom_data)[odd].pred = arc; |
| 1339 | 1231 |
|
| 1340 | 1232 |
int even = _blossom_set->find(_graph.target((*_blossom_data)[odd].next)); |
| 1341 | 1233 |
(*_blossom_data)[even].pred = (*_blossom_data)[even].next; |
| 1342 | 1234 |
_tree_set->insert(even, tree); |
| 1343 | 1235 |
(*_blossom_data)[even].status = EVEN; |
| 1344 | 1236 |
matchedToEven(even, tree); |
| 1345 | 1237 |
} |
| 1346 | 1238 |
|
| 1347 | 1239 |
void shrinkOnEdge(const Edge& edge, int tree) {
|
| 1348 | 1240 |
int nca = -1; |
| 1349 | 1241 |
std::vector<int> left_path, right_path; |
| 1350 | 1242 |
|
| 1351 | 1243 |
{
|
| 1352 | 1244 |
std::set<int> left_set, right_set; |
| 1353 | 1245 |
int left = _blossom_set->find(_graph.u(edge)); |
| 1354 | 1246 |
left_path.push_back(left); |
| 1355 | 1247 |
left_set.insert(left); |
| 1356 | 1248 |
|
| 1357 | 1249 |
int right = _blossom_set->find(_graph.v(edge)); |
| 1358 | 1250 |
right_path.push_back(right); |
| 1359 | 1251 |
right_set.insert(right); |
| 1360 | 1252 |
|
| 1361 | 1253 |
while (true) {
|
| 1362 | 1254 |
|
| 1363 | 1255 |
if ((*_blossom_data)[left].pred == INVALID) break; |
| 1364 | 1256 |
|
| 1365 | 1257 |
left = |
| 1366 | 1258 |
_blossom_set->find(_graph.target((*_blossom_data)[left].pred)); |
| 1367 | 1259 |
left_path.push_back(left); |
| 1368 | 1260 |
left = |
| 1369 | 1261 |
_blossom_set->find(_graph.target((*_blossom_data)[left].pred)); |
| 1370 | 1262 |
left_path.push_back(left); |
| 1371 | 1263 |
|
| 1372 | 1264 |
left_set.insert(left); |
| 1373 | 1265 |
|
| 1374 | 1266 |
if (right_set.find(left) != right_set.end()) {
|
| 1375 | 1267 |
nca = left; |
| 1376 | 1268 |
break; |
| 1377 | 1269 |
} |
| ... | ... |
@@ -1584,97 +1476,97 @@ |
| 1584 | 1476 |
} |
| 1585 | 1477 |
_tree_set->erase(blossom); |
| 1586 | 1478 |
} |
| 1587 | 1479 |
|
| 1588 | 1480 |
void extractBlossom(int blossom, const Node& base, const Arc& matching) {
|
| 1589 | 1481 |
if (_blossom_set->trivial(blossom)) {
|
| 1590 | 1482 |
int bi = (*_node_index)[base]; |
| 1591 | 1483 |
Value pot = (*_node_data)[bi].pot; |
| 1592 | 1484 |
|
| 1593 | 1485 |
(*_matching)[base] = matching; |
| 1594 | 1486 |
_blossom_node_list.push_back(base); |
| 1595 | 1487 |
(*_node_potential)[base] = pot; |
| 1596 | 1488 |
} else {
|
| 1597 | 1489 |
|
| 1598 | 1490 |
Value pot = (*_blossom_data)[blossom].pot; |
| 1599 | 1491 |
int bn = _blossom_node_list.size(); |
| 1600 | 1492 |
|
| 1601 | 1493 |
std::vector<int> subblossoms; |
| 1602 | 1494 |
_blossom_set->split(blossom, std::back_inserter(subblossoms)); |
| 1603 | 1495 |
int b = _blossom_set->find(base); |
| 1604 | 1496 |
int ib = -1; |
| 1605 | 1497 |
for (int i = 0; i < int(subblossoms.size()); ++i) {
|
| 1606 | 1498 |
if (subblossoms[i] == b) { ib = i; break; }
|
| 1607 | 1499 |
} |
| 1608 | 1500 |
|
| 1609 | 1501 |
for (int i = 1; i < int(subblossoms.size()); i += 2) {
|
| 1610 | 1502 |
int sb = subblossoms[(ib + i) % subblossoms.size()]; |
| 1611 | 1503 |
int tb = subblossoms[(ib + i + 1) % subblossoms.size()]; |
| 1612 | 1504 |
|
| 1613 | 1505 |
Arc m = (*_blossom_data)[tb].next; |
| 1614 | 1506 |
extractBlossom(sb, _graph.target(m), _graph.oppositeArc(m)); |
| 1615 | 1507 |
extractBlossom(tb, _graph.source(m), m); |
| 1616 | 1508 |
} |
| 1617 | 1509 |
extractBlossom(subblossoms[ib], base, matching); |
| 1618 | 1510 |
|
| 1619 | 1511 |
int en = _blossom_node_list.size(); |
| 1620 | 1512 |
|
| 1621 | 1513 |
_blossom_potential.push_back(BlossomVariable(bn, en, pot)); |
| 1622 | 1514 |
} |
| 1623 | 1515 |
} |
| 1624 | 1516 |
|
| 1625 | 1517 |
void extractMatching() {
|
| 1626 | 1518 |
std::vector<int> blossoms; |
| 1627 | 1519 |
for (typename BlossomSet::ClassIt c(*_blossom_set); c != INVALID; ++c) {
|
| 1628 | 1520 |
blossoms.push_back(c); |
| 1629 | 1521 |
} |
| 1630 | 1522 |
|
| 1631 | 1523 |
for (int i = 0; i < int(blossoms.size()); ++i) {
|
| 1632 |
if ((*_blossom_data)[blossoms[i]]. |
|
| 1524 |
if ((*_blossom_data)[blossoms[i]].next != INVALID) {
|
|
| 1633 | 1525 |
|
| 1634 | 1526 |
Value offset = (*_blossom_data)[blossoms[i]].offset; |
| 1635 | 1527 |
(*_blossom_data)[blossoms[i]].pot += 2 * offset; |
| 1636 | 1528 |
for (typename BlossomSet::ItemIt n(*_blossom_set, blossoms[i]); |
| 1637 | 1529 |
n != INVALID; ++n) {
|
| 1638 | 1530 |
(*_node_data)[(*_node_index)[n]].pot -= offset; |
| 1639 | 1531 |
} |
| 1640 | 1532 |
|
| 1641 | 1533 |
Arc matching = (*_blossom_data)[blossoms[i]].next; |
| 1642 | 1534 |
Node base = _graph.source(matching); |
| 1643 | 1535 |
extractBlossom(blossoms[i], base, matching); |
| 1644 | 1536 |
} else {
|
| 1645 | 1537 |
Node base = (*_blossom_data)[blossoms[i]].base; |
| 1646 | 1538 |
extractBlossom(blossoms[i], base, INVALID); |
| 1647 | 1539 |
} |
| 1648 | 1540 |
} |
| 1649 | 1541 |
} |
| 1650 | 1542 |
|
| 1651 | 1543 |
public: |
| 1652 | 1544 |
|
| 1653 | 1545 |
/// \brief Constructor |
| 1654 | 1546 |
/// |
| 1655 | 1547 |
/// Constructor. |
| 1656 | 1548 |
MaxWeightedMatching(const Graph& graph, const WeightMap& weight) |
| 1657 | 1549 |
: _graph(graph), _weight(weight), _matching(0), |
| 1658 | 1550 |
_node_potential(0), _blossom_potential(), _blossom_node_list(), |
| 1659 | 1551 |
_node_num(0), _blossom_num(0), |
| 1660 | 1552 |
|
| 1661 | 1553 |
_blossom_index(0), _blossom_set(0), _blossom_data(0), |
| 1662 | 1554 |
_node_index(0), _node_heap_index(0), _node_data(0), |
| 1663 | 1555 |
_tree_set_index(0), _tree_set(0), |
| 1664 | 1556 |
|
| 1665 | 1557 |
_delta1_index(0), _delta1(0), |
| 1666 | 1558 |
_delta2_index(0), _delta2(0), |
| 1667 | 1559 |
_delta3_index(0), _delta3(0), |
| 1668 | 1560 |
_delta4_index(0), _delta4(0), |
| 1669 | 1561 |
|
| 1670 | 1562 |
_delta_sum() {}
|
| 1671 | 1563 |
|
| 1672 | 1564 |
~MaxWeightedMatching() {
|
| 1673 | 1565 |
destroyStructures(); |
| 1674 | 1566 |
} |
| 1675 | 1567 |
|
| 1676 | 1568 |
/// \name Execution Control |
| 1677 | 1569 |
/// The simplest way to execute the algorithm is to use the |
| 1678 | 1570 |
/// \ref run() member function. |
| 1679 | 1571 |
|
| 1680 | 1572 |
///@{
|
| ... | ... |
@@ -1712,196 +1604,188 @@ |
| 1712 | 1604 |
(*_node_data)[index].pot = max; |
| 1713 | 1605 |
_delta1->push(n, max); |
| 1714 | 1606 |
int blossom = |
| 1715 | 1607 |
_blossom_set->insert(n, std::numeric_limits<Value>::max()); |
| 1716 | 1608 |
|
| 1717 | 1609 |
_tree_set->insert(blossom); |
| 1718 | 1610 |
|
| 1719 | 1611 |
(*_blossom_data)[blossom].status = EVEN; |
| 1720 | 1612 |
(*_blossom_data)[blossom].pred = INVALID; |
| 1721 | 1613 |
(*_blossom_data)[blossom].next = INVALID; |
| 1722 | 1614 |
(*_blossom_data)[blossom].pot = 0; |
| 1723 | 1615 |
(*_blossom_data)[blossom].offset = 0; |
| 1724 | 1616 |
++index; |
| 1725 | 1617 |
} |
| 1726 | 1618 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 1727 | 1619 |
int si = (*_node_index)[_graph.u(e)]; |
| 1728 | 1620 |
int ti = (*_node_index)[_graph.v(e)]; |
| 1729 | 1621 |
if (_graph.u(e) != _graph.v(e)) {
|
| 1730 | 1622 |
_delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot - |
| 1731 | 1623 |
dualScale * _weight[e]) / 2); |
| 1732 | 1624 |
} |
| 1733 | 1625 |
} |
| 1734 | 1626 |
} |
| 1735 | 1627 |
|
| 1736 | 1628 |
/// \brief Start the algorithm |
| 1737 | 1629 |
/// |
| 1738 | 1630 |
/// This function starts the algorithm. |
| 1739 | 1631 |
/// |
| 1740 | 1632 |
/// \pre \ref init() must be called before using this function. |
| 1741 | 1633 |
void start() {
|
| 1742 | 1634 |
enum OpType {
|
| 1743 | 1635 |
D1, D2, D3, D4 |
| 1744 | 1636 |
}; |
| 1745 | 1637 |
|
| 1746 | 1638 |
int unmatched = _node_num; |
| 1747 | 1639 |
while (unmatched > 0) {
|
| 1748 | 1640 |
Value d1 = !_delta1->empty() ? |
| 1749 | 1641 |
_delta1->prio() : std::numeric_limits<Value>::max(); |
| 1750 | 1642 |
|
| 1751 | 1643 |
Value d2 = !_delta2->empty() ? |
| 1752 | 1644 |
_delta2->prio() : std::numeric_limits<Value>::max(); |
| 1753 | 1645 |
|
| 1754 | 1646 |
Value d3 = !_delta3->empty() ? |
| 1755 | 1647 |
_delta3->prio() : std::numeric_limits<Value>::max(); |
| 1756 | 1648 |
|
| 1757 | 1649 |
Value d4 = !_delta4->empty() ? |
| 1758 | 1650 |
_delta4->prio() : std::numeric_limits<Value>::max(); |
| 1759 | 1651 |
|
| 1760 |
_delta_sum = |
|
| 1652 |
_delta_sum = d3; OpType ot = D3; |
|
| 1653 |
if (d1 < _delta_sum) { _delta_sum = d1; ot = D1; }
|
|
| 1761 | 1654 |
if (d2 < _delta_sum) { _delta_sum = d2; ot = D2; }
|
| 1762 |
if (d3 < _delta_sum) { _delta_sum = d3; ot = D3; }
|
|
| 1763 | 1655 |
if (d4 < _delta_sum) { _delta_sum = d4; ot = D4; }
|
| 1764 | 1656 |
|
| 1765 |
|
|
| 1766 | 1657 |
switch (ot) {
|
| 1767 | 1658 |
case D1: |
| 1768 | 1659 |
{
|
| 1769 | 1660 |
Node n = _delta1->top(); |
| 1770 | 1661 |
unmatchNode(n); |
| 1771 | 1662 |
--unmatched; |
| 1772 | 1663 |
} |
| 1773 | 1664 |
break; |
| 1774 | 1665 |
case D2: |
| 1775 | 1666 |
{
|
| 1776 | 1667 |
int blossom = _delta2->top(); |
| 1777 | 1668 |
Node n = _blossom_set->classTop(blossom); |
| 1778 |
Arc e = (*_node_data)[(*_node_index)[n]].heap.top(); |
|
| 1779 |
extendOnArc(e); |
|
| 1669 |
Arc a = (*_node_data)[(*_node_index)[n]].heap.top(); |
|
| 1670 |
if ((*_blossom_data)[blossom].next == INVALID) {
|
|
| 1671 |
augmentOnArc(a); |
|
| 1672 |
--unmatched; |
|
| 1673 |
} else {
|
|
| 1674 |
extendOnArc(a); |
|
| 1675 |
} |
|
| 1780 | 1676 |
} |
| 1781 | 1677 |
break; |
| 1782 | 1678 |
case D3: |
| 1783 | 1679 |
{
|
| 1784 | 1680 |
Edge e = _delta3->top(); |
| 1785 | 1681 |
|
| 1786 | 1682 |
int left_blossom = _blossom_set->find(_graph.u(e)); |
| 1787 | 1683 |
int right_blossom = _blossom_set->find(_graph.v(e)); |
| 1788 | 1684 |
|
| 1789 | 1685 |
if (left_blossom == right_blossom) {
|
| 1790 | 1686 |
_delta3->pop(); |
| 1791 | 1687 |
} else {
|
| 1792 |
int left_tree; |
|
| 1793 |
if ((*_blossom_data)[left_blossom].status == EVEN) {
|
|
| 1794 |
left_tree = _tree_set->find(left_blossom); |
|
| 1795 |
} else {
|
|
| 1796 |
left_tree = -1; |
|
| 1797 |
++unmatched; |
|
| 1798 |
} |
|
| 1799 |
int right_tree; |
|
| 1800 |
if ((*_blossom_data)[right_blossom].status == EVEN) {
|
|
| 1801 |
right_tree = _tree_set->find(right_blossom); |
|
| 1802 |
} else {
|
|
| 1803 |
right_tree = -1; |
|
| 1804 |
++unmatched; |
|
| 1805 |
} |
|
| 1688 |
int left_tree = _tree_set->find(left_blossom); |
|
| 1689 |
int right_tree = _tree_set->find(right_blossom); |
|
| 1806 | 1690 |
|
| 1807 | 1691 |
if (left_tree == right_tree) {
|
| 1808 | 1692 |
shrinkOnEdge(e, left_tree); |
| 1809 | 1693 |
} else {
|
| 1810 | 1694 |
augmentOnEdge(e); |
| 1811 | 1695 |
unmatched -= 2; |
| 1812 | 1696 |
} |
| 1813 | 1697 |
} |
| 1814 | 1698 |
} break; |
| 1815 | 1699 |
case D4: |
| 1816 | 1700 |
splitBlossom(_delta4->top()); |
| 1817 | 1701 |
break; |
| 1818 | 1702 |
} |
| 1819 | 1703 |
} |
| 1820 | 1704 |
extractMatching(); |
| 1821 | 1705 |
} |
| 1822 | 1706 |
|
| 1823 | 1707 |
/// \brief Run the algorithm. |
| 1824 | 1708 |
/// |
| 1825 | 1709 |
/// This method runs the \c %MaxWeightedMatching algorithm. |
| 1826 | 1710 |
/// |
| 1827 | 1711 |
/// \note mwm.run() is just a shortcut of the following code. |
| 1828 | 1712 |
/// \code |
| 1829 | 1713 |
/// mwm.init(); |
| 1830 | 1714 |
/// mwm.start(); |
| 1831 | 1715 |
/// \endcode |
| 1832 | 1716 |
void run() {
|
| 1833 | 1717 |
init(); |
| 1834 | 1718 |
start(); |
| 1835 | 1719 |
} |
| 1836 | 1720 |
|
| 1837 | 1721 |
/// @} |
| 1838 | 1722 |
|
| 1839 | 1723 |
/// \name Primal Solution |
| 1840 | 1724 |
/// Functions to get the primal solution, i.e. the maximum weighted |
| 1841 | 1725 |
/// matching.\n |
| 1842 | 1726 |
/// Either \ref run() or \ref start() function should be called before |
| 1843 | 1727 |
/// using them. |
| 1844 | 1728 |
|
| 1845 | 1729 |
/// @{
|
| 1846 | 1730 |
|
| 1847 | 1731 |
/// \brief Return the weight of the matching. |
| 1848 | 1732 |
/// |
| 1849 | 1733 |
/// This function returns the weight of the found matching. |
| 1850 | 1734 |
/// |
| 1851 | 1735 |
/// \pre Either run() or start() must be called before using this function. |
| 1852 | 1736 |
Value matchingWeight() const {
|
| 1853 | 1737 |
Value sum = 0; |
| 1854 | 1738 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1855 | 1739 |
if ((*_matching)[n] != INVALID) {
|
| 1856 | 1740 |
sum += _weight[(*_matching)[n]]; |
| 1857 | 1741 |
} |
| 1858 | 1742 |
} |
| 1859 |
return sum / |
|
| 1743 |
return sum / 2; |
|
| 1860 | 1744 |
} |
| 1861 | 1745 |
|
| 1862 | 1746 |
/// \brief Return the size (cardinality) of the matching. |
| 1863 | 1747 |
/// |
| 1864 | 1748 |
/// This function returns the size (cardinality) of the found matching. |
| 1865 | 1749 |
/// |
| 1866 | 1750 |
/// \pre Either run() or start() must be called before using this function. |
| 1867 | 1751 |
int matchingSize() const {
|
| 1868 | 1752 |
int num = 0; |
| 1869 | 1753 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1870 | 1754 |
if ((*_matching)[n] != INVALID) {
|
| 1871 | 1755 |
++num; |
| 1872 | 1756 |
} |
| 1873 | 1757 |
} |
| 1874 | 1758 |
return num /= 2; |
| 1875 | 1759 |
} |
| 1876 | 1760 |
|
| 1877 | 1761 |
/// \brief Return \c true if the given edge is in the matching. |
| 1878 | 1762 |
/// |
| 1879 | 1763 |
/// This function returns \c true if the given edge is in the found |
| 1880 | 1764 |
/// matching. |
| 1881 | 1765 |
/// |
| 1882 | 1766 |
/// \pre Either run() or start() must be called before using this function. |
| 1883 | 1767 |
bool matching(const Edge& edge) const {
|
| 1884 | 1768 |
return edge == (*_matching)[_graph.u(edge)]; |
| 1885 | 1769 |
} |
| 1886 | 1770 |
|
| 1887 | 1771 |
/// \brief Return the matching arc (or edge) incident to the given node. |
| 1888 | 1772 |
/// |
| 1889 | 1773 |
/// This function returns the matching arc (or edge) incident to the |
| 1890 | 1774 |
/// given node in the found matching or \c INVALID if the node is |
| 1891 | 1775 |
/// not covered by the matching. |
| 1892 | 1776 |
/// |
| 1893 | 1777 |
/// \pre Either run() or start() must be called before using this function. |
| 1894 | 1778 |
Arc matching(const Node& node) const {
|
| 1895 | 1779 |
return (*_matching)[node]; |
| 1896 | 1780 |
} |
| 1897 | 1781 |
|
| 1898 | 1782 |
/// \brief Return a const reference to the matching map. |
| 1899 | 1783 |
/// |
| 1900 | 1784 |
/// This function returns a const reference to a node map that stores |
| 1901 | 1785 |
/// the matching arc (or edge) incident to each node. |
| 1902 | 1786 |
const MatchingMap& matchingMap() const {
|
| 1903 | 1787 |
return *_matching; |
| 1904 | 1788 |
} |
| 1905 | 1789 |
|
| 1906 | 1790 |
/// \brief Return the mate of the given node. |
| 1907 | 1791 |
/// |
| ... | ... |
@@ -2188,99 +2072,96 @@ |
| 2188 | 2072 |
|
| 2189 | 2073 |
IntIntMap *_delta4_index; |
| 2190 | 2074 |
BinHeap<Value, IntIntMap> *_delta4; |
| 2191 | 2075 |
|
| 2192 | 2076 |
Value _delta_sum; |
| 2193 | 2077 |
|
| 2194 | 2078 |
void createStructures() {
|
| 2195 | 2079 |
_node_num = countNodes(_graph); |
| 2196 | 2080 |
_blossom_num = _node_num * 3 / 2; |
| 2197 | 2081 |
|
| 2198 | 2082 |
if (!_matching) {
|
| 2199 | 2083 |
_matching = new MatchingMap(_graph); |
| 2200 | 2084 |
} |
| 2201 | 2085 |
if (!_node_potential) {
|
| 2202 | 2086 |
_node_potential = new NodePotential(_graph); |
| 2203 | 2087 |
} |
| 2204 | 2088 |
if (!_blossom_set) {
|
| 2205 | 2089 |
_blossom_index = new IntNodeMap(_graph); |
| 2206 | 2090 |
_blossom_set = new BlossomSet(*_blossom_index); |
| 2207 | 2091 |
_blossom_data = new RangeMap<BlossomData>(_blossom_num); |
| 2208 | 2092 |
} |
| 2209 | 2093 |
|
| 2210 | 2094 |
if (!_node_index) {
|
| 2211 | 2095 |
_node_index = new IntNodeMap(_graph); |
| 2212 | 2096 |
_node_heap_index = new IntArcMap(_graph); |
| 2213 | 2097 |
_node_data = new RangeMap<NodeData>(_node_num, |
| 2214 | 2098 |
NodeData(*_node_heap_index)); |
| 2215 | 2099 |
} |
| 2216 | 2100 |
|
| 2217 | 2101 |
if (!_tree_set) {
|
| 2218 | 2102 |
_tree_set_index = new IntIntMap(_blossom_num); |
| 2219 | 2103 |
_tree_set = new TreeSet(*_tree_set_index); |
| 2220 | 2104 |
} |
| 2221 | 2105 |
if (!_delta2) {
|
| 2222 | 2106 |
_delta2_index = new IntIntMap(_blossom_num); |
| 2223 | 2107 |
_delta2 = new BinHeap<Value, IntIntMap>(*_delta2_index); |
| 2224 | 2108 |
} |
| 2225 | 2109 |
if (!_delta3) {
|
| 2226 | 2110 |
_delta3_index = new IntEdgeMap(_graph); |
| 2227 | 2111 |
_delta3 = new BinHeap<Value, IntEdgeMap>(*_delta3_index); |
| 2228 | 2112 |
} |
| 2229 | 2113 |
if (!_delta4) {
|
| 2230 | 2114 |
_delta4_index = new IntIntMap(_blossom_num); |
| 2231 | 2115 |
_delta4 = new BinHeap<Value, IntIntMap>(*_delta4_index); |
| 2232 | 2116 |
} |
| 2233 | 2117 |
} |
| 2234 | 2118 |
|
| 2235 | 2119 |
void destroyStructures() {
|
| 2236 |
_node_num = countNodes(_graph); |
|
| 2237 |
_blossom_num = _node_num * 3 / 2; |
|
| 2238 |
|
|
| 2239 | 2120 |
if (_matching) {
|
| 2240 | 2121 |
delete _matching; |
| 2241 | 2122 |
} |
| 2242 | 2123 |
if (_node_potential) {
|
| 2243 | 2124 |
delete _node_potential; |
| 2244 | 2125 |
} |
| 2245 | 2126 |
if (_blossom_set) {
|
| 2246 | 2127 |
delete _blossom_index; |
| 2247 | 2128 |
delete _blossom_set; |
| 2248 | 2129 |
delete _blossom_data; |
| 2249 | 2130 |
} |
| 2250 | 2131 |
|
| 2251 | 2132 |
if (_node_index) {
|
| 2252 | 2133 |
delete _node_index; |
| 2253 | 2134 |
delete _node_heap_index; |
| 2254 | 2135 |
delete _node_data; |
| 2255 | 2136 |
} |
| 2256 | 2137 |
|
| 2257 | 2138 |
if (_tree_set) {
|
| 2258 | 2139 |
delete _tree_set_index; |
| 2259 | 2140 |
delete _tree_set; |
| 2260 | 2141 |
} |
| 2261 | 2142 |
if (_delta2) {
|
| 2262 | 2143 |
delete _delta2_index; |
| 2263 | 2144 |
delete _delta2; |
| 2264 | 2145 |
} |
| 2265 | 2146 |
if (_delta3) {
|
| 2266 | 2147 |
delete _delta3_index; |
| 2267 | 2148 |
delete _delta3; |
| 2268 | 2149 |
} |
| 2269 | 2150 |
if (_delta4) {
|
| 2270 | 2151 |
delete _delta4_index; |
| 2271 | 2152 |
delete _delta4; |
| 2272 | 2153 |
} |
| 2273 | 2154 |
} |
| 2274 | 2155 |
|
| 2275 | 2156 |
void matchedToEven(int blossom, int tree) {
|
| 2276 | 2157 |
if (_delta2->state(blossom) == _delta2->IN_HEAP) {
|
| 2277 | 2158 |
_delta2->erase(blossom); |
| 2278 | 2159 |
} |
| 2279 | 2160 |
|
| 2280 | 2161 |
if (!_blossom_set->trivial(blossom)) {
|
| 2281 | 2162 |
(*_blossom_data)[blossom].pot -= |
| 2282 | 2163 |
2 * (_delta_sum - (*_blossom_data)[blossom].offset); |
| 2283 | 2164 |
} |
| 2284 | 2165 |
|
| 2285 | 2166 |
for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); |
| 2286 | 2167 |
n != INVALID; ++n) {
|
| ... | ... |
@@ -2946,180 +2827,180 @@ |
| 2946 | 2827 |
max = (dualScale * _weight[e]) / 2; |
| 2947 | 2828 |
} |
| 2948 | 2829 |
} |
| 2949 | 2830 |
(*_node_index)[n] = index; |
| 2950 | 2831 |
(*_node_data)[index].pot = max; |
| 2951 | 2832 |
int blossom = |
| 2952 | 2833 |
_blossom_set->insert(n, std::numeric_limits<Value>::max()); |
| 2953 | 2834 |
|
| 2954 | 2835 |
_tree_set->insert(blossom); |
| 2955 | 2836 |
|
| 2956 | 2837 |
(*_blossom_data)[blossom].status = EVEN; |
| 2957 | 2838 |
(*_blossom_data)[blossom].pred = INVALID; |
| 2958 | 2839 |
(*_blossom_data)[blossom].next = INVALID; |
| 2959 | 2840 |
(*_blossom_data)[blossom].pot = 0; |
| 2960 | 2841 |
(*_blossom_data)[blossom].offset = 0; |
| 2961 | 2842 |
++index; |
| 2962 | 2843 |
} |
| 2963 | 2844 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 2964 | 2845 |
int si = (*_node_index)[_graph.u(e)]; |
| 2965 | 2846 |
int ti = (*_node_index)[_graph.v(e)]; |
| 2966 | 2847 |
if (_graph.u(e) != _graph.v(e)) {
|
| 2967 | 2848 |
_delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot - |
| 2968 | 2849 |
dualScale * _weight[e]) / 2); |
| 2969 | 2850 |
} |
| 2970 | 2851 |
} |
| 2971 | 2852 |
} |
| 2972 | 2853 |
|
| 2973 | 2854 |
/// \brief Start the algorithm |
| 2974 | 2855 |
/// |
| 2975 | 2856 |
/// This function starts the algorithm. |
| 2976 | 2857 |
/// |
| 2977 | 2858 |
/// \pre \ref init() must be called before using this function. |
| 2978 | 2859 |
bool start() {
|
| 2979 | 2860 |
enum OpType {
|
| 2980 | 2861 |
D2, D3, D4 |
| 2981 | 2862 |
}; |
| 2982 | 2863 |
|
| 2983 | 2864 |
int unmatched = _node_num; |
| 2984 | 2865 |
while (unmatched > 0) {
|
| 2985 | 2866 |
Value d2 = !_delta2->empty() ? |
| 2986 | 2867 |
_delta2->prio() : std::numeric_limits<Value>::max(); |
| 2987 | 2868 |
|
| 2988 | 2869 |
Value d3 = !_delta3->empty() ? |
| 2989 | 2870 |
_delta3->prio() : std::numeric_limits<Value>::max(); |
| 2990 | 2871 |
|
| 2991 | 2872 |
Value d4 = !_delta4->empty() ? |
| 2992 | 2873 |
_delta4->prio() : std::numeric_limits<Value>::max(); |
| 2993 | 2874 |
|
| 2994 |
_delta_sum = d2; OpType ot = D2; |
|
| 2995 |
if (d3 < _delta_sum) { _delta_sum = d3; ot = D3; }
|
|
| 2875 |
_delta_sum = d3; OpType ot = D3; |
|
| 2876 |
if (d2 < _delta_sum) { _delta_sum = d2; ot = D2; }
|
|
| 2996 | 2877 |
if (d4 < _delta_sum) { _delta_sum = d4; ot = D4; }
|
| 2997 | 2878 |
|
| 2998 | 2879 |
if (_delta_sum == std::numeric_limits<Value>::max()) {
|
| 2999 | 2880 |
return false; |
| 3000 | 2881 |
} |
| 3001 | 2882 |
|
| 3002 | 2883 |
switch (ot) {
|
| 3003 | 2884 |
case D2: |
| 3004 | 2885 |
{
|
| 3005 | 2886 |
int blossom = _delta2->top(); |
| 3006 | 2887 |
Node n = _blossom_set->classTop(blossom); |
| 3007 | 2888 |
Arc e = (*_node_data)[(*_node_index)[n]].heap.top(); |
| 3008 | 2889 |
extendOnArc(e); |
| 3009 | 2890 |
} |
| 3010 | 2891 |
break; |
| 3011 | 2892 |
case D3: |
| 3012 | 2893 |
{
|
| 3013 | 2894 |
Edge e = _delta3->top(); |
| 3014 | 2895 |
|
| 3015 | 2896 |
int left_blossom = _blossom_set->find(_graph.u(e)); |
| 3016 | 2897 |
int right_blossom = _blossom_set->find(_graph.v(e)); |
| 3017 | 2898 |
|
| 3018 | 2899 |
if (left_blossom == right_blossom) {
|
| 3019 | 2900 |
_delta3->pop(); |
| 3020 | 2901 |
} else {
|
| 3021 | 2902 |
int left_tree = _tree_set->find(left_blossom); |
| 3022 | 2903 |
int right_tree = _tree_set->find(right_blossom); |
| 3023 | 2904 |
|
| 3024 | 2905 |
if (left_tree == right_tree) {
|
| 3025 | 2906 |
shrinkOnEdge(e, left_tree); |
| 3026 | 2907 |
} else {
|
| 3027 | 2908 |
augmentOnEdge(e); |
| 3028 | 2909 |
unmatched -= 2; |
| 3029 | 2910 |
} |
| 3030 | 2911 |
} |
| 3031 | 2912 |
} break; |
| 3032 | 2913 |
case D4: |
| 3033 | 2914 |
splitBlossom(_delta4->top()); |
| 3034 | 2915 |
break; |
| 3035 | 2916 |
} |
| 3036 | 2917 |
} |
| 3037 | 2918 |
extractMatching(); |
| 3038 | 2919 |
return true; |
| 3039 | 2920 |
} |
| 3040 | 2921 |
|
| 3041 | 2922 |
/// \brief Run the algorithm. |
| 3042 | 2923 |
/// |
| 3043 | 2924 |
/// This method runs the \c %MaxWeightedPerfectMatching algorithm. |
| 3044 | 2925 |
/// |
| 3045 | 2926 |
/// \note mwpm.run() is just a shortcut of the following code. |
| 3046 | 2927 |
/// \code |
| 3047 | 2928 |
/// mwpm.init(); |
| 3048 | 2929 |
/// mwpm.start(); |
| 3049 | 2930 |
/// \endcode |
| 3050 | 2931 |
bool run() {
|
| 3051 | 2932 |
init(); |
| 3052 | 2933 |
return start(); |
| 3053 | 2934 |
} |
| 3054 | 2935 |
|
| 3055 | 2936 |
/// @} |
| 3056 | 2937 |
|
| 3057 | 2938 |
/// \name Primal Solution |
| 3058 | 2939 |
/// Functions to get the primal solution, i.e. the maximum weighted |
| 3059 | 2940 |
/// perfect matching.\n |
| 3060 | 2941 |
/// Either \ref run() or \ref start() function should be called before |
| 3061 | 2942 |
/// using them. |
| 3062 | 2943 |
|
| 3063 | 2944 |
/// @{
|
| 3064 | 2945 |
|
| 3065 | 2946 |
/// \brief Return the weight of the matching. |
| 3066 | 2947 |
/// |
| 3067 | 2948 |
/// This function returns the weight of the found matching. |
| 3068 | 2949 |
/// |
| 3069 | 2950 |
/// \pre Either run() or start() must be called before using this function. |
| 3070 | 2951 |
Value matchingWeight() const {
|
| 3071 | 2952 |
Value sum = 0; |
| 3072 | 2953 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 3073 | 2954 |
if ((*_matching)[n] != INVALID) {
|
| 3074 | 2955 |
sum += _weight[(*_matching)[n]]; |
| 3075 | 2956 |
} |
| 3076 | 2957 |
} |
| 3077 |
return sum / |
|
| 2958 |
return sum / 2; |
|
| 3078 | 2959 |
} |
| 3079 | 2960 |
|
| 3080 | 2961 |
/// \brief Return \c true if the given edge is in the matching. |
| 3081 | 2962 |
/// |
| 3082 | 2963 |
/// This function returns \c true if the given edge is in the found |
| 3083 | 2964 |
/// matching. |
| 3084 | 2965 |
/// |
| 3085 | 2966 |
/// \pre Either run() or start() must be called before using this function. |
| 3086 | 2967 |
bool matching(const Edge& edge) const {
|
| 3087 | 2968 |
return static_cast<const Edge&>((*_matching)[_graph.u(edge)]) == edge; |
| 3088 | 2969 |
} |
| 3089 | 2970 |
|
| 3090 | 2971 |
/// \brief Return the matching arc (or edge) incident to the given node. |
| 3091 | 2972 |
/// |
| 3092 | 2973 |
/// This function returns the matching arc (or edge) incident to the |
| 3093 | 2974 |
/// given node in the found matching or \c INVALID if the node is |
| 3094 | 2975 |
/// not covered by the matching. |
| 3095 | 2976 |
/// |
| 3096 | 2977 |
/// \pre Either run() or start() must be called before using this function. |
| 3097 | 2978 |
Arc matching(const Node& node) const {
|
| 3098 | 2979 |
return (*_matching)[node]; |
| 3099 | 2980 |
} |
| 3100 | 2981 |
|
| 3101 | 2982 |
/// \brief Return a const reference to the matching map. |
| 3102 | 2983 |
/// |
| 3103 | 2984 |
/// This function returns a const reference to a node map that stores |
| 3104 | 2985 |
/// the matching arc (or edge) incident to each node. |
| 3105 | 2986 |
const MatchingMap& matchingMap() const {
|
| 3106 | 2987 |
return *_matching; |
| 3107 | 2988 |
} |
| 3108 | 2989 |
|
| 3109 | 2990 |
/// \brief Return the mate of the given node. |
| 3110 | 2991 |
/// |
| 3111 | 2992 |
/// This function returns the mate of the given node in the found |
| 3112 | 2993 |
/// matching or \c INVALID if the node is not covered by the matching. |
| 3113 | 2994 |
/// |
| 3114 | 2995 |
/// \pre Either run() or start() must be called before using this function. |
| 3115 | 2996 |
Node mate(const Node& node) const {
|
| 3116 | 2997 |
return _graph.target((*_matching)[node]); |
| 3117 | 2998 |
} |
| 3118 | 2999 |
|
| 3119 | 3000 |
/// @} |
| 3120 | 3001 |
|
| 3121 | 3002 |
/// \name Dual Solution |
| 3122 | 3003 |
/// Functions to get the dual solution.\n |
| 3123 | 3004 |
/// Either \ref run() or \ref start() function should be called before |
| 3124 | 3005 |
/// using them. |
| 3125 | 3006 |
|
| ... | ... |
@@ -3196,49 +3077,49 @@ |
| 3196 | 3077 |
/// |
| 3197 | 3078 |
/// \pre Either \ref MaxWeightedPerfectMatching::run() "algorithm.run()" |
| 3198 | 3079 |
/// or \ref MaxWeightedPerfectMatching::start() "algorithm.start()" |
| 3199 | 3080 |
/// must be called before initializing this iterator. |
| 3200 | 3081 |
BlossomIt(const MaxWeightedPerfectMatching& algorithm, int variable) |
| 3201 | 3082 |
: _algorithm(&algorithm) |
| 3202 | 3083 |
{
|
| 3203 | 3084 |
_index = _algorithm->_blossom_potential[variable].begin; |
| 3204 | 3085 |
_last = _algorithm->_blossom_potential[variable].end; |
| 3205 | 3086 |
} |
| 3206 | 3087 |
|
| 3207 | 3088 |
/// \brief Conversion to \c Node. |
| 3208 | 3089 |
/// |
| 3209 | 3090 |
/// Conversion to \c Node. |
| 3210 | 3091 |
operator Node() const {
|
| 3211 | 3092 |
return _algorithm->_blossom_node_list[_index]; |
| 3212 | 3093 |
} |
| 3213 | 3094 |
|
| 3214 | 3095 |
/// \brief Increment operator. |
| 3215 | 3096 |
/// |
| 3216 | 3097 |
/// Increment operator. |
| 3217 | 3098 |
BlossomIt& operator++() {
|
| 3218 | 3099 |
++_index; |
| 3219 | 3100 |
return *this; |
| 3220 | 3101 |
} |
| 3221 | 3102 |
|
| 3222 | 3103 |
/// \brief Validity checking |
| 3223 | 3104 |
/// |
| 3224 | 3105 |
/// This function checks whether the iterator is invalid. |
| 3225 | 3106 |
bool operator==(Invalid) const { return _index == _last; }
|
| 3226 | 3107 |
|
| 3227 | 3108 |
/// \brief Validity checking |
| 3228 | 3109 |
/// |
| 3229 | 3110 |
/// This function checks whether the iterator is valid. |
| 3230 | 3111 |
bool operator!=(Invalid) const { return _index != _last; }
|
| 3231 | 3112 |
|
| 3232 | 3113 |
private: |
| 3233 | 3114 |
const MaxWeightedPerfectMatching* _algorithm; |
| 3234 | 3115 |
int _last; |
| 3235 | 3116 |
int _index; |
| 3236 | 3117 |
}; |
| 3237 | 3118 |
|
| 3238 | 3119 |
/// @} |
| 3239 | 3120 |
|
| 3240 | 3121 |
}; |
| 3241 | 3122 |
|
| 3242 | 3123 |
} //END OF NAMESPACE LEMON |
| 3243 | 3124 |
|
| 3244 |
#endif // |
|
| 3125 |
#endif //LEMON_MATCHING_H |
0 comments (0 inline)