... |
... |
@@ -621,419 +621,438 @@
|
621 |
621 |
}
|
622 |
622 |
|
623 |
623 |
/// \brief Return a const reference to the status map, which stores
|
624 |
624 |
/// the Edmonds-Gallai decomposition.
|
625 |
625 |
///
|
626 |
626 |
/// This function returns a const reference to a node map that stores the
|
627 |
627 |
/// \ref Status "status" of each node in the Edmonds-Gallai decomposition.
|
628 |
628 |
const StatusMap& statusMap() const {
|
629 |
629 |
return *_status;
|
630 |
630 |
}
|
631 |
631 |
|
632 |
632 |
/// \brief Return \c true if the given node is in the barrier.
|
633 |
633 |
///
|
634 |
634 |
/// This function returns \c true if the given node is in the barrier.
|
635 |
635 |
bool barrier(const Node& n) const {
|
636 |
636 |
return (*_status)[n] == ODD;
|
637 |
637 |
}
|
638 |
638 |
|
639 |
639 |
/// @}
|
640 |
640 |
|
641 |
641 |
};
|
642 |
642 |
|
643 |
643 |
/// \ingroup matching
|
644 |
644 |
///
|
645 |
645 |
/// \brief Weighted matching in general graphs
|
646 |
646 |
///
|
647 |
647 |
/// This class provides an efficient implementation of Edmond's
|
648 |
648 |
/// maximum weighted matching algorithm. The implementation is based
|
649 |
649 |
/// on extensive use of priority queues and provides
|
650 |
650 |
/// \f$O(nm\log n)\f$ time complexity.
|
651 |
651 |
///
|
652 |
652 |
/// The maximum weighted matching problem is to find a subset of the
|
653 |
653 |
/// edges in an undirected graph with maximum overall weight for which
|
654 |
654 |
/// each node has at most one incident edge.
|
655 |
655 |
/// It can be formulated with the following linear program.
|
656 |
656 |
/// \f[ \sum_{e \in \delta(u)}x_e \le 1 \quad \forall u\in V\f]
|
657 |
657 |
/** \f[ \sum_{e \in \gamma(B)}x_e \le \frac{\vert B \vert - 1}{2}
|
658 |
658 |
\quad \forall B\in\mathcal{O}\f] */
|
659 |
659 |
/// \f[x_e \ge 0\quad \forall e\in E\f]
|
660 |
660 |
/// \f[\max \sum_{e\in E}x_ew_e\f]
|
661 |
661 |
/// where \f$\delta(X)\f$ is the set of edges incident to a node in
|
662 |
662 |
/// \f$X\f$, \f$\gamma(X)\f$ is the set of edges with both ends in
|
663 |
663 |
/// \f$X\f$ and \f$\mathcal{O}\f$ is the set of odd cardinality
|
664 |
664 |
/// subsets of the nodes.
|
665 |
665 |
///
|
666 |
666 |
/// The algorithm calculates an optimal matching and a proof of the
|
667 |
667 |
/// optimality. The solution of the dual problem can be used to check
|
668 |
668 |
/// the result of the algorithm. The dual linear problem is the
|
669 |
669 |
/// following.
|
670 |
670 |
/** \f[ y_u + y_v + \sum_{B \in \mathcal{O}, uv \in \gamma(B)}
|
671 |
671 |
z_B \ge w_{uv} \quad \forall uv\in E\f] */
|
672 |
672 |
/// \f[y_u \ge 0 \quad \forall u \in V\f]
|
673 |
673 |
/// \f[z_B \ge 0 \quad \forall B \in \mathcal{O}\f]
|
674 |
674 |
/** \f[\min \sum_{u \in V}y_u + \sum_{B \in \mathcal{O}}
|
675 |
675 |
\frac{\vert B \vert - 1}{2}z_B\f] */
|
676 |
676 |
///
|
677 |
677 |
/// The algorithm can be executed with the run() function.
|
678 |
678 |
/// After it the matching (the primal solution) and the dual solution
|
679 |
679 |
/// can be obtained using the query functions and the
|
680 |
680 |
/// \ref MaxWeightedMatching::BlossomIt "BlossomIt" nested class,
|
681 |
681 |
/// which is able to iterate on the nodes of a blossom.
|
682 |
682 |
/// If the value type is integer, then the dual solution is multiplied
|
683 |
683 |
/// by \ref MaxWeightedMatching::dualScale "4".
|
684 |
684 |
///
|
685 |
685 |
/// \tparam GR The undirected graph type the algorithm runs on.
|
686 |
686 |
/// \tparam WM The type edge weight map. The default type is
|
687 |
687 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<int>".
|
688 |
688 |
#ifdef DOXYGEN
|
689 |
689 |
template <typename GR, typename WM>
|
690 |
690 |
#else
|
691 |
691 |
template <typename GR,
|
692 |
692 |
typename WM = typename GR::template EdgeMap<int> >
|
693 |
693 |
#endif
|
694 |
694 |
class MaxWeightedMatching {
|
695 |
695 |
public:
|
696 |
696 |
|
697 |
697 |
/// The graph type of the algorithm
|
698 |
698 |
typedef GR Graph;
|
699 |
699 |
/// The type of the edge weight map
|
700 |
700 |
typedef WM WeightMap;
|
701 |
701 |
/// The value type of the edge weights
|
702 |
702 |
typedef typename WeightMap::Value Value;
|
703 |
703 |
|
704 |
704 |
/// The type of the matching map
|
705 |
705 |
typedef typename Graph::template NodeMap<typename Graph::Arc>
|
706 |
706 |
MatchingMap;
|
707 |
707 |
|
708 |
708 |
/// \brief Scaling factor for dual solution
|
709 |
709 |
///
|
710 |
710 |
/// Scaling factor for dual solution. It is equal to 4 or 1
|
711 |
711 |
/// according to the value type.
|
712 |
712 |
static const int dualScale =
|
713 |
713 |
std::numeric_limits<Value>::is_integer ? 4 : 1;
|
714 |
714 |
|
715 |
715 |
private:
|
716 |
716 |
|
717 |
717 |
TEMPLATE_GRAPH_TYPEDEFS(Graph);
|
718 |
718 |
|
719 |
719 |
typedef typename Graph::template NodeMap<Value> NodePotential;
|
720 |
720 |
typedef std::vector<Node> BlossomNodeList;
|
721 |
721 |
|
722 |
722 |
struct BlossomVariable {
|
723 |
723 |
int begin, end;
|
724 |
724 |
Value value;
|
725 |
725 |
|
726 |
726 |
BlossomVariable(int _begin, int _end, Value _value)
|
727 |
727 |
: begin(_begin), end(_end), value(_value) {}
|
728 |
728 |
|
729 |
729 |
};
|
730 |
730 |
|
731 |
731 |
typedef std::vector<BlossomVariable> BlossomPotential;
|
732 |
732 |
|
733 |
733 |
const Graph& _graph;
|
734 |
734 |
const WeightMap& _weight;
|
735 |
735 |
|
736 |
736 |
MatchingMap* _matching;
|
737 |
737 |
|
738 |
738 |
NodePotential* _node_potential;
|
739 |
739 |
|
740 |
740 |
BlossomPotential _blossom_potential;
|
741 |
741 |
BlossomNodeList _blossom_node_list;
|
742 |
742 |
|
743 |
743 |
int _node_num;
|
744 |
744 |
int _blossom_num;
|
745 |
745 |
|
746 |
746 |
typedef RangeMap<int> IntIntMap;
|
747 |
747 |
|
748 |
748 |
enum Status {
|
749 |
749 |
EVEN = -1, MATCHED = 0, ODD = 1
|
750 |
750 |
};
|
751 |
751 |
|
752 |
752 |
typedef HeapUnionFind<Value, IntNodeMap> BlossomSet;
|
753 |
753 |
struct BlossomData {
|
754 |
754 |
int tree;
|
755 |
755 |
Status status;
|
756 |
756 |
Arc pred, next;
|
757 |
757 |
Value pot, offset;
|
758 |
758 |
Node base;
|
759 |
759 |
};
|
760 |
760 |
|
761 |
761 |
IntNodeMap *_blossom_index;
|
762 |
762 |
BlossomSet *_blossom_set;
|
763 |
763 |
RangeMap<BlossomData>* _blossom_data;
|
764 |
764 |
|
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 |
}
|
896 |
915 |
|
897 |
916 |
if (!_blossom_set->trivial(blossom)) {
|
898 |
917 |
(*_blossom_data)[blossom].pot -=
|
899 |
918 |
2 * (_delta_sum - (*_blossom_data)[blossom].offset);
|
900 |
919 |
}
|
901 |
920 |
|
902 |
921 |
for (typename BlossomSet::ItemIt n(*_blossom_set, blossom);
|
903 |
922 |
n != INVALID; ++n) {
|
904 |
923 |
|
905 |
924 |
_blossom_set->increase(n, std::numeric_limits<Value>::max());
|
906 |
925 |
int ni = (*_node_index)[n];
|
907 |
926 |
|
908 |
927 |
(*_node_data)[ni].heap.clear();
|
909 |
928 |
(*_node_data)[ni].heap_index.clear();
|
910 |
929 |
|
911 |
930 |
(*_node_data)[ni].pot += _delta_sum - (*_blossom_data)[blossom].offset;
|
912 |
931 |
|
913 |
932 |
_delta1->push(n, (*_node_data)[ni].pot);
|
914 |
933 |
|
915 |
934 |
for (InArcIt e(_graph, n); e != INVALID; ++e) {
|
916 |
935 |
Node v = _graph.source(e);
|
917 |
936 |
int vb = _blossom_set->find(v);
|
918 |
937 |
int vi = (*_node_index)[v];
|
919 |
938 |
|
920 |
939 |
Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot -
|
921 |
940 |
dualScale * _weight[e];
|
922 |
941 |
|
923 |
942 |
if ((*_blossom_data)[vb].status == EVEN) {
|
924 |
943 |
if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) {
|
925 |
944 |
_delta3->push(e, rw / 2);
|
926 |
945 |
}
|
927 |
946 |
} else {
|
928 |
947 |
typename std::map<int, Arc>::iterator it =
|
929 |
948 |
(*_node_data)[vi].heap_index.find(tree);
|
930 |
949 |
|
931 |
950 |
if (it != (*_node_data)[vi].heap_index.end()) {
|
932 |
951 |
if ((*_node_data)[vi].heap[it->second] > rw) {
|
933 |
952 |
(*_node_data)[vi].heap.replace(it->second, e);
|
934 |
953 |
(*_node_data)[vi].heap.decrease(e, rw);
|
935 |
954 |
it->second = e;
|
936 |
955 |
}
|
937 |
956 |
} else {
|
938 |
957 |
(*_node_data)[vi].heap.push(e, rw);
|
939 |
958 |
(*_node_data)[vi].heap_index.insert(std::make_pair(tree, e));
|
940 |
959 |
}
|
941 |
960 |
|
942 |
961 |
if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) {
|
943 |
962 |
_blossom_set->decrease(v, (*_node_data)[vi].heap.prio());
|
944 |
963 |
|
945 |
964 |
if ((*_blossom_data)[vb].status == MATCHED) {
|
946 |
965 |
if (_delta2->state(vb) != _delta2->IN_HEAP) {
|
947 |
966 |
_delta2->push(vb, _blossom_set->classPrio(vb) -
|
948 |
967 |
(*_blossom_data)[vb].offset);
|
949 |
968 |
} else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) -
|
950 |
969 |
(*_blossom_data)[vb].offset) {
|
951 |
970 |
_delta2->decrease(vb, _blossom_set->classPrio(vb) -
|
952 |
971 |
(*_blossom_data)[vb].offset);
|
953 |
972 |
}
|
954 |
973 |
}
|
955 |
974 |
}
|
956 |
975 |
}
|
957 |
976 |
}
|
958 |
977 |
}
|
959 |
978 |
(*_blossom_data)[blossom].offset = 0;
|
960 |
979 |
}
|
961 |
980 |
|
962 |
981 |
void matchedToOdd(int blossom) {
|
963 |
982 |
if (_delta2->state(blossom) == _delta2->IN_HEAP) {
|
964 |
983 |
_delta2->erase(blossom);
|
965 |
984 |
}
|
966 |
985 |
(*_blossom_data)[blossom].offset += _delta_sum;
|
967 |
986 |
if (!_blossom_set->trivial(blossom)) {
|
968 |
987 |
_delta4->push(blossom, (*_blossom_data)[blossom].pot / 2 +
|
969 |
988 |
(*_blossom_data)[blossom].offset);
|
970 |
989 |
}
|
971 |
990 |
}
|
972 |
991 |
|
973 |
992 |
void evenToMatched(int blossom, int tree) {
|
974 |
993 |
if (!_blossom_set->trivial(blossom)) {
|
975 |
994 |
(*_blossom_data)[blossom].pot += 2 * _delta_sum;
|
976 |
995 |
}
|
977 |
996 |
|
978 |
997 |
for (typename BlossomSet::ItemIt n(*_blossom_set, blossom);
|
979 |
998 |
n != INVALID; ++n) {
|
980 |
999 |
int ni = (*_node_index)[n];
|
981 |
1000 |
(*_node_data)[ni].pot -= _delta_sum;
|
982 |
1001 |
|
983 |
1002 |
_delta1->erase(n);
|
984 |
1003 |
|
985 |
1004 |
for (InArcIt e(_graph, n); e != INVALID; ++e) {
|
986 |
1005 |
Node v = _graph.source(e);
|
987 |
1006 |
int vb = _blossom_set->find(v);
|
988 |
1007 |
int vi = (*_node_index)[v];
|
989 |
1008 |
|
990 |
1009 |
Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot -
|
991 |
1010 |
dualScale * _weight[e];
|
992 |
1011 |
|
993 |
1012 |
if (vb == blossom) {
|
994 |
1013 |
if (_delta3->state(e) == _delta3->IN_HEAP) {
|
995 |
1014 |
_delta3->erase(e);
|
996 |
1015 |
}
|
997 |
1016 |
} else if ((*_blossom_data)[vb].status == EVEN) {
|
998 |
1017 |
|
999 |
1018 |
if (_delta3->state(e) == _delta3->IN_HEAP) {
|
1000 |
1019 |
_delta3->erase(e);
|
1001 |
1020 |
}
|
1002 |
1021 |
|
1003 |
1022 |
int vt = _tree_set->find(vb);
|
1004 |
1023 |
|
1005 |
1024 |
if (vt != tree) {
|
1006 |
1025 |
|
1007 |
1026 |
Arc r = _graph.oppositeArc(e);
|
1008 |
1027 |
|
1009 |
1028 |
typename std::map<int, Arc>::iterator it =
|
1010 |
1029 |
(*_node_data)[ni].heap_index.find(vt);
|
1011 |
1030 |
|
1012 |
1031 |
if (it != (*_node_data)[ni].heap_index.end()) {
|
1013 |
1032 |
if ((*_node_data)[ni].heap[it->second] > rw) {
|
1014 |
1033 |
(*_node_data)[ni].heap.replace(it->second, r);
|
1015 |
1034 |
(*_node_data)[ni].heap.decrease(r, rw);
|
1016 |
1035 |
it->second = r;
|
1017 |
1036 |
}
|
1018 |
1037 |
} else {
|
1019 |
1038 |
(*_node_data)[ni].heap.push(r, rw);
|
1020 |
1039 |
(*_node_data)[ni].heap_index.insert(std::make_pair(vt, r));
|
1021 |
1040 |
}
|
1022 |
1041 |
|
1023 |
1042 |
if ((*_blossom_set)[n] > (*_node_data)[ni].heap.prio()) {
|
1024 |
1043 |
_blossom_set->decrease(n, (*_node_data)[ni].heap.prio());
|
1025 |
1044 |
|
1026 |
1045 |
if (_delta2->state(blossom) != _delta2->IN_HEAP) {
|
1027 |
1046 |
_delta2->push(blossom, _blossom_set->classPrio(blossom) -
|
1028 |
1047 |
(*_blossom_data)[blossom].offset);
|
1029 |
1048 |
} else if ((*_delta2)[blossom] >
|
1030 |
1049 |
_blossom_set->classPrio(blossom) -
|
1031 |
1050 |
(*_blossom_data)[blossom].offset){
|
1032 |
1051 |
_delta2->decrease(blossom, _blossom_set->classPrio(blossom) -
|
1033 |
1052 |
(*_blossom_data)[blossom].offset);
|
1034 |
1053 |
}
|
1035 |
1054 |
}
|
1036 |
1055 |
}
|
1037 |
1056 |
} else {
|
1038 |
1057 |
|
1039 |
1058 |
typename std::map<int, Arc>::iterator it =
|
... |
... |
@@ -1399,410 +1418,422 @@
|
1399 |
1418 |
if (!_blossom_set->trivial(subblossoms[i])) {
|
1400 |
1419 |
(*_blossom_data)[subblossoms[i]].pot -= 2 * offset;
|
1401 |
1420 |
}
|
1402 |
1421 |
if (_blossom_set->classPrio(subblossoms[i]) !=
|
1403 |
1422 |
std::numeric_limits<Value>::max()) {
|
1404 |
1423 |
_delta2->push(subblossoms[i],
|
1405 |
1424 |
_blossom_set->classPrio(subblossoms[i]) -
|
1406 |
1425 |
(*_blossom_data)[subblossoms[i]].offset);
|
1407 |
1426 |
}
|
1408 |
1427 |
}
|
1409 |
1428 |
|
1410 |
1429 |
if (id > ib ? ((id - ib) % 2 == 0) : ((ib - id) % 2 == 1)) {
|
1411 |
1430 |
for (int i = (id + 1) % subblossoms.size();
|
1412 |
1431 |
i != ib; i = (i + 2) % subblossoms.size()) {
|
1413 |
1432 |
int sb = subblossoms[i];
|
1414 |
1433 |
int tb = subblossoms[(i + 1) % subblossoms.size()];
|
1415 |
1434 |
(*_blossom_data)[sb].next =
|
1416 |
1435 |
_graph.oppositeArc((*_blossom_data)[tb].next);
|
1417 |
1436 |
}
|
1418 |
1437 |
|
1419 |
1438 |
for (int i = ib; i != id; i = (i + 2) % subblossoms.size()) {
|
1420 |
1439 |
int sb = subblossoms[i];
|
1421 |
1440 |
int tb = subblossoms[(i + 1) % subblossoms.size()];
|
1422 |
1441 |
int ub = subblossoms[(i + 2) % subblossoms.size()];
|
1423 |
1442 |
|
1424 |
1443 |
(*_blossom_data)[sb].status = ODD;
|
1425 |
1444 |
matchedToOdd(sb);
|
1426 |
1445 |
_tree_set->insert(sb, tree);
|
1427 |
1446 |
(*_blossom_data)[sb].pred = pred;
|
1428 |
1447 |
(*_blossom_data)[sb].next =
|
1429 |
1448 |
_graph.oppositeArc((*_blossom_data)[tb].next);
|
1430 |
1449 |
|
1431 |
1450 |
pred = (*_blossom_data)[ub].next;
|
1432 |
1451 |
|
1433 |
1452 |
(*_blossom_data)[tb].status = EVEN;
|
1434 |
1453 |
matchedToEven(tb, tree);
|
1435 |
1454 |
_tree_set->insert(tb, tree);
|
1436 |
1455 |
(*_blossom_data)[tb].pred = (*_blossom_data)[tb].next;
|
1437 |
1456 |
}
|
1438 |
1457 |
|
1439 |
1458 |
(*_blossom_data)[subblossoms[id]].status = ODD;
|
1440 |
1459 |
matchedToOdd(subblossoms[id]);
|
1441 |
1460 |
_tree_set->insert(subblossoms[id], tree);
|
1442 |
1461 |
(*_blossom_data)[subblossoms[id]].next = next;
|
1443 |
1462 |
(*_blossom_data)[subblossoms[id]].pred = pred;
|
1444 |
1463 |
|
1445 |
1464 |
} else {
|
1446 |
1465 |
|
1447 |
1466 |
for (int i = (ib + 1) % subblossoms.size();
|
1448 |
1467 |
i != id; i = (i + 2) % subblossoms.size()) {
|
1449 |
1468 |
int sb = subblossoms[i];
|
1450 |
1469 |
int tb = subblossoms[(i + 1) % subblossoms.size()];
|
1451 |
1470 |
(*_blossom_data)[sb].next =
|
1452 |
1471 |
_graph.oppositeArc((*_blossom_data)[tb].next);
|
1453 |
1472 |
}
|
1454 |
1473 |
|
1455 |
1474 |
for (int i = id; i != ib; i = (i + 2) % subblossoms.size()) {
|
1456 |
1475 |
int sb = subblossoms[i];
|
1457 |
1476 |
int tb = subblossoms[(i + 1) % subblossoms.size()];
|
1458 |
1477 |
int ub = subblossoms[(i + 2) % subblossoms.size()];
|
1459 |
1478 |
|
1460 |
1479 |
(*_blossom_data)[sb].status = ODD;
|
1461 |
1480 |
matchedToOdd(sb);
|
1462 |
1481 |
_tree_set->insert(sb, tree);
|
1463 |
1482 |
(*_blossom_data)[sb].next = next;
|
1464 |
1483 |
(*_blossom_data)[sb].pred =
|
1465 |
1484 |
_graph.oppositeArc((*_blossom_data)[tb].next);
|
1466 |
1485 |
|
1467 |
1486 |
(*_blossom_data)[tb].status = EVEN;
|
1468 |
1487 |
matchedToEven(tb, tree);
|
1469 |
1488 |
_tree_set->insert(tb, tree);
|
1470 |
1489 |
(*_blossom_data)[tb].pred =
|
1471 |
1490 |
(*_blossom_data)[tb].next =
|
1472 |
1491 |
_graph.oppositeArc((*_blossom_data)[ub].next);
|
1473 |
1492 |
next = (*_blossom_data)[ub].next;
|
1474 |
1493 |
}
|
1475 |
1494 |
|
1476 |
1495 |
(*_blossom_data)[subblossoms[ib]].status = ODD;
|
1477 |
1496 |
matchedToOdd(subblossoms[ib]);
|
1478 |
1497 |
_tree_set->insert(subblossoms[ib], tree);
|
1479 |
1498 |
(*_blossom_data)[subblossoms[ib]].next = next;
|
1480 |
1499 |
(*_blossom_data)[subblossoms[ib]].pred = pred;
|
1481 |
1500 |
}
|
1482 |
1501 |
_tree_set->erase(blossom);
|
1483 |
1502 |
}
|
1484 |
1503 |
|
1485 |
1504 |
void extractBlossom(int blossom, const Node& base, const Arc& matching) {
|
1486 |
1505 |
if (_blossom_set->trivial(blossom)) {
|
1487 |
1506 |
int bi = (*_node_index)[base];
|
1488 |
1507 |
Value pot = (*_node_data)[bi].pot;
|
1489 |
1508 |
|
1490 |
1509 |
(*_matching)[base] = matching;
|
1491 |
1510 |
_blossom_node_list.push_back(base);
|
1492 |
1511 |
(*_node_potential)[base] = pot;
|
1493 |
1512 |
} else {
|
1494 |
1513 |
|
1495 |
1514 |
Value pot = (*_blossom_data)[blossom].pot;
|
1496 |
1515 |
int bn = _blossom_node_list.size();
|
1497 |
1516 |
|
1498 |
1517 |
std::vector<int> subblossoms;
|
1499 |
1518 |
_blossom_set->split(blossom, std::back_inserter(subblossoms));
|
1500 |
1519 |
int b = _blossom_set->find(base);
|
1501 |
1520 |
int ib = -1;
|
1502 |
1521 |
for (int i = 0; i < int(subblossoms.size()); ++i) {
|
1503 |
1522 |
if (subblossoms[i] == b) { ib = i; break; }
|
1504 |
1523 |
}
|
1505 |
1524 |
|
1506 |
1525 |
for (int i = 1; i < int(subblossoms.size()); i += 2) {
|
1507 |
1526 |
int sb = subblossoms[(ib + i) % subblossoms.size()];
|
1508 |
1527 |
int tb = subblossoms[(ib + i + 1) % subblossoms.size()];
|
1509 |
1528 |
|
1510 |
1529 |
Arc m = (*_blossom_data)[tb].next;
|
1511 |
1530 |
extractBlossom(sb, _graph.target(m), _graph.oppositeArc(m));
|
1512 |
1531 |
extractBlossom(tb, _graph.source(m), m);
|
1513 |
1532 |
}
|
1514 |
1533 |
extractBlossom(subblossoms[ib], base, matching);
|
1515 |
1534 |
|
1516 |
1535 |
int en = _blossom_node_list.size();
|
1517 |
1536 |
|
1518 |
1537 |
_blossom_potential.push_back(BlossomVariable(bn, en, pot));
|
1519 |
1538 |
}
|
1520 |
1539 |
}
|
1521 |
1540 |
|
1522 |
1541 |
void extractMatching() {
|
1523 |
1542 |
std::vector<int> blossoms;
|
1524 |
1543 |
for (typename BlossomSet::ClassIt c(*_blossom_set); c != INVALID; ++c) {
|
1525 |
1544 |
blossoms.push_back(c);
|
1526 |
1545 |
}
|
1527 |
1546 |
|
1528 |
1547 |
for (int i = 0; i < int(blossoms.size()); ++i) {
|
1529 |
1548 |
if ((*_blossom_data)[blossoms[i]].next != INVALID) {
|
1530 |
1549 |
|
1531 |
1550 |
Value offset = (*_blossom_data)[blossoms[i]].offset;
|
1532 |
1551 |
(*_blossom_data)[blossoms[i]].pot += 2 * offset;
|
1533 |
1552 |
for (typename BlossomSet::ItemIt n(*_blossom_set, blossoms[i]);
|
1534 |
1553 |
n != INVALID; ++n) {
|
1535 |
1554 |
(*_node_data)[(*_node_index)[n]].pot -= offset;
|
1536 |
1555 |
}
|
1537 |
1556 |
|
1538 |
1557 |
Arc matching = (*_blossom_data)[blossoms[i]].next;
|
1539 |
1558 |
Node base = _graph.source(matching);
|
1540 |
1559 |
extractBlossom(blossoms[i], base, matching);
|
1541 |
1560 |
} else {
|
1542 |
1561 |
Node base = (*_blossom_data)[blossoms[i]].base;
|
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;
|
1665 |
1696 |
}
|
1666 |
1697 |
|
1667 |
1698 |
_unmatched = 0;
|
1668 |
1699 |
|
1669 |
1700 |
int index = 0;
|
1670 |
1701 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
1671 |
1702 |
Value pot = _fractional->nodeValue(n);
|
1672 |
1703 |
(*_node_index)[n] = index;
|
1673 |
1704 |
(*_node_data)[index].pot = pot;
|
1674 |
1705 |
int blossom =
|
1675 |
1706 |
_blossom_set->insert(n, std::numeric_limits<Value>::max());
|
1676 |
1707 |
|
1677 |
1708 |
(*_blossom_data)[blossom].status = MATCHED;
|
1678 |
1709 |
(*_blossom_data)[blossom].pred = INVALID;
|
1679 |
1710 |
(*_blossom_data)[blossom].next = _fractional->matching(n);
|
1680 |
1711 |
if (_fractional->matching(n) == INVALID) {
|
1681 |
1712 |
(*_blossom_data)[blossom].base = n;
|
1682 |
1713 |
}
|
1683 |
1714 |
(*_blossom_data)[blossom].pot = 0;
|
1684 |
1715 |
(*_blossom_data)[blossom].offset = 0;
|
1685 |
1716 |
++index;
|
1686 |
1717 |
}
|
1687 |
1718 |
|
1688 |
1719 |
typename Graph::template NodeMap<bool> processed(_graph, false);
|
1689 |
1720 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
1690 |
1721 |
if (processed[n]) continue;
|
1691 |
1722 |
processed[n] = true;
|
1692 |
1723 |
if (_fractional->matching(n) == INVALID) continue;
|
1693 |
1724 |
int num = 1;
|
1694 |
1725 |
Node v = _graph.target(_fractional->matching(n));
|
1695 |
1726 |
while (n != v) {
|
1696 |
1727 |
processed[v] = true;
|
1697 |
1728 |
v = _graph.target(_fractional->matching(v));
|
1698 |
1729 |
++num;
|
1699 |
1730 |
}
|
1700 |
1731 |
|
1701 |
1732 |
if (num % 2 == 1) {
|
1702 |
1733 |
std::vector<int> subblossoms(num);
|
1703 |
1734 |
|
1704 |
1735 |
subblossoms[--num] = _blossom_set->find(n);
|
1705 |
1736 |
_delta1->push(n, _fractional->nodeValue(n));
|
1706 |
1737 |
v = _graph.target(_fractional->matching(n));
|
1707 |
1738 |
while (n != v) {
|
1708 |
1739 |
subblossoms[--num] = _blossom_set->find(v);
|
1709 |
1740 |
_delta1->push(v, _fractional->nodeValue(v));
|
1710 |
1741 |
v = _graph.target(_fractional->matching(v));
|
1711 |
1742 |
}
|
1712 |
1743 |
|
1713 |
1744 |
int surface =
|
1714 |
1745 |
_blossom_set->join(subblossoms.begin(), subblossoms.end());
|
1715 |
1746 |
(*_blossom_data)[surface].status = EVEN;
|
1716 |
1747 |
(*_blossom_data)[surface].pred = INVALID;
|
1717 |
1748 |
(*_blossom_data)[surface].next = INVALID;
|
1718 |
1749 |
(*_blossom_data)[surface].pot = 0;
|
1719 |
1750 |
(*_blossom_data)[surface].offset = 0;
|
1720 |
1751 |
|
1721 |
1752 |
_tree_set->insert(surface);
|
1722 |
1753 |
++_unmatched;
|
1723 |
1754 |
}
|
1724 |
1755 |
}
|
1725 |
1756 |
|
1726 |
1757 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
1727 |
1758 |
int si = (*_node_index)[_graph.u(e)];
|
1728 |
1759 |
int sb = _blossom_set->find(_graph.u(e));
|
1729 |
1760 |
int ti = (*_node_index)[_graph.v(e)];
|
1730 |
1761 |
int tb = _blossom_set->find(_graph.v(e));
|
1731 |
1762 |
if ((*_blossom_data)[sb].status == EVEN &&
|
1732 |
1763 |
(*_blossom_data)[tb].status == EVEN && sb != tb) {
|
1733 |
1764 |
_delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot -
|
1734 |
1765 |
dualScale * _weight[e]) / 2);
|
1735 |
1766 |
}
|
1736 |
1767 |
}
|
1737 |
1768 |
|
1738 |
1769 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
1739 |
1770 |
int nb = _blossom_set->find(n);
|
1740 |
1771 |
if ((*_blossom_data)[nb].status != MATCHED) continue;
|
1741 |
1772 |
int ni = (*_node_index)[n];
|
1742 |
1773 |
|
1743 |
1774 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
1744 |
1775 |
Node v = _graph.target(e);
|
1745 |
1776 |
int vb = _blossom_set->find(v);
|
1746 |
1777 |
int vi = (*_node_index)[v];
|
1747 |
1778 |
|
1748 |
1779 |
Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot -
|
1749 |
1780 |
dualScale * _weight[e];
|
1750 |
1781 |
|
1751 |
1782 |
if ((*_blossom_data)[vb].status == EVEN) {
|
1752 |
1783 |
|
1753 |
1784 |
int vt = _tree_set->find(vb);
|
1754 |
1785 |
|
1755 |
1786 |
typename std::map<int, Arc>::iterator it =
|
1756 |
1787 |
(*_node_data)[ni].heap_index.find(vt);
|
1757 |
1788 |
|
1758 |
1789 |
if (it != (*_node_data)[ni].heap_index.end()) {
|
1759 |
1790 |
if ((*_node_data)[ni].heap[it->second] > rw) {
|
1760 |
1791 |
(*_node_data)[ni].heap.replace(it->second, e);
|
1761 |
1792 |
(*_node_data)[ni].heap.decrease(e, rw);
|
1762 |
1793 |
it->second = e;
|
1763 |
1794 |
}
|
1764 |
1795 |
} else {
|
1765 |
1796 |
(*_node_data)[ni].heap.push(e, rw);
|
1766 |
1797 |
(*_node_data)[ni].heap_index.insert(std::make_pair(vt, e));
|
1767 |
1798 |
}
|
1768 |
1799 |
}
|
1769 |
1800 |
}
|
1770 |
1801 |
|
1771 |
1802 |
if (!(*_node_data)[ni].heap.empty()) {
|
1772 |
1803 |
_blossom_set->decrease(n, (*_node_data)[ni].heap.prio());
|
1773 |
1804 |
_delta2->push(nb, _blossom_set->classPrio(nb));
|
1774 |
1805 |
}
|
1775 |
1806 |
}
|
1776 |
1807 |
}
|
1777 |
1808 |
|
1778 |
1809 |
/// \brief Start the algorithm
|
1779 |
1810 |
///
|
1780 |
1811 |
/// This function starts the algorithm.
|
1781 |
1812 |
///
|
1782 |
1813 |
/// \pre \ref init() or \ref fractionalInit() must be called
|
1783 |
1814 |
/// before using this function.
|
1784 |
1815 |
void start() {
|
1785 |
1816 |
enum OpType {
|
1786 |
1817 |
D1, D2, D3, D4
|
1787 |
1818 |
};
|
1788 |
1819 |
|
1789 |
1820 |
while (_unmatched > 0) {
|
1790 |
1821 |
Value d1 = !_delta1->empty() ?
|
1791 |
1822 |
_delta1->prio() : std::numeric_limits<Value>::max();
|
1792 |
1823 |
|
1793 |
1824 |
Value d2 = !_delta2->empty() ?
|
1794 |
1825 |
_delta2->prio() : std::numeric_limits<Value>::max();
|
1795 |
1826 |
|
1796 |
1827 |
Value d3 = !_delta3->empty() ?
|
1797 |
1828 |
_delta3->prio() : std::numeric_limits<Value>::max();
|
1798 |
1829 |
|
1799 |
1830 |
Value d4 = !_delta4->empty() ?
|
1800 |
1831 |
_delta4->prio() : std::numeric_limits<Value>::max();
|
1801 |
1832 |
|
1802 |
1833 |
_delta_sum = d3; OpType ot = D3;
|
1803 |
1834 |
if (d1 < _delta_sum) { _delta_sum = d1; ot = D1; }
|
1804 |
1835 |
if (d2 < _delta_sum) { _delta_sum = d2; ot = D2; }
|
1805 |
1836 |
if (d4 < _delta_sum) { _delta_sum = d4; ot = D4; }
|
1806 |
1837 |
|
1807 |
1838 |
switch (ot) {
|
1808 |
1839 |
case D1:
|
... |
... |
@@ -2048,415 +2079,433 @@
|
2048 |
2079 |
/// Increment operator.
|
2049 |
2080 |
BlossomIt& operator++() {
|
2050 |
2081 |
++_index;
|
2051 |
2082 |
return *this;
|
2052 |
2083 |
}
|
2053 |
2084 |
|
2054 |
2085 |
/// \brief Validity checking
|
2055 |
2086 |
///
|
2056 |
2087 |
/// Checks whether the iterator is invalid.
|
2057 |
2088 |
bool operator==(Invalid) const { return _index == _last; }
|
2058 |
2089 |
|
2059 |
2090 |
/// \brief Validity checking
|
2060 |
2091 |
///
|
2061 |
2092 |
/// Checks whether the iterator is valid.
|
2062 |
2093 |
bool operator!=(Invalid) const { return _index != _last; }
|
2063 |
2094 |
|
2064 |
2095 |
private:
|
2065 |
2096 |
const MaxWeightedMatching* _algorithm;
|
2066 |
2097 |
int _last;
|
2067 |
2098 |
int _index;
|
2068 |
2099 |
};
|
2069 |
2100 |
|
2070 |
2101 |
/// @}
|
2071 |
2102 |
|
2072 |
2103 |
};
|
2073 |
2104 |
|
2074 |
2105 |
/// \ingroup matching
|
2075 |
2106 |
///
|
2076 |
2107 |
/// \brief Weighted perfect matching in general graphs
|
2077 |
2108 |
///
|
2078 |
2109 |
/// This class provides an efficient implementation of Edmond's
|
2079 |
2110 |
/// maximum weighted perfect matching algorithm. The implementation
|
2080 |
2111 |
/// is based on extensive use of priority queues and provides
|
2081 |
2112 |
/// \f$O(nm\log n)\f$ time complexity.
|
2082 |
2113 |
///
|
2083 |
2114 |
/// The maximum weighted perfect matching problem is to find a subset of
|
2084 |
2115 |
/// the edges in an undirected graph with maximum overall weight for which
|
2085 |
2116 |
/// each node has exactly one incident edge.
|
2086 |
2117 |
/// It can be formulated with the following linear program.
|
2087 |
2118 |
/// \f[ \sum_{e \in \delta(u)}x_e = 1 \quad \forall u\in V\f]
|
2088 |
2119 |
/** \f[ \sum_{e \in \gamma(B)}x_e \le \frac{\vert B \vert - 1}{2}
|
2089 |
2120 |
\quad \forall B\in\mathcal{O}\f] */
|
2090 |
2121 |
/// \f[x_e \ge 0\quad \forall e\in E\f]
|
2091 |
2122 |
/// \f[\max \sum_{e\in E}x_ew_e\f]
|
2092 |
2123 |
/// where \f$\delta(X)\f$ is the set of edges incident to a node in
|
2093 |
2124 |
/// \f$X\f$, \f$\gamma(X)\f$ is the set of edges with both ends in
|
2094 |
2125 |
/// \f$X\f$ and \f$\mathcal{O}\f$ is the set of odd cardinality
|
2095 |
2126 |
/// subsets of the nodes.
|
2096 |
2127 |
///
|
2097 |
2128 |
/// The algorithm calculates an optimal matching and a proof of the
|
2098 |
2129 |
/// optimality. The solution of the dual problem can be used to check
|
2099 |
2130 |
/// the result of the algorithm. The dual linear problem is the
|
2100 |
2131 |
/// following.
|
2101 |
2132 |
/** \f[ y_u + y_v + \sum_{B \in \mathcal{O}, uv \in \gamma(B)}z_B \ge
|
2102 |
2133 |
w_{uv} \quad \forall uv\in E\f] */
|
2103 |
2134 |
/// \f[z_B \ge 0 \quad \forall B \in \mathcal{O}\f]
|
2104 |
2135 |
/** \f[\min \sum_{u \in V}y_u + \sum_{B \in \mathcal{O}}
|
2105 |
2136 |
\frac{\vert B \vert - 1}{2}z_B\f] */
|
2106 |
2137 |
///
|
2107 |
2138 |
/// The algorithm can be executed with the run() function.
|
2108 |
2139 |
/// After it the matching (the primal solution) and the dual solution
|
2109 |
2140 |
/// can be obtained using the query functions and the
|
2110 |
2141 |
/// \ref MaxWeightedPerfectMatching::BlossomIt "BlossomIt" nested class,
|
2111 |
2142 |
/// which is able to iterate on the nodes of a blossom.
|
2112 |
2143 |
/// If the value type is integer, then the dual solution is multiplied
|
2113 |
2144 |
/// by \ref MaxWeightedMatching::dualScale "4".
|
2114 |
2145 |
///
|
2115 |
2146 |
/// \tparam GR The undirected graph type the algorithm runs on.
|
2116 |
2147 |
/// \tparam WM The type edge weight map. The default type is
|
2117 |
2148 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<int>".
|
2118 |
2149 |
#ifdef DOXYGEN
|
2119 |
2150 |
template <typename GR, typename WM>
|
2120 |
2151 |
#else
|
2121 |
2152 |
template <typename GR,
|
2122 |
2153 |
typename WM = typename GR::template EdgeMap<int> >
|
2123 |
2154 |
#endif
|
2124 |
2155 |
class MaxWeightedPerfectMatching {
|
2125 |
2156 |
public:
|
2126 |
2157 |
|
2127 |
2158 |
/// The graph type of the algorithm
|
2128 |
2159 |
typedef GR Graph;
|
2129 |
2160 |
/// The type of the edge weight map
|
2130 |
2161 |
typedef WM WeightMap;
|
2131 |
2162 |
/// The value type of the edge weights
|
2132 |
2163 |
typedef typename WeightMap::Value Value;
|
2133 |
2164 |
|
2134 |
2165 |
/// \brief Scaling factor for dual solution
|
2135 |
2166 |
///
|
2136 |
2167 |
/// Scaling factor for dual solution, it is equal to 4 or 1
|
2137 |
2168 |
/// according to the value type.
|
2138 |
2169 |
static const int dualScale =
|
2139 |
2170 |
std::numeric_limits<Value>::is_integer ? 4 : 1;
|
2140 |
2171 |
|
2141 |
2172 |
/// The type of the matching map
|
2142 |
2173 |
typedef typename Graph::template NodeMap<typename Graph::Arc>
|
2143 |
2174 |
MatchingMap;
|
2144 |
2175 |
|
2145 |
2176 |
private:
|
2146 |
2177 |
|
2147 |
2178 |
TEMPLATE_GRAPH_TYPEDEFS(Graph);
|
2148 |
2179 |
|
2149 |
2180 |
typedef typename Graph::template NodeMap<Value> NodePotential;
|
2150 |
2181 |
typedef std::vector<Node> BlossomNodeList;
|
2151 |
2182 |
|
2152 |
2183 |
struct BlossomVariable {
|
2153 |
2184 |
int begin, end;
|
2154 |
2185 |
Value value;
|
2155 |
2186 |
|
2156 |
2187 |
BlossomVariable(int _begin, int _end, Value _value)
|
2157 |
2188 |
: begin(_begin), end(_end), value(_value) {}
|
2158 |
2189 |
|
2159 |
2190 |
};
|
2160 |
2191 |
|
2161 |
2192 |
typedef std::vector<BlossomVariable> BlossomPotential;
|
2162 |
2193 |
|
2163 |
2194 |
const Graph& _graph;
|
2164 |
2195 |
const WeightMap& _weight;
|
2165 |
2196 |
|
2166 |
2197 |
MatchingMap* _matching;
|
2167 |
2198 |
|
2168 |
2199 |
NodePotential* _node_potential;
|
2169 |
2200 |
|
2170 |
2201 |
BlossomPotential _blossom_potential;
|
2171 |
2202 |
BlossomNodeList _blossom_node_list;
|
2172 |
2203 |
|
2173 |
2204 |
int _node_num;
|
2174 |
2205 |
int _blossom_num;
|
2175 |
2206 |
|
2176 |
2207 |
typedef RangeMap<int> IntIntMap;
|
2177 |
2208 |
|
2178 |
2209 |
enum Status {
|
2179 |
2210 |
EVEN = -1, MATCHED = 0, ODD = 1
|
2180 |
2211 |
};
|
2181 |
2212 |
|
2182 |
2213 |
typedef HeapUnionFind<Value, IntNodeMap> BlossomSet;
|
2183 |
2214 |
struct BlossomData {
|
2184 |
2215 |
int tree;
|
2185 |
2216 |
Status status;
|
2186 |
2217 |
Arc pred, next;
|
2187 |
2218 |
Value pot, offset;
|
2188 |
2219 |
};
|
2189 |
2220 |
|
2190 |
2221 |
IntNodeMap *_blossom_index;
|
2191 |
2222 |
BlossomSet *_blossom_set;
|
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);
|
2319 |
2368 |
}
|
2320 |
2369 |
|
2321 |
2370 |
for (typename BlossomSet::ItemIt n(*_blossom_set, blossom);
|
2322 |
2371 |
n != INVALID; ++n) {
|
2323 |
2372 |
|
2324 |
2373 |
_blossom_set->increase(n, std::numeric_limits<Value>::max());
|
2325 |
2374 |
int ni = (*_node_index)[n];
|
2326 |
2375 |
|
2327 |
2376 |
(*_node_data)[ni].heap.clear();
|
2328 |
2377 |
(*_node_data)[ni].heap_index.clear();
|
2329 |
2378 |
|
2330 |
2379 |
(*_node_data)[ni].pot += _delta_sum - (*_blossom_data)[blossom].offset;
|
2331 |
2380 |
|
2332 |
2381 |
for (InArcIt e(_graph, n); e != INVALID; ++e) {
|
2333 |
2382 |
Node v = _graph.source(e);
|
2334 |
2383 |
int vb = _blossom_set->find(v);
|
2335 |
2384 |
int vi = (*_node_index)[v];
|
2336 |
2385 |
|
2337 |
2386 |
Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot -
|
2338 |
2387 |
dualScale * _weight[e];
|
2339 |
2388 |
|
2340 |
2389 |
if ((*_blossom_data)[vb].status == EVEN) {
|
2341 |
2390 |
if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) {
|
2342 |
2391 |
_delta3->push(e, rw / 2);
|
2343 |
2392 |
}
|
2344 |
2393 |
} else {
|
2345 |
2394 |
typename std::map<int, Arc>::iterator it =
|
2346 |
2395 |
(*_node_data)[vi].heap_index.find(tree);
|
2347 |
2396 |
|
2348 |
2397 |
if (it != (*_node_data)[vi].heap_index.end()) {
|
2349 |
2398 |
if ((*_node_data)[vi].heap[it->second] > rw) {
|
2350 |
2399 |
(*_node_data)[vi].heap.replace(it->second, e);
|
2351 |
2400 |
(*_node_data)[vi].heap.decrease(e, rw);
|
2352 |
2401 |
it->second = e;
|
2353 |
2402 |
}
|
2354 |
2403 |
} else {
|
2355 |
2404 |
(*_node_data)[vi].heap.push(e, rw);
|
2356 |
2405 |
(*_node_data)[vi].heap_index.insert(std::make_pair(tree, e));
|
2357 |
2406 |
}
|
2358 |
2407 |
|
2359 |
2408 |
if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) {
|
2360 |
2409 |
_blossom_set->decrease(v, (*_node_data)[vi].heap.prio());
|
2361 |
2410 |
|
2362 |
2411 |
if ((*_blossom_data)[vb].status == MATCHED) {
|
2363 |
2412 |
if (_delta2->state(vb) != _delta2->IN_HEAP) {
|
2364 |
2413 |
_delta2->push(vb, _blossom_set->classPrio(vb) -
|
2365 |
2414 |
(*_blossom_data)[vb].offset);
|
2366 |
2415 |
} else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) -
|
2367 |
2416 |
(*_blossom_data)[vb].offset){
|
2368 |
2417 |
_delta2->decrease(vb, _blossom_set->classPrio(vb) -
|
2369 |
2418 |
(*_blossom_data)[vb].offset);
|
2370 |
2419 |
}
|
2371 |
2420 |
}
|
2372 |
2421 |
}
|
2373 |
2422 |
}
|
2374 |
2423 |
}
|
2375 |
2424 |
}
|
2376 |
2425 |
(*_blossom_data)[blossom].offset = 0;
|
2377 |
2426 |
}
|
2378 |
2427 |
|
2379 |
2428 |
void matchedToOdd(int blossom) {
|
2380 |
2429 |
if (_delta2->state(blossom) == _delta2->IN_HEAP) {
|
2381 |
2430 |
_delta2->erase(blossom);
|
2382 |
2431 |
}
|
2383 |
2432 |
(*_blossom_data)[blossom].offset += _delta_sum;
|
2384 |
2433 |
if (!_blossom_set->trivial(blossom)) {
|
2385 |
2434 |
_delta4->push(blossom, (*_blossom_data)[blossom].pot / 2 +
|
2386 |
2435 |
(*_blossom_data)[blossom].offset);
|
2387 |
2436 |
}
|
2388 |
2437 |
}
|
2389 |
2438 |
|
2390 |
2439 |
void evenToMatched(int blossom, int tree) {
|
2391 |
2440 |
if (!_blossom_set->trivial(blossom)) {
|
2392 |
2441 |
(*_blossom_data)[blossom].pot += 2 * _delta_sum;
|
2393 |
2442 |
}
|
2394 |
2443 |
|
2395 |
2444 |
for (typename BlossomSet::ItemIt n(*_blossom_set, blossom);
|
2396 |
2445 |
n != INVALID; ++n) {
|
2397 |
2446 |
int ni = (*_node_index)[n];
|
2398 |
2447 |
(*_node_data)[ni].pot -= _delta_sum;
|
2399 |
2448 |
|
2400 |
2449 |
for (InArcIt e(_graph, n); e != INVALID; ++e) {
|
2401 |
2450 |
Node v = _graph.source(e);
|
2402 |
2451 |
int vb = _blossom_set->find(v);
|
2403 |
2452 |
int vi = (*_node_index)[v];
|
2404 |
2453 |
|
2405 |
2454 |
Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot -
|
2406 |
2455 |
dualScale * _weight[e];
|
2407 |
2456 |
|
2408 |
2457 |
if (vb == blossom) {
|
2409 |
2458 |
if (_delta3->state(e) == _delta3->IN_HEAP) {
|
2410 |
2459 |
_delta3->erase(e);
|
2411 |
2460 |
}
|
2412 |
2461 |
} else if ((*_blossom_data)[vb].status == EVEN) {
|
2413 |
2462 |
|
2414 |
2463 |
if (_delta3->state(e) == _delta3->IN_HEAP) {
|
2415 |
2464 |
_delta3->erase(e);
|
2416 |
2465 |
}
|
2417 |
2466 |
|
2418 |
2467 |
int vt = _tree_set->find(vb);
|
2419 |
2468 |
|
2420 |
2469 |
if (vt != tree) {
|
2421 |
2470 |
|
2422 |
2471 |
Arc r = _graph.oppositeArc(e);
|
2423 |
2472 |
|
2424 |
2473 |
typename std::map<int, Arc>::iterator it =
|
2425 |
2474 |
(*_node_data)[ni].heap_index.find(vt);
|
2426 |
2475 |
|
2427 |
2476 |
if (it != (*_node_data)[ni].heap_index.end()) {
|
2428 |
2477 |
if ((*_node_data)[ni].heap[it->second] > rw) {
|
2429 |
2478 |
(*_node_data)[ni].heap.replace(it->second, r);
|
2430 |
2479 |
(*_node_data)[ni].heap.decrease(r, rw);
|
2431 |
2480 |
it->second = r;
|
2432 |
2481 |
}
|
2433 |
2482 |
} else {
|
2434 |
2483 |
(*_node_data)[ni].heap.push(r, rw);
|
2435 |
2484 |
(*_node_data)[ni].heap_index.insert(std::make_pair(vt, r));
|
2436 |
2485 |
}
|
2437 |
2486 |
|
2438 |
2487 |
if ((*_blossom_set)[n] > (*_node_data)[ni].heap.prio()) {
|
2439 |
2488 |
_blossom_set->decrease(n, (*_node_data)[ni].heap.prio());
|
2440 |
2489 |
|
2441 |
2490 |
if (_delta2->state(blossom) != _delta2->IN_HEAP) {
|
2442 |
2491 |
_delta2->push(blossom, _blossom_set->classPrio(blossom) -
|
2443 |
2492 |
(*_blossom_data)[blossom].offset);
|
2444 |
2493 |
} else if ((*_delta2)[blossom] >
|
2445 |
2494 |
_blossom_set->classPrio(blossom) -
|
2446 |
2495 |
(*_blossom_data)[blossom].offset){
|
2447 |
2496 |
_delta2->decrease(blossom, _blossom_set->classPrio(blossom) -
|
2448 |
2497 |
(*_blossom_data)[blossom].offset);
|
2449 |
2498 |
}
|
2450 |
2499 |
}
|
2451 |
2500 |
}
|
2452 |
2501 |
} else {
|
2453 |
2502 |
|
2454 |
2503 |
typename std::map<int, Arc>::iterator it =
|
2455 |
2504 |
(*_node_data)[vi].heap_index.find(tree);
|
2456 |
2505 |
|
2457 |
2506 |
if (it != (*_node_data)[vi].heap_index.end()) {
|
2458 |
2507 |
(*_node_data)[vi].heap.erase(it->second);
|
2459 |
2508 |
(*_node_data)[vi].heap_index.erase(it);
|
2460 |
2509 |
if ((*_node_data)[vi].heap.empty()) {
|
2461 |
2510 |
_blossom_set->increase(v, std::numeric_limits<Value>::max());
|
2462 |
2511 |
} else if ((*_blossom_set)[v] < (*_node_data)[vi].heap.prio()) {
|
... |
... |
@@ -2779,407 +2828,418 @@
|
2779 |
2828 |
int ib = -1, id = -1;
|
2780 |
2829 |
for (int i = 0; i < int(subblossoms.size()); ++i) {
|
2781 |
2830 |
if (subblossoms[i] == b) ib = i;
|
2782 |
2831 |
if (subblossoms[i] == d) id = i;
|
2783 |
2832 |
|
2784 |
2833 |
(*_blossom_data)[subblossoms[i]].offset = offset;
|
2785 |
2834 |
if (!_blossom_set->trivial(subblossoms[i])) {
|
2786 |
2835 |
(*_blossom_data)[subblossoms[i]].pot -= 2 * offset;
|
2787 |
2836 |
}
|
2788 |
2837 |
if (_blossom_set->classPrio(subblossoms[i]) !=
|
2789 |
2838 |
std::numeric_limits<Value>::max()) {
|
2790 |
2839 |
_delta2->push(subblossoms[i],
|
2791 |
2840 |
_blossom_set->classPrio(subblossoms[i]) -
|
2792 |
2841 |
(*_blossom_data)[subblossoms[i]].offset);
|
2793 |
2842 |
}
|
2794 |
2843 |
}
|
2795 |
2844 |
|
2796 |
2845 |
if (id > ib ? ((id - ib) % 2 == 0) : ((ib - id) % 2 == 1)) {
|
2797 |
2846 |
for (int i = (id + 1) % subblossoms.size();
|
2798 |
2847 |
i != ib; i = (i + 2) % subblossoms.size()) {
|
2799 |
2848 |
int sb = subblossoms[i];
|
2800 |
2849 |
int tb = subblossoms[(i + 1) % subblossoms.size()];
|
2801 |
2850 |
(*_blossom_data)[sb].next =
|
2802 |
2851 |
_graph.oppositeArc((*_blossom_data)[tb].next);
|
2803 |
2852 |
}
|
2804 |
2853 |
|
2805 |
2854 |
for (int i = ib; i != id; i = (i + 2) % subblossoms.size()) {
|
2806 |
2855 |
int sb = subblossoms[i];
|
2807 |
2856 |
int tb = subblossoms[(i + 1) % subblossoms.size()];
|
2808 |
2857 |
int ub = subblossoms[(i + 2) % subblossoms.size()];
|
2809 |
2858 |
|
2810 |
2859 |
(*_blossom_data)[sb].status = ODD;
|
2811 |
2860 |
matchedToOdd(sb);
|
2812 |
2861 |
_tree_set->insert(sb, tree);
|
2813 |
2862 |
(*_blossom_data)[sb].pred = pred;
|
2814 |
2863 |
(*_blossom_data)[sb].next =
|
2815 |
2864 |
_graph.oppositeArc((*_blossom_data)[tb].next);
|
2816 |
2865 |
|
2817 |
2866 |
pred = (*_blossom_data)[ub].next;
|
2818 |
2867 |
|
2819 |
2868 |
(*_blossom_data)[tb].status = EVEN;
|
2820 |
2869 |
matchedToEven(tb, tree);
|
2821 |
2870 |
_tree_set->insert(tb, tree);
|
2822 |
2871 |
(*_blossom_data)[tb].pred = (*_blossom_data)[tb].next;
|
2823 |
2872 |
}
|
2824 |
2873 |
|
2825 |
2874 |
(*_blossom_data)[subblossoms[id]].status = ODD;
|
2826 |
2875 |
matchedToOdd(subblossoms[id]);
|
2827 |
2876 |
_tree_set->insert(subblossoms[id], tree);
|
2828 |
2877 |
(*_blossom_data)[subblossoms[id]].next = next;
|
2829 |
2878 |
(*_blossom_data)[subblossoms[id]].pred = pred;
|
2830 |
2879 |
|
2831 |
2880 |
} else {
|
2832 |
2881 |
|
2833 |
2882 |
for (int i = (ib + 1) % subblossoms.size();
|
2834 |
2883 |
i != id; i = (i + 2) % subblossoms.size()) {
|
2835 |
2884 |
int sb = subblossoms[i];
|
2836 |
2885 |
int tb = subblossoms[(i + 1) % subblossoms.size()];
|
2837 |
2886 |
(*_blossom_data)[sb].next =
|
2838 |
2887 |
_graph.oppositeArc((*_blossom_data)[tb].next);
|
2839 |
2888 |
}
|
2840 |
2889 |
|
2841 |
2890 |
for (int i = id; i != ib; i = (i + 2) % subblossoms.size()) {
|
2842 |
2891 |
int sb = subblossoms[i];
|
2843 |
2892 |
int tb = subblossoms[(i + 1) % subblossoms.size()];
|
2844 |
2893 |
int ub = subblossoms[(i + 2) % subblossoms.size()];
|
2845 |
2894 |
|
2846 |
2895 |
(*_blossom_data)[sb].status = ODD;
|
2847 |
2896 |
matchedToOdd(sb);
|
2848 |
2897 |
_tree_set->insert(sb, tree);
|
2849 |
2898 |
(*_blossom_data)[sb].next = next;
|
2850 |
2899 |
(*_blossom_data)[sb].pred =
|
2851 |
2900 |
_graph.oppositeArc((*_blossom_data)[tb].next);
|
2852 |
2901 |
|
2853 |
2902 |
(*_blossom_data)[tb].status = EVEN;
|
2854 |
2903 |
matchedToEven(tb, tree);
|
2855 |
2904 |
_tree_set->insert(tb, tree);
|
2856 |
2905 |
(*_blossom_data)[tb].pred =
|
2857 |
2906 |
(*_blossom_data)[tb].next =
|
2858 |
2907 |
_graph.oppositeArc((*_blossom_data)[ub].next);
|
2859 |
2908 |
next = (*_blossom_data)[ub].next;
|
2860 |
2909 |
}
|
2861 |
2910 |
|
2862 |
2911 |
(*_blossom_data)[subblossoms[ib]].status = ODD;
|
2863 |
2912 |
matchedToOdd(subblossoms[ib]);
|
2864 |
2913 |
_tree_set->insert(subblossoms[ib], tree);
|
2865 |
2914 |
(*_blossom_data)[subblossoms[ib]].next = next;
|
2866 |
2915 |
(*_blossom_data)[subblossoms[ib]].pred = pred;
|
2867 |
2916 |
}
|
2868 |
2917 |
_tree_set->erase(blossom);
|
2869 |
2918 |
}
|
2870 |
2919 |
|
2871 |
2920 |
void extractBlossom(int blossom, const Node& base, const Arc& matching) {
|
2872 |
2921 |
if (_blossom_set->trivial(blossom)) {
|
2873 |
2922 |
int bi = (*_node_index)[base];
|
2874 |
2923 |
Value pot = (*_node_data)[bi].pot;
|
2875 |
2924 |
|
2876 |
2925 |
(*_matching)[base] = matching;
|
2877 |
2926 |
_blossom_node_list.push_back(base);
|
2878 |
2927 |
(*_node_potential)[base] = pot;
|
2879 |
2928 |
} else {
|
2880 |
2929 |
|
2881 |
2930 |
Value pot = (*_blossom_data)[blossom].pot;
|
2882 |
2931 |
int bn = _blossom_node_list.size();
|
2883 |
2932 |
|
2884 |
2933 |
std::vector<int> subblossoms;
|
2885 |
2934 |
_blossom_set->split(blossom, std::back_inserter(subblossoms));
|
2886 |
2935 |
int b = _blossom_set->find(base);
|
2887 |
2936 |
int ib = -1;
|
2888 |
2937 |
for (int i = 0; i < int(subblossoms.size()); ++i) {
|
2889 |
2938 |
if (subblossoms[i] == b) { ib = i; break; }
|
2890 |
2939 |
}
|
2891 |
2940 |
|
2892 |
2941 |
for (int i = 1; i < int(subblossoms.size()); i += 2) {
|
2893 |
2942 |
int sb = subblossoms[(ib + i) % subblossoms.size()];
|
2894 |
2943 |
int tb = subblossoms[(ib + i + 1) % subblossoms.size()];
|
2895 |
2944 |
|
2896 |
2945 |
Arc m = (*_blossom_data)[tb].next;
|
2897 |
2946 |
extractBlossom(sb, _graph.target(m), _graph.oppositeArc(m));
|
2898 |
2947 |
extractBlossom(tb, _graph.source(m), m);
|
2899 |
2948 |
}
|
2900 |
2949 |
extractBlossom(subblossoms[ib], base, matching);
|
2901 |
2950 |
|
2902 |
2951 |
int en = _blossom_node_list.size();
|
2903 |
2952 |
|
2904 |
2953 |
_blossom_potential.push_back(BlossomVariable(bn, en, pot));
|
2905 |
2954 |
}
|
2906 |
2955 |
}
|
2907 |
2956 |
|
2908 |
2957 |
void extractMatching() {
|
2909 |
2958 |
std::vector<int> blossoms;
|
2910 |
2959 |
for (typename BlossomSet::ClassIt c(*_blossom_set); c != INVALID; ++c) {
|
2911 |
2960 |
blossoms.push_back(c);
|
2912 |
2961 |
}
|
2913 |
2962 |
|
2914 |
2963 |
for (int i = 0; i < int(blossoms.size()); ++i) {
|
2915 |
2964 |
|
2916 |
2965 |
Value offset = (*_blossom_data)[blossoms[i]].offset;
|
2917 |
2966 |
(*_blossom_data)[blossoms[i]].pot += 2 * offset;
|
2918 |
2967 |
for (typename BlossomSet::ItemIt n(*_blossom_set, blossoms[i]);
|
2919 |
2968 |
n != INVALID; ++n) {
|
2920 |
2969 |
(*_node_data)[(*_node_index)[n]].pot -= offset;
|
2921 |
2970 |
}
|
2922 |
2971 |
|
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 |
}
|
3042 |
3102 |
|
3043 |
3103 |
_unmatched = 0;
|
3044 |
3104 |
|
3045 |
3105 |
int index = 0;
|
3046 |
3106 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
3047 |
3107 |
Value pot = _fractional->nodeValue(n);
|
3048 |
3108 |
(*_node_index)[n] = index;
|
3049 |
3109 |
(*_node_data)[index].pot = pot;
|
3050 |
3110 |
int blossom =
|
3051 |
3111 |
_blossom_set->insert(n, std::numeric_limits<Value>::max());
|
3052 |
3112 |
|
3053 |
3113 |
(*_blossom_data)[blossom].status = MATCHED;
|
3054 |
3114 |
(*_blossom_data)[blossom].pred = INVALID;
|
3055 |
3115 |
(*_blossom_data)[blossom].next = _fractional->matching(n);
|
3056 |
3116 |
(*_blossom_data)[blossom].pot = 0;
|
3057 |
3117 |
(*_blossom_data)[blossom].offset = 0;
|
3058 |
3118 |
++index;
|
3059 |
3119 |
}
|
3060 |
3120 |
|
3061 |
3121 |
typename Graph::template NodeMap<bool> processed(_graph, false);
|
3062 |
3122 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
3063 |
3123 |
if (processed[n]) continue;
|
3064 |
3124 |
processed[n] = true;
|
3065 |
3125 |
if (_fractional->matching(n) == INVALID) continue;
|
3066 |
3126 |
int num = 1;
|
3067 |
3127 |
Node v = _graph.target(_fractional->matching(n));
|
3068 |
3128 |
while (n != v) {
|
3069 |
3129 |
processed[v] = true;
|
3070 |
3130 |
v = _graph.target(_fractional->matching(v));
|
3071 |
3131 |
++num;
|
3072 |
3132 |
}
|
3073 |
3133 |
|
3074 |
3134 |
if (num % 2 == 1) {
|
3075 |
3135 |
std::vector<int> subblossoms(num);
|
3076 |
3136 |
|
3077 |
3137 |
subblossoms[--num] = _blossom_set->find(n);
|
3078 |
3138 |
v = _graph.target(_fractional->matching(n));
|
3079 |
3139 |
while (n != v) {
|
3080 |
3140 |
subblossoms[--num] = _blossom_set->find(v);
|
3081 |
3141 |
v = _graph.target(_fractional->matching(v));
|
3082 |
3142 |
}
|
3083 |
3143 |
|
3084 |
3144 |
int surface =
|
3085 |
3145 |
_blossom_set->join(subblossoms.begin(), subblossoms.end());
|
3086 |
3146 |
(*_blossom_data)[surface].status = EVEN;
|
3087 |
3147 |
(*_blossom_data)[surface].pred = INVALID;
|
3088 |
3148 |
(*_blossom_data)[surface].next = INVALID;
|
3089 |
3149 |
(*_blossom_data)[surface].pot = 0;
|
3090 |
3150 |
(*_blossom_data)[surface].offset = 0;
|
3091 |
3151 |
|
3092 |
3152 |
_tree_set->insert(surface);
|
3093 |
3153 |
++_unmatched;
|
3094 |
3154 |
}
|
3095 |
3155 |
}
|
3096 |
3156 |
|
3097 |
3157 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
3098 |
3158 |
int si = (*_node_index)[_graph.u(e)];
|
3099 |
3159 |
int sb = _blossom_set->find(_graph.u(e));
|
3100 |
3160 |
int ti = (*_node_index)[_graph.v(e)];
|
3101 |
3161 |
int tb = _blossom_set->find(_graph.v(e));
|
3102 |
3162 |
if ((*_blossom_data)[sb].status == EVEN &&
|
3103 |
3163 |
(*_blossom_data)[tb].status == EVEN && sb != tb) {
|
3104 |
3164 |
_delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot -
|
3105 |
3165 |
dualScale * _weight[e]) / 2);
|
3106 |
3166 |
}
|
3107 |
3167 |
}
|
3108 |
3168 |
|
3109 |
3169 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
3110 |
3170 |
int nb = _blossom_set->find(n);
|
3111 |
3171 |
if ((*_blossom_data)[nb].status != MATCHED) continue;
|
3112 |
3172 |
int ni = (*_node_index)[n];
|
3113 |
3173 |
|
3114 |
3174 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
3115 |
3175 |
Node v = _graph.target(e);
|
3116 |
3176 |
int vb = _blossom_set->find(v);
|
3117 |
3177 |
int vi = (*_node_index)[v];
|
3118 |
3178 |
|
3119 |
3179 |
Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot -
|
3120 |
3180 |
dualScale * _weight[e];
|
3121 |
3181 |
|
3122 |
3182 |
if ((*_blossom_data)[vb].status == EVEN) {
|
3123 |
3183 |
|
3124 |
3184 |
int vt = _tree_set->find(vb);
|
3125 |
3185 |
|
3126 |
3186 |
typename std::map<int, Arc>::iterator it =
|
3127 |
3187 |
(*_node_data)[ni].heap_index.find(vt);
|
3128 |
3188 |
|
3129 |
3189 |
if (it != (*_node_data)[ni].heap_index.end()) {
|
3130 |
3190 |
if ((*_node_data)[ni].heap[it->second] > rw) {
|
3131 |
3191 |
(*_node_data)[ni].heap.replace(it->second, e);
|
3132 |
3192 |
(*_node_data)[ni].heap.decrease(e, rw);
|
3133 |
3193 |
it->second = e;
|
3134 |
3194 |
}
|
3135 |
3195 |
} else {
|
3136 |
3196 |
(*_node_data)[ni].heap.push(e, rw);
|
3137 |
3197 |
(*_node_data)[ni].heap_index.insert(std::make_pair(vt, e));
|
3138 |
3198 |
}
|
3139 |
3199 |
}
|
3140 |
3200 |
}
|
3141 |
3201 |
|
3142 |
3202 |
if (!(*_node_data)[ni].heap.empty()) {
|
3143 |
3203 |
_blossom_set->decrease(n, (*_node_data)[ni].heap.prio());
|
3144 |
3204 |
_delta2->push(nb, _blossom_set->classPrio(nb));
|
3145 |
3205 |
}
|
3146 |
3206 |
}
|
3147 |
3207 |
}
|
3148 |
3208 |
|
3149 |
3209 |
/// \brief Start the algorithm
|
3150 |
3210 |
///
|
3151 |
3211 |
/// This function starts the algorithm.
|
3152 |
3212 |
///
|
3153 |
3213 |
/// \pre \ref init() or \ref fractionalInit() must be called before
|
3154 |
3214 |
/// using this function.
|
3155 |
3215 |
bool start() {
|
3156 |
3216 |
enum OpType {
|
3157 |
3217 |
D2, D3, D4
|
3158 |
3218 |
};
|
3159 |
3219 |
|
3160 |
3220 |
if (_unmatched == -1) return false;
|
3161 |
3221 |
|
3162 |
3222 |
while (_unmatched > 0) {
|
3163 |
3223 |
Value d2 = !_delta2->empty() ?
|
3164 |
3224 |
_delta2->prio() : std::numeric_limits<Value>::max();
|
3165 |
3225 |
|
3166 |
3226 |
Value d3 = !_delta3->empty() ?
|
3167 |
3227 |
_delta3->prio() : std::numeric_limits<Value>::max();
|
3168 |
3228 |
|
3169 |
3229 |
Value d4 = !_delta4->empty() ?
|
3170 |
3230 |
_delta4->prio() : std::numeric_limits<Value>::max();
|
3171 |
3231 |
|
3172 |
3232 |
_delta_sum = d3; OpType ot = D3;
|
3173 |
3233 |
if (d2 < _delta_sum) { _delta_sum = d2; ot = D2; }
|
3174 |
3234 |
if (d4 < _delta_sum) { _delta_sum = d4; ot = D4; }
|
3175 |
3235 |
|
3176 |
3236 |
if (_delta_sum == std::numeric_limits<Value>::max()) {
|
3177 |
3237 |
return false;
|
3178 |
3238 |
}
|
3179 |
3239 |
|
3180 |
3240 |
switch (ot) {
|
3181 |
3241 |
case D2:
|
3182 |
3242 |
{
|
3183 |
3243 |
int blossom = _delta2->top();
|
3184 |
3244 |
Node n = _blossom_set->classTop(blossom);
|
3185 |
3245 |
Arc e = (*_node_data)[(*_node_index)[n]].heap.top();
|