3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_BIPARTITE_MATCHING
20 #define LEMON_BIPARTITE_MATCHING
24 #include <lemon/bin_heap.h>
25 #include <lemon/maps.h>
31 ///\brief Maximum matching algorithms in bipartite graphs.
37 /// \brief Bipartite Max Cardinality Matching algorithm
39 /// Bipartite Max Cardinality Matching algorithm. This class implements
40 /// the Hopcroft-Karp algorithm which has \f$ O(e\sqrt{n}) \f$ time
42 template <typename BpUGraph>
43 class MaxBipartiteMatching {
46 typedef BpUGraph Graph;
48 typedef typename Graph::Node Node;
49 typedef typename Graph::ANodeIt ANodeIt;
50 typedef typename Graph::BNodeIt BNodeIt;
51 typedef typename Graph::UEdge UEdge;
52 typedef typename Graph::UEdgeIt UEdgeIt;
53 typedef typename Graph::IncEdgeIt IncEdgeIt;
55 typedef typename BpUGraph::template ANodeMap<UEdge> ANodeMatchingMap;
56 typedef typename BpUGraph::template BNodeMap<UEdge> BNodeMatchingMap;
61 /// \brief Constructor.
63 /// Constructor of the algorithm.
64 MaxBipartiteMatching(const BpUGraph& _graph)
65 : anode_matching(_graph), bnode_matching(_graph), graph(&_graph) {}
67 /// \name Execution control
68 /// The simplest way to execute the algorithm is to use
69 /// one of the member functions called \c run().
71 /// If you need more control on the execution,
72 /// first you must call \ref init() or one alternative for it.
73 /// Finally \ref start() will perform the matching computation or
74 /// with step-by-step execution you can augment the solution.
78 /// \brief Initalize the data structures.
80 /// It initalizes the data structures and creates an empty matching.
82 for (ANodeIt it(*graph); it != INVALID; ++it) {
83 anode_matching[it] = INVALID;
85 for (BNodeIt it(*graph); it != INVALID; ++it) {
86 bnode_matching[it] = INVALID;
91 /// \brief Initalize the data structures.
93 /// It initalizes the data structures and creates a greedy
94 /// matching. From this matching sometimes it is faster to get
95 /// the matching than from the initial empty matching.
98 for (BNodeIt it(*graph); it != INVALID; ++it) {
99 bnode_matching[it] = INVALID;
101 for (ANodeIt it(*graph); it != INVALID; ++it) {
102 anode_matching[it] = INVALID;
103 for (IncEdgeIt jt(*graph, it); jt != INVALID; ++jt) {
104 if (bnode_matching[graph->bNode(jt)] == INVALID) {
105 anode_matching[it] = jt;
106 bnode_matching[graph->bNode(jt)] = jt;
114 /// \brief Initalize the data structures with an initial matching.
116 /// It initalizes the data structures with an initial matching.
117 template <typename MatchingMap>
118 void matchingInit(const MatchingMap& matching) {
119 for (ANodeIt it(*graph); it != INVALID; ++it) {
120 anode_matching[it] = INVALID;
122 for (BNodeIt it(*graph); it != INVALID; ++it) {
123 bnode_matching[it] = INVALID;
126 for (UEdgeIt it(*graph); it != INVALID; ++it) {
129 anode_matching[graph->aNode(it)] = it;
130 bnode_matching[graph->bNode(it)] = it;
135 /// \brief Initalize the data structures with an initial matching.
137 /// It initalizes the data structures with an initial matching.
138 /// \return %True when the given map contains really a matching.
139 template <typename MatchingMap>
140 void checkedMatchingInit(const MatchingMap& matching) {
141 for (ANodeIt it(*graph); it != INVALID; ++it) {
142 anode_matching[it] = INVALID;
144 for (BNodeIt it(*graph); it != INVALID; ++it) {
145 bnode_matching[it] = INVALID;
148 for (UEdgeIt it(*graph); it != INVALID; ++it) {
151 if (anode_matching[graph->aNode(it)] != INVALID) {
154 anode_matching[graph->aNode(it)] = it;
155 if (bnode_matching[graph->aNode(it)] != INVALID) {
158 bnode_matching[graph->bNode(it)] = it;
164 /// \brief An augmenting phase of the Hopcroft-Karp algorithm
166 /// It runs an augmenting phase of the Hopcroft-Karp
167 /// algorithm. The phase finds maximum count of edge disjoint
168 /// augmenting paths and augments on these paths. The algorithm
169 /// consists at most of \f$ O(\sqrt{n}) \f$ phase and one phase is
170 /// \f$ O(e) \f$ long.
173 typename Graph::template ANodeMap<bool> areached(*graph, false);
174 typename Graph::template BNodeMap<bool> breached(*graph, false);
176 typename Graph::template BNodeMap<UEdge> bpred(*graph, INVALID);
178 std::vector<Node> queue, bqueue;
179 for (ANodeIt it(*graph); it != INVALID; ++it) {
180 if (anode_matching[it] == INVALID) {
186 bool success = false;
188 while (!success && !queue.empty()) {
189 std::vector<Node> newqueue;
190 for (int i = 0; i < (int)queue.size(); ++i) {
191 Node anode = queue[i];
192 for (IncEdgeIt jt(*graph, anode); jt != INVALID; ++jt) {
193 Node bnode = graph->bNode(jt);
194 if (breached[bnode]) continue;
195 breached[bnode] = true;
197 if (bnode_matching[bnode] == INVALID) {
198 bqueue.push_back(bnode);
201 Node newanode = graph->aNode(bnode_matching[bnode]);
202 if (!areached[newanode]) {
203 areached[newanode] = true;
204 newqueue.push_back(newanode);
209 queue.swap(newqueue);
214 typename Graph::template ANodeMap<bool> aused(*graph, false);
216 for (int i = 0; i < (int)bqueue.size(); ++i) {
217 Node bnode = bqueue[i];
221 while (bnode != INVALID) {
222 UEdge uedge = bpred[bnode];
223 Node anode = graph->aNode(uedge);
230 bnode = anode_matching[anode] != INVALID ?
231 graph->bNode(anode_matching[anode]) : INVALID;
238 while (bnode != INVALID) {
239 UEdge uedge = bpred[bnode];
240 Node anode = graph->aNode(uedge);
242 bnode_matching[bnode] = uedge;
244 bnode = anode_matching[anode] != INVALID ?
245 graph->bNode(anode_matching[anode]) : INVALID;
247 anode_matching[anode] = uedge;
258 /// \brief An augmenting phase of the Ford-Fulkerson algorithm
260 /// It runs an augmenting phase of the Ford-Fulkerson
261 /// algorithm. The phase finds only one augmenting path and
262 /// augments only on this paths. The algorithm consists at most
263 /// of \f$ O(n) \f$ simple phase and one phase is at most
264 /// \f$ O(e) \f$ long.
265 bool simpleAugment() {
267 typename Graph::template ANodeMap<bool> areached(*graph, false);
268 typename Graph::template BNodeMap<bool> breached(*graph, false);
270 typename Graph::template BNodeMap<UEdge> bpred(*graph, INVALID);
272 std::vector<Node> queue;
273 for (ANodeIt it(*graph); it != INVALID; ++it) {
274 if (anode_matching[it] == INVALID) {
280 while (!queue.empty()) {
281 std::vector<Node> newqueue;
282 for (int i = 0; i < (int)queue.size(); ++i) {
283 Node anode = queue[i];
284 for (IncEdgeIt jt(*graph, anode); jt != INVALID; ++jt) {
285 Node bnode = graph->bNode(jt);
286 if (breached[bnode]) continue;
287 breached[bnode] = true;
289 if (bnode_matching[bnode] == INVALID) {
290 while (bnode != INVALID) {
291 UEdge uedge = bpred[bnode];
292 anode = graph->aNode(uedge);
294 bnode_matching[bnode] = uedge;
296 bnode = anode_matching[anode] != INVALID ?
297 graph->bNode(anode_matching[anode]) : INVALID;
299 anode_matching[anode] = uedge;
305 Node newanode = graph->aNode(bnode_matching[bnode]);
306 if (!areached[newanode]) {
307 areached[newanode] = true;
308 newqueue.push_back(newanode);
313 queue.swap(newqueue);
319 /// \brief Starts the algorithm.
321 /// Starts the algorithm. It runs augmenting phases until the optimal
322 /// solution reached.
327 /// \brief Runs the algorithm.
329 /// It just initalize the algorithm and then start it.
337 /// \name Query Functions
338 /// The result of the %Matching algorithm can be obtained using these
340 /// Before the use of these functions,
341 /// either run() or start() must be called.
345 /// \brief Returns an minimum covering of the nodes.
347 /// The minimum covering set problem is the dual solution of the
348 /// maximum bipartite matching. It provides an solution for this
349 /// problem what is proof of the optimality of the matching.
350 /// \return The size of the cover set.
351 template <typename CoverMap>
352 int coverSet(CoverMap& covering) {
354 typename Graph::template ANodeMap<bool> areached(*graph, false);
355 typename Graph::template BNodeMap<bool> breached(*graph, false);
357 std::vector<Node> queue;
358 for (ANodeIt it(*graph); it != INVALID; ++it) {
359 if (anode_matching[it] == INVALID) {
364 while (!queue.empty()) {
365 std::vector<Node> newqueue;
366 for (int i = 0; i < (int)queue.size(); ++i) {
367 Node anode = queue[i];
368 for (IncEdgeIt jt(*graph, anode); jt != INVALID; ++jt) {
369 Node bnode = graph->bNode(jt);
370 if (breached[bnode]) continue;
371 breached[bnode] = true;
372 if (bnode_matching[bnode] != INVALID) {
373 Node newanode = graph->aNode(bnode_matching[bnode]);
374 if (!areached[newanode]) {
375 areached[newanode] = true;
376 newqueue.push_back(newanode);
381 queue.swap(newqueue);
385 for (ANodeIt it(*graph); it != INVALID; ++it) {
386 covering[it] = !areached[it] && anode_matching[it] != INVALID;
387 if (!areached[it] && anode_matching[it] != INVALID) {
391 for (BNodeIt it(*graph); it != INVALID; ++it) {
392 covering[it] = breached[it];
400 /// \brief Set true all matching uedge in the map.
402 /// Set true all matching uedge in the map. It does not change the
403 /// value mapped to the other uedges.
404 /// \return The number of the matching edges.
405 template <typename MatchingMap>
406 int quickMatching(MatchingMap& matching) {
407 for (ANodeIt it(*graph); it != INVALID; ++it) {
408 if (anode_matching[it] != INVALID) {
409 matching[anode_matching[it]] = true;
412 return matching_size;
415 /// \brief Set true all matching uedge in the map and the others to false.
417 /// Set true all matching uedge in the map and the others to false.
418 /// \return The number of the matching edges.
419 template <typename MatchingMap>
420 int matching(MatchingMap& matching) {
421 for (UEdgeIt it(*graph); it != INVALID; ++it) {
422 matching[it] = it == anode_matching[graph->aNode(it)];
424 return matching_size;
428 /// \brief Return true if the given uedge is in the matching.
430 /// It returns true if the given uedge is in the matching.
431 bool matchingEdge(const UEdge& edge) {
432 return anode_matching[graph->aNode(edge)] == edge;
435 /// \brief Returns the matching edge from the node.
437 /// Returns the matching edge from the node. If there is not such
438 /// edge it gives back \c INVALID.
439 UEdge matchingEdge(const Node& node) {
440 if (graph->aNode(node)) {
441 return anode_matching[node];
443 return bnode_matching[node];
447 /// \brief Gives back the number of the matching edges.
449 /// Gives back the number of the matching edges.
450 int matchingSize() const {
451 return matching_size;
458 ANodeMatchingMap anode_matching;
459 BNodeMatchingMap bnode_matching;
466 /// \brief Default traits class for weighted bipartite matching algoritms.
468 /// Default traits class for weighted bipartite matching algoritms.
469 /// \param _BpUGraph The bipartite undirected graph type.
470 /// \param _WeightMap Type of weight map.
471 template <typename _BpUGraph, typename _WeightMap>
472 struct WeightedBipartiteMatchingDefaultTraits {
473 /// \brief The type of the weight of the undirected edges.
474 typedef typename _WeightMap::Value Value;
476 /// The undirected bipartite graph type the algorithm runs on.
477 typedef _BpUGraph BpUGraph;
479 /// The map of the edges weights
480 typedef _WeightMap WeightMap;
482 /// \brief The cross reference type used by heap.
484 /// The cross reference type used by heap.
485 /// Usually it is \c Graph::NodeMap<int>.
486 typedef typename BpUGraph::template NodeMap<int> HeapCrossRef;
488 /// \brief Instantiates a HeapCrossRef.
490 /// This function instantiates a \ref HeapCrossRef.
491 /// \param graph is the graph, to which we would like to define the
493 static HeapCrossRef *createHeapCrossRef(const BpUGraph &graph) {
494 return new HeapCrossRef(graph);
497 /// \brief The heap type used by weighted matching algorithms.
499 /// The heap type used by weighted matching algorithms. It should
500 /// minimize the priorities and the heap's key type is the graph's
501 /// anode graph's node.
504 typedef BinHeap<typename BpUGraph::Node, Value, HeapCrossRef> Heap;
506 /// \brief Instantiates a Heap.
508 /// This function instantiates a \ref Heap.
509 /// \param crossref The cross reference of the heap.
510 static Heap *createHeap(HeapCrossRef& crossref) {
511 return new Heap(crossref);
517 /// \ingroup matching
519 /// \brief Bipartite Max Weighted Matching algorithm
521 /// This class implements the bipartite Max Weighted Matching
522 /// algorithm. It uses the successive shortest path algorithm to
523 /// calculate the maximum weighted matching in the bipartite
524 /// graph. The algorithm can be used also to calculate the maximum
525 /// cardinality maximum weighted matching. The time complexity
526 /// of the algorithm is \f$ O(ne\log(n)) \f$ with the default binary
527 /// heap implementation but this can be improved to
528 /// \f$ O(n^2\log(n)+ne) \f$ if we use fibonacci heaps.
530 /// The algorithm also provides a potential function on the nodes
531 /// which a dual solution of the matching algorithm and it can be
532 /// used to proof the optimality of the given pimal solution.
534 template <typename _BpUGraph, typename _WeightMap, typename _Traits>
536 template <typename _BpUGraph,
537 typename _WeightMap = typename _BpUGraph::template UEdgeMap<int>,
538 typename _Traits = WeightedBipartiteMatchingDefaultTraits<_BpUGraph, _WeightMap> >
540 class MaxWeightedBipartiteMatching {
543 typedef _Traits Traits;
544 typedef typename Traits::BpUGraph BpUGraph;
545 typedef typename Traits::WeightMap WeightMap;
546 typedef typename Traits::Value Value;
550 typedef typename Traits::HeapCrossRef HeapCrossRef;
551 typedef typename Traits::Heap Heap;
554 typedef typename BpUGraph::Node Node;
555 typedef typename BpUGraph::ANodeIt ANodeIt;
556 typedef typename BpUGraph::BNodeIt BNodeIt;
557 typedef typename BpUGraph::UEdge UEdge;
558 typedef typename BpUGraph::UEdgeIt UEdgeIt;
559 typedef typename BpUGraph::IncEdgeIt IncEdgeIt;
561 typedef typename BpUGraph::template ANodeMap<UEdge> ANodeMatchingMap;
562 typedef typename BpUGraph::template BNodeMap<UEdge> BNodeMatchingMap;
564 typedef typename BpUGraph::template ANodeMap<Value> ANodePotentialMap;
565 typedef typename BpUGraph::template BNodeMap<Value> BNodePotentialMap;
570 /// \brief \ref Exception for uninitialized parameters.
572 /// This error represents problems in the initialization
573 /// of the parameters of the algorithms.
574 class UninitializedParameter : public lemon::UninitializedParameter {
576 virtual const char* exceptionName() const {
577 return "lemon::MaxWeightedBipartiteMatching::UninitializedParameter";
581 ///\name Named template parameters
585 template <class H, class CR>
586 struct DefHeapTraits : public Traits {
587 typedef CR HeapCrossRef;
589 static HeapCrossRef *createHeapCrossRef(const BpUGraph &) {
590 throw UninitializedParameter();
592 static Heap *createHeap(HeapCrossRef &) {
593 throw UninitializedParameter();
597 /// \brief \ref named-templ-param "Named parameter" for setting heap
598 /// and cross reference type
600 /// \ref named-templ-param "Named parameter" for setting heap and cross
602 template <class H, class CR = typename BpUGraph::template NodeMap<int> >
604 : public MaxWeightedBipartiteMatching<BpUGraph, WeightMap,
605 DefHeapTraits<H, CR> > {
606 typedef MaxWeightedBipartiteMatching<BpUGraph, WeightMap,
607 DefHeapTraits<H, CR> > Create;
610 template <class H, class CR>
611 struct DefStandardHeapTraits : public Traits {
612 typedef CR HeapCrossRef;
614 static HeapCrossRef *createHeapCrossRef(const BpUGraph &graph) {
615 return new HeapCrossRef(graph);
617 static Heap *createHeap(HeapCrossRef &crossref) {
618 return new Heap(crossref);
622 /// \brief \ref named-templ-param "Named parameter" for setting heap and
623 /// cross reference type with automatic allocation
625 /// \ref named-templ-param "Named parameter" for setting heap and cross
626 /// reference type. It can allocate the heap and the cross reference
627 /// object if the cross reference's constructor waits for the graph as
628 /// parameter and the heap's constructor waits for the cross reference.
629 template <class H, class CR = typename BpUGraph::template NodeMap<int> >
630 struct DefStandardHeap
631 : public MaxWeightedBipartiteMatching<BpUGraph, WeightMap,
632 DefStandardHeapTraits<H, CR> > {
633 typedef MaxWeightedBipartiteMatching<BpUGraph, WeightMap,
634 DefStandardHeapTraits<H, CR> >
641 /// \brief Constructor.
643 /// Constructor of the algorithm.
644 MaxWeightedBipartiteMatching(const BpUGraph& _graph,
645 const WeightMap& _weight)
646 : graph(&_graph), weight(&_weight),
647 anode_matching(_graph), bnode_matching(_graph),
648 anode_potential(_graph), bnode_potential(_graph),
649 _heap_cross_ref(0), local_heap_cross_ref(false),
650 _heap(0), local_heap(0) {}
652 /// \brief Destructor.
654 /// Destructor of the algorithm.
655 ~MaxWeightedBipartiteMatching() {
659 /// \brief Sets the heap and the cross reference used by algorithm.
661 /// Sets the heap and the cross reference used by algorithm.
662 /// If you don't use this function before calling \ref run(),
663 /// it will allocate one. The destuctor deallocates this
664 /// automatically allocated map, of course.
665 /// \return \c (*this)
666 MaxWeightedBipartiteMatching& heap(Heap& heap, HeapCrossRef &crossRef) {
667 if(local_heap_cross_ref) {
668 delete _heap_cross_ref;
669 local_heap_cross_ref = false;
671 _heap_cross_ref = &crossRef;
680 /// \name Execution control
681 /// The simplest way to execute the algorithm is to use
682 /// one of the member functions called \c run().
684 /// If you need more control on the execution,
685 /// first you must call \ref init() or one alternative for it.
686 /// Finally \ref start() will perform the matching computation or
687 /// with step-by-step execution you can augment the solution.
691 /// \brief Initalize the data structures.
693 /// It initalizes the data structures and creates an empty matching.
696 for (ANodeIt it(*graph); it != INVALID; ++it) {
697 anode_matching[it] = INVALID;
698 anode_potential[it] = 0;
700 for (BNodeIt it(*graph); it != INVALID; ++it) {
701 bnode_matching[it] = INVALID;
702 bnode_potential[it] = 0;
703 for (IncEdgeIt jt(*graph, it); jt != INVALID; ++jt) {
704 if (-(*weight)[jt] <= bnode_potential[it]) {
705 bnode_potential[it] = -(*weight)[jt];
714 /// \brief An augmenting phase of the weighted matching algorithm
716 /// It runs an augmenting phase of the weighted matching
717 /// algorithm. The phase finds the best augmenting path and
718 /// augments only on this paths.
720 /// The algorithm consists at most
721 /// of \f$ O(n) \f$ phase and one phase is \f$ O(n\log(n)+e) \f$
722 /// long with Fibonacci heap or \f$ O((n+e)\log(n)) \f$ long
723 /// with binary heap.
724 /// \param decrease If the given parameter true the matching value
725 /// can be decreased in the augmenting phase. If we would like
726 /// to calculate the maximum cardinality maximum weighted matching
727 /// then we should let the algorithm to decrease the matching
728 /// value in order to increase the number of the matching edges.
729 bool augment(bool decrease = false) {
731 typename BpUGraph::template BNodeMap<Value> bdist(*graph);
732 typename BpUGraph::template BNodeMap<UEdge> bpred(*graph, INVALID);
734 Node bestNode = INVALID;
738 for (ANodeIt it(*graph); it != INVALID; ++it) {
739 (*_heap_cross_ref)[it] = Heap::PRE_HEAP;
742 for (ANodeIt it(*graph); it != INVALID; ++it) {
743 if (anode_matching[it] == INVALID) {
749 while (!_heap->empty()) {
750 Node anode = _heap->top();
751 Value avalue = _heap->prio();
753 for (IncEdgeIt jt(*graph, anode); jt != INVALID; ++jt) {
754 if (jt == anode_matching[anode]) continue;
755 Node bnode = graph->bNode(jt);
756 Value bvalue = avalue + anode_potential[anode] -
757 bnode_potential[bnode] - (*weight)[jt];
758 if (bpred[bnode] == INVALID || bvalue < bdist[bnode]) {
759 bdist[bnode] = bvalue;
762 if (bvalue > bdistMax) {
765 if (bnode_matching[bnode] != INVALID) {
766 Node newanode = graph->aNode(bnode_matching[bnode]);
767 switch (_heap->state(newanode)) {
769 _heap->push(newanode, bvalue);
772 if (bvalue < (*_heap)[newanode]) {
773 _heap->decrease(newanode, bvalue);
776 case Heap::POST_HEAP:
780 if (bestNode == INVALID ||
781 - bvalue - bnode_potential[bnode] > bestValue) {
782 bestValue = - bvalue - bnode_potential[bnode];
789 if (bestNode == INVALID || (!decrease && bestValue < 0)) {
793 matching_value += bestValue;
796 for (BNodeIt it(*graph); it != INVALID; ++it) {
797 if (bpred[it] != INVALID) {
798 bnode_potential[it] += bdist[it];
800 bnode_potential[it] += bdistMax;
803 for (ANodeIt it(*graph); it != INVALID; ++it) {
804 if (anode_matching[it] != INVALID) {
805 Node bnode = graph->bNode(anode_matching[it]);
806 if (bpred[bnode] != INVALID) {
807 anode_potential[it] += bdist[bnode];
809 anode_potential[it] += bdistMax;
814 while (bestNode != INVALID) {
815 UEdge uedge = bpred[bestNode];
816 Node anode = graph->aNode(uedge);
818 bnode_matching[bestNode] = uedge;
819 if (anode_matching[anode] != INVALID) {
820 bestNode = graph->bNode(anode_matching[anode]);
824 anode_matching[anode] = uedge;
831 /// \brief Starts the algorithm.
833 /// Starts the algorithm. It runs augmenting phases until the
834 /// optimal solution reached.
836 /// \param maxCardinality If the given value is true it will
837 /// calculate the maximum cardinality maximum matching instead of
838 /// the maximum matching.
839 void start(bool maxCardinality = false) {
840 while (augment(maxCardinality)) {}
843 /// \brief Runs the algorithm.
845 /// It just initalize the algorithm and then start it.
847 /// \param maxCardinality If the given value is true it will
848 /// calculate the maximum cardinality maximum matching instead of
849 /// the maximum matching.
850 void run(bool maxCardinality = false) {
852 start(maxCardinality);
857 /// \name Query Functions
858 /// The result of the %Matching algorithm can be obtained using these
860 /// Before the use of these functions,
861 /// either run() or start() must be called.
865 /// \brief Gives back the potential in the NodeMap
867 /// Gives back the potential in the NodeMap. The potential
868 /// is feasible if \f$ \pi(a) - \pi(b) - w(ab) = 0 \f$ for
869 /// each matching edges and \f$ \pi(a) - \pi(b) - w(ab) \ge 0 \f$
871 template <typename PotentialMap>
872 void potential(PotentialMap& potential) {
873 for (ANodeIt it(*graph); it != INVALID; ++it) {
874 potential[it] = anode_potential[it];
876 for (BNodeIt it(*graph); it != INVALID; ++it) {
877 potential[it] = bnode_potential[it];
881 /// \brief Set true all matching uedge in the map.
883 /// Set true all matching uedge in the map. It does not change the
884 /// value mapped to the other uedges.
885 /// \return The number of the matching edges.
886 template <typename MatchingMap>
887 int quickMatching(MatchingMap& matching) {
888 for (ANodeIt it(*graph); it != INVALID; ++it) {
889 if (anode_matching[it] != INVALID) {
890 matching[anode_matching[it]] = true;
893 return matching_size;
896 /// \brief Set true all matching uedge in the map and the others to false.
898 /// Set true all matching uedge in the map and the others to false.
899 /// \return The number of the matching edges.
900 template <typename MatchingMap>
901 int matching(MatchingMap& matching) {
902 for (UEdgeIt it(*graph); it != INVALID; ++it) {
903 matching[it] = it == anode_matching[graph->aNode(it)];
905 return matching_size;
909 /// \brief Return true if the given uedge is in the matching.
911 /// It returns true if the given uedge is in the matching.
912 bool matchingEdge(const UEdge& edge) {
913 return anode_matching[graph->aNode(edge)] == edge;
916 /// \brief Returns the matching edge from the node.
918 /// Returns the matching edge from the node. If there is not such
919 /// edge it gives back \c INVALID.
920 UEdge matchingEdge(const Node& node) {
921 if (graph->aNode(node)) {
922 return anode_matching[node];
924 return bnode_matching[node];
928 /// \brief Gives back the sum of weights of the matching edges.
930 /// Gives back the sum of weights of the matching edges.
931 Value matchingValue() const {
932 return matching_value;
935 /// \brief Gives back the number of the matching edges.
937 /// Gives back the number of the matching edges.
938 int matchingSize() const {
939 return matching_size;
946 void initStructures() {
947 if (!_heap_cross_ref) {
948 local_heap_cross_ref = true;
949 _heap_cross_ref = Traits::createHeapCrossRef(*graph);
953 _heap = Traits::createHeap(*_heap_cross_ref);
957 void destroyStructures() {
958 if (local_heap_cross_ref) delete _heap_cross_ref;
959 if (local_heap) delete _heap;
965 const BpUGraph *graph;
966 const WeightMap* weight;
968 ANodeMatchingMap anode_matching;
969 BNodeMatchingMap bnode_matching;
971 ANodePotentialMap anode_potential;
972 BNodePotentialMap bnode_potential;
974 Value matching_value;
977 HeapCrossRef *_heap_cross_ref;
978 bool local_heap_cross_ref;
985 /// \brief Default traits class for minimum cost bipartite matching
988 /// Default traits class for minimum cost bipartite matching
991 /// \param _BpUGraph The bipartite undirected graph
994 /// \param _CostMap Type of cost map.
995 template <typename _BpUGraph, typename _CostMap>
996 struct MinCostMaxBipartiteMatchingDefaultTraits {
997 /// \brief The type of the cost of the undirected edges.
998 typedef typename _CostMap::Value Value;
1000 /// The undirected bipartite graph type the algorithm runs on.
1001 typedef _BpUGraph BpUGraph;
1003 /// The map of the edges costs
1004 typedef _CostMap CostMap;
1006 /// \brief The cross reference type used by heap.
1008 /// The cross reference type used by heap.
1009 /// Usually it is \c Graph::NodeMap<int>.
1010 typedef typename BpUGraph::template NodeMap<int> HeapCrossRef;
1012 /// \brief Instantiates a HeapCrossRef.
1014 /// This function instantiates a \ref HeapCrossRef.
1015 /// \param graph is the graph, to which we would like to define the
1017 static HeapCrossRef *createHeapCrossRef(const BpUGraph &graph) {
1018 return new HeapCrossRef(graph);
1021 /// \brief The heap type used by costed matching algorithms.
1023 /// The heap type used by costed matching algorithms. It should
1024 /// minimize the priorities and the heap's key type is the graph's
1025 /// anode graph's node.
1028 typedef BinHeap<typename BpUGraph::Node, Value, HeapCrossRef> Heap;
1030 /// \brief Instantiates a Heap.
1032 /// This function instantiates a \ref Heap.
1033 /// \param crossref The cross reference of the heap.
1034 static Heap *createHeap(HeapCrossRef& crossref) {
1035 return new Heap(crossref);
1041 /// \ingroup matching
1043 /// \brief Bipartite Min Cost Matching algorithm
1045 /// This class implements the bipartite Min Cost Matching algorithm.
1046 /// It uses the successive shortest path algorithm to calculate the
1047 /// minimum cost maximum matching in the bipartite graph. The time
1048 /// complexity of the algorithm is \f$ O(ne\log(n)) \f$ with the
1049 /// default binary heap implementation but this can be improved to
1050 /// \f$ O(n^2\log(n)+ne) \f$ if we use fibonacci heaps.
1052 /// The algorithm also provides a potential function on the nodes
1053 /// which a dual solution of the matching algorithm and it can be
1054 /// used to proof the optimality of the given pimal solution.
1056 template <typename _BpUGraph, typename _CostMap, typename _Traits>
1058 template <typename _BpUGraph,
1059 typename _CostMap = typename _BpUGraph::template UEdgeMap<int>,
1060 typename _Traits = MinCostMaxBipartiteMatchingDefaultTraits<_BpUGraph, _CostMap> >
1062 class MinCostMaxBipartiteMatching {
1065 typedef _Traits Traits;
1066 typedef typename Traits::BpUGraph BpUGraph;
1067 typedef typename Traits::CostMap CostMap;
1068 typedef typename Traits::Value Value;
1072 typedef typename Traits::HeapCrossRef HeapCrossRef;
1073 typedef typename Traits::Heap Heap;
1076 typedef typename BpUGraph::Node Node;
1077 typedef typename BpUGraph::ANodeIt ANodeIt;
1078 typedef typename BpUGraph::BNodeIt BNodeIt;
1079 typedef typename BpUGraph::UEdge UEdge;
1080 typedef typename BpUGraph::UEdgeIt UEdgeIt;
1081 typedef typename BpUGraph::IncEdgeIt IncEdgeIt;
1083 typedef typename BpUGraph::template ANodeMap<UEdge> ANodeMatchingMap;
1084 typedef typename BpUGraph::template BNodeMap<UEdge> BNodeMatchingMap;
1086 typedef typename BpUGraph::template ANodeMap<Value> ANodePotentialMap;
1087 typedef typename BpUGraph::template BNodeMap<Value> BNodePotentialMap;
1092 /// \brief \ref Exception for uninitialized parameters.
1094 /// This error represents problems in the initialization
1095 /// of the parameters of the algorithms.
1096 class UninitializedParameter : public lemon::UninitializedParameter {
1098 virtual const char* exceptionName() const {
1099 return "lemon::MinCostMaxBipartiteMatching::UninitializedParameter";
1103 ///\name Named template parameters
1107 template <class H, class CR>
1108 struct DefHeapTraits : public Traits {
1109 typedef CR HeapCrossRef;
1111 static HeapCrossRef *createHeapCrossRef(const BpUGraph &) {
1112 throw UninitializedParameter();
1114 static Heap *createHeap(HeapCrossRef &) {
1115 throw UninitializedParameter();
1119 /// \brief \ref named-templ-param "Named parameter" for setting heap
1120 /// and cross reference type
1122 /// \ref named-templ-param "Named parameter" for setting heap and cross
1124 template <class H, class CR = typename BpUGraph::template NodeMap<int> >
1126 : public MinCostMaxBipartiteMatching<BpUGraph, CostMap,
1127 DefHeapTraits<H, CR> > {
1128 typedef MinCostMaxBipartiteMatching<BpUGraph, CostMap,
1129 DefHeapTraits<H, CR> > Create;
1132 template <class H, class CR>
1133 struct DefStandardHeapTraits : public Traits {
1134 typedef CR HeapCrossRef;
1136 static HeapCrossRef *createHeapCrossRef(const BpUGraph &graph) {
1137 return new HeapCrossRef(graph);
1139 static Heap *createHeap(HeapCrossRef &crossref) {
1140 return new Heap(crossref);
1144 /// \brief \ref named-templ-param "Named parameter" for setting heap and
1145 /// cross reference type with automatic allocation
1147 /// \ref named-templ-param "Named parameter" for setting heap and cross
1148 /// reference type. It can allocate the heap and the cross reference
1149 /// object if the cross reference's constructor waits for the graph as
1150 /// parameter and the heap's constructor waits for the cross reference.
1151 template <class H, class CR = typename BpUGraph::template NodeMap<int> >
1152 struct DefStandardHeap
1153 : public MinCostMaxBipartiteMatching<BpUGraph, CostMap,
1154 DefStandardHeapTraits<H, CR> > {
1155 typedef MinCostMaxBipartiteMatching<BpUGraph, CostMap,
1156 DefStandardHeapTraits<H, CR> >
1163 /// \brief Constructor.
1165 /// Constructor of the algorithm.
1166 MinCostMaxBipartiteMatching(const BpUGraph& _graph,
1167 const CostMap& _cost)
1168 : graph(&_graph), cost(&_cost),
1169 anode_matching(_graph), bnode_matching(_graph),
1170 anode_potential(_graph), bnode_potential(_graph),
1171 _heap_cross_ref(0), local_heap_cross_ref(false),
1172 _heap(0), local_heap(0) {}
1174 /// \brief Destructor.
1176 /// Destructor of the algorithm.
1177 ~MinCostMaxBipartiteMatching() {
1178 destroyStructures();
1181 /// \brief Sets the heap and the cross reference used by algorithm.
1183 /// Sets the heap and the cross reference used by algorithm.
1184 /// If you don't use this function before calling \ref run(),
1185 /// it will allocate one. The destuctor deallocates this
1186 /// automatically allocated map, of course.
1187 /// \return \c (*this)
1188 MinCostMaxBipartiteMatching& heap(Heap& heap, HeapCrossRef &crossRef) {
1189 if(local_heap_cross_ref) {
1190 delete _heap_cross_ref;
1191 local_heap_cross_ref = false;
1193 _heap_cross_ref = &crossRef;
1202 /// \name Execution control
1203 /// The simplest way to execute the algorithm is to use
1204 /// one of the member functions called \c run().
1206 /// If you need more control on the execution,
1207 /// first you must call \ref init() or one alternative for it.
1208 /// Finally \ref start() will perform the matching computation or
1209 /// with step-by-step execution you can augment the solution.
1213 /// \brief Initalize the data structures.
1215 /// It initalizes the data structures and creates an empty matching.
1218 for (ANodeIt it(*graph); it != INVALID; ++it) {
1219 anode_matching[it] = INVALID;
1220 anode_potential[it] = 0;
1222 for (BNodeIt it(*graph); it != INVALID; ++it) {
1223 bnode_matching[it] = INVALID;
1224 bnode_potential[it] = 0;
1231 /// \brief An augmenting phase of the costed matching algorithm
1233 /// It runs an augmenting phase of the matching algorithm. The
1234 /// phase finds the best augmenting path and augments only on this
1237 /// The algorithm consists at most
1238 /// of \f$ O(n) \f$ phase and one phase is \f$ O(n\log(n)+e) \f$
1239 /// long with Fibonacci heap or \f$ O((n+e)\log(n)) \f$ long
1240 /// with binary heap.
1243 typename BpUGraph::template BNodeMap<Value> bdist(*graph);
1244 typename BpUGraph::template BNodeMap<UEdge> bpred(*graph, INVALID);
1246 Node bestNode = INVALID;
1247 Value bestValue = 0;
1250 for (ANodeIt it(*graph); it != INVALID; ++it) {
1251 (*_heap_cross_ref)[it] = Heap::PRE_HEAP;
1254 for (ANodeIt it(*graph); it != INVALID; ++it) {
1255 if (anode_matching[it] == INVALID) {
1260 while (!_heap->empty()) {
1261 Node anode = _heap->top();
1262 Value avalue = _heap->prio();
1264 for (IncEdgeIt jt(*graph, anode); jt != INVALID; ++jt) {
1265 if (jt == anode_matching[anode]) continue;
1266 Node bnode = graph->bNode(jt);
1267 Value bvalue = avalue + (*cost)[jt] +
1268 anode_potential[anode] - bnode_potential[bnode];
1269 if (bpred[bnode] == INVALID || bvalue < bdist[bnode]) {
1270 bdist[bnode] = bvalue;
1273 if (bnode_matching[bnode] != INVALID) {
1274 Node newanode = graph->aNode(bnode_matching[bnode]);
1275 switch (_heap->state(newanode)) {
1276 case Heap::PRE_HEAP:
1277 _heap->push(newanode, bvalue);
1280 if (bvalue < (*_heap)[newanode]) {
1281 _heap->decrease(newanode, bvalue);
1284 case Heap::POST_HEAP:
1288 if (bestNode == INVALID ||
1289 bvalue + bnode_potential[bnode] < bestValue) {
1290 bestValue = bvalue + bnode_potential[bnode];
1297 if (bestNode == INVALID) {
1301 matching_cost += bestValue;
1304 for (BNodeIt it(*graph); it != INVALID; ++it) {
1305 if (bpred[it] != INVALID) {
1306 bnode_potential[it] += bdist[it];
1309 for (ANodeIt it(*graph); it != INVALID; ++it) {
1310 if (anode_matching[it] != INVALID) {
1311 Node bnode = graph->bNode(anode_matching[it]);
1312 if (bpred[bnode] != INVALID) {
1313 anode_potential[it] += bdist[bnode];
1318 while (bestNode != INVALID) {
1319 UEdge uedge = bpred[bestNode];
1320 Node anode = graph->aNode(uedge);
1322 bnode_matching[bestNode] = uedge;
1323 if (anode_matching[anode] != INVALID) {
1324 bestNode = graph->bNode(anode_matching[anode]);
1328 anode_matching[anode] = uedge;
1335 /// \brief Starts the algorithm.
1337 /// Starts the algorithm. It runs augmenting phases until the
1338 /// optimal solution reached.
1340 while (augment()) {}
1343 /// \brief Runs the algorithm.
1345 /// It just initalize the algorithm and then start it.
1353 /// \name Query Functions
1354 /// The result of the %Matching algorithm can be obtained using these
1356 /// Before the use of these functions,
1357 /// either run() or start() must be called.
1361 /// \brief Gives back the potential in the NodeMap
1363 /// Gives back the potential in the NodeMap. The potential
1364 /// is feasible if \f$ \pi(a) - \pi(b) + w(ab) = 0 \f$ for
1365 /// each matching edges and \f$ \pi(a) - \pi(b) + w(ab) \ge 0 \f$
1367 template <typename PotentialMap>
1368 void potential(PotentialMap& potential) {
1369 for (ANodeIt it(*graph); it != INVALID; ++it) {
1370 potential[it] = anode_potential[it];
1372 for (BNodeIt it(*graph); it != INVALID; ++it) {
1373 potential[it] = bnode_potential[it];
1377 /// \brief Set true all matching uedge in the map.
1379 /// Set true all matching uedge in the map. It does not change the
1380 /// value mapped to the other uedges.
1381 /// \return The number of the matching edges.
1382 template <typename MatchingMap>
1383 int quickMatching(MatchingMap& matching) {
1384 for (ANodeIt it(*graph); it != INVALID; ++it) {
1385 if (anode_matching[it] != INVALID) {
1386 matching[anode_matching[it]] = true;
1389 return matching_size;
1392 /// \brief Set true all matching uedge in the map and the others to false.
1394 /// Set true all matching uedge in the map and the others to false.
1395 /// \return The number of the matching edges.
1396 template <typename MatchingMap>
1397 int matching(MatchingMap& matching) {
1398 for (UEdgeIt it(*graph); it != INVALID; ++it) {
1399 matching[it] = it == anode_matching[graph->aNode(it)];
1401 return matching_size;
1405 /// \brief Return true if the given uedge is in the matching.
1407 /// It returns true if the given uedge is in the matching.
1408 bool matchingEdge(const UEdge& edge) {
1409 return anode_matching[graph->aNode(edge)] == edge;
1412 /// \brief Returns the matching edge from the node.
1414 /// Returns the matching edge from the node. If there is not such
1415 /// edge it gives back \c INVALID.
1416 UEdge matchingEdge(const Node& node) {
1417 if (graph->aNode(node)) {
1418 return anode_matching[node];
1420 return bnode_matching[node];
1424 /// \brief Gives back the sum of costs of the matching edges.
1426 /// Gives back the sum of costs of the matching edges.
1427 Value matchingCost() const {
1428 return matching_cost;
1431 /// \brief Gives back the number of the matching edges.
1433 /// Gives back the number of the matching edges.
1434 int matchingSize() const {
1435 return matching_size;
1442 void initStructures() {
1443 if (!_heap_cross_ref) {
1444 local_heap_cross_ref = true;
1445 _heap_cross_ref = Traits::createHeapCrossRef(*graph);
1449 _heap = Traits::createHeap(*_heap_cross_ref);
1453 void destroyStructures() {
1454 if (local_heap_cross_ref) delete _heap_cross_ref;
1455 if (local_heap) delete _heap;
1461 const BpUGraph *graph;
1462 const CostMap* cost;
1464 ANodeMatchingMap anode_matching;
1465 BNodeMatchingMap bnode_matching;
1467 ANodePotentialMap anode_potential;
1468 BNodePotentialMap bnode_potential;
1470 Value matching_cost;
1473 HeapCrossRef *_heap_cross_ref;
1474 bool local_heap_cross_ref;