deba@326: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@326: * deba@326: * This file is a part of LEMON, a generic C++ optimization library. deba@326: * deba@326: * Copyright (C) 2003-2008 deba@326: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@326: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@326: * deba@326: * Permission to use, modify and distribute this software is granted deba@326: * provided that this copyright notice appears in all copies. For deba@326: * precise terms see the accompanying LICENSE file. deba@326: * deba@326: * This software is provided "AS IS" with no warranty of any kind, deba@326: * express or implied, and with no claim as to its suitability for any deba@326: * purpose. deba@326: * deba@326: */ deba@326: deba@326: #ifndef LEMON_MAX_MATCHING_H deba@326: #define LEMON_MAX_MATCHING_H deba@326: deba@326: #include deba@326: #include deba@326: #include deba@326: #include deba@326: deba@326: #include deba@326: #include deba@326: #include deba@326: #include deba@326: deba@326: ///\ingroup matching deba@326: ///\file deba@326: ///\brief Maximum matching algorithms in graph. deba@326: deba@326: namespace lemon { deba@326: deba@326: ///\ingroup matching deba@326: /// deba@326: ///\brief Edmonds' alternating forest maximum matching algorithm. deba@326: /// deba@326: ///This class provides Edmonds' alternating forest matching deba@326: ///algorithm. The starting matching (if any) can be passed to the deba@326: ///algorithm using some of init functions. deba@326: /// deba@326: ///The dual side of a matching is a map of the nodes to deba@326: ///MaxMatching::DecompType, having values \c D, \c A and \c C deba@326: ///showing the Gallai-Edmonds decomposition of the digraph. The nodes deba@326: ///in \c D induce a digraph with factor-critical components, the nodes deba@326: ///in \c A form the barrier, and the nodes in \c C induce a digraph deba@326: ///having a perfect matching. This decomposition can be attained by deba@326: ///calling \c decomposition() after running the algorithm. deba@326: /// deba@326: ///\param Digraph The graph type the algorithm runs on. deba@326: template deba@326: class MaxMatching { deba@326: deba@326: protected: deba@326: deba@326: TEMPLATE_GRAPH_TYPEDEFS(Graph); deba@326: deba@326: typedef typename Graph::template NodeMap UFECrossRef; deba@326: typedef UnionFindEnum UFE; deba@326: typedef std::vector NV; deba@326: deba@326: typedef typename Graph::template NodeMap EFECrossRef; deba@326: typedef ExtendFindEnum EFE; deba@326: deba@326: public: deba@326: deba@326: ///\brief Indicates the Gallai-Edmonds decomposition of the digraph. deba@326: /// deba@326: ///Indicates the Gallai-Edmonds decomposition of the digraph, which deba@326: ///shows an upper bound on the size of a maximum matching. The deba@326: ///nodes with DecompType \c D induce a digraph with factor-critical deba@326: ///components, the nodes in \c A form the canonical barrier, and the deba@326: ///nodes in \c C induce a digraph having a perfect matching. deba@326: enum DecompType { deba@326: D=0, deba@326: A=1, deba@326: C=2 deba@326: }; deba@326: deba@326: protected: deba@326: deba@326: static const int HEUR_density=2; deba@326: const Graph& g; deba@326: typename Graph::template NodeMap _mate; deba@326: typename Graph::template NodeMap position; deba@326: deba@326: public: deba@326: deba@326: MaxMatching(const Graph& _g) deba@326: : g(_g), _mate(_g), position(_g) {} deba@326: deba@326: ///\brief Sets the actual matching to the empty matching. deba@326: /// deba@326: ///Sets the actual matching to the empty matching. deba@326: /// deba@326: void init() { deba@326: for(NodeIt v(g); v!=INVALID; ++v) { deba@326: _mate.set(v,INVALID); deba@326: position.set(v,C); deba@326: } deba@326: } deba@326: deba@326: ///\brief Finds a greedy matching for initial matching. deba@326: /// deba@326: ///For initial matchig it finds a maximal greedy matching. deba@326: void greedyInit() { deba@326: for(NodeIt v(g); v!=INVALID; ++v) { deba@326: _mate.set(v,INVALID); deba@326: position.set(v,C); deba@326: } deba@326: for(NodeIt v(g); v!=INVALID; ++v) deba@326: if ( _mate[v]==INVALID ) { deba@326: for( IncEdgeIt e(g,v); e!=INVALID ; ++e ) { deba@326: Node y=g.runningNode(e); deba@326: if ( _mate[y]==INVALID && y!=v ) { deba@326: _mate.set(v,y); deba@326: _mate.set(y,v); deba@326: break; deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@326: ///\brief Initialize the matching from each nodes' mate. deba@326: /// deba@326: ///Initialize the matching from a \c Node valued \c Node map. This deba@326: ///map must be \e symmetric, i.e. if \c map[u]==v then \c deba@326: ///map[v]==u must hold, and \c uv will be an arc of the initial deba@326: ///matching. deba@326: template deba@326: void mateMapInit(MateMap& map) { deba@326: for(NodeIt v(g); v!=INVALID; ++v) { deba@326: _mate.set(v,map[v]); deba@326: position.set(v,C); deba@326: } deba@326: } deba@326: deba@326: ///\brief Initialize the matching from a node map with the deba@326: ///incident matching arcs. deba@326: /// deba@326: ///Initialize the matching from an \c Edge valued \c Node map. \c deba@326: ///map[v] must be an \c Edge incident to \c v. This map must have deba@326: ///the property that if \c g.oppositeNode(u,map[u])==v then \c \c deba@326: ///g.oppositeNode(v,map[v])==u holds, and now some arc joining \c deba@326: ///u to \c v will be an arc of the matching. deba@326: template deba@326: void matchingMapInit(MatchingMap& map) { deba@326: for(NodeIt v(g); v!=INVALID; ++v) { deba@326: position.set(v,C); deba@326: Edge e=map[v]; deba@326: if ( e!=INVALID ) deba@326: _mate.set(v,g.oppositeNode(v,e)); deba@326: else deba@326: _mate.set(v,INVALID); deba@326: } deba@326: } deba@326: deba@326: ///\brief Initialize the matching from the map containing the deba@326: ///undirected matching arcs. deba@326: /// deba@326: ///Initialize the matching from a \c bool valued \c Edge map. This deba@326: ///map must have the property that there are no two incident arcs deba@326: ///\c e, \c f with \c map[e]==map[f]==true. The arcs \c e with \c deba@326: ///map[e]==true form the matching. deba@326: template deba@326: void matchingInit(MatchingMap& map) { deba@326: for(NodeIt v(g); v!=INVALID; ++v) { deba@326: _mate.set(v,INVALID); deba@326: position.set(v,C); deba@326: } deba@326: for(EdgeIt e(g); e!=INVALID; ++e) { deba@326: if ( map[e] ) { deba@326: Node u=g.u(e); deba@326: Node v=g.v(e); deba@326: _mate.set(u,v); deba@326: _mate.set(v,u); deba@326: } deba@326: } deba@326: } deba@326: deba@326: deba@326: ///\brief Runs Edmonds' algorithm. deba@326: /// deba@326: ///Runs Edmonds' algorithm for sparse digraphs (number of arcs < deba@326: ///2*number of nodes), and a heuristical Edmonds' algorithm with a deba@326: ///heuristic of postponing shrinks for dense digraphs. deba@326: void run() { deba@326: if (countEdges(g) < HEUR_density * countNodes(g)) { deba@326: greedyInit(); deba@326: startSparse(); deba@326: } else { deba@326: init(); deba@326: startDense(); deba@326: } deba@326: } deba@326: deba@326: deba@326: ///\brief Starts Edmonds' algorithm. deba@326: /// deba@326: ///If runs the original Edmonds' algorithm. deba@326: void startSparse() { deba@326: deba@326: typename Graph::template NodeMap ear(g,INVALID); deba@326: //undefined for the base nodes of the blossoms (i.e. for the deba@326: //representative elements of UFE blossom) and for the nodes in C deba@326: deba@326: UFECrossRef blossom_base(g); deba@326: UFE blossom(blossom_base); deba@326: NV rep(countNodes(g)); deba@326: deba@326: EFECrossRef tree_base(g); deba@326: EFE tree(tree_base); deba@326: deba@326: //If these UFE's would be members of the class then also deba@326: //blossom_base and tree_base should be a member. deba@326: deba@326: //We build only one tree and the other vertices uncovered by the deba@326: //matching belong to C. (They can be considered as singleton deba@326: //trees.) If this tree can be augmented or no more deba@326: //grow/augmentation/shrink is possible then we return to this deba@326: //"for" cycle. deba@326: for(NodeIt v(g); v!=INVALID; ++v) { deba@326: if (position[v]==C && _mate[v]==INVALID) { deba@326: rep[blossom.insert(v)] = v; deba@326: tree.insert(v); deba@326: position.set(v,D); deba@326: normShrink(v, ear, blossom, rep, tree); deba@326: } deba@326: } deba@326: } deba@326: deba@326: ///\brief Starts Edmonds' algorithm. deba@326: /// deba@326: ///It runs Edmonds' algorithm with a heuristic of postponing deba@326: ///shrinks, giving a faster algorithm for dense digraphs. deba@326: void startDense() { deba@326: deba@326: typename Graph::template NodeMap ear(g,INVALID); deba@326: //undefined for the base nodes of the blossoms (i.e. for the deba@326: //representative elements of UFE blossom) and for the nodes in C deba@326: deba@326: UFECrossRef blossom_base(g); deba@326: UFE blossom(blossom_base); deba@326: NV rep(countNodes(g)); deba@326: deba@326: EFECrossRef tree_base(g); deba@326: EFE tree(tree_base); deba@326: deba@326: //If these UFE's would be members of the class then also deba@326: //blossom_base and tree_base should be a member. deba@326: deba@326: //We build only one tree and the other vertices uncovered by the deba@326: //matching belong to C. (They can be considered as singleton deba@326: //trees.) If this tree can be augmented or no more deba@326: //grow/augmentation/shrink is possible then we return to this deba@326: //"for" cycle. deba@326: for(NodeIt v(g); v!=INVALID; ++v) { deba@326: if ( position[v]==C && _mate[v]==INVALID ) { deba@326: rep[blossom.insert(v)] = v; deba@326: tree.insert(v); deba@326: position.set(v,D); deba@326: lateShrink(v, ear, blossom, rep, tree); deba@326: } deba@326: } deba@326: } deba@326: deba@326: deba@326: deba@326: ///\brief Returns the size of the actual matching stored. deba@326: /// deba@326: ///Returns the size of the actual matching stored. After \ref deba@326: ///run() it returns the size of a maximum matching in the digraph. deba@326: int size() const { deba@326: int s=0; deba@326: for(NodeIt v(g); v!=INVALID; ++v) { deba@326: if ( _mate[v]!=INVALID ) { deba@326: ++s; deba@326: } deba@326: } deba@326: return s/2; deba@326: } deba@326: deba@326: deba@326: ///\brief Returns the mate of a node in the actual matching. deba@326: /// deba@326: ///Returns the mate of a \c node in the actual matching. deba@326: ///Returns INVALID if the \c node is not covered by the actual matching. deba@326: Node mate(const Node& node) const { deba@326: return _mate[node]; deba@326: } deba@326: deba@326: ///\brief Returns the matching arc incident to the given node. deba@326: /// deba@326: ///Returns the matching arc of a \c node in the actual matching. deba@326: ///Returns INVALID if the \c node is not covered by the actual matching. deba@326: Edge matchingArc(const Node& node) const { deba@326: if (_mate[node] == INVALID) return INVALID; deba@326: Node n = node < _mate[node] ? node : _mate[node]; deba@326: for (IncEdgeIt e(g, n); e != INVALID; ++e) { deba@326: if (g.oppositeNode(n, e) == _mate[n]) { deba@326: return e; deba@326: } deba@326: } deba@326: return INVALID; deba@326: } deba@326: deba@326: /// \brief Returns the class of the node in the Edmonds-Gallai deba@326: /// decomposition. deba@326: /// deba@326: /// Returns the class of the node in the Edmonds-Gallai deba@326: /// decomposition. deba@326: DecompType decomposition(const Node& n) { deba@326: return position[n] == A; deba@326: } deba@326: deba@326: /// \brief Returns true when the node is in the barrier. deba@326: /// deba@326: /// Returns true when the node is in the barrier. deba@326: bool barrier(const Node& n) { deba@326: return position[n] == A; deba@326: } deba@326: deba@326: ///\brief Gives back the matching in a \c Node of mates. deba@326: /// deba@326: ///Writes the stored matching to a \c Node valued \c Node map. The deba@326: ///resulting map will be \e symmetric, i.e. if \c map[u]==v then \c deba@326: ///map[v]==u will hold, and now \c uv is an arc of the matching. deba@326: template deba@326: void mateMap(MateMap& map) const { deba@326: for(NodeIt v(g); v!=INVALID; ++v) { deba@326: map.set(v,_mate[v]); deba@326: } deba@326: } deba@326: deba@326: ///\brief Gives back the matching in an \c Edge valued \c Node deba@326: ///map. deba@326: /// deba@326: ///Writes the stored matching to an \c Edge valued \c Node deba@326: ///map. \c map[v] will be an \c Edge incident to \c v. This deba@326: ///map will have the property that if \c g.oppositeNode(u,map[u]) deba@326: ///== v then \c map[u]==map[v] holds, and now this arc is an arc deba@326: ///of the matching. deba@326: template deba@326: void matchingMap(MatchingMap& map) const { deba@326: typename Graph::template NodeMap todo(g,true); deba@326: for(NodeIt v(g); v!=INVALID; ++v) { deba@326: if (_mate[v]!=INVALID && v < _mate[v]) { deba@326: Node u=_mate[v]; deba@326: for(IncEdgeIt e(g,v); e!=INVALID; ++e) { deba@326: if ( g.runningNode(e) == u ) { deba@326: map.set(u,e); deba@326: map.set(v,e); deba@326: todo.set(u,false); deba@326: todo.set(v,false); deba@326: break; deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@326: deba@326: ///\brief Gives back the matching in a \c bool valued \c Edge deba@326: ///map. deba@326: /// deba@326: ///Writes the matching stored to a \c bool valued \c Arc deba@326: ///map. This map will have the property that there are no two deba@326: ///incident arcs \c e, \c f with \c map[e]==map[f]==true. The deba@326: ///arcs \c e with \c map[e]==true form the matching. deba@326: template deba@326: void matching(MatchingMap& map) const { deba@326: for(EdgeIt e(g); e!=INVALID; ++e) map.set(e,false); deba@326: deba@326: typename Graph::template NodeMap todo(g,true); deba@326: for(NodeIt v(g); v!=INVALID; ++v) { deba@326: if ( todo[v] && _mate[v]!=INVALID ) { deba@326: Node u=_mate[v]; deba@326: for(IncEdgeIt e(g,v); e!=INVALID; ++e) { deba@326: if ( g.runningNode(e) == u ) { deba@326: map.set(e,true); deba@326: todo.set(u,false); deba@326: todo.set(v,false); deba@326: break; deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@326: deba@326: ///\brief Returns the canonical decomposition of the digraph after running deba@326: ///the algorithm. deba@326: /// deba@326: ///After calling any run methods of the class, it writes the deba@326: ///Gallai-Edmonds canonical decomposition of the digraph. \c map deba@326: ///must be a node map of \ref DecompType 's. deba@326: template deba@326: void decomposition(DecompositionMap& map) const { deba@326: for(NodeIt v(g); v!=INVALID; ++v) map.set(v,position[v]); deba@326: } deba@326: deba@326: ///\brief Returns a barrier on the nodes. deba@326: /// deba@326: ///After calling any run methods of the class, it writes a deba@326: ///canonical barrier on the nodes. The odd component number of the deba@326: ///remaining digraph minus the barrier size is a lower bound for the deba@326: ///uncovered nodes in the digraph. The \c map must be a node map of deba@326: ///bools. deba@326: template deba@326: void barrier(BarrierMap& barrier) { deba@326: for(NodeIt v(g); v!=INVALID; ++v) barrier.set(v,position[v] == A); deba@326: } deba@326: deba@326: private: deba@326: deba@326: deba@326: void lateShrink(Node v, typename Graph::template NodeMap& ear, deba@326: UFE& blossom, NV& rep, EFE& tree) { deba@326: //We have one tree which we grow, and also shrink but only if it deba@326: //cannot be postponed. If we augment then we return to the "for" deba@326: //cycle of runEdmonds(). deba@326: deba@326: std::queue Q; //queue of the totally unscanned nodes deba@326: Q.push(v); deba@326: std::queue R; deba@326: //queue of the nodes which must be scanned for a possible shrink deba@326: deba@326: while ( !Q.empty() ) { deba@326: Node x=Q.front(); deba@326: Q.pop(); deba@326: for( IncEdgeIt e(g,x); e!= INVALID; ++e ) { deba@326: Node y=g.runningNode(e); deba@326: //growOrAugment grows if y is covered by the matching and deba@326: //augments if not. In this latter case it returns 1. deba@326: if (position[y]==C && deba@326: growOrAugment(y, x, ear, blossom, rep, tree, Q)) return; deba@326: } deba@326: R.push(x); deba@326: } deba@326: deba@326: while ( !R.empty() ) { deba@326: Node x=R.front(); deba@326: R.pop(); deba@326: deba@326: for( IncEdgeIt e(g,x); e!=INVALID ; ++e ) { deba@326: Node y=g.runningNode(e); deba@326: deba@326: if ( position[y] == D && blossom.find(x) != blossom.find(y) ) deba@326: //Recall that we have only one tree. deba@326: shrink( x, y, ear, blossom, rep, tree, Q); deba@326: deba@326: while ( !Q.empty() ) { deba@326: Node z=Q.front(); deba@326: Q.pop(); deba@326: for( IncEdgeIt f(g,z); f!= INVALID; ++f ) { deba@326: Node w=g.runningNode(f); deba@326: //growOrAugment grows if y is covered by the matching and deba@326: //augments if not. In this latter case it returns 1. deba@326: if (position[w]==C && deba@326: growOrAugment(w, z, ear, blossom, rep, tree, Q)) return; deba@326: } deba@326: R.push(z); deba@326: } deba@326: } //for e deba@326: } // while ( !R.empty() ) deba@326: } deba@326: deba@326: void normShrink(Node v, typename Graph::template NodeMap& ear, deba@326: UFE& blossom, NV& rep, EFE& tree) { deba@326: //We have one tree, which we grow and shrink. If we augment then we deba@326: //return to the "for" cycle of runEdmonds(). deba@326: deba@326: std::queue Q; //queue of the unscanned nodes deba@326: Q.push(v); deba@326: while ( !Q.empty() ) { deba@326: deba@326: Node x=Q.front(); deba@326: Q.pop(); deba@326: deba@326: for( IncEdgeIt e(g,x); e!=INVALID; ++e ) { deba@326: Node y=g.runningNode(e); deba@326: deba@326: switch ( position[y] ) { deba@326: case D: //x and y must be in the same tree deba@326: if ( blossom.find(x) != blossom.find(y)) deba@326: //x and y are in the same tree deba@326: shrink(x, y, ear, blossom, rep, tree, Q); deba@326: break; deba@326: case C: deba@326: //growOrAugment grows if y is covered by the matching and deba@326: //augments if not. In this latter case it returns 1. deba@326: if (growOrAugment(y, x, ear, blossom, rep, tree, Q)) return; deba@326: break; deba@326: default: break; deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@326: void shrink(Node x,Node y, typename Graph::template NodeMap& ear, deba@326: UFE& blossom, NV& rep, EFE& tree,std::queue& Q) { deba@326: //x and y are the two adjacent vertices in two blossoms. deba@326: deba@326: typename Graph::template NodeMap path(g,false); deba@326: deba@326: Node b=rep[blossom.find(x)]; deba@326: path.set(b,true); deba@326: b=_mate[b]; deba@326: while ( b!=INVALID ) { deba@326: b=rep[blossom.find(ear[b])]; deba@326: path.set(b,true); deba@326: b=_mate[b]; deba@326: } //we go until the root through bases of blossoms and odd vertices deba@326: deba@326: Node top=y; deba@326: Node middle=rep[blossom.find(top)]; deba@326: Node bottom=x; deba@326: while ( !path[middle] ) deba@326: shrinkStep(top, middle, bottom, ear, blossom, rep, tree, Q); deba@326: //Until we arrive to a node on the path, we update blossom, tree deba@326: //and the positions of the odd nodes. deba@326: deba@326: Node base=middle; deba@326: top=x; deba@326: middle=rep[blossom.find(top)]; deba@326: bottom=y; deba@326: Node blossom_base=rep[blossom.find(base)]; deba@326: while ( middle!=blossom_base ) deba@326: shrinkStep(top, middle, bottom, ear, blossom, rep, tree, Q); deba@326: //Until we arrive to a node on the path, we update blossom, tree deba@326: //and the positions of the odd nodes. deba@326: deba@326: rep[blossom.find(base)] = base; deba@326: } deba@326: deba@326: void shrinkStep(Node& top, Node& middle, Node& bottom, deba@326: typename Graph::template NodeMap& ear, deba@326: UFE& blossom, NV& rep, EFE& tree, std::queue& Q) { deba@326: //We traverse a blossom and update everything. deba@326: deba@326: ear.set(top,bottom); deba@326: Node t=top; deba@326: while ( t!=middle ) { deba@326: Node u=_mate[t]; deba@326: t=ear[u]; deba@326: ear.set(t,u); deba@326: } deba@326: bottom=_mate[middle]; deba@326: position.set(bottom,D); deba@326: Q.push(bottom); deba@326: top=ear[bottom]; deba@326: Node oldmiddle=middle; deba@326: middle=rep[blossom.find(top)]; deba@326: tree.erase(bottom); deba@326: tree.erase(oldmiddle); deba@326: blossom.insert(bottom); deba@326: blossom.join(bottom, oldmiddle); deba@326: blossom.join(top, oldmiddle); deba@326: } deba@326: deba@326: deba@326: deba@326: bool growOrAugment(Node& y, Node& x, typename Graph::template deba@326: NodeMap& ear, UFE& blossom, NV& rep, EFE& tree, deba@326: std::queue& Q) { deba@326: //x is in a blossom in the tree, y is outside. If y is covered by deba@326: //the matching we grow, otherwise we augment. In this case we deba@326: //return 1. deba@326: deba@326: if ( _mate[y]!=INVALID ) { //grow deba@326: ear.set(y,x); deba@326: Node w=_mate[y]; deba@326: rep[blossom.insert(w)] = w; deba@326: position.set(y,A); deba@326: position.set(w,D); deba@326: int t = tree.find(rep[blossom.find(x)]); deba@326: tree.insert(y,t); deba@326: tree.insert(w,t); deba@326: Q.push(w); deba@326: } else { //augment deba@326: augment(x, ear, blossom, rep, tree); deba@326: _mate.set(x,y); deba@326: _mate.set(y,x); deba@326: return true; deba@326: } deba@326: return false; deba@326: } deba@326: deba@326: void augment(Node x, typename Graph::template NodeMap& ear, deba@326: UFE& blossom, NV& rep, EFE& tree) { deba@326: Node v=_mate[x]; deba@326: while ( v!=INVALID ) { deba@326: deba@326: Node u=ear[v]; deba@326: _mate.set(v,u); deba@326: Node tmp=v; deba@326: v=_mate[u]; deba@326: _mate.set(u,tmp); deba@326: } deba@326: int y = tree.find(rep[blossom.find(x)]); deba@326: for (typename EFE::ItemIt tit(tree, y); tit != INVALID; ++tit) { deba@326: if ( position[tit] == D ) { deba@326: int b = blossom.find(tit); deba@326: for (typename UFE::ItemIt bit(blossom, b); bit != INVALID; ++bit) { deba@326: position.set(bit, C); deba@326: } deba@326: blossom.eraseClass(b); deba@326: } else position.set(tit, C); deba@326: } deba@326: tree.eraseClass(y); deba@326: deba@326: } deba@326: deba@326: }; deba@326: deba@326: /// \ingroup matching deba@326: /// deba@326: /// \brief Weighted matching in general graphs deba@326: /// deba@326: /// This class provides an efficient implementation of Edmond's deba@326: /// maximum weighted matching algorithm. The implementation is based deba@326: /// on extensive use of priority queues and provides deba@326: /// \f$O(nm\log(n))\f$ time complexity. deba@326: /// deba@326: /// The maximum weighted matching problem is to find undirected deba@326: /// arcs in the digraph with maximum overall weight and no two of deba@326: /// them shares their endpoints. The problem can be formulated with deba@326: /// the next linear program: deba@326: /// \f[ \sum_{e \in \delta(u)}x_e \le 1 \quad \forall u\in V\f] deba@326: ///\f[ \sum_{e \in \gamma(B)}x_e \le \frac{\vert B \vert - 1}{2} \quad \forall B\in\mathcal{O}\f] deba@326: /// \f[x_e \ge 0\quad \forall e\in E\f] deba@326: /// \f[\max \sum_{e\in E}x_ew_e\f] deba@326: /// where \f$\delta(X)\f$ is the set of arcs incident to a node in deba@326: /// \f$X\f$, \f$\gamma(X)\f$ is the set of arcs with both endpoints in deba@326: /// \f$X\f$ and \f$\mathcal{O}\f$ is the set of odd cardinality subsets of deba@326: /// the nodes. deba@326: /// deba@326: /// The algorithm calculates an optimal matching and a proof of the deba@326: /// optimality. The solution of the dual problem can be used to check deba@326: /// the result of the algorithm. The dual linear problem is the next: deba@326: /// \f[ y_u + y_v + \sum_{B \in \mathcal{O}, uv \in \gamma(B)}z_B \ge w_{uv} \quad \forall uv\in E\f] deba@326: /// \f[y_u \ge 0 \quad \forall u \in V\f] deba@326: /// \f[z_B \ge 0 \quad \forall B \in \mathcal{O}\f] deba@326: /// \f[\min \sum_{u \in V}y_u + \sum_{B \in \mathcal{O}}\frac{\vert B \vert - 1}{2}z_B\f] deba@326: /// deba@326: /// The algorithm can be executed with \c run() or the \c init() and deba@326: /// then the \c start() member functions. After it the matching can deba@326: /// be asked with \c matching() or mate() functions. The dual deba@326: /// solution can be get with \c nodeValue(), \c blossomNum() and \c deba@326: /// blossomValue() members and \ref MaxWeightedMatching::BlossomIt deba@326: /// "BlossomIt" nested class which is able to iterate on the nodes deba@326: /// of a blossom. If the value type is integral then the dual deba@326: /// solution is multiplied by \ref MaxWeightedMatching::dualScale "4". deba@326: template > deba@326: class MaxWeightedMatching { deba@326: public: deba@326: deba@326: typedef _Graph Graph; deba@326: typedef _WeightMap WeightMap; deba@326: typedef typename WeightMap::Value Value; deba@326: deba@326: /// \brief Scaling factor for dual solution deba@326: /// deba@326: /// Scaling factor for dual solution, it is equal to 4 or 1 deba@326: /// according to the value type. deba@326: static const int dualScale = deba@326: std::numeric_limits::is_integer ? 4 : 1; deba@326: deba@326: typedef typename Graph::template NodeMap deba@326: MatchingMap; deba@326: deba@326: private: deba@326: deba@326: TEMPLATE_GRAPH_TYPEDEFS(Graph); deba@326: deba@326: typedef typename Graph::template NodeMap NodePotential; deba@326: typedef std::vector BlossomNodeList; deba@326: deba@326: struct BlossomVariable { deba@326: int begin, end; deba@326: Value value; deba@326: deba@326: BlossomVariable(int _begin, int _end, Value _value) deba@326: : begin(_begin), end(_end), value(_value) {} deba@326: deba@326: }; deba@326: deba@326: typedef std::vector BlossomPotential; deba@326: deba@326: const Graph& _graph; deba@326: const WeightMap& _weight; deba@326: deba@326: MatchingMap* _matching; deba@326: deba@326: NodePotential* _node_potential; deba@326: deba@326: BlossomPotential _blossom_potential; deba@326: BlossomNodeList _blossom_node_list; deba@326: deba@326: int _node_num; deba@326: int _blossom_num; deba@326: deba@326: typedef typename Graph::template NodeMap NodeIntMap; deba@326: typedef typename Graph::template ArcMap ArcIntMap; deba@326: typedef typename Graph::template EdgeMap EdgeIntMap; deba@326: typedef RangeMap IntIntMap; deba@326: deba@326: enum Status { deba@326: EVEN = -1, MATCHED = 0, ODD = 1, UNMATCHED = -2 deba@326: }; deba@326: deba@326: typedef HeapUnionFind BlossomSet; deba@326: struct BlossomData { deba@326: int tree; deba@326: Status status; deba@326: Arc pred, next; deba@326: Value pot, offset; deba@326: Node base; deba@326: }; deba@326: deba@326: NodeIntMap *_blossom_index; deba@326: BlossomSet *_blossom_set; deba@326: RangeMap* _blossom_data; deba@326: deba@326: NodeIntMap *_node_index; deba@326: ArcIntMap *_node_heap_index; deba@326: deba@326: struct NodeData { deba@326: deba@326: NodeData(ArcIntMap& node_heap_index) deba@326: : heap(node_heap_index) {} deba@326: deba@326: int blossom; deba@326: Value pot; deba@326: BinHeap heap; deba@326: std::map heap_index; deba@326: deba@326: int tree; deba@326: }; deba@326: deba@326: RangeMap* _node_data; deba@326: deba@326: typedef ExtendFindEnum TreeSet; deba@326: deba@326: IntIntMap *_tree_set_index; deba@326: TreeSet *_tree_set; deba@326: deba@326: NodeIntMap *_delta1_index; deba@326: BinHeap *_delta1; deba@326: deba@326: IntIntMap *_delta2_index; deba@326: BinHeap *_delta2; deba@326: deba@326: EdgeIntMap *_delta3_index; deba@326: BinHeap *_delta3; deba@326: deba@326: IntIntMap *_delta4_index; deba@326: BinHeap *_delta4; deba@326: deba@326: Value _delta_sum; deba@326: deba@326: void createStructures() { deba@326: _node_num = countNodes(_graph); deba@326: _blossom_num = _node_num * 3 / 2; deba@326: deba@326: if (!_matching) { deba@326: _matching = new MatchingMap(_graph); deba@326: } deba@326: if (!_node_potential) { deba@326: _node_potential = new NodePotential(_graph); deba@326: } deba@326: if (!_blossom_set) { deba@326: _blossom_index = new NodeIntMap(_graph); deba@326: _blossom_set = new BlossomSet(*_blossom_index); deba@326: _blossom_data = new RangeMap(_blossom_num); deba@326: } deba@326: deba@326: if (!_node_index) { deba@326: _node_index = new NodeIntMap(_graph); deba@326: _node_heap_index = new ArcIntMap(_graph); deba@326: _node_data = new RangeMap(_node_num, deba@326: NodeData(*_node_heap_index)); deba@326: } deba@326: deba@326: if (!_tree_set) { deba@326: _tree_set_index = new IntIntMap(_blossom_num); deba@326: _tree_set = new TreeSet(*_tree_set_index); deba@326: } deba@326: if (!_delta1) { deba@326: _delta1_index = new NodeIntMap(_graph); deba@326: _delta1 = new BinHeap(*_delta1_index); deba@326: } deba@326: if (!_delta2) { deba@326: _delta2_index = new IntIntMap(_blossom_num); deba@326: _delta2 = new BinHeap(*_delta2_index); deba@326: } deba@326: if (!_delta3) { deba@326: _delta3_index = new EdgeIntMap(_graph); deba@326: _delta3 = new BinHeap(*_delta3_index); deba@326: } deba@326: if (!_delta4) { deba@326: _delta4_index = new IntIntMap(_blossom_num); deba@326: _delta4 = new BinHeap(*_delta4_index); deba@326: } deba@326: } deba@326: deba@326: void destroyStructures() { deba@326: _node_num = countNodes(_graph); deba@326: _blossom_num = _node_num * 3 / 2; deba@326: deba@326: if (_matching) { deba@326: delete _matching; deba@326: } deba@326: if (_node_potential) { deba@326: delete _node_potential; deba@326: } deba@326: if (_blossom_set) { deba@326: delete _blossom_index; deba@326: delete _blossom_set; deba@326: delete _blossom_data; deba@326: } deba@326: deba@326: if (_node_index) { deba@326: delete _node_index; deba@326: delete _node_heap_index; deba@326: delete _node_data; deba@326: } deba@326: deba@326: if (_tree_set) { deba@326: delete _tree_set_index; deba@326: delete _tree_set; deba@326: } deba@326: if (_delta1) { deba@326: delete _delta1_index; deba@326: delete _delta1; deba@326: } deba@326: if (_delta2) { deba@326: delete _delta2_index; deba@326: delete _delta2; deba@326: } deba@326: if (_delta3) { deba@326: delete _delta3_index; deba@326: delete _delta3; deba@326: } deba@326: if (_delta4) { deba@326: delete _delta4_index; deba@326: delete _delta4; deba@326: } deba@326: } deba@326: deba@326: void matchedToEven(int blossom, int tree) { deba@326: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@326: _delta2->erase(blossom); deba@326: } deba@326: deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: (*_blossom_data)[blossom].pot -= deba@326: 2 * (_delta_sum - (*_blossom_data)[blossom].offset); deba@326: } deba@326: deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@326: n != INVALID; ++n) { deba@326: deba@326: _blossom_set->increase(n, std::numeric_limits::max()); deba@326: int ni = (*_node_index)[n]; deba@326: deba@326: (*_node_data)[ni].heap.clear(); deba@326: (*_node_data)[ni].heap_index.clear(); deba@326: deba@326: (*_node_data)[ni].pot += _delta_sum - (*_blossom_data)[blossom].offset; deba@326: deba@326: _delta1->push(n, (*_node_data)[ni].pot); deba@326: deba@326: for (InArcIt e(_graph, n); e != INVALID; ++e) { deba@326: Node v = _graph.source(e); deba@326: int vb = _blossom_set->find(v); deba@326: int vi = (*_node_index)[v]; deba@326: deba@326: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@326: dualScale * _weight[e]; deba@326: deba@326: if ((*_blossom_data)[vb].status == EVEN) { deba@326: if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) { deba@326: _delta3->push(e, rw / 2); deba@326: } deba@326: } else if ((*_blossom_data)[vb].status == UNMATCHED) { deba@326: if (_delta3->state(e) != _delta3->IN_HEAP) { deba@326: _delta3->push(e, rw); deba@326: } deba@326: } else { deba@326: typename std::map::iterator it = deba@326: (*_node_data)[vi].heap_index.find(tree); deba@326: deba@326: if (it != (*_node_data)[vi].heap_index.end()) { deba@326: if ((*_node_data)[vi].heap[it->second] > rw) { deba@326: (*_node_data)[vi].heap.replace(it->second, e); deba@326: (*_node_data)[vi].heap.decrease(e, rw); deba@326: it->second = e; deba@326: } deba@326: } else { deba@326: (*_node_data)[vi].heap.push(e, rw); deba@326: (*_node_data)[vi].heap_index.insert(std::make_pair(tree, e)); deba@326: } deba@326: deba@326: if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) { deba@326: _blossom_set->decrease(v, (*_node_data)[vi].heap.prio()); deba@326: deba@326: if ((*_blossom_data)[vb].status == MATCHED) { deba@326: if (_delta2->state(vb) != _delta2->IN_HEAP) { deba@326: _delta2->push(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset){ deba@326: _delta2->decrease(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: (*_blossom_data)[blossom].offset = 0; deba@326: } deba@326: deba@326: void matchedToOdd(int blossom) { deba@326: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@326: _delta2->erase(blossom); deba@326: } deba@326: (*_blossom_data)[blossom].offset += _delta_sum; deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: _delta4->push(blossom, (*_blossom_data)[blossom].pot / 2 + deba@326: (*_blossom_data)[blossom].offset); deba@326: } deba@326: } deba@326: deba@326: void evenToMatched(int blossom, int tree) { deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: (*_blossom_data)[blossom].pot += 2 * _delta_sum; deba@326: } deba@326: deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@326: n != INVALID; ++n) { deba@326: int ni = (*_node_index)[n]; deba@326: (*_node_data)[ni].pot -= _delta_sum; deba@326: deba@326: _delta1->erase(n); deba@326: deba@326: for (InArcIt e(_graph, n); e != INVALID; ++e) { deba@326: Node v = _graph.source(e); deba@326: int vb = _blossom_set->find(v); deba@326: int vi = (*_node_index)[v]; deba@326: deba@326: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@326: dualScale * _weight[e]; deba@326: deba@326: if (vb == blossom) { deba@326: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@326: _delta3->erase(e); deba@326: } deba@326: } else if ((*_blossom_data)[vb].status == EVEN) { deba@326: deba@326: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@326: _delta3->erase(e); deba@326: } deba@326: deba@326: int vt = _tree_set->find(vb); deba@326: deba@326: if (vt != tree) { deba@326: deba@326: Arc r = _graph.oppositeArc(e); deba@326: deba@326: typename std::map::iterator it = deba@326: (*_node_data)[ni].heap_index.find(vt); deba@326: deba@326: if (it != (*_node_data)[ni].heap_index.end()) { deba@326: if ((*_node_data)[ni].heap[it->second] > rw) { deba@326: (*_node_data)[ni].heap.replace(it->second, r); deba@326: (*_node_data)[ni].heap.decrease(r, rw); deba@326: it->second = r; deba@326: } deba@326: } else { deba@326: (*_node_data)[ni].heap.push(r, rw); deba@326: (*_node_data)[ni].heap_index.insert(std::make_pair(vt, r)); deba@326: } deba@326: deba@326: if ((*_blossom_set)[n] > (*_node_data)[ni].heap.prio()) { deba@326: _blossom_set->decrease(n, (*_node_data)[ni].heap.prio()); deba@326: deba@326: if (_delta2->state(blossom) != _delta2->IN_HEAP) { deba@326: _delta2->push(blossom, _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset); deba@326: } else if ((*_delta2)[blossom] > deba@326: _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset){ deba@326: _delta2->decrease(blossom, _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset); deba@326: } deba@326: } deba@326: } deba@326: deba@326: } else if ((*_blossom_data)[vb].status == UNMATCHED) { deba@326: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@326: _delta3->erase(e); deba@326: } deba@326: } else { deba@326: deba@326: typename std::map::iterator it = deba@326: (*_node_data)[vi].heap_index.find(tree); deba@326: deba@326: if (it != (*_node_data)[vi].heap_index.end()) { deba@326: (*_node_data)[vi].heap.erase(it->second); deba@326: (*_node_data)[vi].heap_index.erase(it); deba@326: if ((*_node_data)[vi].heap.empty()) { deba@326: _blossom_set->increase(v, std::numeric_limits::max()); deba@326: } else if ((*_blossom_set)[v] < (*_node_data)[vi].heap.prio()) { deba@326: _blossom_set->increase(v, (*_node_data)[vi].heap.prio()); deba@326: } deba@326: deba@326: if ((*_blossom_data)[vb].status == MATCHED) { deba@326: if (_blossom_set->classPrio(vb) == deba@326: std::numeric_limits::max()) { deba@326: _delta2->erase(vb); deba@326: } else if ((*_delta2)[vb] < _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset) { deba@326: _delta2->increase(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@326: void oddToMatched(int blossom) { deba@326: (*_blossom_data)[blossom].offset -= _delta_sum; deba@326: deba@326: if (_blossom_set->classPrio(blossom) != deba@326: std::numeric_limits::max()) { deba@326: _delta2->push(blossom, _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset); deba@326: } deba@326: deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: _delta4->erase(blossom); deba@326: } deba@326: } deba@326: deba@326: void oddToEven(int blossom, int tree) { deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: _delta4->erase(blossom); deba@326: (*_blossom_data)[blossom].pot -= deba@326: 2 * (2 * _delta_sum - (*_blossom_data)[blossom].offset); deba@326: } deba@326: deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@326: n != INVALID; ++n) { deba@326: int ni = (*_node_index)[n]; deba@326: deba@326: _blossom_set->increase(n, std::numeric_limits::max()); deba@326: deba@326: (*_node_data)[ni].heap.clear(); deba@326: (*_node_data)[ni].heap_index.clear(); deba@326: (*_node_data)[ni].pot += deba@326: 2 * _delta_sum - (*_blossom_data)[blossom].offset; deba@326: deba@326: _delta1->push(n, (*_node_data)[ni].pot); deba@326: deba@326: for (InArcIt e(_graph, n); e != INVALID; ++e) { deba@326: Node v = _graph.source(e); deba@326: int vb = _blossom_set->find(v); deba@326: int vi = (*_node_index)[v]; deba@326: deba@326: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@326: dualScale * _weight[e]; deba@326: deba@326: if ((*_blossom_data)[vb].status == EVEN) { deba@326: if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) { deba@326: _delta3->push(e, rw / 2); deba@326: } deba@326: } else if ((*_blossom_data)[vb].status == UNMATCHED) { deba@326: if (_delta3->state(e) != _delta3->IN_HEAP) { deba@326: _delta3->push(e, rw); deba@326: } deba@326: } else { deba@326: deba@326: typename std::map::iterator it = deba@326: (*_node_data)[vi].heap_index.find(tree); deba@326: deba@326: if (it != (*_node_data)[vi].heap_index.end()) { deba@326: if ((*_node_data)[vi].heap[it->second] > rw) { deba@326: (*_node_data)[vi].heap.replace(it->second, e); deba@326: (*_node_data)[vi].heap.decrease(e, rw); deba@326: it->second = e; deba@326: } deba@326: } else { deba@326: (*_node_data)[vi].heap.push(e, rw); deba@326: (*_node_data)[vi].heap_index.insert(std::make_pair(tree, e)); deba@326: } deba@326: deba@326: if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) { deba@326: _blossom_set->decrease(v, (*_node_data)[vi].heap.prio()); deba@326: deba@326: if ((*_blossom_data)[vb].status == MATCHED) { deba@326: if (_delta2->state(vb) != _delta2->IN_HEAP) { deba@326: _delta2->push(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset) { deba@326: _delta2->decrease(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: (*_blossom_data)[blossom].offset = 0; deba@326: } deba@326: deba@326: deba@326: void matchedToUnmatched(int blossom) { deba@326: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@326: _delta2->erase(blossom); deba@326: } deba@326: deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@326: n != INVALID; ++n) { deba@326: int ni = (*_node_index)[n]; deba@326: deba@326: _blossom_set->increase(n, std::numeric_limits::max()); deba@326: deba@326: (*_node_data)[ni].heap.clear(); deba@326: (*_node_data)[ni].heap_index.clear(); deba@326: deba@326: for (OutArcIt e(_graph, n); e != INVALID; ++e) { deba@326: Node v = _graph.target(e); deba@326: int vb = _blossom_set->find(v); deba@326: int vi = (*_node_index)[v]; deba@326: deba@326: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@326: dualScale * _weight[e]; deba@326: deba@326: if ((*_blossom_data)[vb].status == EVEN) { deba@326: if (_delta3->state(e) != _delta3->IN_HEAP) { deba@326: _delta3->push(e, rw); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@326: void unmatchedToMatched(int blossom) { deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@326: n != INVALID; ++n) { deba@326: int ni = (*_node_index)[n]; deba@326: deba@326: for (InArcIt e(_graph, n); e != INVALID; ++e) { deba@326: Node v = _graph.source(e); deba@326: int vb = _blossom_set->find(v); deba@326: int vi = (*_node_index)[v]; deba@326: deba@326: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@326: dualScale * _weight[e]; deba@326: deba@326: if (vb == blossom) { deba@326: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@326: _delta3->erase(e); deba@326: } deba@326: } else if ((*_blossom_data)[vb].status == EVEN) { deba@326: deba@326: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@326: _delta3->erase(e); deba@326: } deba@326: deba@326: int vt = _tree_set->find(vb); deba@326: deba@326: Arc r = _graph.oppositeArc(e); deba@326: deba@326: typename std::map::iterator it = deba@326: (*_node_data)[ni].heap_index.find(vt); deba@326: deba@326: if (it != (*_node_data)[ni].heap_index.end()) { deba@326: if ((*_node_data)[ni].heap[it->second] > rw) { deba@326: (*_node_data)[ni].heap.replace(it->second, r); deba@326: (*_node_data)[ni].heap.decrease(r, rw); deba@326: it->second = r; deba@326: } deba@326: } else { deba@326: (*_node_data)[ni].heap.push(r, rw); deba@326: (*_node_data)[ni].heap_index.insert(std::make_pair(vt, r)); deba@326: } deba@326: deba@326: if ((*_blossom_set)[n] > (*_node_data)[ni].heap.prio()) { deba@326: _blossom_set->decrease(n, (*_node_data)[ni].heap.prio()); deba@326: deba@326: if (_delta2->state(blossom) != _delta2->IN_HEAP) { deba@326: _delta2->push(blossom, _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset); deba@326: } else if ((*_delta2)[blossom] > _blossom_set->classPrio(blossom)- deba@326: (*_blossom_data)[blossom].offset){ deba@326: _delta2->decrease(blossom, _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset); deba@326: } deba@326: } deba@326: deba@326: } else if ((*_blossom_data)[vb].status == UNMATCHED) { deba@326: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@326: _delta3->erase(e); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@326: void alternatePath(int even, int tree) { deba@326: int odd; deba@326: deba@326: evenToMatched(even, tree); deba@326: (*_blossom_data)[even].status = MATCHED; deba@326: deba@326: while ((*_blossom_data)[even].pred != INVALID) { deba@326: odd = _blossom_set->find(_graph.target((*_blossom_data)[even].pred)); deba@326: (*_blossom_data)[odd].status = MATCHED; deba@326: oddToMatched(odd); deba@326: (*_blossom_data)[odd].next = (*_blossom_data)[odd].pred; deba@326: deba@326: even = _blossom_set->find(_graph.target((*_blossom_data)[odd].pred)); deba@326: (*_blossom_data)[even].status = MATCHED; deba@326: evenToMatched(even, tree); deba@326: (*_blossom_data)[even].next = deba@326: _graph.oppositeArc((*_blossom_data)[odd].pred); deba@326: } deba@326: deba@326: } deba@326: deba@326: void destroyTree(int tree) { deba@326: for (TreeSet::ItemIt b(*_tree_set, tree); b != INVALID; ++b) { deba@326: if ((*_blossom_data)[b].status == EVEN) { deba@326: (*_blossom_data)[b].status = MATCHED; deba@326: evenToMatched(b, tree); deba@326: } else if ((*_blossom_data)[b].status == ODD) { deba@326: (*_blossom_data)[b].status = MATCHED; deba@326: oddToMatched(b); deba@326: } deba@326: } deba@326: _tree_set->eraseClass(tree); deba@326: } deba@326: deba@326: deba@326: void unmatchNode(const Node& node) { deba@326: int blossom = _blossom_set->find(node); deba@326: int tree = _tree_set->find(blossom); deba@326: deba@326: alternatePath(blossom, tree); deba@326: destroyTree(tree); deba@326: deba@326: (*_blossom_data)[blossom].status = UNMATCHED; deba@326: (*_blossom_data)[blossom].base = node; deba@326: matchedToUnmatched(blossom); deba@326: } deba@326: deba@326: deba@326: void augmentOnArc(const Edge& arc) { deba@326: deba@326: int left = _blossom_set->find(_graph.u(arc)); deba@326: int right = _blossom_set->find(_graph.v(arc)); deba@326: deba@326: if ((*_blossom_data)[left].status == EVEN) { deba@326: int left_tree = _tree_set->find(left); deba@326: alternatePath(left, left_tree); deba@326: destroyTree(left_tree); deba@326: } else { deba@326: (*_blossom_data)[left].status = MATCHED; deba@326: unmatchedToMatched(left); deba@326: } deba@326: deba@326: if ((*_blossom_data)[right].status == EVEN) { deba@326: int right_tree = _tree_set->find(right); deba@326: alternatePath(right, right_tree); deba@326: destroyTree(right_tree); deba@326: } else { deba@326: (*_blossom_data)[right].status = MATCHED; deba@326: unmatchedToMatched(right); deba@326: } deba@326: deba@326: (*_blossom_data)[left].next = _graph.direct(arc, true); deba@326: (*_blossom_data)[right].next = _graph.direct(arc, false); deba@326: } deba@326: deba@326: void extendOnArc(const Arc& arc) { deba@326: int base = _blossom_set->find(_graph.target(arc)); deba@326: int tree = _tree_set->find(base); deba@326: deba@326: int odd = _blossom_set->find(_graph.source(arc)); deba@326: _tree_set->insert(odd, tree); deba@326: (*_blossom_data)[odd].status = ODD; deba@326: matchedToOdd(odd); deba@326: (*_blossom_data)[odd].pred = arc; deba@326: deba@326: int even = _blossom_set->find(_graph.target((*_blossom_data)[odd].next)); deba@326: (*_blossom_data)[even].pred = (*_blossom_data)[even].next; deba@326: _tree_set->insert(even, tree); deba@326: (*_blossom_data)[even].status = EVEN; deba@326: matchedToEven(even, tree); deba@326: } deba@326: deba@326: void shrinkOnArc(const Edge& edge, int tree) { deba@326: int nca = -1; deba@326: std::vector left_path, right_path; deba@326: deba@326: { deba@326: std::set left_set, right_set; deba@326: int left = _blossom_set->find(_graph.u(edge)); deba@326: left_path.push_back(left); deba@326: left_set.insert(left); deba@326: deba@326: int right = _blossom_set->find(_graph.v(edge)); deba@326: right_path.push_back(right); deba@326: right_set.insert(right); deba@326: deba@326: while (true) { deba@326: deba@326: if ((*_blossom_data)[left].pred == INVALID) break; deba@326: deba@326: left = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[left].pred)); deba@326: left_path.push_back(left); deba@326: left = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[left].pred)); deba@326: left_path.push_back(left); deba@326: deba@326: left_set.insert(left); deba@326: deba@326: if (right_set.find(left) != right_set.end()) { deba@326: nca = left; deba@326: break; deba@326: } deba@326: deba@326: if ((*_blossom_data)[right].pred == INVALID) break; deba@326: deba@326: right = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[right].pred)); deba@326: right_path.push_back(right); deba@326: right = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[right].pred)); deba@326: right_path.push_back(right); deba@326: deba@326: right_set.insert(right); deba@326: deba@326: if (left_set.find(right) != left_set.end()) { deba@326: nca = right; deba@326: break; deba@326: } deba@326: deba@326: } deba@326: deba@326: if (nca == -1) { deba@326: if ((*_blossom_data)[left].pred == INVALID) { deba@326: nca = right; deba@326: while (left_set.find(nca) == left_set.end()) { deba@326: nca = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[nca].pred)); deba@326: right_path.push_back(nca); deba@326: nca = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[nca].pred)); deba@326: right_path.push_back(nca); deba@326: } deba@326: } else { deba@326: nca = left; deba@326: while (right_set.find(nca) == right_set.end()) { deba@326: nca = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[nca].pred)); deba@326: left_path.push_back(nca); deba@326: nca = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[nca].pred)); deba@326: left_path.push_back(nca); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@326: std::vector subblossoms; deba@326: Arc prev; deba@326: deba@326: prev = _graph.direct(edge, true); deba@326: for (int i = 0; left_path[i] != nca; i += 2) { deba@326: subblossoms.push_back(left_path[i]); deba@326: (*_blossom_data)[left_path[i]].next = prev; deba@326: _tree_set->erase(left_path[i]); deba@326: deba@326: subblossoms.push_back(left_path[i + 1]); deba@326: (*_blossom_data)[left_path[i + 1]].status = EVEN; deba@326: oddToEven(left_path[i + 1], tree); deba@326: _tree_set->erase(left_path[i + 1]); deba@326: prev = _graph.oppositeArc((*_blossom_data)[left_path[i + 1]].pred); deba@326: } deba@326: deba@326: int k = 0; deba@326: while (right_path[k] != nca) ++k; deba@326: deba@326: subblossoms.push_back(nca); deba@326: (*_blossom_data)[nca].next = prev; deba@326: deba@326: for (int i = k - 2; i >= 0; i -= 2) { deba@326: subblossoms.push_back(right_path[i + 1]); deba@326: (*_blossom_data)[right_path[i + 1]].status = EVEN; deba@326: oddToEven(right_path[i + 1], tree); deba@326: _tree_set->erase(right_path[i + 1]); deba@326: deba@326: (*_blossom_data)[right_path[i + 1]].next = deba@326: (*_blossom_data)[right_path[i + 1]].pred; deba@326: deba@326: subblossoms.push_back(right_path[i]); deba@326: _tree_set->erase(right_path[i]); deba@326: } deba@326: deba@326: int surface = deba@326: _blossom_set->join(subblossoms.begin(), subblossoms.end()); deba@326: deba@326: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@326: if (!_blossom_set->trivial(subblossoms[i])) { deba@326: (*_blossom_data)[subblossoms[i]].pot += 2 * _delta_sum; deba@326: } deba@326: (*_blossom_data)[subblossoms[i]].status = MATCHED; deba@326: } deba@326: deba@326: (*_blossom_data)[surface].pot = -2 * _delta_sum; deba@326: (*_blossom_data)[surface].offset = 0; deba@326: (*_blossom_data)[surface].status = EVEN; deba@326: (*_blossom_data)[surface].pred = (*_blossom_data)[nca].pred; deba@326: (*_blossom_data)[surface].next = (*_blossom_data)[nca].pred; deba@326: deba@326: _tree_set->insert(surface, tree); deba@326: _tree_set->erase(nca); deba@326: } deba@326: deba@326: void splitBlossom(int blossom) { deba@326: Arc next = (*_blossom_data)[blossom].next; deba@326: Arc pred = (*_blossom_data)[blossom].pred; deba@326: deba@326: int tree = _tree_set->find(blossom); deba@326: deba@326: (*_blossom_data)[blossom].status = MATCHED; deba@326: oddToMatched(blossom); deba@326: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@326: _delta2->erase(blossom); deba@326: } deba@326: deba@326: std::vector subblossoms; deba@326: _blossom_set->split(blossom, std::back_inserter(subblossoms)); deba@326: deba@326: Value offset = (*_blossom_data)[blossom].offset; deba@326: int b = _blossom_set->find(_graph.source(pred)); deba@326: int d = _blossom_set->find(_graph.source(next)); deba@326: deba@326: int ib = -1, id = -1; deba@326: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@326: if (subblossoms[i] == b) ib = i; deba@326: if (subblossoms[i] == d) id = i; deba@326: deba@326: (*_blossom_data)[subblossoms[i]].offset = offset; deba@326: if (!_blossom_set->trivial(subblossoms[i])) { deba@326: (*_blossom_data)[subblossoms[i]].pot -= 2 * offset; deba@326: } deba@326: if (_blossom_set->classPrio(subblossoms[i]) != deba@326: std::numeric_limits::max()) { deba@326: _delta2->push(subblossoms[i], deba@326: _blossom_set->classPrio(subblossoms[i]) - deba@326: (*_blossom_data)[subblossoms[i]].offset); deba@326: } deba@326: } deba@326: deba@326: if (id > ib ? ((id - ib) % 2 == 0) : ((ib - id) % 2 == 1)) { deba@326: for (int i = (id + 1) % subblossoms.size(); deba@326: i != ib; i = (i + 2) % subblossoms.size()) { deba@326: int sb = subblossoms[i]; deba@326: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@326: (*_blossom_data)[sb].next = deba@326: _graph.oppositeArc((*_blossom_data)[tb].next); deba@326: } deba@326: deba@326: for (int i = ib; i != id; i = (i + 2) % subblossoms.size()) { deba@326: int sb = subblossoms[i]; deba@326: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@326: int ub = subblossoms[(i + 2) % subblossoms.size()]; deba@326: deba@326: (*_blossom_data)[sb].status = ODD; deba@326: matchedToOdd(sb); deba@326: _tree_set->insert(sb, tree); deba@326: (*_blossom_data)[sb].pred = pred; deba@326: (*_blossom_data)[sb].next = deba@326: _graph.oppositeArc((*_blossom_data)[tb].next); deba@326: deba@326: pred = (*_blossom_data)[ub].next; deba@326: deba@326: (*_blossom_data)[tb].status = EVEN; deba@326: matchedToEven(tb, tree); deba@326: _tree_set->insert(tb, tree); deba@326: (*_blossom_data)[tb].pred = (*_blossom_data)[tb].next; deba@326: } deba@326: deba@326: (*_blossom_data)[subblossoms[id]].status = ODD; deba@326: matchedToOdd(subblossoms[id]); deba@326: _tree_set->insert(subblossoms[id], tree); deba@326: (*_blossom_data)[subblossoms[id]].next = next; deba@326: (*_blossom_data)[subblossoms[id]].pred = pred; deba@326: deba@326: } else { deba@326: deba@326: for (int i = (ib + 1) % subblossoms.size(); deba@326: i != id; i = (i + 2) % subblossoms.size()) { deba@326: int sb = subblossoms[i]; deba@326: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@326: (*_blossom_data)[sb].next = deba@326: _graph.oppositeArc((*_blossom_data)[tb].next); deba@326: } deba@326: deba@326: for (int i = id; i != ib; i = (i + 2) % subblossoms.size()) { deba@326: int sb = subblossoms[i]; deba@326: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@326: int ub = subblossoms[(i + 2) % subblossoms.size()]; deba@326: deba@326: (*_blossom_data)[sb].status = ODD; deba@326: matchedToOdd(sb); deba@326: _tree_set->insert(sb, tree); deba@326: (*_blossom_data)[sb].next = next; deba@326: (*_blossom_data)[sb].pred = deba@326: _graph.oppositeArc((*_blossom_data)[tb].next); deba@326: deba@326: (*_blossom_data)[tb].status = EVEN; deba@326: matchedToEven(tb, tree); deba@326: _tree_set->insert(tb, tree); deba@326: (*_blossom_data)[tb].pred = deba@326: (*_blossom_data)[tb].next = deba@326: _graph.oppositeArc((*_blossom_data)[ub].next); deba@326: next = (*_blossom_data)[ub].next; deba@326: } deba@326: deba@326: (*_blossom_data)[subblossoms[ib]].status = ODD; deba@326: matchedToOdd(subblossoms[ib]); deba@326: _tree_set->insert(subblossoms[ib], tree); deba@326: (*_blossom_data)[subblossoms[ib]].next = next; deba@326: (*_blossom_data)[subblossoms[ib]].pred = pred; deba@326: } deba@326: _tree_set->erase(blossom); deba@326: } deba@326: deba@326: void extractBlossom(int blossom, const Node& base, const Arc& matching) { deba@326: if (_blossom_set->trivial(blossom)) { deba@326: int bi = (*_node_index)[base]; deba@326: Value pot = (*_node_data)[bi].pot; deba@326: deba@326: _matching->set(base, matching); deba@326: _blossom_node_list.push_back(base); deba@326: _node_potential->set(base, pot); deba@326: } else { deba@326: deba@326: Value pot = (*_blossom_data)[blossom].pot; deba@326: int bn = _blossom_node_list.size(); deba@326: deba@326: std::vector subblossoms; deba@326: _blossom_set->split(blossom, std::back_inserter(subblossoms)); deba@326: int b = _blossom_set->find(base); deba@326: int ib = -1; deba@326: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@326: if (subblossoms[i] == b) { ib = i; break; } deba@326: } deba@326: deba@326: for (int i = 1; i < int(subblossoms.size()); i += 2) { deba@326: int sb = subblossoms[(ib + i) % subblossoms.size()]; deba@326: int tb = subblossoms[(ib + i + 1) % subblossoms.size()]; deba@326: deba@326: Arc m = (*_blossom_data)[tb].next; deba@326: extractBlossom(sb, _graph.target(m), _graph.oppositeArc(m)); deba@326: extractBlossom(tb, _graph.source(m), m); deba@326: } deba@326: extractBlossom(subblossoms[ib], base, matching); deba@326: deba@326: int en = _blossom_node_list.size(); deba@326: deba@326: _blossom_potential.push_back(BlossomVariable(bn, en, pot)); deba@326: } deba@326: } deba@326: deba@326: void extractMatching() { deba@326: std::vector blossoms; deba@326: for (typename BlossomSet::ClassIt c(*_blossom_set); c != INVALID; ++c) { deba@326: blossoms.push_back(c); deba@326: } deba@326: deba@326: for (int i = 0; i < int(blossoms.size()); ++i) { deba@326: if ((*_blossom_data)[blossoms[i]].status == MATCHED) { deba@326: deba@326: Value offset = (*_blossom_data)[blossoms[i]].offset; deba@326: (*_blossom_data)[blossoms[i]].pot += 2 * offset; deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossoms[i]); deba@326: n != INVALID; ++n) { deba@326: (*_node_data)[(*_node_index)[n]].pot -= offset; deba@326: } deba@326: deba@326: Arc matching = (*_blossom_data)[blossoms[i]].next; deba@326: Node base = _graph.source(matching); deba@326: extractBlossom(blossoms[i], base, matching); deba@326: } else { deba@326: Node base = (*_blossom_data)[blossoms[i]].base; deba@326: extractBlossom(blossoms[i], base, INVALID); deba@326: } deba@326: } deba@326: } deba@326: deba@326: public: deba@326: deba@326: /// \brief Constructor deba@326: /// deba@326: /// Constructor. deba@326: MaxWeightedMatching(const Graph& graph, const WeightMap& weight) deba@326: : _graph(graph), _weight(weight), _matching(0), deba@326: _node_potential(0), _blossom_potential(), _blossom_node_list(), deba@326: _node_num(0), _blossom_num(0), deba@326: deba@326: _blossom_index(0), _blossom_set(0), _blossom_data(0), deba@326: _node_index(0), _node_heap_index(0), _node_data(0), deba@326: _tree_set_index(0), _tree_set(0), deba@326: deba@326: _delta1_index(0), _delta1(0), deba@326: _delta2_index(0), _delta2(0), deba@326: _delta3_index(0), _delta3(0), deba@326: _delta4_index(0), _delta4(0), deba@326: deba@326: _delta_sum() {} deba@326: deba@326: ~MaxWeightedMatching() { deba@326: destroyStructures(); deba@326: } deba@326: deba@326: /// \name Execution control deba@326: /// The simplest way to execute the algorithm is to use the member deba@326: /// \c run() member function. deba@326: deba@326: ///@{ deba@326: deba@326: /// \brief Initialize the algorithm deba@326: /// deba@326: /// Initialize the algorithm deba@326: void init() { deba@326: createStructures(); deba@326: deba@326: for (ArcIt e(_graph); e != INVALID; ++e) { deba@326: _node_heap_index->set(e, BinHeap::PRE_HEAP); deba@326: } deba@326: for (NodeIt n(_graph); n != INVALID; ++n) { deba@326: _delta1_index->set(n, _delta1->PRE_HEAP); deba@326: } deba@326: for (EdgeIt e(_graph); e != INVALID; ++e) { deba@326: _delta3_index->set(e, _delta3->PRE_HEAP); deba@326: } deba@326: for (int i = 0; i < _blossom_num; ++i) { deba@326: _delta2_index->set(i, _delta2->PRE_HEAP); deba@326: _delta4_index->set(i, _delta4->PRE_HEAP); deba@326: } deba@326: deba@326: int index = 0; deba@326: for (NodeIt n(_graph); n != INVALID; ++n) { deba@326: Value max = 0; deba@326: for (OutArcIt e(_graph, n); e != INVALID; ++e) { deba@326: if (_graph.target(e) == n) continue; deba@326: if ((dualScale * _weight[e]) / 2 > max) { deba@326: max = (dualScale * _weight[e]) / 2; deba@326: } deba@326: } deba@326: _node_index->set(n, index); deba@326: (*_node_data)[index].pot = max; deba@326: _delta1->push(n, max); deba@326: int blossom = deba@326: _blossom_set->insert(n, std::numeric_limits::max()); deba@326: deba@326: _tree_set->insert(blossom); deba@326: deba@326: (*_blossom_data)[blossom].status = EVEN; deba@326: (*_blossom_data)[blossom].pred = INVALID; deba@326: (*_blossom_data)[blossom].next = INVALID; deba@326: (*_blossom_data)[blossom].pot = 0; deba@326: (*_blossom_data)[blossom].offset = 0; deba@326: ++index; deba@326: } deba@326: for (EdgeIt e(_graph); e != INVALID; ++e) { deba@326: int si = (*_node_index)[_graph.u(e)]; deba@326: int ti = (*_node_index)[_graph.v(e)]; deba@326: if (_graph.u(e) != _graph.v(e)) { deba@326: _delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot - deba@326: dualScale * _weight[e]) / 2); deba@326: } deba@326: } deba@326: } deba@326: deba@326: /// \brief Starts the algorithm deba@326: /// deba@326: /// Starts the algorithm deba@326: void start() { deba@326: enum OpType { deba@326: D1, D2, D3, D4 deba@326: }; deba@326: deba@326: int unmatched = _node_num; deba@326: while (unmatched > 0) { deba@326: Value d1 = !_delta1->empty() ? deba@326: _delta1->prio() : std::numeric_limits::max(); deba@326: deba@326: Value d2 = !_delta2->empty() ? deba@326: _delta2->prio() : std::numeric_limits::max(); deba@326: deba@326: Value d3 = !_delta3->empty() ? deba@326: _delta3->prio() : std::numeric_limits::max(); deba@326: deba@326: Value d4 = !_delta4->empty() ? deba@326: _delta4->prio() : std::numeric_limits::max(); deba@326: deba@326: _delta_sum = d1; OpType ot = D1; deba@326: if (d2 < _delta_sum) { _delta_sum = d2; ot = D2; } deba@326: if (d3 < _delta_sum) { _delta_sum = d3; ot = D3; } deba@326: if (d4 < _delta_sum) { _delta_sum = d4; ot = D4; } deba@326: deba@326: deba@326: switch (ot) { deba@326: case D1: deba@326: { deba@326: Node n = _delta1->top(); deba@326: unmatchNode(n); deba@326: --unmatched; deba@326: } deba@326: break; deba@326: case D2: deba@326: { deba@326: int blossom = _delta2->top(); deba@326: Node n = _blossom_set->classTop(blossom); deba@326: Arc e = (*_node_data)[(*_node_index)[n]].heap.top(); deba@326: extendOnArc(e); deba@326: } deba@326: break; deba@326: case D3: deba@326: { deba@326: Edge e = _delta3->top(); deba@326: deba@326: int left_blossom = _blossom_set->find(_graph.u(e)); deba@326: int right_blossom = _blossom_set->find(_graph.v(e)); deba@326: deba@326: if (left_blossom == right_blossom) { deba@326: _delta3->pop(); deba@326: } else { deba@326: int left_tree; deba@326: if ((*_blossom_data)[left_blossom].status == EVEN) { deba@326: left_tree = _tree_set->find(left_blossom); deba@326: } else { deba@326: left_tree = -1; deba@326: ++unmatched; deba@326: } deba@326: int right_tree; deba@326: if ((*_blossom_data)[right_blossom].status == EVEN) { deba@326: right_tree = _tree_set->find(right_blossom); deba@326: } else { deba@326: right_tree = -1; deba@326: ++unmatched; deba@326: } deba@326: deba@326: if (left_tree == right_tree) { deba@326: shrinkOnArc(e, left_tree); deba@326: } else { deba@326: augmentOnArc(e); deba@326: unmatched -= 2; deba@326: } deba@326: } deba@326: } break; deba@326: case D4: deba@326: splitBlossom(_delta4->top()); deba@326: break; deba@326: } deba@326: } deba@326: extractMatching(); deba@326: } deba@326: deba@326: /// \brief Runs %MaxWeightedMatching algorithm. deba@326: /// deba@326: /// This method runs the %MaxWeightedMatching algorithm. deba@326: /// deba@326: /// \note mwm.run() is just a shortcut of the following code. deba@326: /// \code deba@326: /// mwm.init(); deba@326: /// mwm.start(); deba@326: /// \endcode deba@326: void run() { deba@326: init(); deba@326: start(); deba@326: } deba@326: deba@326: /// @} deba@326: deba@326: /// \name Primal solution deba@326: /// Functions for get the primal solution, ie. the matching. deba@326: deba@326: /// @{ deba@326: deba@326: /// \brief Returns the matching value. deba@326: /// deba@326: /// Returns the matching value. deba@326: Value matchingValue() const { deba@326: Value sum = 0; deba@326: for (NodeIt n(_graph); n != INVALID; ++n) { deba@326: if ((*_matching)[n] != INVALID) { deba@326: sum += _weight[(*_matching)[n]]; deba@326: } deba@326: } deba@326: return sum /= 2; deba@326: } deba@326: deba@326: /// \brief Returns true when the arc is in the matching. deba@326: /// deba@326: /// Returns true when the arc is in the matching. deba@326: bool matching(const Edge& arc) const { deba@326: return (*_matching)[_graph.u(arc)] == _graph.direct(arc, true); deba@326: } deba@326: deba@326: /// \brief Returns the incident matching arc. deba@326: /// deba@326: /// Returns the incident matching arc from given node. If the deba@326: /// node is not matched then it gives back \c INVALID. deba@326: Arc matching(const Node& node) const { deba@326: return (*_matching)[node]; deba@326: } deba@326: deba@326: /// \brief Returns the mate of the node. deba@326: /// deba@326: /// Returns the adjancent node in a mathcing arc. If the node is deba@326: /// not matched then it gives back \c INVALID. deba@326: Node mate(const Node& node) const { deba@326: return (*_matching)[node] != INVALID ? deba@326: _graph.target((*_matching)[node]) : INVALID; deba@326: } deba@326: deba@326: /// @} deba@326: deba@326: /// \name Dual solution deba@326: /// Functions for get the dual solution. deba@326: deba@326: /// @{ deba@326: deba@326: /// \brief Returns the value of the dual solution. deba@326: /// deba@326: /// Returns the value of the dual solution. It should be equal to deba@326: /// the primal value scaled by \ref dualScale "dual scale". deba@326: Value dualValue() const { deba@326: Value sum = 0; deba@326: for (NodeIt n(_graph); n != INVALID; ++n) { deba@326: sum += nodeValue(n); deba@326: } deba@326: for (int i = 0; i < blossomNum(); ++i) { deba@326: sum += blossomValue(i) * (blossomSize(i) / 2); deba@326: } deba@326: return sum; deba@326: } deba@326: deba@326: /// \brief Returns the value of the node. deba@326: /// deba@326: /// Returns the the value of the node. deba@326: Value nodeValue(const Node& n) const { deba@326: return (*_node_potential)[n]; deba@326: } deba@326: deba@326: /// \brief Returns the number of the blossoms in the basis. deba@326: /// deba@326: /// Returns the number of the blossoms in the basis. deba@326: /// \see BlossomIt deba@326: int blossomNum() const { deba@326: return _blossom_potential.size(); deba@326: } deba@326: deba@326: deba@326: /// \brief Returns the number of the nodes in the blossom. deba@326: /// deba@326: /// Returns the number of the nodes in the blossom. deba@326: int blossomSize(int k) const { deba@326: return _blossom_potential[k].end - _blossom_potential[k].begin; deba@326: } deba@326: deba@326: /// \brief Returns the value of the blossom. deba@326: /// deba@326: /// Returns the the value of the blossom. deba@326: /// \see BlossomIt deba@326: Value blossomValue(int k) const { deba@326: return _blossom_potential[k].value; deba@326: } deba@326: deba@326: /// \brief Lemon iterator for get the items of the blossom. deba@326: /// deba@326: /// Lemon iterator for get the nodes of the blossom. This class deba@326: /// provides a common style lemon iterator which gives back a deba@326: /// subset of the nodes. deba@326: class BlossomIt { deba@326: public: deba@326: deba@326: /// \brief Constructor. deba@326: /// deba@326: /// Constructor for get the nodes of the variable. deba@326: BlossomIt(const MaxWeightedMatching& algorithm, int variable) deba@326: : _algorithm(&algorithm) deba@326: { deba@326: _index = _algorithm->_blossom_potential[variable].begin; deba@326: _last = _algorithm->_blossom_potential[variable].end; deba@326: } deba@326: deba@326: /// \brief Invalid constructor. deba@326: /// deba@326: /// Invalid constructor. deba@326: BlossomIt(Invalid) : _index(-1) {} deba@326: deba@326: /// \brief Conversion to node. deba@326: /// deba@326: /// Conversion to node. deba@326: operator Node() const { deba@326: return _algorithm ? _algorithm->_blossom_node_list[_index] : INVALID; deba@326: } deba@326: deba@326: /// \brief Increment operator. deba@326: /// deba@326: /// Increment operator. deba@326: BlossomIt& operator++() { deba@326: ++_index; deba@326: if (_index == _last) { deba@326: _index = -1; deba@326: } deba@326: return *this; deba@326: } deba@326: deba@326: bool operator==(const BlossomIt& it) const { deba@326: return _index == it._index; deba@326: } deba@326: bool operator!=(const BlossomIt& it) const { deba@326: return _index != it._index; deba@326: } deba@326: deba@326: private: deba@326: const MaxWeightedMatching* _algorithm; deba@326: int _last; deba@326: int _index; deba@326: }; deba@326: deba@326: /// @} deba@326: deba@326: }; deba@326: deba@326: /// \ingroup matching deba@326: /// deba@326: /// \brief Weighted perfect matching in general graphs deba@326: /// deba@326: /// This class provides an efficient implementation of Edmond's deba@326: /// maximum weighted perfecr matching algorithm. The implementation deba@326: /// is based on extensive use of priority queues and provides deba@326: /// \f$O(nm\log(n))\f$ time complexity. deba@326: /// deba@326: /// The maximum weighted matching problem is to find undirected deba@326: /// arcs in the digraph with maximum overall weight and no two of deba@326: /// them shares their endpoints and covers all nodes. The problem deba@326: /// can be formulated with the next linear program: deba@326: /// \f[ \sum_{e \in \delta(u)}x_e = 1 \quad \forall u\in V\f] deba@326: ///\f[ \sum_{e \in \gamma(B)}x_e \le \frac{\vert B \vert - 1}{2} \quad \forall B\in\mathcal{O}\f] deba@326: /// \f[x_e \ge 0\quad \forall e\in E\f] deba@326: /// \f[\max \sum_{e\in E}x_ew_e\f] deba@326: /// where \f$\delta(X)\f$ is the set of arcs incident to a node in deba@326: /// \f$X\f$, \f$\gamma(X)\f$ is the set of arcs with both endpoints in deba@326: /// \f$X\f$ and \f$\mathcal{O}\f$ is the set of odd cardinality subsets of deba@326: /// the nodes. deba@326: /// deba@326: /// The algorithm calculates an optimal matching and a proof of the deba@326: /// optimality. The solution of the dual problem can be used to check deba@326: /// the result of the algorithm. The dual linear problem is the next: deba@326: /// \f[ y_u + y_v + \sum_{B \in \mathcal{O}, uv \in \gamma(B)}z_B \ge w_{uv} \quad \forall uv\in E\f] deba@326: /// \f[z_B \ge 0 \quad \forall B \in \mathcal{O}\f] deba@326: /// \f[\min \sum_{u \in V}y_u + \sum_{B \in \mathcal{O}}\frac{\vert B \vert - 1}{2}z_B\f] deba@326: /// deba@326: /// The algorithm can be executed with \c run() or the \c init() and deba@326: /// then the \c start() member functions. After it the matching can deba@326: /// be asked with \c matching() or mate() functions. The dual deba@326: /// solution can be get with \c nodeValue(), \c blossomNum() and \c deba@326: /// blossomValue() members and \ref MaxWeightedMatching::BlossomIt deba@326: /// "BlossomIt" nested class which is able to iterate on the nodes deba@326: /// of a blossom. If the value type is integral then the dual deba@326: /// solution is multiplied by \ref MaxWeightedMatching::dualScale "4". deba@326: template > deba@326: class MaxWeightedPerfectMatching { deba@326: public: deba@326: deba@326: typedef _Graph Graph; deba@326: typedef _WeightMap WeightMap; deba@326: typedef typename WeightMap::Value Value; deba@326: deba@326: /// \brief Scaling factor for dual solution deba@326: /// deba@326: /// Scaling factor for dual solution, it is equal to 4 or 1 deba@326: /// according to the value type. deba@326: static const int dualScale = deba@326: std::numeric_limits::is_integer ? 4 : 1; deba@326: deba@326: typedef typename Graph::template NodeMap deba@326: MatchingMap; deba@326: deba@326: private: deba@326: deba@326: TEMPLATE_GRAPH_TYPEDEFS(Graph); deba@326: deba@326: typedef typename Graph::template NodeMap NodePotential; deba@326: typedef std::vector BlossomNodeList; deba@326: deba@326: struct BlossomVariable { deba@326: int begin, end; deba@326: Value value; deba@326: deba@326: BlossomVariable(int _begin, int _end, Value _value) deba@326: : begin(_begin), end(_end), value(_value) {} deba@326: deba@326: }; deba@326: deba@326: typedef std::vector BlossomPotential; deba@326: deba@326: const Graph& _graph; deba@326: const WeightMap& _weight; deba@326: deba@326: MatchingMap* _matching; deba@326: deba@326: NodePotential* _node_potential; deba@326: deba@326: BlossomPotential _blossom_potential; deba@326: BlossomNodeList _blossom_node_list; deba@326: deba@326: int _node_num; deba@326: int _blossom_num; deba@326: deba@326: typedef typename Graph::template NodeMap NodeIntMap; deba@326: typedef typename Graph::template ArcMap ArcIntMap; deba@326: typedef typename Graph::template EdgeMap EdgeIntMap; deba@326: typedef RangeMap IntIntMap; deba@326: deba@326: enum Status { deba@326: EVEN = -1, MATCHED = 0, ODD = 1 deba@326: }; deba@326: deba@326: typedef HeapUnionFind BlossomSet; deba@326: struct BlossomData { deba@326: int tree; deba@326: Status status; deba@326: Arc pred, next; deba@326: Value pot, offset; deba@326: }; deba@326: deba@326: NodeIntMap *_blossom_index; deba@326: BlossomSet *_blossom_set; deba@326: RangeMap* _blossom_data; deba@326: deba@326: NodeIntMap *_node_index; deba@326: ArcIntMap *_node_heap_index; deba@326: deba@326: struct NodeData { deba@326: deba@326: NodeData(ArcIntMap& node_heap_index) deba@326: : heap(node_heap_index) {} deba@326: deba@326: int blossom; deba@326: Value pot; deba@326: BinHeap heap; deba@326: std::map heap_index; deba@326: deba@326: int tree; deba@326: }; deba@326: deba@326: RangeMap* _node_data; deba@326: deba@326: typedef ExtendFindEnum TreeSet; deba@326: deba@326: IntIntMap *_tree_set_index; deba@326: TreeSet *_tree_set; deba@326: deba@326: IntIntMap *_delta2_index; deba@326: BinHeap *_delta2; deba@326: deba@326: EdgeIntMap *_delta3_index; deba@326: BinHeap *_delta3; deba@326: deba@326: IntIntMap *_delta4_index; deba@326: BinHeap *_delta4; deba@326: deba@326: Value _delta_sum; deba@326: deba@326: void createStructures() { deba@326: _node_num = countNodes(_graph); deba@326: _blossom_num = _node_num * 3 / 2; deba@326: deba@326: if (!_matching) { deba@326: _matching = new MatchingMap(_graph); deba@326: } deba@326: if (!_node_potential) { deba@326: _node_potential = new NodePotential(_graph); deba@326: } deba@326: if (!_blossom_set) { deba@326: _blossom_index = new NodeIntMap(_graph); deba@326: _blossom_set = new BlossomSet(*_blossom_index); deba@326: _blossom_data = new RangeMap(_blossom_num); deba@326: } deba@326: deba@326: if (!_node_index) { deba@326: _node_index = new NodeIntMap(_graph); deba@326: _node_heap_index = new ArcIntMap(_graph); deba@326: _node_data = new RangeMap(_node_num, deba@326: NodeData(*_node_heap_index)); deba@326: } deba@326: deba@326: if (!_tree_set) { deba@326: _tree_set_index = new IntIntMap(_blossom_num); deba@326: _tree_set = new TreeSet(*_tree_set_index); deba@326: } deba@326: if (!_delta2) { deba@326: _delta2_index = new IntIntMap(_blossom_num); deba@326: _delta2 = new BinHeap(*_delta2_index); deba@326: } deba@326: if (!_delta3) { deba@326: _delta3_index = new EdgeIntMap(_graph); deba@326: _delta3 = new BinHeap(*_delta3_index); deba@326: } deba@326: if (!_delta4) { deba@326: _delta4_index = new IntIntMap(_blossom_num); deba@326: _delta4 = new BinHeap(*_delta4_index); deba@326: } deba@326: } deba@326: deba@326: void destroyStructures() { deba@326: _node_num = countNodes(_graph); deba@326: _blossom_num = _node_num * 3 / 2; deba@326: deba@326: if (_matching) { deba@326: delete _matching; deba@326: } deba@326: if (_node_potential) { deba@326: delete _node_potential; deba@326: } deba@326: if (_blossom_set) { deba@326: delete _blossom_index; deba@326: delete _blossom_set; deba@326: delete _blossom_data; deba@326: } deba@326: deba@326: if (_node_index) { deba@326: delete _node_index; deba@326: delete _node_heap_index; deba@326: delete _node_data; deba@326: } deba@326: deba@326: if (_tree_set) { deba@326: delete _tree_set_index; deba@326: delete _tree_set; deba@326: } deba@326: if (_delta2) { deba@326: delete _delta2_index; deba@326: delete _delta2; deba@326: } deba@326: if (_delta3) { deba@326: delete _delta3_index; deba@326: delete _delta3; deba@326: } deba@326: if (_delta4) { deba@326: delete _delta4_index; deba@326: delete _delta4; deba@326: } deba@326: } deba@326: deba@326: void matchedToEven(int blossom, int tree) { deba@326: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@326: _delta2->erase(blossom); deba@326: } deba@326: deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: (*_blossom_data)[blossom].pot -= deba@326: 2 * (_delta_sum - (*_blossom_data)[blossom].offset); deba@326: } deba@326: deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@326: n != INVALID; ++n) { deba@326: deba@326: _blossom_set->increase(n, std::numeric_limits::max()); deba@326: int ni = (*_node_index)[n]; deba@326: deba@326: (*_node_data)[ni].heap.clear(); deba@326: (*_node_data)[ni].heap_index.clear(); deba@326: deba@326: (*_node_data)[ni].pot += _delta_sum - (*_blossom_data)[blossom].offset; deba@326: deba@326: for (InArcIt e(_graph, n); e != INVALID; ++e) { deba@326: Node v = _graph.source(e); deba@326: int vb = _blossom_set->find(v); deba@326: int vi = (*_node_index)[v]; deba@326: deba@326: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@326: dualScale * _weight[e]; deba@326: deba@326: if ((*_blossom_data)[vb].status == EVEN) { deba@326: if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) { deba@326: _delta3->push(e, rw / 2); deba@326: } deba@326: } else { deba@326: typename std::map::iterator it = deba@326: (*_node_data)[vi].heap_index.find(tree); deba@326: deba@326: if (it != (*_node_data)[vi].heap_index.end()) { deba@326: if ((*_node_data)[vi].heap[it->second] > rw) { deba@326: (*_node_data)[vi].heap.replace(it->second, e); deba@326: (*_node_data)[vi].heap.decrease(e, rw); deba@326: it->second = e; deba@326: } deba@326: } else { deba@326: (*_node_data)[vi].heap.push(e, rw); deba@326: (*_node_data)[vi].heap_index.insert(std::make_pair(tree, e)); deba@326: } deba@326: deba@326: if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) { deba@326: _blossom_set->decrease(v, (*_node_data)[vi].heap.prio()); deba@326: deba@326: if ((*_blossom_data)[vb].status == MATCHED) { deba@326: if (_delta2->state(vb) != _delta2->IN_HEAP) { deba@326: _delta2->push(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset){ deba@326: _delta2->decrease(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: (*_blossom_data)[blossom].offset = 0; deba@326: } deba@326: deba@326: void matchedToOdd(int blossom) { deba@326: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@326: _delta2->erase(blossom); deba@326: } deba@326: (*_blossom_data)[blossom].offset += _delta_sum; deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: _delta4->push(blossom, (*_blossom_data)[blossom].pot / 2 + deba@326: (*_blossom_data)[blossom].offset); deba@326: } deba@326: } deba@326: deba@326: void evenToMatched(int blossom, int tree) { deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: (*_blossom_data)[blossom].pot += 2 * _delta_sum; deba@326: } deba@326: deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@326: n != INVALID; ++n) { deba@326: int ni = (*_node_index)[n]; deba@326: (*_node_data)[ni].pot -= _delta_sum; deba@326: deba@326: for (InArcIt e(_graph, n); e != INVALID; ++e) { deba@326: Node v = _graph.source(e); deba@326: int vb = _blossom_set->find(v); deba@326: int vi = (*_node_index)[v]; deba@326: deba@326: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@326: dualScale * _weight[e]; deba@326: deba@326: if (vb == blossom) { deba@326: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@326: _delta3->erase(e); deba@326: } deba@326: } else if ((*_blossom_data)[vb].status == EVEN) { deba@326: deba@326: if (_delta3->state(e) == _delta3->IN_HEAP) { deba@326: _delta3->erase(e); deba@326: } deba@326: deba@326: int vt = _tree_set->find(vb); deba@326: deba@326: if (vt != tree) { deba@326: deba@326: Arc r = _graph.oppositeArc(e); deba@326: deba@326: typename std::map::iterator it = deba@326: (*_node_data)[ni].heap_index.find(vt); deba@326: deba@326: if (it != (*_node_data)[ni].heap_index.end()) { deba@326: if ((*_node_data)[ni].heap[it->second] > rw) { deba@326: (*_node_data)[ni].heap.replace(it->second, r); deba@326: (*_node_data)[ni].heap.decrease(r, rw); deba@326: it->second = r; deba@326: } deba@326: } else { deba@326: (*_node_data)[ni].heap.push(r, rw); deba@326: (*_node_data)[ni].heap_index.insert(std::make_pair(vt, r)); deba@326: } deba@326: deba@326: if ((*_blossom_set)[n] > (*_node_data)[ni].heap.prio()) { deba@326: _blossom_set->decrease(n, (*_node_data)[ni].heap.prio()); deba@326: deba@326: if (_delta2->state(blossom) != _delta2->IN_HEAP) { deba@326: _delta2->push(blossom, _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset); deba@326: } else if ((*_delta2)[blossom] > deba@326: _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset){ deba@326: _delta2->decrease(blossom, _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset); deba@326: } deba@326: } deba@326: } deba@326: } else { deba@326: deba@326: typename std::map::iterator it = deba@326: (*_node_data)[vi].heap_index.find(tree); deba@326: deba@326: if (it != (*_node_data)[vi].heap_index.end()) { deba@326: (*_node_data)[vi].heap.erase(it->second); deba@326: (*_node_data)[vi].heap_index.erase(it); deba@326: if ((*_node_data)[vi].heap.empty()) { deba@326: _blossom_set->increase(v, std::numeric_limits::max()); deba@326: } else if ((*_blossom_set)[v] < (*_node_data)[vi].heap.prio()) { deba@326: _blossom_set->increase(v, (*_node_data)[vi].heap.prio()); deba@326: } deba@326: deba@326: if ((*_blossom_data)[vb].status == MATCHED) { deba@326: if (_blossom_set->classPrio(vb) == deba@326: std::numeric_limits::max()) { deba@326: _delta2->erase(vb); deba@326: } else if ((*_delta2)[vb] < _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset) { deba@326: _delta2->increase(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@326: void oddToMatched(int blossom) { deba@326: (*_blossom_data)[blossom].offset -= _delta_sum; deba@326: deba@326: if (_blossom_set->classPrio(blossom) != deba@326: std::numeric_limits::max()) { deba@326: _delta2->push(blossom, _blossom_set->classPrio(blossom) - deba@326: (*_blossom_data)[blossom].offset); deba@326: } deba@326: deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: _delta4->erase(blossom); deba@326: } deba@326: } deba@326: deba@326: void oddToEven(int blossom, int tree) { deba@326: if (!_blossom_set->trivial(blossom)) { deba@326: _delta4->erase(blossom); deba@326: (*_blossom_data)[blossom].pot -= deba@326: 2 * (2 * _delta_sum - (*_blossom_data)[blossom].offset); deba@326: } deba@326: deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossom); deba@326: n != INVALID; ++n) { deba@326: int ni = (*_node_index)[n]; deba@326: deba@326: _blossom_set->increase(n, std::numeric_limits::max()); deba@326: deba@326: (*_node_data)[ni].heap.clear(); deba@326: (*_node_data)[ni].heap_index.clear(); deba@326: (*_node_data)[ni].pot += deba@326: 2 * _delta_sum - (*_blossom_data)[blossom].offset; deba@326: deba@326: for (InArcIt e(_graph, n); e != INVALID; ++e) { deba@326: Node v = _graph.source(e); deba@326: int vb = _blossom_set->find(v); deba@326: int vi = (*_node_index)[v]; deba@326: deba@326: Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - deba@326: dualScale * _weight[e]; deba@326: deba@326: if ((*_blossom_data)[vb].status == EVEN) { deba@326: if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) { deba@326: _delta3->push(e, rw / 2); deba@326: } deba@326: } else { deba@326: deba@326: typename std::map::iterator it = deba@326: (*_node_data)[vi].heap_index.find(tree); deba@326: deba@326: if (it != (*_node_data)[vi].heap_index.end()) { deba@326: if ((*_node_data)[vi].heap[it->second] > rw) { deba@326: (*_node_data)[vi].heap.replace(it->second, e); deba@326: (*_node_data)[vi].heap.decrease(e, rw); deba@326: it->second = e; deba@326: } deba@326: } else { deba@326: (*_node_data)[vi].heap.push(e, rw); deba@326: (*_node_data)[vi].heap_index.insert(std::make_pair(tree, e)); deba@326: } deba@326: deba@326: if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) { deba@326: _blossom_set->decrease(v, (*_node_data)[vi].heap.prio()); deba@326: deba@326: if ((*_blossom_data)[vb].status == MATCHED) { deba@326: if (_delta2->state(vb) != _delta2->IN_HEAP) { deba@326: _delta2->push(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset) { deba@326: _delta2->decrease(vb, _blossom_set->classPrio(vb) - deba@326: (*_blossom_data)[vb].offset); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: } deba@326: (*_blossom_data)[blossom].offset = 0; deba@326: } deba@326: deba@326: void alternatePath(int even, int tree) { deba@326: int odd; deba@326: deba@326: evenToMatched(even, tree); deba@326: (*_blossom_data)[even].status = MATCHED; deba@326: deba@326: while ((*_blossom_data)[even].pred != INVALID) { deba@326: odd = _blossom_set->find(_graph.target((*_blossom_data)[even].pred)); deba@326: (*_blossom_data)[odd].status = MATCHED; deba@326: oddToMatched(odd); deba@326: (*_blossom_data)[odd].next = (*_blossom_data)[odd].pred; deba@326: deba@326: even = _blossom_set->find(_graph.target((*_blossom_data)[odd].pred)); deba@326: (*_blossom_data)[even].status = MATCHED; deba@326: evenToMatched(even, tree); deba@326: (*_blossom_data)[even].next = deba@326: _graph.oppositeArc((*_blossom_data)[odd].pred); deba@326: } deba@326: deba@326: } deba@326: deba@326: void destroyTree(int tree) { deba@326: for (TreeSet::ItemIt b(*_tree_set, tree); b != INVALID; ++b) { deba@326: if ((*_blossom_data)[b].status == EVEN) { deba@326: (*_blossom_data)[b].status = MATCHED; deba@326: evenToMatched(b, tree); deba@326: } else if ((*_blossom_data)[b].status == ODD) { deba@326: (*_blossom_data)[b].status = MATCHED; deba@326: oddToMatched(b); deba@326: } deba@326: } deba@326: _tree_set->eraseClass(tree); deba@326: } deba@326: deba@326: void augmentOnArc(const Edge& arc) { deba@326: deba@326: int left = _blossom_set->find(_graph.u(arc)); deba@326: int right = _blossom_set->find(_graph.v(arc)); deba@326: deba@326: int left_tree = _tree_set->find(left); deba@326: alternatePath(left, left_tree); deba@326: destroyTree(left_tree); deba@326: deba@326: int right_tree = _tree_set->find(right); deba@326: alternatePath(right, right_tree); deba@326: destroyTree(right_tree); deba@326: deba@326: (*_blossom_data)[left].next = _graph.direct(arc, true); deba@326: (*_blossom_data)[right].next = _graph.direct(arc, false); deba@326: } deba@326: deba@326: void extendOnArc(const Arc& arc) { deba@326: int base = _blossom_set->find(_graph.target(arc)); deba@326: int tree = _tree_set->find(base); deba@326: deba@326: int odd = _blossom_set->find(_graph.source(arc)); deba@326: _tree_set->insert(odd, tree); deba@326: (*_blossom_data)[odd].status = ODD; deba@326: matchedToOdd(odd); deba@326: (*_blossom_data)[odd].pred = arc; deba@326: deba@326: int even = _blossom_set->find(_graph.target((*_blossom_data)[odd].next)); deba@326: (*_blossom_data)[even].pred = (*_blossom_data)[even].next; deba@326: _tree_set->insert(even, tree); deba@326: (*_blossom_data)[even].status = EVEN; deba@326: matchedToEven(even, tree); deba@326: } deba@326: deba@326: void shrinkOnArc(const Edge& edge, int tree) { deba@326: int nca = -1; deba@326: std::vector left_path, right_path; deba@326: deba@326: { deba@326: std::set left_set, right_set; deba@326: int left = _blossom_set->find(_graph.u(edge)); deba@326: left_path.push_back(left); deba@326: left_set.insert(left); deba@326: deba@326: int right = _blossom_set->find(_graph.v(edge)); deba@326: right_path.push_back(right); deba@326: right_set.insert(right); deba@326: deba@326: while (true) { deba@326: deba@326: if ((*_blossom_data)[left].pred == INVALID) break; deba@326: deba@326: left = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[left].pred)); deba@326: left_path.push_back(left); deba@326: left = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[left].pred)); deba@326: left_path.push_back(left); deba@326: deba@326: left_set.insert(left); deba@326: deba@326: if (right_set.find(left) != right_set.end()) { deba@326: nca = left; deba@326: break; deba@326: } deba@326: deba@326: if ((*_blossom_data)[right].pred == INVALID) break; deba@326: deba@326: right = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[right].pred)); deba@326: right_path.push_back(right); deba@326: right = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[right].pred)); deba@326: right_path.push_back(right); deba@326: deba@326: right_set.insert(right); deba@326: deba@326: if (left_set.find(right) != left_set.end()) { deba@326: nca = right; deba@326: break; deba@326: } deba@326: deba@326: } deba@326: deba@326: if (nca == -1) { deba@326: if ((*_blossom_data)[left].pred == INVALID) { deba@326: nca = right; deba@326: while (left_set.find(nca) == left_set.end()) { deba@326: nca = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[nca].pred)); deba@326: right_path.push_back(nca); deba@326: nca = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[nca].pred)); deba@326: right_path.push_back(nca); deba@326: } deba@326: } else { deba@326: nca = left; deba@326: while (right_set.find(nca) == right_set.end()) { deba@326: nca = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[nca].pred)); deba@326: left_path.push_back(nca); deba@326: nca = deba@326: _blossom_set->find(_graph.target((*_blossom_data)[nca].pred)); deba@326: left_path.push_back(nca); deba@326: } deba@326: } deba@326: } deba@326: } deba@326: deba@326: std::vector subblossoms; deba@326: Arc prev; deba@326: deba@326: prev = _graph.direct(edge, true); deba@326: for (int i = 0; left_path[i] != nca; i += 2) { deba@326: subblossoms.push_back(left_path[i]); deba@326: (*_blossom_data)[left_path[i]].next = prev; deba@326: _tree_set->erase(left_path[i]); deba@326: deba@326: subblossoms.push_back(left_path[i + 1]); deba@326: (*_blossom_data)[left_path[i + 1]].status = EVEN; deba@326: oddToEven(left_path[i + 1], tree); deba@326: _tree_set->erase(left_path[i + 1]); deba@326: prev = _graph.oppositeArc((*_blossom_data)[left_path[i + 1]].pred); deba@326: } deba@326: deba@326: int k = 0; deba@326: while (right_path[k] != nca) ++k; deba@326: deba@326: subblossoms.push_back(nca); deba@326: (*_blossom_data)[nca].next = prev; deba@326: deba@326: for (int i = k - 2; i >= 0; i -= 2) { deba@326: subblossoms.push_back(right_path[i + 1]); deba@326: (*_blossom_data)[right_path[i + 1]].status = EVEN; deba@326: oddToEven(right_path[i + 1], tree); deba@326: _tree_set->erase(right_path[i + 1]); deba@326: deba@326: (*_blossom_data)[right_path[i + 1]].next = deba@326: (*_blossom_data)[right_path[i + 1]].pred; deba@326: deba@326: subblossoms.push_back(right_path[i]); deba@326: _tree_set->erase(right_path[i]); deba@326: } deba@326: deba@326: int surface = deba@326: _blossom_set->join(subblossoms.begin(), subblossoms.end()); deba@326: deba@326: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@326: if (!_blossom_set->trivial(subblossoms[i])) { deba@326: (*_blossom_data)[subblossoms[i]].pot += 2 * _delta_sum; deba@326: } deba@326: (*_blossom_data)[subblossoms[i]].status = MATCHED; deba@326: } deba@326: deba@326: (*_blossom_data)[surface].pot = -2 * _delta_sum; deba@326: (*_blossom_data)[surface].offset = 0; deba@326: (*_blossom_data)[surface].status = EVEN; deba@326: (*_blossom_data)[surface].pred = (*_blossom_data)[nca].pred; deba@326: (*_blossom_data)[surface].next = (*_blossom_data)[nca].pred; deba@326: deba@326: _tree_set->insert(surface, tree); deba@326: _tree_set->erase(nca); deba@326: } deba@326: deba@326: void splitBlossom(int blossom) { deba@326: Arc next = (*_blossom_data)[blossom].next; deba@326: Arc pred = (*_blossom_data)[blossom].pred; deba@326: deba@326: int tree = _tree_set->find(blossom); deba@326: deba@326: (*_blossom_data)[blossom].status = MATCHED; deba@326: oddToMatched(blossom); deba@326: if (_delta2->state(blossom) == _delta2->IN_HEAP) { deba@326: _delta2->erase(blossom); deba@326: } deba@326: deba@326: std::vector subblossoms; deba@326: _blossom_set->split(blossom, std::back_inserter(subblossoms)); deba@326: deba@326: Value offset = (*_blossom_data)[blossom].offset; deba@326: int b = _blossom_set->find(_graph.source(pred)); deba@326: int d = _blossom_set->find(_graph.source(next)); deba@326: deba@326: int ib = -1, id = -1; deba@326: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@326: if (subblossoms[i] == b) ib = i; deba@326: if (subblossoms[i] == d) id = i; deba@326: deba@326: (*_blossom_data)[subblossoms[i]].offset = offset; deba@326: if (!_blossom_set->trivial(subblossoms[i])) { deba@326: (*_blossom_data)[subblossoms[i]].pot -= 2 * offset; deba@326: } deba@326: if (_blossom_set->classPrio(subblossoms[i]) != deba@326: std::numeric_limits::max()) { deba@326: _delta2->push(subblossoms[i], deba@326: _blossom_set->classPrio(subblossoms[i]) - deba@326: (*_blossom_data)[subblossoms[i]].offset); deba@326: } deba@326: } deba@326: deba@326: if (id > ib ? ((id - ib) % 2 == 0) : ((ib - id) % 2 == 1)) { deba@326: for (int i = (id + 1) % subblossoms.size(); deba@326: i != ib; i = (i + 2) % subblossoms.size()) { deba@326: int sb = subblossoms[i]; deba@326: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@326: (*_blossom_data)[sb].next = deba@326: _graph.oppositeArc((*_blossom_data)[tb].next); deba@326: } deba@326: deba@326: for (int i = ib; i != id; i = (i + 2) % subblossoms.size()) { deba@326: int sb = subblossoms[i]; deba@326: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@326: int ub = subblossoms[(i + 2) % subblossoms.size()]; deba@326: deba@326: (*_blossom_data)[sb].status = ODD; deba@326: matchedToOdd(sb); deba@326: _tree_set->insert(sb, tree); deba@326: (*_blossom_data)[sb].pred = pred; deba@326: (*_blossom_data)[sb].next = deba@326: _graph.oppositeArc((*_blossom_data)[tb].next); deba@326: deba@326: pred = (*_blossom_data)[ub].next; deba@326: deba@326: (*_blossom_data)[tb].status = EVEN; deba@326: matchedToEven(tb, tree); deba@326: _tree_set->insert(tb, tree); deba@326: (*_blossom_data)[tb].pred = (*_blossom_data)[tb].next; deba@326: } deba@326: deba@326: (*_blossom_data)[subblossoms[id]].status = ODD; deba@326: matchedToOdd(subblossoms[id]); deba@326: _tree_set->insert(subblossoms[id], tree); deba@326: (*_blossom_data)[subblossoms[id]].next = next; deba@326: (*_blossom_data)[subblossoms[id]].pred = pred; deba@326: deba@326: } else { deba@326: deba@326: for (int i = (ib + 1) % subblossoms.size(); deba@326: i != id; i = (i + 2) % subblossoms.size()) { deba@326: int sb = subblossoms[i]; deba@326: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@326: (*_blossom_data)[sb].next = deba@326: _graph.oppositeArc((*_blossom_data)[tb].next); deba@326: } deba@326: deba@326: for (int i = id; i != ib; i = (i + 2) % subblossoms.size()) { deba@326: int sb = subblossoms[i]; deba@326: int tb = subblossoms[(i + 1) % subblossoms.size()]; deba@326: int ub = subblossoms[(i + 2) % subblossoms.size()]; deba@326: deba@326: (*_blossom_data)[sb].status = ODD; deba@326: matchedToOdd(sb); deba@326: _tree_set->insert(sb, tree); deba@326: (*_blossom_data)[sb].next = next; deba@326: (*_blossom_data)[sb].pred = deba@326: _graph.oppositeArc((*_blossom_data)[tb].next); deba@326: deba@326: (*_blossom_data)[tb].status = EVEN; deba@326: matchedToEven(tb, tree); deba@326: _tree_set->insert(tb, tree); deba@326: (*_blossom_data)[tb].pred = deba@326: (*_blossom_data)[tb].next = deba@326: _graph.oppositeArc((*_blossom_data)[ub].next); deba@326: next = (*_blossom_data)[ub].next; deba@326: } deba@326: deba@326: (*_blossom_data)[subblossoms[ib]].status = ODD; deba@326: matchedToOdd(subblossoms[ib]); deba@326: _tree_set->insert(subblossoms[ib], tree); deba@326: (*_blossom_data)[subblossoms[ib]].next = next; deba@326: (*_blossom_data)[subblossoms[ib]].pred = pred; deba@326: } deba@326: _tree_set->erase(blossom); deba@326: } deba@326: deba@326: void extractBlossom(int blossom, const Node& base, const Arc& matching) { deba@326: if (_blossom_set->trivial(blossom)) { deba@326: int bi = (*_node_index)[base]; deba@326: Value pot = (*_node_data)[bi].pot; deba@326: deba@326: _matching->set(base, matching); deba@326: _blossom_node_list.push_back(base); deba@326: _node_potential->set(base, pot); deba@326: } else { deba@326: deba@326: Value pot = (*_blossom_data)[blossom].pot; deba@326: int bn = _blossom_node_list.size(); deba@326: deba@326: std::vector subblossoms; deba@326: _blossom_set->split(blossom, std::back_inserter(subblossoms)); deba@326: int b = _blossom_set->find(base); deba@326: int ib = -1; deba@326: for (int i = 0; i < int(subblossoms.size()); ++i) { deba@326: if (subblossoms[i] == b) { ib = i; break; } deba@326: } deba@326: deba@326: for (int i = 1; i < int(subblossoms.size()); i += 2) { deba@326: int sb = subblossoms[(ib + i) % subblossoms.size()]; deba@326: int tb = subblossoms[(ib + i + 1) % subblossoms.size()]; deba@326: deba@326: Arc m = (*_blossom_data)[tb].next; deba@326: extractBlossom(sb, _graph.target(m), _graph.oppositeArc(m)); deba@326: extractBlossom(tb, _graph.source(m), m); deba@326: } deba@326: extractBlossom(subblossoms[ib], base, matching); deba@326: deba@326: int en = _blossom_node_list.size(); deba@326: deba@326: _blossom_potential.push_back(BlossomVariable(bn, en, pot)); deba@326: } deba@326: } deba@326: deba@326: void extractMatching() { deba@326: std::vector blossoms; deba@326: for (typename BlossomSet::ClassIt c(*_blossom_set); c != INVALID; ++c) { deba@326: blossoms.push_back(c); deba@326: } deba@326: deba@326: for (int i = 0; i < int(blossoms.size()); ++i) { deba@326: deba@326: Value offset = (*_blossom_data)[blossoms[i]].offset; deba@326: (*_blossom_data)[blossoms[i]].pot += 2 * offset; deba@326: for (typename BlossomSet::ItemIt n(*_blossom_set, blossoms[i]); deba@326: n != INVALID; ++n) { deba@326: (*_node_data)[(*_node_index)[n]].pot -= offset; deba@326: } deba@326: deba@326: Arc matching = (*_blossom_data)[blossoms[i]].next; deba@326: Node base = _graph.source(matching); deba@326: extractBlossom(blossoms[i], base, matching); deba@326: } deba@326: } deba@326: deba@326: public: deba@326: deba@326: /// \brief Constructor deba@326: /// deba@326: /// Constructor. deba@326: MaxWeightedPerfectMatching(const Graph& graph, const WeightMap& weight) deba@326: : _graph(graph), _weight(weight), _matching(0), deba@326: _node_potential(0), _blossom_potential(), _blossom_node_list(), deba@326: _node_num(0), _blossom_num(0), deba@326: deba@326: _blossom_index(0), _blossom_set(0), _blossom_data(0), deba@326: _node_index(0), _node_heap_index(0), _node_data(0), deba@326: _tree_set_index(0), _tree_set(0), deba@326: deba@326: _delta2_index(0), _delta2(0), deba@326: _delta3_index(0), _delta3(0), deba@326: _delta4_index(0), _delta4(0), deba@326: deba@326: _delta_sum() {} deba@326: deba@326: ~MaxWeightedPerfectMatching() { deba@326: destroyStructures(); deba@326: } deba@326: deba@326: /// \name Execution control deba@326: /// The simplest way to execute the algorithm is to use the member deba@326: /// \c run() member function. deba@326: deba@326: ///@{ deba@326: deba@326: /// \brief Initialize the algorithm deba@326: /// deba@326: /// Initialize the algorithm deba@326: void init() { deba@326: createStructures(); deba@326: deba@326: for (ArcIt e(_graph); e != INVALID; ++e) { deba@326: _node_heap_index->set(e, BinHeap::PRE_HEAP); deba@326: } deba@326: for (EdgeIt e(_graph); e != INVALID; ++e) { deba@326: _delta3_index->set(e, _delta3->PRE_HEAP); deba@326: } deba@326: for (int i = 0; i < _blossom_num; ++i) { deba@326: _delta2_index->set(i, _delta2->PRE_HEAP); deba@326: _delta4_index->set(i, _delta4->PRE_HEAP); deba@326: } deba@326: deba@326: int index = 0; deba@326: for (NodeIt n(_graph); n != INVALID; ++n) { deba@326: Value max = - std::numeric_limits::max(); deba@326: for (OutArcIt e(_graph, n); e != INVALID; ++e) { deba@326: if (_graph.target(e) == n) continue; deba@326: if ((dualScale * _weight[e]) / 2 > max) { deba@326: max = (dualScale * _weight[e]) / 2; deba@326: } deba@326: } deba@326: _node_index->set(n, index); deba@326: (*_node_data)[index].pot = max; deba@326: int blossom = deba@326: _blossom_set->insert(n, std::numeric_limits::max()); deba@326: deba@326: _tree_set->insert(blossom); deba@326: deba@326: (*_blossom_data)[blossom].status = EVEN; deba@326: (*_blossom_data)[blossom].pred = INVALID; deba@326: (*_blossom_data)[blossom].next = INVALID; deba@326: (*_blossom_data)[blossom].pot = 0; deba@326: (*_blossom_data)[blossom].offset = 0; deba@326: ++index; deba@326: } deba@326: for (EdgeIt e(_graph); e != INVALID; ++e) { deba@326: int si = (*_node_index)[_graph.u(e)]; deba@326: int ti = (*_node_index)[_graph.v(e)]; deba@326: if (_graph.u(e) != _graph.v(e)) { deba@326: _delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot - deba@326: dualScale * _weight[e]) / 2); deba@326: } deba@326: } deba@326: } deba@326: deba@326: /// \brief Starts the algorithm deba@326: /// deba@326: /// Starts the algorithm deba@326: bool start() { deba@326: enum OpType { deba@326: D2, D3, D4 deba@326: }; deba@326: deba@326: int unmatched = _node_num; deba@326: while (unmatched > 0) { deba@326: Value d2 = !_delta2->empty() ? deba@326: _delta2->prio() : std::numeric_limits::max(); deba@326: deba@326: Value d3 = !_delta3->empty() ? deba@326: _delta3->prio() : std::numeric_limits::max(); deba@326: deba@326: Value d4 = !_delta4->empty() ? deba@326: _delta4->prio() : std::numeric_limits::max(); deba@326: deba@326: _delta_sum = d2; OpType ot = D2; deba@326: if (d3 < _delta_sum) { _delta_sum = d3; ot = D3; } deba@326: if (d4 < _delta_sum) { _delta_sum = d4; ot = D4; } deba@326: deba@326: if (_delta_sum == std::numeric_limits::max()) { deba@326: return false; deba@326: } deba@326: deba@326: switch (ot) { deba@326: case D2: deba@326: { deba@326: int blossom = _delta2->top(); deba@326: Node n = _blossom_set->classTop(blossom); deba@326: Arc e = (*_node_data)[(*_node_index)[n]].heap.top(); deba@326: extendOnArc(e); deba@326: } deba@326: break; deba@326: case D3: deba@326: { deba@326: Edge e = _delta3->top(); deba@326: deba@326: int left_blossom = _blossom_set->find(_graph.u(e)); deba@326: int right_blossom = _blossom_set->find(_graph.v(e)); deba@326: deba@326: if (left_blossom == right_blossom) { deba@326: _delta3->pop(); deba@326: } else { deba@326: int left_tree = _tree_set->find(left_blossom); deba@326: int right_tree = _tree_set->find(right_blossom); deba@326: deba@326: if (left_tree == right_tree) { deba@326: shrinkOnArc(e, left_tree); deba@326: } else { deba@326: augmentOnArc(e); deba@326: unmatched -= 2; deba@326: } deba@326: } deba@326: } break; deba@326: case D4: deba@326: splitBlossom(_delta4->top()); deba@326: break; deba@326: } deba@326: } deba@326: extractMatching(); deba@326: return true; deba@326: } deba@326: deba@326: /// \brief Runs %MaxWeightedPerfectMatching algorithm. deba@326: /// deba@326: /// This method runs the %MaxWeightedPerfectMatching algorithm. deba@326: /// deba@326: /// \note mwm.run() is just a shortcut of the following code. deba@326: /// \code deba@326: /// mwm.init(); deba@326: /// mwm.start(); deba@326: /// \endcode deba@326: bool run() { deba@326: init(); deba@326: return start(); deba@326: } deba@326: deba@326: /// @} deba@326: deba@326: /// \name Primal solution deba@326: /// Functions for get the primal solution, ie. the matching. deba@326: deba@326: /// @{ deba@326: deba@326: /// \brief Returns the matching value. deba@326: /// deba@326: /// Returns the matching value. deba@326: Value matchingValue() const { deba@326: Value sum = 0; deba@326: for (NodeIt n(_graph); n != INVALID; ++n) { deba@326: if ((*_matching)[n] != INVALID) { deba@326: sum += _weight[(*_matching)[n]]; deba@326: } deba@326: } deba@326: return sum /= 2; deba@326: } deba@326: deba@326: /// \brief Returns true when the arc is in the matching. deba@326: /// deba@326: /// Returns true when the arc is in the matching. deba@326: bool matching(const Edge& arc) const { deba@326: return (*_matching)[_graph.u(arc)] == _graph.direct(arc, true); deba@326: } deba@326: deba@326: /// \brief Returns the incident matching arc. deba@326: /// deba@326: /// Returns the incident matching arc from given node. deba@326: Arc matching(const Node& node) const { deba@326: return (*_matching)[node]; deba@326: } deba@326: deba@326: /// \brief Returns the mate of the node. deba@326: /// deba@326: /// Returns the adjancent node in a mathcing arc. deba@326: Node mate(const Node& node) const { deba@326: return _graph.target((*_matching)[node]); deba@326: } deba@326: deba@326: /// @} deba@326: deba@326: /// \name Dual solution deba@326: /// Functions for get the dual solution. deba@326: deba@326: /// @{ deba@326: deba@326: /// \brief Returns the value of the dual solution. deba@326: /// deba@326: /// Returns the value of the dual solution. It should be equal to deba@326: /// the primal value scaled by \ref dualScale "dual scale". deba@326: Value dualValue() const { deba@326: Value sum = 0; deba@326: for (NodeIt n(_graph); n != INVALID; ++n) { deba@326: sum += nodeValue(n); deba@326: } deba@326: for (int i = 0; i < blossomNum(); ++i) { deba@326: sum += blossomValue(i) * (blossomSize(i) / 2); deba@326: } deba@326: return sum; deba@326: } deba@326: deba@326: /// \brief Returns the value of the node. deba@326: /// deba@326: /// Returns the the value of the node. deba@326: Value nodeValue(const Node& n) const { deba@326: return (*_node_potential)[n]; deba@326: } deba@326: deba@326: /// \brief Returns the number of the blossoms in the basis. deba@326: /// deba@326: /// Returns the number of the blossoms in the basis. deba@326: /// \see BlossomIt deba@326: int blossomNum() const { deba@326: return _blossom_potential.size(); deba@326: } deba@326: deba@326: deba@326: /// \brief Returns the number of the nodes in the blossom. deba@326: /// deba@326: /// Returns the number of the nodes in the blossom. deba@326: int blossomSize(int k) const { deba@326: return _blossom_potential[k].end - _blossom_potential[k].begin; deba@326: } deba@326: deba@326: /// \brief Returns the value of the blossom. deba@326: /// deba@326: /// Returns the the value of the blossom. deba@326: /// \see BlossomIt deba@326: Value blossomValue(int k) const { deba@326: return _blossom_potential[k].value; deba@326: } deba@326: deba@326: /// \brief Lemon iterator for get the items of the blossom. deba@326: /// deba@326: /// Lemon iterator for get the nodes of the blossom. This class deba@326: /// provides a common style lemon iterator which gives back a deba@326: /// subset of the nodes. deba@326: class BlossomIt { deba@326: public: deba@326: deba@326: /// \brief Constructor. deba@326: /// deba@326: /// Constructor for get the nodes of the variable. deba@326: BlossomIt(const MaxWeightedPerfectMatching& algorithm, int variable) deba@326: : _algorithm(&algorithm) deba@326: { deba@326: _index = _algorithm->_blossom_potential[variable].begin; deba@326: _last = _algorithm->_blossom_potential[variable].end; deba@326: } deba@326: deba@326: /// \brief Invalid constructor. deba@326: /// deba@326: /// Invalid constructor. deba@326: BlossomIt(Invalid) : _index(-1) {} deba@326: deba@326: /// \brief Conversion to node. deba@326: /// deba@326: /// Conversion to node. deba@326: operator Node() const { deba@326: return _algorithm ? _algorithm->_blossom_node_list[_index] : INVALID; deba@326: } deba@326: deba@326: /// \brief Increment operator. deba@326: /// deba@326: /// Increment operator. deba@326: BlossomIt& operator++() { deba@326: ++_index; deba@326: if (_index == _last) { deba@326: _index = -1; deba@326: } deba@326: return *this; deba@326: } deba@326: deba@326: bool operator==(const BlossomIt& it) const { deba@326: return _index == it._index; deba@326: } deba@326: bool operator!=(const BlossomIt& it) const { deba@326: return _index != it._index; deba@326: } deba@326: deba@326: private: deba@326: const MaxWeightedPerfectMatching* _algorithm; deba@326: int _last; deba@326: int _index; deba@326: }; deba@326: deba@326: /// @} deba@326: deba@326: }; deba@326: deba@326: deba@326: } //END OF NAMESPACE LEMON deba@326: deba@326: #endif //LEMON_MAX_MATCHING_H