lemon/matching.h
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 21 Jan 2021 08:36:53 +0100
changeset 1204 736a341e604b
parent 1092 dceba191c00d
child 1208 c6aa2cc1af04
permissions -rw-r--r--
Update CMakeLists.txt to match current CMAKE versions (#641)
deba@326
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@326
     2
 *
deba@326
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@326
     4
 *
alpar@1092
     5
 * Copyright (C) 2003-2013
deba@326
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@326
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@326
     8
 *
deba@326
     9
 * Permission to use, modify and distribute this software is granted
deba@326
    10
 * provided that this copyright notice appears in all copies. For
deba@326
    11
 * precise terms see the accompanying LICENSE file.
deba@326
    12
 *
deba@326
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@326
    14
 * express or implied, and with no claim as to its suitability for any
deba@326
    15
 * purpose.
deba@326
    16
 *
deba@326
    17
 */
deba@326
    18
deba@868
    19
#ifndef LEMON_MATCHING_H
deba@868
    20
#define LEMON_MATCHING_H
deba@326
    21
deba@326
    22
#include <vector>
deba@326
    23
#include <queue>
deba@326
    24
#include <set>
deba@326
    25
#include <limits>
deba@326
    26
deba@326
    27
#include <lemon/core.h>
deba@326
    28
#include <lemon/unionfind.h>
deba@326
    29
#include <lemon/bin_heap.h>
deba@326
    30
#include <lemon/maps.h>
deba@870
    31
#include <lemon/fractional_matching.h>
deba@326
    32
deba@326
    33
///\ingroup matching
deba@326
    34
///\file
deba@327
    35
///\brief Maximum matching algorithms in general graphs.
deba@326
    36
deba@326
    37
namespace lemon {
deba@326
    38
deba@327
    39
  /// \ingroup matching
deba@326
    40
  ///
kpeter@590
    41
  /// \brief Maximum cardinality matching in general graphs
deba@326
    42
  ///
kpeter@590
    43
  /// This class implements Edmonds' alternating forest matching algorithm
kpeter@593
    44
  /// for finding a maximum cardinality matching in a general undirected graph.
deba@868
    45
  /// It can be started from an arbitrary initial matching
kpeter@590
    46
  /// (the default is the empty one).
deba@326
    47
  ///
alpar@330
    48
  /// The dual solution of the problem is a map of the nodes to
kpeter@590
    49
  /// \ref MaxMatching::Status "Status", having values \c EVEN (or \c D),
kpeter@590
    50
  /// \c ODD (or \c A) and \c MATCHED (or \c C) defining the Gallai-Edmonds
kpeter@590
    51
  /// decomposition of the graph. The nodes in \c EVEN/D induce a subgraph
kpeter@590
    52
  /// with factor-critical components, the nodes in \c ODD/A form the
kpeter@590
    53
  /// canonical barrier, and the nodes in \c MATCHED/C induce a graph having
kpeter@590
    54
  /// a perfect matching. The number of the factor-critical components
deba@327
    55
  /// minus the number of barrier nodes is a lower bound on the
alpar@330
    56
  /// unmatched nodes, and the matching is optimal if and only if this bound is
kpeter@593
    57
  /// tight. This decomposition can be obtained using \ref status() or
kpeter@593
    58
  /// \ref statusMap() after running the algorithm.
deba@326
    59
  ///
kpeter@593
    60
  /// \tparam GR The undirected graph type the algorithm runs on.
kpeter@559
    61
  template <typename GR>
deba@326
    62
  class MaxMatching {
deba@327
    63
  public:
deba@327
    64
kpeter@590
    65
    /// The graph type of the algorithm
kpeter@559
    66
    typedef GR Graph;
kpeter@593
    67
    /// The type of the matching map
deba@327
    68
    typedef typename Graph::template NodeMap<typename Graph::Arc>
deba@327
    69
    MatchingMap;
deba@327
    70
kpeter@590
    71
    ///\brief Status constants for Gallai-Edmonds decomposition.
deba@327
    72
    ///
deba@868
    73
    ///These constants are used for indicating the Gallai-Edmonds
kpeter@590
    74
    ///decomposition of a graph. The nodes with status \c EVEN (or \c D)
kpeter@590
    75
    ///induce a subgraph with factor-critical components, the nodes with
kpeter@590
    76
    ///status \c ODD (or \c A) form the canonical barrier, and the nodes
deba@868
    77
    ///with status \c MATCHED (or \c C) induce a subgraph having a
kpeter@590
    78
    ///perfect matching.
deba@327
    79
    enum Status {
kpeter@590
    80
      EVEN = 1,       ///< = 1. (\c D is an alias for \c EVEN.)
kpeter@590
    81
      D = 1,
kpeter@590
    82
      MATCHED = 0,    ///< = 0. (\c C is an alias for \c MATCHED.)
kpeter@590
    83
      C = 0,
kpeter@590
    84
      ODD = -1,       ///< = -1. (\c A is an alias for \c ODD.)
kpeter@590
    85
      A = -1,
kpeter@590
    86
      UNMATCHED = -2  ///< = -2.
deba@327
    87
    };
deba@327
    88
kpeter@593
    89
    /// The type of the status map
deba@327
    90
    typedef typename Graph::template NodeMap<Status> StatusMap;
deba@327
    91
deba@327
    92
  private:
deba@326
    93
deba@326
    94
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@326
    95
deba@327
    96
    typedef UnionFindEnum<IntNodeMap> BlossomSet;
deba@327
    97
    typedef ExtendFindEnum<IntNodeMap> TreeSet;
deba@327
    98
    typedef RangeMap<Node> NodeIntMap;
deba@327
    99
    typedef MatchingMap EarMap;
deba@327
   100
    typedef std::vector<Node> NodeQueue;
deba@327
   101
deba@327
   102
    const Graph& _graph;
deba@327
   103
    MatchingMap* _matching;
deba@327
   104
    StatusMap* _status;
deba@327
   105
deba@327
   106
    EarMap* _ear;
deba@327
   107
deba@327
   108
    IntNodeMap* _blossom_set_index;
deba@327
   109
    BlossomSet* _blossom_set;
deba@327
   110
    NodeIntMap* _blossom_rep;
deba@327
   111
deba@327
   112
    IntNodeMap* _tree_set_index;
deba@327
   113
    TreeSet* _tree_set;
deba@327
   114
deba@327
   115
    NodeQueue _node_queue;
deba@327
   116
    int _process, _postpone, _last;
deba@327
   117
deba@1203
   118
    int _node_num, _unmatched;
deba@327
   119
deba@327
   120
  private:
deba@327
   121
deba@327
   122
    void createStructures() {
deba@327
   123
      _node_num = countNodes(_graph);
deba@327
   124
      if (!_matching) {
deba@327
   125
        _matching = new MatchingMap(_graph);
deba@327
   126
      }
deba@327
   127
      if (!_status) {
deba@327
   128
        _status = new StatusMap(_graph);
deba@327
   129
      }
deba@327
   130
      if (!_ear) {
deba@327
   131
        _ear = new EarMap(_graph);
deba@327
   132
      }
deba@327
   133
      if (!_blossom_set) {
deba@327
   134
        _blossom_set_index = new IntNodeMap(_graph);
deba@327
   135
        _blossom_set = new BlossomSet(*_blossom_set_index);
deba@327
   136
      }
deba@327
   137
      if (!_blossom_rep) {
deba@327
   138
        _blossom_rep = new NodeIntMap(_node_num);
deba@327
   139
      }
deba@327
   140
      if (!_tree_set) {
deba@327
   141
        _tree_set_index = new IntNodeMap(_graph);
deba@327
   142
        _tree_set = new TreeSet(*_tree_set_index);
deba@327
   143
      }
deba@327
   144
      _node_queue.resize(_node_num);
deba@327
   145
    }
deba@327
   146
deba@327
   147
    void destroyStructures() {
deba@327
   148
      if (_matching) {
deba@327
   149
        delete _matching;
deba@327
   150
      }
deba@327
   151
      if (_status) {
deba@327
   152
        delete _status;
deba@327
   153
      }
deba@327
   154
      if (_ear) {
deba@327
   155
        delete _ear;
deba@327
   156
      }
deba@327
   157
      if (_blossom_set) {
deba@327
   158
        delete _blossom_set;
deba@327
   159
        delete _blossom_set_index;
deba@327
   160
      }
deba@327
   161
      if (_blossom_rep) {
deba@327
   162
        delete _blossom_rep;
deba@327
   163
      }
deba@327
   164
      if (_tree_set) {
deba@327
   165
        delete _tree_set_index;
deba@327
   166
        delete _tree_set;
deba@327
   167
      }
deba@327
   168
    }
deba@327
   169
deba@327
   170
    void processDense(const Node& n) {
deba@327
   171
      _process = _postpone = _last = 0;
deba@327
   172
      _node_queue[_last++] = n;
deba@327
   173
deba@327
   174
      while (_process != _last) {
deba@327
   175
        Node u = _node_queue[_process++];
deba@327
   176
        for (OutArcIt a(_graph, u); a != INVALID; ++a) {
deba@327
   177
          Node v = _graph.target(a);
deba@327
   178
          if ((*_status)[v] == MATCHED) {
deba@327
   179
            extendOnArc(a);
deba@327
   180
          } else if ((*_status)[v] == UNMATCHED) {
deba@327
   181
            augmentOnArc(a);
deba@1203
   182
            --_unmatched;
deba@327
   183
            return;
deba@327
   184
          }
deba@327
   185
        }
deba@327
   186
      }
deba@327
   187
deba@327
   188
      while (_postpone != _last) {
deba@327
   189
        Node u = _node_queue[_postpone++];
deba@327
   190
deba@327
   191
        for (OutArcIt a(_graph, u); a != INVALID ; ++a) {
deba@327
   192
          Node v = _graph.target(a);
deba@327
   193
deba@327
   194
          if ((*_status)[v] == EVEN) {
deba@327
   195
            if (_blossom_set->find(u) != _blossom_set->find(v)) {
deba@327
   196
              shrinkOnEdge(a);
deba@327
   197
            }
deba@327
   198
          }
deba@327
   199
deba@327
   200
          while (_process != _last) {
deba@327
   201
            Node w = _node_queue[_process++];
deba@327
   202
            for (OutArcIt b(_graph, w); b != INVALID; ++b) {
deba@327
   203
              Node x = _graph.target(b);
deba@327
   204
              if ((*_status)[x] == MATCHED) {
deba@327
   205
                extendOnArc(b);
deba@327
   206
              } else if ((*_status)[x] == UNMATCHED) {
deba@327
   207
                augmentOnArc(b);
deba@1203
   208
                --_unmatched;
deba@327
   209
                return;
deba@327
   210
              }
deba@327
   211
            }
deba@327
   212
          }
deba@327
   213
        }
deba@327
   214
      }
deba@327
   215
    }
deba@327
   216
deba@327
   217
    void processSparse(const Node& n) {
deba@327
   218
      _process = _last = 0;
deba@327
   219
      _node_queue[_last++] = n;
deba@327
   220
      while (_process != _last) {
deba@327
   221
        Node u = _node_queue[_process++];
deba@327
   222
        for (OutArcIt a(_graph, u); a != INVALID; ++a) {
deba@327
   223
          Node v = _graph.target(a);
deba@327
   224
deba@327
   225
          if ((*_status)[v] == EVEN) {
deba@327
   226
            if (_blossom_set->find(u) != _blossom_set->find(v)) {
deba@327
   227
              shrinkOnEdge(a);
deba@327
   228
            }
deba@327
   229
          } else if ((*_status)[v] == MATCHED) {
deba@327
   230
            extendOnArc(a);
deba@327
   231
          } else if ((*_status)[v] == UNMATCHED) {
deba@327
   232
            augmentOnArc(a);
deba@1203
   233
            --_unmatched;
deba@327
   234
            return;
deba@327
   235
          }
deba@327
   236
        }
deba@327
   237
      }
deba@327
   238
    }
deba@327
   239
deba@327
   240
    void shrinkOnEdge(const Edge& e) {
deba@327
   241
      Node nca = INVALID;
deba@327
   242
deba@327
   243
      {
deba@327
   244
        std::set<Node> left_set, right_set;
deba@327
   245
deba@327
   246
        Node left = (*_blossom_rep)[_blossom_set->find(_graph.u(e))];
deba@327
   247
        left_set.insert(left);
deba@327
   248
deba@327
   249
        Node right = (*_blossom_rep)[_blossom_set->find(_graph.v(e))];
deba@327
   250
        right_set.insert(right);
deba@327
   251
deba@327
   252
        while (true) {
deba@327
   253
          if ((*_matching)[left] == INVALID) break;
deba@327
   254
          left = _graph.target((*_matching)[left]);
deba@327
   255
          left = (*_blossom_rep)[_blossom_set->
deba@327
   256
                                 find(_graph.target((*_ear)[left]))];
deba@327
   257
          if (right_set.find(left) != right_set.end()) {
deba@327
   258
            nca = left;
deba@327
   259
            break;
deba@327
   260
          }
deba@327
   261
          left_set.insert(left);
deba@327
   262
deba@327
   263
          if ((*_matching)[right] == INVALID) break;
deba@327
   264
          right = _graph.target((*_matching)[right]);
deba@327
   265
          right = (*_blossom_rep)[_blossom_set->
deba@327
   266
                                  find(_graph.target((*_ear)[right]))];
deba@327
   267
          if (left_set.find(right) != left_set.end()) {
deba@327
   268
            nca = right;
deba@327
   269
            break;
deba@327
   270
          }
deba@327
   271
          right_set.insert(right);
deba@327
   272
        }
deba@327
   273
deba@327
   274
        if (nca == INVALID) {
deba@327
   275
          if ((*_matching)[left] == INVALID) {
deba@327
   276
            nca = right;
deba@327
   277
            while (left_set.find(nca) == left_set.end()) {
deba@327
   278
              nca = _graph.target((*_matching)[nca]);
deba@327
   279
              nca =(*_blossom_rep)[_blossom_set->
deba@327
   280
                                   find(_graph.target((*_ear)[nca]))];
deba@327
   281
            }
deba@327
   282
          } else {
deba@327
   283
            nca = left;
deba@327
   284
            while (right_set.find(nca) == right_set.end()) {
deba@327
   285
              nca = _graph.target((*_matching)[nca]);
deba@327
   286
              nca = (*_blossom_rep)[_blossom_set->
deba@327
   287
                                   find(_graph.target((*_ear)[nca]))];
deba@327
   288
            }
deba@327
   289
          }
deba@327
   290
        }
deba@327
   291
      }
deba@327
   292
deba@327
   293
      {
deba@327
   294
deba@327
   295
        Node node = _graph.u(e);
deba@327
   296
        Arc arc = _graph.direct(e, true);
deba@327
   297
        Node base = (*_blossom_rep)[_blossom_set->find(node)];
deba@327
   298
deba@327
   299
        while (base != nca) {
kpeter@581
   300
          (*_ear)[node] = arc;
deba@327
   301
deba@327
   302
          Node n = node;
deba@327
   303
          while (n != base) {
deba@327
   304
            n = _graph.target((*_matching)[n]);
deba@327
   305
            Arc a = (*_ear)[n];
deba@327
   306
            n = _graph.target(a);
kpeter@581
   307
            (*_ear)[n] = _graph.oppositeArc(a);
deba@327
   308
          }
deba@327
   309
          node = _graph.target((*_matching)[base]);
deba@327
   310
          _tree_set->erase(base);
deba@327
   311
          _tree_set->erase(node);
deba@327
   312
          _blossom_set->insert(node, _blossom_set->find(base));
kpeter@581
   313
          (*_status)[node] = EVEN;
deba@327
   314
          _node_queue[_last++] = node;
deba@327
   315
          arc = _graph.oppositeArc((*_ear)[node]);
deba@327
   316
          node = _graph.target((*_ear)[node]);
deba@327
   317
          base = (*_blossom_rep)[_blossom_set->find(node)];
deba@327
   318
          _blossom_set->join(_graph.target(arc), base);
deba@327
   319
        }
deba@327
   320
      }
deba@327
   321
kpeter@581
   322
      (*_blossom_rep)[_blossom_set->find(nca)] = nca;
deba@327
   323
deba@327
   324
      {
deba@327
   325
deba@327
   326
        Node node = _graph.v(e);
deba@327
   327
        Arc arc = _graph.direct(e, false);
deba@327
   328
        Node base = (*_blossom_rep)[_blossom_set->find(node)];
deba@327
   329
deba@327
   330
        while (base != nca) {
kpeter@581
   331
          (*_ear)[node] = arc;
deba@327
   332
deba@327
   333
          Node n = node;
deba@327
   334
          while (n != base) {
deba@327
   335
            n = _graph.target((*_matching)[n]);
deba@327
   336
            Arc a = (*_ear)[n];
deba@327
   337
            n = _graph.target(a);
kpeter@581
   338
            (*_ear)[n] = _graph.oppositeArc(a);
deba@327
   339
          }
deba@327
   340
          node = _graph.target((*_matching)[base]);
deba@327
   341
          _tree_set->erase(base);
deba@327
   342
          _tree_set->erase(node);
deba@327
   343
          _blossom_set->insert(node, _blossom_set->find(base));
kpeter@581
   344
          (*_status)[node] = EVEN;
deba@327
   345
          _node_queue[_last++] = node;
deba@327
   346
          arc = _graph.oppositeArc((*_ear)[node]);
deba@327
   347
          node = _graph.target((*_ear)[node]);
deba@327
   348
          base = (*_blossom_rep)[_blossom_set->find(node)];
deba@327
   349
          _blossom_set->join(_graph.target(arc), base);
deba@327
   350
        }
deba@327
   351
      }
deba@327
   352
kpeter@581
   353
      (*_blossom_rep)[_blossom_set->find(nca)] = nca;
deba@327
   354
    }
deba@327
   355
deba@327
   356
    void extendOnArc(const Arc& a) {
deba@327
   357
      Node base = _graph.source(a);
deba@327
   358
      Node odd = _graph.target(a);
deba@327
   359
kpeter@581
   360
      (*_ear)[odd] = _graph.oppositeArc(a);
deba@327
   361
      Node even = _graph.target((*_matching)[odd]);
kpeter@581
   362
      (*_blossom_rep)[_blossom_set->insert(even)] = even;
kpeter@581
   363
      (*_status)[odd] = ODD;
kpeter@581
   364
      (*_status)[even] = EVEN;
deba@327
   365
      int tree = _tree_set->find((*_blossom_rep)[_blossom_set->find(base)]);
deba@327
   366
      _tree_set->insert(odd, tree);
deba@327
   367
      _tree_set->insert(even, tree);
deba@327
   368
      _node_queue[_last++] = even;
deba@327
   369
deba@327
   370
    }
deba@327
   371
deba@327
   372
    void augmentOnArc(const Arc& a) {
deba@327
   373
      Node even = _graph.source(a);
deba@327
   374
      Node odd = _graph.target(a);
deba@327
   375
deba@327
   376
      int tree = _tree_set->find((*_blossom_rep)[_blossom_set->find(even)]);
deba@327
   377
kpeter@581
   378
      (*_matching)[odd] = _graph.oppositeArc(a);
kpeter@581
   379
      (*_status)[odd] = MATCHED;
deba@327
   380
deba@327
   381
      Arc arc = (*_matching)[even];
kpeter@581
   382
      (*_matching)[even] = a;
deba@327
   383
deba@327
   384
      while (arc != INVALID) {
deba@327
   385
        odd = _graph.target(arc);
deba@327
   386
        arc = (*_ear)[odd];
deba@327
   387
        even = _graph.target(arc);
kpeter@581
   388
        (*_matching)[odd] = arc;
deba@327
   389
        arc = (*_matching)[even];
kpeter@581
   390
        (*_matching)[even] = _graph.oppositeArc((*_matching)[odd]);
deba@327
   391
      }
deba@327
   392
deba@327
   393
      for (typename TreeSet::ItemIt it(*_tree_set, tree);
deba@327
   394
           it != INVALID; ++it) {
deba@327
   395
        if ((*_status)[it] == ODD) {
kpeter@581
   396
          (*_status)[it] = MATCHED;
deba@327
   397
        } else {
deba@327
   398
          int blossom = _blossom_set->find(it);
deba@327
   399
          for (typename BlossomSet::ItemIt jt(*_blossom_set, blossom);
deba@327
   400
               jt != INVALID; ++jt) {
kpeter@581
   401
            (*_status)[jt] = MATCHED;
deba@327
   402
          }
deba@327
   403
          _blossom_set->eraseClass(blossom);
deba@327
   404
        }
deba@327
   405
      }
deba@327
   406
      _tree_set->eraseClass(tree);
deba@327
   407
deba@327
   408
    }
deba@326
   409
deba@326
   410
  public:
deba@326
   411
deba@327
   412
    /// \brief Constructor
deba@326
   413
    ///
deba@327
   414
    /// Constructor.
deba@327
   415
    MaxMatching(const Graph& graph)
deba@327
   416
      : _graph(graph), _matching(0), _status(0), _ear(0),
deba@327
   417
        _blossom_set_index(0), _blossom_set(0), _blossom_rep(0),
deba@327
   418
        _tree_set_index(0), _tree_set(0) {}
deba@327
   419
deba@327
   420
    ~MaxMatching() {
deba@327
   421
      destroyStructures();
deba@327
   422
    }
deba@327
   423
kpeter@590
   424
    /// \name Execution Control
alpar@330
   425
    /// The simplest way to execute the algorithm is to use the
kpeter@590
   426
    /// \c run() member function.\n
kpeter@590
   427
    /// If you need better control on the execution, you have to call
kpeter@590
   428
    /// one of the functions \ref init(), \ref greedyInit() or
kpeter@590
   429
    /// \ref matchingInit() first, then you can start the algorithm with
kpeter@590
   430
    /// \ref startSparse() or \ref startDense().
deba@327
   431
deba@327
   432
    ///@{
deba@327
   433
kpeter@590
   434
    /// \brief Set the initial matching to the empty matching.
deba@326
   435
    ///
kpeter@590
   436
    /// This function sets the initial matching to the empty matching.
deba@326
   437
    void init() {
deba@327
   438
      createStructures();
deba@327
   439
      for(NodeIt n(_graph); n != INVALID; ++n) {
kpeter@581
   440
        (*_matching)[n] = INVALID;
kpeter@581
   441
        (*_status)[n] = UNMATCHED;
deba@326
   442
      }
deba@1203
   443
      _unmatched = _node_num;
deba@326
   444
    }
deba@326
   445
kpeter@590
   446
    /// \brief Find an initial matching in a greedy way.
deba@326
   447
    ///
kpeter@590
   448
    /// This function finds an initial matching in a greedy way.
deba@326
   449
    void greedyInit() {
deba@327
   450
      createStructures();
deba@327
   451
      for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@581
   452
        (*_matching)[n] = INVALID;
kpeter@581
   453
        (*_status)[n] = UNMATCHED;
deba@326
   454
      }
deba@1203
   455
      _unmatched = _node_num;
deba@327
   456
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@327
   457
        if ((*_matching)[n] == INVALID) {
deba@327
   458
          for (OutArcIt a(_graph, n); a != INVALID ; ++a) {
deba@327
   459
            Node v = _graph.target(a);
deba@327
   460
            if ((*_matching)[v] == INVALID && v != n) {
kpeter@581
   461
              (*_matching)[n] = a;
kpeter@581
   462
              (*_status)[n] = MATCHED;
kpeter@581
   463
              (*_matching)[v] = _graph.oppositeArc(a);
kpeter@581
   464
              (*_status)[v] = MATCHED;
deba@1203
   465
              _unmatched -= 2;
deba@326
   466
              break;
deba@326
   467
            }
deba@326
   468
          }
deba@326
   469
        }
deba@326
   470
      }
deba@326
   471
    }
deba@326
   472
deba@327
   473
kpeter@590
   474
    /// \brief Initialize the matching from a map.
deba@326
   475
    ///
kpeter@590
   476
    /// This function initializes the matching from a \c bool valued edge
kpeter@590
   477
    /// map. This map should have the property that there are no two incident
kpeter@590
   478
    /// edges with \c true value, i.e. it really contains a matching.
kpeter@559
   479
    /// \return \c true if the map contains a matching.
deba@327
   480
    template <typename MatchingMap>
deba@327
   481
    bool matchingInit(const MatchingMap& matching) {
deba@327
   482
      createStructures();
deba@327
   483
deba@327
   484
      for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@581
   485
        (*_matching)[n] = INVALID;
kpeter@581
   486
        (*_status)[n] = UNMATCHED;
deba@326
   487
      }
deba@1203
   488
      _unmatched = _node_num;
deba@327
   489
      for(EdgeIt e(_graph); e!=INVALID; ++e) {
deba@327
   490
        if (matching[e]) {
deba@327
   491
deba@327
   492
          Node u = _graph.u(e);
deba@327
   493
          if ((*_matching)[u] != INVALID) return false;
kpeter@581
   494
          (*_matching)[u] = _graph.direct(e, true);
kpeter@581
   495
          (*_status)[u] = MATCHED;
deba@327
   496
deba@327
   497
          Node v = _graph.v(e);
deba@327
   498
          if ((*_matching)[v] != INVALID) return false;
kpeter@581
   499
          (*_matching)[v] = _graph.direct(e, false);
kpeter@581
   500
          (*_status)[v] = MATCHED;
deba@1203
   501
deba@1203
   502
          _unmatched -= 2;
deba@327
   503
        }
deba@327
   504
      }
deba@327
   505
      return true;
deba@326
   506
    }
deba@326
   507
kpeter@590
   508
    /// \brief Start Edmonds' algorithm
deba@326
   509
    ///
deba@1203
   510
    /// This function runs the original Edmonds' algorithm. If the
deba@1203
   511
    /// \c decomposition parameter is set to false, then the Gallai-Edmonds
deba@1203
   512
    /// decomposition is not computed.
kpeter@590
   513
    ///
kpeter@651
   514
    /// \pre \ref init(), \ref greedyInit() or \ref matchingInit() must be
kpeter@590
   515
    /// called before using this function.
deba@1203
   516
    void startSparse(bool decomposition = true) {
deba@1203
   517
      int _unmatched_limit = decomposition ? 0 : 1;
deba@1203
   518
      for(NodeIt n(_graph); _unmatched > _unmatched_limit; ++n) {
deba@327
   519
        if ((*_status)[n] == UNMATCHED) {
deba@327
   520
          (*_blossom_rep)[_blossom_set->insert(n)] = n;
deba@327
   521
          _tree_set->insert(n);
kpeter@581
   522
          (*_status)[n] = EVEN;
deba@1203
   523
          --_unmatched;
deba@327
   524
          processSparse(n);
deba@326
   525
        }
deba@326
   526
      }
deba@326
   527
    }
deba@326
   528
deba@868
   529
    /// \brief Start Edmonds' algorithm with a heuristic improvement
kpeter@590
   530
    /// for dense graphs
deba@326
   531
    ///
kpeter@590
   532
    /// This function runs Edmonds' algorithm with a heuristic of postponing
deba@1203
   533
    /// shrinks, therefore resulting in a faster algorithm for dense graphs.  If
deba@1203
   534
    /// the \c decomposition parameter is set to false, then the Gallai-Edmonds
deba@1203
   535
    /// decomposition is not computed.
kpeter@590
   536
    ///
kpeter@651
   537
    /// \pre \ref init(), \ref greedyInit() or \ref matchingInit() must be
kpeter@590
   538
    /// called before using this function.
deba@1203
   539
    void startDense(bool decomposition = true) {
deba@1203
   540
      int _unmatched_limit = decomposition ? 0 : 1;
deba@1203
   541
      for(NodeIt n(_graph); _unmatched > _unmatched_limit; ++n) {
deba@327
   542
        if ((*_status)[n] == UNMATCHED) {
deba@327
   543
          (*_blossom_rep)[_blossom_set->insert(n)] = n;
deba@327
   544
          _tree_set->insert(n);
kpeter@581
   545
          (*_status)[n] = EVEN;
deba@1203
   546
          --_unmatched;
deba@327
   547
          processDense(n);
deba@327
   548
        }
deba@327
   549
      }
deba@327
   550
    }
deba@327
   551
deba@327
   552
kpeter@590
   553
    /// \brief Run Edmonds' algorithm
deba@327
   554
    ///
deba@868
   555
    /// This function runs Edmonds' algorithm. An additional heuristic of
deba@1203
   556
    /// postponing shrinks is used for relatively dense graphs (for which
deba@1203
   557
    /// <tt>m>=2*n</tt> holds). If the \c decomposition parameter is set to
deba@1203
   558
    /// false, then the Gallai-Edmonds decomposition is not computed. In some
deba@1203
   559
    /// cases, this can speed up the algorithm significantly, especially when a
deba@1203
   560
    /// maximum matching is computed in a dense graph with odd number of nodes.
deba@1203
   561
    void run(bool decomposition = true) {
deba@327
   562
      if (countEdges(_graph) < 2 * countNodes(_graph)) {
deba@326
   563
        greedyInit();
deba@1203
   564
        startSparse(decomposition);
deba@326
   565
      } else {
deba@326
   566
        init();
deba@1203
   567
        startDense(decomposition);
deba@326
   568
      }
deba@326
   569
    }
deba@326
   570
deba@327
   571
    /// @}
deba@327
   572
kpeter@590
   573
    /// \name Primal Solution
kpeter@590
   574
    /// Functions to get the primal solution, i.e. the maximum matching.
deba@327
   575
deba@327
   576
    /// @{
deba@326
   577
kpeter@590
   578
    /// \brief Return the size (cardinality) of the matching.
deba@326
   579
    ///
deba@868
   580
    /// This function returns the size (cardinality) of the current matching.
kpeter@590
   581
    /// After run() it returns the size of the maximum matching in the graph.
deba@327
   582
    int matchingSize() const {
deba@327
   583
      int size = 0;
deba@327
   584
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@327
   585
        if ((*_matching)[n] != INVALID) {
deba@327
   586
          ++size;
deba@326
   587
        }
deba@326
   588
      }
deba@327
   589
      return size / 2;
deba@326
   590
    }
deba@326
   591
kpeter@590
   592
    /// \brief Return \c true if the given edge is in the matching.
deba@327
   593
    ///
deba@868
   594
    /// This function returns \c true if the given edge is in the current
kpeter@590
   595
    /// matching.
deba@327
   596
    bool matching(const Edge& edge) const {
deba@327
   597
      return edge == (*_matching)[_graph.u(edge)];
deba@327
   598
    }
deba@327
   599
kpeter@590
   600
    /// \brief Return the matching arc (or edge) incident to the given node.
deba@327
   601
    ///
kpeter@590
   602
    /// This function returns the matching arc (or edge) incident to the
deba@868
   603
    /// given node in the current matching or \c INVALID if the node is
kpeter@590
   604
    /// not covered by the matching.
deba@327
   605
    Arc matching(const Node& n) const {
deba@327
   606
      return (*_matching)[n];
deba@327
   607
    }
deba@326
   608
kpeter@593
   609
    /// \brief Return a const reference to the matching map.
kpeter@593
   610
    ///
kpeter@593
   611
    /// This function returns a const reference to a node map that stores
kpeter@593
   612
    /// the matching arc (or edge) incident to each node.
kpeter@593
   613
    const MatchingMap& matchingMap() const {
kpeter@593
   614
      return *_matching;
kpeter@593
   615
    }
kpeter@593
   616
kpeter@590
   617
    /// \brief Return the mate of the given node.
deba@326
   618
    ///
deba@868
   619
    /// This function returns the mate of the given node in the current
kpeter@590
   620
    /// matching or \c INVALID if the node is not covered by the matching.
deba@327
   621
    Node mate(const Node& n) const {
deba@327
   622
      return (*_matching)[n] != INVALID ?
deba@327
   623
        _graph.target((*_matching)[n]) : INVALID;
deba@326
   624
    }
deba@326
   625
deba@327
   626
    /// @}
deba@327
   627
kpeter@590
   628
    /// \name Dual Solution
deba@868
   629
    /// Functions to get the dual solution, i.e. the Gallai-Edmonds
kpeter@590
   630
    /// decomposition.
deba@327
   631
deba@327
   632
    /// @{
deba@326
   633
kpeter@590
   634
    /// \brief Return the status of the given node in the Edmonds-Gallai
deba@326
   635
    /// decomposition.
deba@326
   636
    ///
kpeter@590
   637
    /// This function returns the \ref Status "status" of the given node
kpeter@590
   638
    /// in the Edmonds-Gallai decomposition.
kpeter@593
   639
    Status status(const Node& n) const {
deba@327
   640
      return (*_status)[n];
deba@326
   641
    }
deba@326
   642
kpeter@593
   643
    /// \brief Return a const reference to the status map, which stores
kpeter@593
   644
    /// the Edmonds-Gallai decomposition.
kpeter@593
   645
    ///
kpeter@593
   646
    /// This function returns a const reference to a node map that stores the
kpeter@593
   647
    /// \ref Status "status" of each node in the Edmonds-Gallai decomposition.
kpeter@593
   648
    const StatusMap& statusMap() const {
kpeter@593
   649
      return *_status;
kpeter@593
   650
    }
kpeter@593
   651
kpeter@590
   652
    /// \brief Return \c true if the given node is in the barrier.
deba@326
   653
    ///
kpeter@590
   654
    /// This function returns \c true if the given node is in the barrier.
deba@327
   655
    bool barrier(const Node& n) const {
deba@327
   656
      return (*_status)[n] == ODD;
deba@326
   657
    }
deba@326
   658
deba@327
   659
    /// @}
deba@326
   660
deba@326
   661
  };
deba@326
   662
deba@326
   663
  /// \ingroup matching
deba@326
   664
  ///
deba@326
   665
  /// \brief Weighted matching in general graphs
deba@326
   666
  ///
deba@326
   667
  /// This class provides an efficient implementation of Edmond's
deba@326
   668
  /// maximum weighted matching algorithm. The implementation is based
deba@326
   669
  /// on extensive use of priority queues and provides
kpeter@559
   670
  /// \f$O(nm\log n)\f$ time complexity.
deba@326
   671
  ///
deba@868
   672
  /// The maximum weighted matching problem is to find a subset of the
deba@868
   673
  /// edges in an undirected graph with maximum overall weight for which
kpeter@590
   674
  /// each node has at most one incident edge.
kpeter@590
   675
  /// It can be formulated with the following linear program.
deba@326
   676
  /// \f[ \sum_{e \in \delta(u)}x_e \le 1 \quad \forall u\in V\f]
deba@327
   677
  /** \f[ \sum_{e \in \gamma(B)}x_e \le \frac{\vert B \vert - 1}{2}
deba@327
   678
      \quad \forall B\in\mathcal{O}\f] */
deba@326
   679
  /// \f[x_e \ge 0\quad \forall e\in E\f]
deba@326
   680
  /// \f[\max \sum_{e\in E}x_ew_e\f]
deba@327
   681
  /// where \f$\delta(X)\f$ is the set of edges incident to a node in
deba@327
   682
  /// \f$X\f$, \f$\gamma(X)\f$ is the set of edges with both ends in
deba@327
   683
  /// \f$X\f$ and \f$\mathcal{O}\f$ is the set of odd cardinality
deba@327
   684
  /// subsets of the nodes.
deba@326
   685
  ///
deba@326
   686
  /// The algorithm calculates an optimal matching and a proof of the
deba@326
   687
  /// optimality. The solution of the dual problem can be used to check
deba@327
   688
  /// the result of the algorithm. The dual linear problem is the
kpeter@590
   689
  /// following.
deba@327
   690
  /** \f[ y_u + y_v + \sum_{B \in \mathcal{O}, uv \in \gamma(B)}
deba@327
   691
      z_B \ge w_{uv} \quad \forall uv\in E\f] */
deba@326
   692
  /// \f[y_u \ge 0 \quad \forall u \in V\f]
deba@326
   693
  /// \f[z_B \ge 0 \quad \forall B \in \mathcal{O}\f]
deba@327
   694
  /** \f[\min \sum_{u \in V}y_u + \sum_{B \in \mathcal{O}}
deba@327
   695
      \frac{\vert B \vert - 1}{2}z_B\f] */
deba@326
   696
  ///
deba@868
   697
  /// The algorithm can be executed with the run() function.
kpeter@590
   698
  /// After it the matching (the primal solution) and the dual solution
deba@868
   699
  /// can be obtained using the query functions and the
deba@868
   700
  /// \ref MaxWeightedMatching::BlossomIt "BlossomIt" nested class,
deba@868
   701
  /// which is able to iterate on the nodes of a blossom.
kpeter@590
   702
  /// If the value type is integer, then the dual solution is multiplied
kpeter@590
   703
  /// by \ref MaxWeightedMatching::dualScale "4".
kpeter@590
   704
  ///
kpeter@593
   705
  /// \tparam GR The undirected graph type the algorithm runs on.
deba@868
   706
  /// \tparam WM The type edge weight map. The default type is
kpeter@590
   707
  /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<int>".
kpeter@590
   708
#ifdef DOXYGEN
kpeter@590
   709
  template <typename GR, typename WM>
kpeter@590
   710
#else
kpeter@559
   711
  template <typename GR,
kpeter@559
   712
            typename WM = typename GR::template EdgeMap<int> >
kpeter@590
   713
#endif
deba@326
   714
  class MaxWeightedMatching {
deba@326
   715
  public:
deba@326
   716
kpeter@590
   717
    /// The graph type of the algorithm
kpeter@559
   718
    typedef GR Graph;
kpeter@590
   719
    /// The type of the edge weight map
kpeter@559
   720
    typedef WM WeightMap;
kpeter@590
   721
    /// The value type of the edge weights
deba@326
   722
    typedef typename WeightMap::Value Value;
deba@326
   723
kpeter@593
   724
    /// The type of the matching map
kpeter@590
   725
    typedef typename Graph::template NodeMap<typename Graph::Arc>
kpeter@590
   726
    MatchingMap;
kpeter@590
   727
deba@326
   728
    /// \brief Scaling factor for dual solution
deba@326
   729
    ///
kpeter@590
   730
    /// Scaling factor for dual solution. It is equal to 4 or 1
deba@326
   731
    /// according to the value type.
deba@326
   732
    static const int dualScale =
deba@326
   733
      std::numeric_limits<Value>::is_integer ? 4 : 1;
deba@326
   734
deba@326
   735
  private:
deba@326
   736
deba@326
   737
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@326
   738
deba@326
   739
    typedef typename Graph::template NodeMap<Value> NodePotential;
deba@326
   740
    typedef std::vector<Node> BlossomNodeList;
deba@326
   741
deba@326
   742
    struct BlossomVariable {
deba@326
   743
      int begin, end;
deba@326
   744
      Value value;
deba@326
   745
deba@326
   746
      BlossomVariable(int _begin, int _end, Value _value)
deba@326
   747
        : begin(_begin), end(_end), value(_value) {}
deba@326
   748
deba@326
   749
    };
deba@326
   750
deba@326
   751
    typedef std::vector<BlossomVariable> BlossomPotential;
deba@326
   752
deba@326
   753
    const Graph& _graph;
deba@326
   754
    const WeightMap& _weight;
deba@326
   755
deba@326
   756
    MatchingMap* _matching;
deba@326
   757
deba@326
   758
    NodePotential* _node_potential;
deba@326
   759
deba@326
   760
    BlossomPotential _blossom_potential;
deba@326
   761
    BlossomNodeList _blossom_node_list;
deba@326
   762
deba@326
   763
    int _node_num;
deba@326
   764
    int _blossom_num;
deba@326
   765
deba@326
   766
    typedef RangeMap<int> IntIntMap;
deba@326
   767
deba@326
   768
    enum Status {
deba@868
   769
      EVEN = -1, MATCHED = 0, ODD = 1
deba@326
   770
    };
deba@326
   771
deba@327
   772
    typedef HeapUnionFind<Value, IntNodeMap> BlossomSet;
deba@326
   773
    struct BlossomData {
deba@326
   774
      int tree;
deba@326
   775
      Status status;
deba@326
   776
      Arc pred, next;
deba@326
   777
      Value pot, offset;
deba@326
   778
      Node base;
deba@326
   779
    };
deba@326
   780
deba@327
   781
    IntNodeMap *_blossom_index;
deba@326
   782
    BlossomSet *_blossom_set;
deba@326
   783
    RangeMap<BlossomData>* _blossom_data;
deba@326
   784
deba@327
   785
    IntNodeMap *_node_index;
deba@327
   786
    IntArcMap *_node_heap_index;
deba@326
   787
deba@326
   788
    struct NodeData {
deba@326
   789
deba@327
   790
      NodeData(IntArcMap& node_heap_index)
deba@326
   791
        : heap(node_heap_index) {}
deba@326
   792
deba@326
   793
      int blossom;
deba@326
   794
      Value pot;
deba@327
   795
      BinHeap<Value, IntArcMap> heap;
deba@326
   796
      std::map<int, Arc> heap_index;
deba@326
   797
deba@326
   798
      int tree;
deba@326
   799
    };
deba@326
   800
deba@326
   801
    RangeMap<NodeData>* _node_data;
deba@326
   802
deba@326
   803
    typedef ExtendFindEnum<IntIntMap> TreeSet;
deba@326
   804
deba@326
   805
    IntIntMap *_tree_set_index;
deba@326
   806
    TreeSet *_tree_set;
deba@326
   807
deba@327
   808
    IntNodeMap *_delta1_index;
deba@327
   809
    BinHeap<Value, IntNodeMap> *_delta1;
deba@326
   810
deba@326
   811
    IntIntMap *_delta2_index;
deba@326
   812
    BinHeap<Value, IntIntMap> *_delta2;
deba@326
   813
deba@327
   814
    IntEdgeMap *_delta3_index;
deba@327
   815
    BinHeap<Value, IntEdgeMap> *_delta3;
deba@326
   816
deba@326
   817
    IntIntMap *_delta4_index;
deba@326
   818
    BinHeap<Value, IntIntMap> *_delta4;
deba@326
   819
deba@326
   820
    Value _delta_sum;
deba@870
   821
    int _unmatched;
deba@870
   822
deba@870
   823
    typedef MaxWeightedFractionalMatching<Graph, WeightMap> FractionalMatching;
deba@870
   824
    FractionalMatching *_fractional;
deba@326
   825
deba@326
   826
    void createStructures() {
deba@326
   827
      _node_num = countNodes(_graph);
deba@326
   828
      _blossom_num = _node_num * 3 / 2;
deba@326
   829
deba@326
   830
      if (!_matching) {
deba@326
   831
        _matching = new MatchingMap(_graph);
deba@326
   832
      }
deba@867
   833
deba@326
   834
      if (!_node_potential) {
deba@326
   835
        _node_potential = new NodePotential(_graph);
deba@326
   836
      }
deba@867
   837
deba@326
   838
      if (!_blossom_set) {
deba@327
   839
        _blossom_index = new IntNodeMap(_graph);
deba@326
   840
        _blossom_set = new BlossomSet(*_blossom_index);
deba@326
   841
        _blossom_data = new RangeMap<BlossomData>(_blossom_num);
deba@867
   842
      } else if (_blossom_data->size() != _blossom_num) {
deba@867
   843
        delete _blossom_data;
deba@867
   844
        _blossom_data = new RangeMap<BlossomData>(_blossom_num);
deba@326
   845
      }
deba@326
   846
deba@326
   847
      if (!_node_index) {
deba@327
   848
        _node_index = new IntNodeMap(_graph);
deba@327
   849
        _node_heap_index = new IntArcMap(_graph);
deba@326
   850
        _node_data = new RangeMap<NodeData>(_node_num,
deba@867
   851
                                            NodeData(*_node_heap_index));
deba@867
   852
      } else {
deba@867
   853
        delete _node_data;
deba@867
   854
        _node_data = new RangeMap<NodeData>(_node_num,
deba@867
   855
                                            NodeData(*_node_heap_index));
deba@326
   856
      }
deba@326
   857
deba@326
   858
      if (!_tree_set) {
deba@326
   859
        _tree_set_index = new IntIntMap(_blossom_num);
deba@326
   860
        _tree_set = new TreeSet(*_tree_set_index);
deba@867
   861
      } else {
deba@867
   862
        _tree_set_index->resize(_blossom_num);
deba@326
   863
      }
deba@867
   864
deba@326
   865
      if (!_delta1) {
deba@327
   866
        _delta1_index = new IntNodeMap(_graph);
deba@327
   867
        _delta1 = new BinHeap<Value, IntNodeMap>(*_delta1_index);
deba@326
   868
      }
deba@867
   869
deba@326
   870
      if (!_delta2) {
deba@326
   871
        _delta2_index = new IntIntMap(_blossom_num);
deba@326
   872
        _delta2 = new BinHeap<Value, IntIntMap>(*_delta2_index);
deba@867
   873
      } else {
deba@867
   874
        _delta2_index->resize(_blossom_num);
deba@326
   875
      }
deba@867
   876
deba@326
   877
      if (!_delta3) {
deba@327
   878
        _delta3_index = new IntEdgeMap(_graph);
deba@327
   879
        _delta3 = new BinHeap<Value, IntEdgeMap>(*_delta3_index);
deba@326
   880
      }
deba@867
   881
deba@326
   882
      if (!_delta4) {
deba@326
   883
        _delta4_index = new IntIntMap(_blossom_num);
deba@326
   884
        _delta4 = new BinHeap<Value, IntIntMap>(*_delta4_index);
deba@867
   885
      } else {
deba@867
   886
        _delta4_index->resize(_blossom_num);
deba@326
   887
      }
deba@326
   888
    }
deba@326
   889
deba@326
   890
    void destroyStructures() {
deba@326
   891
      if (_matching) {
deba@326
   892
        delete _matching;
deba@326
   893
      }
deba@326
   894
      if (_node_potential) {
deba@326
   895
        delete _node_potential;
deba@326
   896
      }
deba@326
   897
      if (_blossom_set) {
deba@326
   898
        delete _blossom_index;
deba@326
   899
        delete _blossom_set;
deba@326
   900
        delete _blossom_data;
deba@326
   901
      }
deba@326
   902
deba@326
   903
      if (_node_index) {
deba@326
   904
        delete _node_index;
deba@326
   905
        delete _node_heap_index;
deba@326
   906
        delete _node_data;
deba@326
   907
      }
deba@326
   908
deba@326
   909
      if (_tree_set) {
deba@326
   910
        delete _tree_set_index;
deba@326
   911
        delete _tree_set;
deba@326
   912
      }
deba@326
   913
      if (_delta1) {
deba@326
   914
        delete _delta1_index;
deba@326
   915
        delete _delta1;
deba@326
   916
      }
deba@326
   917
      if (_delta2) {
deba@326
   918
        delete _delta2_index;
deba@326
   919
        delete _delta2;
deba@326
   920
      }
deba@326
   921
      if (_delta3) {
deba@326
   922
        delete _delta3_index;
deba@326
   923
        delete _delta3;
deba@326
   924
      }
deba@326
   925
      if (_delta4) {
deba@326
   926
        delete _delta4_index;
deba@326
   927
        delete _delta4;
deba@326
   928
      }
deba@326
   929
    }
deba@326
   930
deba@326
   931
    void matchedToEven(int blossom, int tree) {
deba@326
   932
      if (_delta2->state(blossom) == _delta2->IN_HEAP) {
deba@326
   933
        _delta2->erase(blossom);
deba@326
   934
      }
deba@326
   935
deba@326
   936
      if (!_blossom_set->trivial(blossom)) {
deba@326
   937
        (*_blossom_data)[blossom].pot -=
deba@326
   938
          2 * (_delta_sum - (*_blossom_data)[blossom].offset);
deba@326
   939
      }
deba@326
   940
deba@326
   941
      for (typename BlossomSet::ItemIt n(*_blossom_set, blossom);
deba@326
   942
           n != INVALID; ++n) {
deba@326
   943
deba@326
   944
        _blossom_set->increase(n, std::numeric_limits<Value>::max());
deba@326
   945
        int ni = (*_node_index)[n];
deba@326
   946
deba@326
   947
        (*_node_data)[ni].heap.clear();
deba@326
   948
        (*_node_data)[ni].heap_index.clear();
deba@326
   949
deba@326
   950
        (*_node_data)[ni].pot += _delta_sum - (*_blossom_data)[blossom].offset;
deba@326
   951
deba@326
   952
        _delta1->push(n, (*_node_data)[ni].pot);
deba@326
   953
deba@326
   954
        for (InArcIt e(_graph, n); e != INVALID; ++e) {
deba@326
   955
          Node v = _graph.source(e);
deba@326
   956
          int vb = _blossom_set->find(v);
deba@326
   957
          int vi = (*_node_index)[v];
deba@326
   958
deba@326
   959
          Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot -
deba@326
   960
            dualScale * _weight[e];
deba@326
   961
deba@326
   962
          if ((*_blossom_data)[vb].status == EVEN) {
deba@326
   963
            if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) {
deba@326
   964
              _delta3->push(e, rw / 2);
deba@326
   965
            }
deba@326
   966
          } else {
deba@326
   967
            typename std::map<int, Arc>::iterator it =
deba@326
   968
              (*_node_data)[vi].heap_index.find(tree);
deba@326
   969
deba@326
   970
            if (it != (*_node_data)[vi].heap_index.end()) {
deba@326
   971
              if ((*_node_data)[vi].heap[it->second] > rw) {
deba@326
   972
                (*_node_data)[vi].heap.replace(it->second, e);
deba@326
   973
                (*_node_data)[vi].heap.decrease(e, rw);
deba@326
   974
                it->second = e;
deba@326
   975
              }
deba@326
   976
            } else {
deba@326
   977
              (*_node_data)[vi].heap.push(e, rw);
deba@326
   978
              (*_node_data)[vi].heap_index.insert(std::make_pair(tree, e));
deba@326
   979
            }
deba@326
   980
deba@326
   981
            if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) {
deba@326
   982
              _blossom_set->decrease(v, (*_node_data)[vi].heap.prio());
deba@326
   983
deba@326
   984
              if ((*_blossom_data)[vb].status == MATCHED) {
deba@326
   985
                if (_delta2->state(vb) != _delta2->IN_HEAP) {
deba@326
   986
                  _delta2->push(vb, _blossom_set->classPrio(vb) -
deba@326
   987
                               (*_blossom_data)[vb].offset);
deba@326
   988
                } else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) -
deba@326
   989
                           (*_blossom_data)[vb].offset) {
deba@326
   990
                  _delta2->decrease(vb, _blossom_set->classPrio(vb) -
deba@326
   991
                                   (*_blossom_data)[vb].offset);
deba@326
   992
                }
deba@326
   993
              }
deba@326
   994
            }
deba@326
   995
          }
deba@326
   996
        }
deba@326
   997
      }
deba@326
   998
      (*_blossom_data)[blossom].offset = 0;
deba@326
   999
    }
deba@326
  1000
deba@868
  1001
    void matchedToOdd(int blossom) {
deba@326
  1002
      if (_delta2->state(blossom) == _delta2->IN_HEAP) {
deba@326
  1003
        _delta2->erase(blossom);
deba@326
  1004
      }
deba@868
  1005
      (*_blossom_data)[blossom].offset += _delta_sum;
deba@868
  1006
      if (!_blossom_set->trivial(blossom)) {
deba@868
  1007
        _delta4->push(blossom, (*_blossom_data)[blossom].pot / 2 +
deba@868
  1008
                      (*_blossom_data)[blossom].offset);
deba@868
  1009
      }
deba@868
  1010
    }
deba@868
  1011
deba@868
  1012
    void evenToMatched(int blossom, int tree) {
deba@868
  1013
      if (!_blossom_set->trivial(blossom)) {
deba@868
  1014
        (*_blossom_data)[blossom].pot += 2 * _delta_sum;
deba@868
  1015
      }
deba@326
  1016
deba@326
  1017
      for (typename BlossomSet::ItemIt n(*_blossom_set, blossom);
deba@326
  1018
           n != INVALID; ++n) {
deba@326
  1019
        int ni = (*_node_index)[n];
deba@868
  1020
        (*_node_data)[ni].pot -= _delta_sum;
deba@868
  1021
deba@868
  1022
        _delta1->erase(n);
deba@868
  1023
deba@868
  1024
        for (InArcIt e(_graph, n); e != INVALID; ++e) {
deba@868
  1025
          Node v = _graph.source(e);
deba@326
  1026
          int vb = _blossom_set->find(v);
deba@326
  1027
          int vi = (*_node_index)[v];
deba@326
  1028
deba@326
  1029
          Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot -
deba@326
  1030
            dualScale * _weight[e];
deba@326
  1031
deba@868
  1032
          if (vb == blossom) {
deba@868
  1033
            if (_delta3->state(e) == _delta3->IN_HEAP) {
deba@868
  1034
              _delta3->erase(e);
deba@868
  1035
            }
deba@868
  1036
          } else if ((*_blossom_data)[vb].status == EVEN) {
deba@868
  1037
deba@868
  1038
            if (_delta3->state(e) == _delta3->IN_HEAP) {
deba@868
  1039
              _delta3->erase(e);
deba@868
  1040
            }
deba@868
  1041
deba@868
  1042
            int vt = _tree_set->find(vb);
deba@868
  1043
deba@868
  1044
            if (vt != tree) {
deba@868
  1045
deba@868
  1046
              Arc r = _graph.oppositeArc(e);
deba@868
  1047
deba@868
  1048
              typename std::map<int, Arc>::iterator it =
deba@868
  1049
                (*_node_data)[ni].heap_index.find(vt);
deba@868
  1050
deba@868
  1051
              if (it != (*_node_data)[ni].heap_index.end()) {
deba@868
  1052
                if ((*_node_data)[ni].heap[it->second] > rw) {
deba@868
  1053
                  (*_node_data)[ni].heap.replace(it->second, r);
deba@868
  1054
                  (*_node_data)[ni].heap.decrease(r, rw);
deba@868
  1055
                  it->second = r;
deba@868
  1056
                }
deba@868
  1057
              } else {
deba@868
  1058
                (*_node_data)[ni].heap.push(r, rw);
deba@868
  1059
                (*_node_data)[ni].heap_index.insert(std::make_pair(vt, r));
deba@868
  1060
              }
deba@868
  1061
deba@868
  1062
              if ((*_blossom_set)[n] > (*_node_data)[ni].heap.prio()) {
deba@868
  1063
                _blossom_set->decrease(n, (*_node_data)[ni].heap.prio());
deba@868
  1064
deba@868
  1065
                if (_delta2->state(blossom) != _delta2->IN_HEAP) {
deba@868
  1066
                  _delta2->push(blossom, _blossom_set->classPrio(blossom) -
deba@868
  1067
                               (*_blossom_data)[blossom].offset);
deba@868
  1068
                } else if ((*_delta2)[blossom] >
deba@868
  1069
                           _blossom_set->classPrio(blossom) -
deba@868
  1070
                           (*_blossom_data)[blossom].offset){
deba@868
  1071
                  _delta2->decrease(blossom, _blossom_set->classPrio(blossom) -
deba@868
  1072
                                   (*_blossom_data)[blossom].offset);
deba@868
  1073
                }
deba@868
  1074
              }
deba@868
  1075
            }
deba@868
  1076
          } else {
deba@868
  1077
deba@868
  1078
            typename std::map<int, Arc>::iterator it =
deba@868
  1079
              (*_node_data)[vi].heap_index.find(tree);
deba@868
  1080
deba@868
  1081
            if (it != (*_node_data)[vi].heap_index.end()) {
deba@868
  1082
              (*_node_data)[vi].heap.erase(it->second);
deba@868
  1083
              (*_node_data)[vi].heap_index.erase(it);
deba@868
  1084
              if ((*_node_data)[vi].heap.empty()) {
deba@868
  1085
                _blossom_set->increase(v, std::numeric_limits<Value>::max());
deba@868
  1086
              } else if ((*_blossom_set)[v] < (*_node_data)[vi].heap.prio()) {
deba@868
  1087
                _blossom_set->increase(v, (*_node_data)[vi].heap.prio());
deba@868
  1088
              }
deba@868
  1089
deba@868
  1090
              if ((*_blossom_data)[vb].status == MATCHED) {
deba@868
  1091
                if (_blossom_set->classPrio(vb) ==
deba@868
  1092
                    std::numeric_limits<Value>::max()) {
deba@868
  1093
                  _delta2->erase(vb);
deba@868
  1094
                } else if ((*_delta2)[vb] < _blossom_set->classPrio(vb) -
deba@868
  1095
                           (*_blossom_data)[vb].offset) {
deba@868
  1096
                  _delta2->increase(vb, _blossom_set->classPrio(vb) -
deba@868
  1097
                                   (*_blossom_data)[vb].offset);
deba@868
  1098
                }
deba@868
  1099
              }
deba@326
  1100
            }
deba@326
  1101
          }
deba@326
  1102
        }
deba@326
  1103
      }
deba@326
  1104
    }
deba@326
  1105
deba@868
  1106
    void oddToMatched(int blossom) {
deba@868
  1107
      (*_blossom_data)[blossom].offset -= _delta_sum;
deba@868
  1108
deba@868
  1109
      if (_blossom_set->classPrio(blossom) !=
deba@868
  1110
          std::numeric_limits<Value>::max()) {
deba@868
  1111
        _delta2->push(blossom, _blossom_set->classPrio(blossom) -
deba@868
  1112
                      (*_blossom_data)[blossom].offset);
deba@868
  1113
      }
deba@868
  1114
deba@868
  1115
      if (!_blossom_set->trivial(blossom)) {
deba@868
  1116
        _delta4->erase(blossom);
deba@868
  1117
      }
deba@868
  1118
    }
deba@868
  1119
deba@868
  1120
    void oddToEven(int blossom, int tree) {
deba@868
  1121
      if (!_blossom_set->trivial(blossom)) {
deba@868
  1122
        _delta4->erase(blossom);
deba@868
  1123
        (*_blossom_data)[blossom].pot -=
deba@868
  1124
          2 * (2 * _delta_sum - (*_blossom_data)[blossom].offset);
deba@868
  1125
      }
deba@868
  1126
deba@326
  1127
      for (typename BlossomSet::ItemIt n(*_blossom_set, blossom);
deba@326
  1128
           n != INVALID; ++n) {
deba@326
  1129
        int ni = (*_node_index)[n];
deba@326
  1130
deba@868
  1131
        _blossom_set->increase(n, std::numeric_limits<Value>::max());
deba@868
  1132
deba@868
  1133
        (*_node_data)[ni].heap.clear();
deba@868
  1134
        (*_node_data)[ni].heap_index.clear();
deba@868
  1135
        (*_node_data)[ni].pot +=
deba@868
  1136
          2 * _delta_sum - (*_blossom_data)[blossom].offset;
deba@868
  1137
deba@868
  1138
        _delta1->push(n, (*_node_data)[ni].pot);
deba@868
  1139
deba@326
  1140
        for (InArcIt e(_graph, n); e != INVALID; ++e) {
deba@326
  1141
          Node v = _graph.source(e);
deba@326
  1142
          int vb = _blossom_set->find(v);
deba@326
  1143
          int vi = (*_node_index)[v];
deba@326
  1144
deba@326
  1145
          Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot -
deba@326
  1146
            dualScale * _weight[e];
deba@326
  1147
deba@868
  1148
          if ((*_blossom_data)[vb].status == EVEN) {
deba@868
  1149
            if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) {
deba@868
  1150
              _delta3->push(e, rw / 2);
deba@326
  1151
            }
deba@868
  1152
          } else {
deba@326
  1153
deba@326
  1154
            typename std::map<int, Arc>::iterator it =
deba@868
  1155
              (*_node_data)[vi].heap_index.find(tree);
deba@868
  1156
deba@868
  1157
            if (it != (*_node_data)[vi].heap_index.end()) {
deba@868
  1158
              if ((*_node_data)[vi].heap[it->second] > rw) {
deba@868
  1159
                (*_node_data)[vi].heap.replace(it->second, e);
deba@868
  1160
                (*_node_data)[vi].heap.decrease(e, rw);
deba@868
  1161
                it->second = e;
deba@326
  1162
              }
deba@326
  1163
            } else {
deba@868
  1164
              (*_node_data)[vi].heap.push(e, rw);
deba@868
  1165
              (*_node_data)[vi].heap_index.insert(std::make_pair(tree, e));
deba@326
  1166
            }
deba@326
  1167
deba@868
  1168
            if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) {
deba@868
  1169
              _blossom_set->decrease(v, (*_node_data)[vi].heap.prio());
deba@868
  1170
deba@868
  1171
              if ((*_blossom_data)[vb].status == MATCHED) {
deba@868
  1172
                if (_delta2->state(vb) != _delta2->IN_HEAP) {
deba@868
  1173
                  _delta2->push(vb, _blossom_set->classPrio(vb) -
deba@868
  1174
                               (*_blossom_data)[vb].offset);
deba@868
  1175
                } else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) -
deba@868
  1176
                           (*_blossom_data)[vb].offset) {
deba@868
  1177
                  _delta2->decrease(vb, _blossom_set->classPrio(vb) -
deba@868
  1178
                                   (*_blossom_data)[vb].offset);
deba@868
  1179
                }
deba@326
  1180
              }
deba@326
  1181
            }
deba@326
  1182
          }
deba@326
  1183
        }
deba@326
  1184
      }
deba@868
  1185
      (*_blossom_data)[blossom].offset = 0;
deba@326
  1186
    }
deba@326
  1187
deba@326
  1188
    void alternatePath(int even, int tree) {
deba@326
  1189
      int odd;
deba@326
  1190
deba@326
  1191
      evenToMatched(even, tree);
deba@326
  1192
      (*_blossom_data)[even].status = MATCHED;
deba@326
  1193
deba@326
  1194
      while ((*_blossom_data)[even].pred != INVALID) {
deba@326
  1195
        odd = _blossom_set->find(_graph.target((*_blossom_data)[even].pred));
deba@326
  1196
        (*_blossom_data)[odd].status = MATCHED;
deba@326
  1197
        oddToMatched(odd);
deba@326
  1198
        (*_blossom_data)[odd].next = (*_blossom_data)[odd].pred;
deba@326
  1199
deba@326
  1200
        even = _blossom_set->find(_graph.target((*_blossom_data)[odd].pred));
deba@326
  1201
        (*_blossom_data)[even].status = MATCHED;
deba@326
  1202
        evenToMatched(even, tree);
deba@326
  1203
        (*_blossom_data)[even].next =
deba@326
  1204
          _graph.oppositeArc((*_blossom_data)[odd].pred);
deba@326
  1205
      }
deba@326
  1206
deba@326
  1207
    }
deba@326
  1208
deba@326
  1209
    void destroyTree(int tree) {
deba@326
  1210
      for (TreeSet::ItemIt b(*_tree_set, tree); b != INVALID; ++b) {
deba@326
  1211
        if ((*_blossom_data)[b].status == EVEN) {
deba@326
  1212
          (*_blossom_data)[b].status = MATCHED;
deba@326
  1213
          evenToMatched(b, tree);
deba@326
  1214
        } else if ((*_blossom_data)[b].status == ODD) {
deba@326
  1215
          (*_blossom_data)[b].status = MATCHED;
deba@326
  1216
          oddToMatched(b);
deba@326
  1217
        }
deba@326
  1218
      }
deba@326
  1219
      _tree_set->eraseClass(tree);
deba@326
  1220
    }
deba@326
  1221
deba@326
  1222
deba@326
  1223
    void unmatchNode(const Node& node) {
deba@326
  1224
      int blossom = _blossom_set->find(node);
deba@326
  1225
      int tree = _tree_set->find(blossom);
deba@326
  1226
deba@326
  1227
      alternatePath(blossom, tree);
deba@326
  1228
      destroyTree(tree);
deba@326
  1229
deba@326
  1230
      (*_blossom_data)[blossom].base = node;
deba@868
  1231
      (*_blossom_data)[blossom].next = INVALID;
deba@326
  1232
    }
deba@326
  1233
deba@327
  1234
    void augmentOnEdge(const Edge& edge) {
deba@327
  1235
deba@327
  1236
      int left = _blossom_set->find(_graph.u(edge));
deba@327
  1237
      int right = _blossom_set->find(_graph.v(edge));
deba@326
  1238
deba@868
  1239
      int left_tree = _tree_set->find(left);
deba@868
  1240
      alternatePath(left, left_tree);
deba@868
  1241
      destroyTree(left_tree);
deba@868
  1242
deba@868
  1243
      int right_tree = _tree_set->find(right);
deba@868
  1244
      alternatePath(right, right_tree);
deba@868
  1245
      destroyTree(right_tree);
deba@326
  1246
deba@327
  1247
      (*_blossom_data)[left].next = _graph.direct(edge, true);
deba@327
  1248
      (*_blossom_data)[right].next = _graph.direct(edge, false);
deba@326
  1249
    }
deba@326
  1250
deba@868
  1251
    void augmentOnArc(const Arc& arc) {
deba@868
  1252
deba@868
  1253
      int left = _blossom_set->find(_graph.source(arc));
deba@868
  1254
      int right = _blossom_set->find(_graph.target(arc));
deba@868
  1255
deba@868
  1256
      (*_blossom_data)[left].status = MATCHED;
deba@868
  1257
deba@868
  1258
      int right_tree = _tree_set->find(right);
deba@868
  1259
      alternatePath(right, right_tree);
deba@868
  1260
      destroyTree(right_tree);
deba@868
  1261
deba@868
  1262
      (*_blossom_data)[left].next = arc;
deba@868
  1263
      (*_blossom_data)[right].next = _graph.oppositeArc(arc);
deba@868
  1264
    }
deba@868
  1265
deba@326
  1266
    void extendOnArc(const Arc& arc) {
deba@326
  1267
      int base = _blossom_set->find(_graph.target(arc));
deba@326
  1268
      int tree = _tree_set->find(base);
deba@326
  1269
deba@326
  1270
      int odd = _blossom_set->find(_graph.source(arc));
deba@326
  1271
      _tree_set->insert(odd, tree);
deba@326
  1272
      (*_blossom_data)[odd].status = ODD;
deba@326
  1273
      matchedToOdd(odd);
deba@326
  1274
      (*_blossom_data)[odd].pred = arc;
deba@326
  1275
deba@326
  1276
      int even = _blossom_set->find(_graph.target((*_blossom_data)[odd].next));
deba@326
  1277
      (*_blossom_data)[even].pred = (*_blossom_data)[even].next;
deba@326
  1278
      _tree_set->insert(even, tree);
deba@326
  1279
      (*_blossom_data)[even].status = EVEN;
deba@326
  1280
      matchedToEven(even, tree);
deba@326
  1281
    }
deba@326
  1282
deba@327
  1283
    void shrinkOnEdge(const Edge& edge, int tree) {
deba@326
  1284
      int nca = -1;
deba@326
  1285
      std::vector<int> left_path, right_path;
deba@326
  1286
deba@326
  1287
      {
deba@326
  1288
        std::set<int> left_set, right_set;
deba@326
  1289
        int left = _blossom_set->find(_graph.u(edge));
deba@326
  1290
        left_path.push_back(left);
deba@326
  1291
        left_set.insert(left);
deba@326
  1292
deba@326
  1293
        int right = _blossom_set->find(_graph.v(edge));
deba@326
  1294
        right_path.push_back(right);
deba@326
  1295
        right_set.insert(right);
deba@326
  1296
deba@326
  1297
        while (true) {
deba@326
  1298
deba@326
  1299
          if ((*_blossom_data)[left].pred == INVALID) break;
deba@326
  1300
deba@326
  1301
          left =
deba@326
  1302
            _blossom_set->find(_graph.target((*_blossom_data)[left].pred));
deba@326
  1303
          left_path.push_back(left);
deba@326
  1304
          left =
deba@326
  1305
            _blossom_set->find(_graph.target((*_blossom_data)[left].pred));
deba@326
  1306
          left_path.push_back(left);
deba@326
  1307
deba@326
  1308
          left_set.insert(left);
deba@326
  1309
deba@326
  1310
          if (right_set.find(left) != right_set.end()) {
deba@326
  1311
            nca = left;
deba@326
  1312
            break;
deba@326
  1313
          }
deba@326
  1314
deba@326
  1315
          if ((*_blossom_data)[right].pred == INVALID) break;
deba@326
  1316
deba@326
  1317
          right =
deba@326
  1318
            _blossom_set->find(_graph.target((*_blossom_data)[right].pred));
deba@326
  1319
          right_path.push_back(right);
deba@326
  1320
          right =
deba@326
  1321
            _blossom_set->find(_graph.target((*_blossom_data)[right].pred));
deba@326
  1322
          right_path.push_back(right);
deba@326
  1323
deba@326
  1324
          right_set.insert(right);
deba@326
  1325
deba@326
  1326
          if (left_set.find(right) != left_set.end()) {
deba@326
  1327
            nca = right;
deba@326
  1328
            break;
deba@326
  1329
          }
deba@326
  1330
deba@326
  1331
        }
deba@326
  1332
deba@326
  1333
        if (nca == -1) {
deba@326
  1334
          if ((*_blossom_data)[left].pred == INVALID) {
deba@326
  1335
            nca = right;
deba@326
  1336
            while (left_set.find(nca) == left_set.end()) {
deba@326
  1337
              nca =
deba@326
  1338
                _blossom_set->find(_graph.target((*_blossom_data)[nca].pred));
deba@326
  1339
              right_path.push_back(nca);
deba@326
  1340
              nca =
deba@326
  1341
                _blossom_set->find(_graph.target((*_blossom_data)[nca].pred));
deba@326
  1342
              right_path.push_back(nca);
deba@326
  1343
            }
deba@326
  1344
          } else {
deba@326
  1345
            nca = left;
deba@326
  1346
            while (right_set.find(nca) == right_set.end()) {
deba@326
  1347
              nca =
deba@326
  1348
                _blossom_set->find(_graph.target((*_blossom_data)[nca].pred));
deba@326
  1349
              left_path.push_back(nca);
deba@326
  1350
              nca =
deba@326
  1351
                _blossom_set->find(_graph.target((*_blossom_data)[nca].pred));
deba@326
  1352
              left_path.push_back(nca);
deba@326
  1353
            }
deba@326
  1354
          }
deba@326
  1355
        }
deba@326
  1356
      }
deba@326
  1357
deba@326
  1358
      std::vector<int> subblossoms;
deba@326
  1359
      Arc prev;
deba@326
  1360
deba@326
  1361
      prev = _graph.direct(edge, true);
deba@326
  1362
      for (int i = 0; left_path[i] != nca; i += 2) {
deba@326
  1363
        subblossoms.push_back(left_path[i]);
deba@326
  1364
        (*_blossom_data)[left_path[i]].next = prev;
deba@326
  1365
        _tree_set->erase(left_path[i]);
deba@326
  1366
deba@326
  1367
        subblossoms.push_back(left_path[i + 1]);
deba@326
  1368
        (*_blossom_data)[left_path[i + 1]].status = EVEN;
deba@326
  1369
        oddToEven(left_path[i + 1], tree);
deba@326
  1370
        _tree_set->erase(left_path[i + 1]);
deba@326
  1371
        prev = _graph.oppositeArc((*_blossom_data)[left_path[i + 1]].pred);
deba@326
  1372
      }
deba@326
  1373
deba@326
  1374
      int k = 0;
deba@326
  1375
      while (right_path[k] != nca) ++k;
deba@326
  1376
deba@326
  1377
      subblossoms.push_back(nca);
deba@326
  1378
      (*_blossom_data)[nca].next = prev;
deba@326
  1379
deba@326
  1380
      for (int i = k - 2; i >= 0; i -= 2) {
deba@326
  1381
        subblossoms.push_back(right_path[i + 1]);
deba@326
  1382
        (*_blossom_data)[right_path[i + 1]].status = EVEN;
deba@326
  1383
        oddToEven(right_path[i + 1], tree);
deba@326
  1384
        _tree_set->erase(right_path[i + 1]);
deba@326
  1385
deba@326
  1386
        (*_blossom_data)[right_path[i + 1]].next =
deba@326
  1387
          (*_blossom_data)[right_path[i + 1]].pred;
deba@326
  1388
deba@326
  1389
        subblossoms.push_back(right_path[i]);
deba@326
  1390
        _tree_set->erase(right_path[i]);
deba@326
  1391
      }
deba@326
  1392
deba@326
  1393
      int surface =
deba@326
  1394
        _blossom_set->join(subblossoms.begin(), subblossoms.end());
deba@326
  1395
deba@326
  1396
      for (int i = 0; i < int(subblossoms.size()); ++i) {
deba@326
  1397
        if (!_blossom_set->trivial(subblossoms[i])) {
deba@326
  1398
          (*_blossom_data)[subblossoms[i]].pot += 2 * _delta_sum;
deba@326
  1399
        }
deba@326
  1400
        (*_blossom_data)[subblossoms[i]].status = MATCHED;
deba@326
  1401
      }
deba@326
  1402
deba@326
  1403
      (*_blossom_data)[surface].pot = -2 * _delta_sum;
deba@326
  1404
      (*_blossom_data)[surface].offset = 0;
deba@326
  1405
      (*_blossom_data)[surface].status = EVEN;
deba@326
  1406
      (*_blossom_data)[surface].pred = (*_blossom_data)[nca].pred;
deba@326
  1407
      (*_blossom_data)[surface].next = (*_blossom_data)[nca].pred;
deba@326
  1408
deba@326
  1409
      _tree_set->insert(surface, tree);
deba@326
  1410
      _tree_set->erase(nca);
deba@326
  1411
    }
deba@326
  1412
deba@326
  1413
    void splitBlossom(int blossom) {
deba@326
  1414
      Arc next = (*_blossom_data)[blossom].next;
deba@326
  1415
      Arc pred = (*_blossom_data)[blossom].pred;
deba@326
  1416
deba@326
  1417
      int tree = _tree_set->find(blossom);
deba@326
  1418
deba@326
  1419
      (*_blossom_data)[blossom].status = MATCHED;
deba@326
  1420
      oddToMatched(blossom);
deba@326
  1421
      if (_delta2->state(blossom) == _delta2->IN_HEAP) {
deba@326
  1422
        _delta2->erase(blossom);
deba@326
  1423
      }
deba@326
  1424
deba@326
  1425
      std::vector<int> subblossoms;
deba@326
  1426
      _blossom_set->split(blossom, std::back_inserter(subblossoms));
deba@326
  1427
deba@326
  1428
      Value offset = (*_blossom_data)[blossom].offset;
deba@326
  1429
      int b = _blossom_set->find(_graph.source(pred));
deba@326
  1430
      int d = _blossom_set->find(_graph.source(next));
deba@326
  1431
deba@326
  1432
      int ib = -1, id = -1;
deba@326
  1433
      for (int i = 0; i < int(subblossoms.size()); ++i) {
deba@326
  1434
        if (subblossoms[i] == b) ib = i;
deba@326
  1435
        if (subblossoms[i] == d) id = i;
deba@326
  1436
deba@326
  1437
        (*_blossom_data)[subblossoms[i]].offset = offset;
deba@326
  1438
        if (!_blossom_set->trivial(subblossoms[i])) {
deba@326
  1439
          (*_blossom_data)[subblossoms[i]].pot -= 2 * offset;
deba@326
  1440
        }
deba@326
  1441
        if (_blossom_set->classPrio(subblossoms[i]) !=
deba@326
  1442
            std::numeric_limits<Value>::max()) {
deba@326
  1443
          _delta2->push(subblossoms[i],
deba@326
  1444
                        _blossom_set->classPrio(subblossoms[i]) -
deba@326
  1445
                        (*_blossom_data)[subblossoms[i]].offset);
deba@326
  1446
        }
deba@326
  1447
      }
deba@326
  1448
deba@326
  1449
      if (id > ib ? ((id - ib) % 2 == 0) : ((ib - id) % 2 == 1)) {
deba@326
  1450
        for (int i = (id + 1) % subblossoms.size();
deba@326
  1451
             i != ib; i = (i + 2) % subblossoms.size()) {
deba@326
  1452
          int sb = subblossoms[i];
deba@326
  1453
          int tb = subblossoms[(i + 1) % subblossoms.size()];
deba@326
  1454
          (*_blossom_data)[sb].next =
deba@326
  1455
            _graph.oppositeArc((*_blossom_data)[tb].next);
deba@326
  1456
        }
deba@326
  1457
deba@326
  1458
        for (int i = ib; i != id; i = (i + 2) % subblossoms.size()) {
deba@326
  1459
          int sb = subblossoms[i];
deba@326
  1460
          int tb = subblossoms[(i + 1) % subblossoms.size()];
deba@326
  1461
          int ub = subblossoms[(i + 2) % subblossoms.size()];
deba@326
  1462
deba@326
  1463
          (*_blossom_data)[sb].status = ODD;
deba@326
  1464
          matchedToOdd(sb);
deba@326
  1465
          _tree_set->insert(sb, tree);
deba@326
  1466
          (*_blossom_data)[sb].pred = pred;
deba@326
  1467
          (*_blossom_data)[sb].next =
deba@868
  1468
            _graph.oppositeArc((*_blossom_data)[tb].next);
deba@326
  1469
deba@326
  1470
          pred = (*_blossom_data)[ub].next;
deba@326
  1471
deba@326
  1472
          (*_blossom_data)[tb].status = EVEN;
deba@326
  1473
          matchedToEven(tb, tree);
deba@326
  1474
          _tree_set->insert(tb, tree);
deba@326
  1475
          (*_blossom_data)[tb].pred = (*_blossom_data)[tb].next;
deba@326
  1476
        }
deba@326
  1477
deba@326
  1478
        (*_blossom_data)[subblossoms[id]].status = ODD;
deba@326
  1479
        matchedToOdd(subblossoms[id]);
deba@326
  1480
        _tree_set->insert(subblossoms[id], tree);
deba@326
  1481
        (*_blossom_data)[subblossoms[id]].next = next;
deba@326
  1482
        (*_blossom_data)[subblossoms[id]].pred = pred;
deba@326
  1483
deba@326
  1484
      } else {
deba@326
  1485
deba@326
  1486
        for (int i = (ib + 1) % subblossoms.size();
deba@326
  1487
             i != id; i = (i + 2) % subblossoms.size()) {
deba@326
  1488
          int sb = subblossoms[i];
deba@326
  1489
          int tb = subblossoms[(i + 1) % subblossoms.size()];
deba@326
  1490
          (*_blossom_data)[sb].next =
deba@326
  1491
            _graph.oppositeArc((*_blossom_data)[tb].next);
deba@326
  1492
        }
deba@326
  1493
deba@326
  1494
        for (int i = id; i != ib; i = (i + 2) % subblossoms.size()) {
deba@326
  1495
          int sb = subblossoms[i];
deba@326
  1496
          int tb = subblossoms[(i + 1) % subblossoms.size()];
deba@326
  1497
          int ub = subblossoms[(i + 2) % subblossoms.size()];
deba@326
  1498
deba@326
  1499
          (*_blossom_data)[sb].status = ODD;
deba@326
  1500
          matchedToOdd(sb);
deba@326
  1501
          _tree_set->insert(sb, tree);
deba@326
  1502
          (*_blossom_data)[sb].next = next;
deba@326
  1503
          (*_blossom_data)[sb].pred =
deba@326
  1504
            _graph.oppositeArc((*_blossom_data)[tb].next);
deba@326
  1505
deba@326
  1506
          (*_blossom_data)[tb].status = EVEN;
deba@326
  1507
          matchedToEven(tb, tree);
deba@326
  1508
          _tree_set->insert(tb, tree);
deba@326
  1509
          (*_blossom_data)[tb].pred =
deba@326
  1510
            (*_blossom_data)[tb].next =
deba@326
  1511
            _graph.oppositeArc((*_blossom_data)[ub].next);
deba@326
  1512
          next = (*_blossom_data)[ub].next;
deba@326
  1513
        }
deba@326
  1514
deba@326
  1515
        (*_blossom_data)[subblossoms[ib]].status = ODD;
deba@326
  1516
        matchedToOdd(subblossoms[ib]);
deba@326
  1517
        _tree_set->insert(subblossoms[ib], tree);
deba@326
  1518
        (*_blossom_data)[subblossoms[ib]].next = next;
deba@326
  1519
        (*_blossom_data)[subblossoms[ib]].pred = pred;
deba@326
  1520
      }
deba@326
  1521
      _tree_set->erase(blossom);
deba@326
  1522
    }
deba@326
  1523
deba@326
  1524
    void extractBlossom(int blossom, const Node& base, const Arc& matching) {
deba@326
  1525
      if (_blossom_set->trivial(blossom)) {
deba@326
  1526
        int bi = (*_node_index)[base];
deba@326
  1527
        Value pot = (*_node_data)[bi].pot;
deba@326
  1528
kpeter@581
  1529
        (*_matching)[base] = matching;
deba@326
  1530
        _blossom_node_list.push_back(base);
kpeter@581
  1531
        (*_node_potential)[base] = pot;
deba@326
  1532
      } else {
deba@326
  1533
deba@326
  1534
        Value pot = (*_blossom_data)[blossom].pot;
deba@326
  1535
        int bn = _blossom_node_list.size();
deba@326
  1536
deba@326
  1537
        std::vector<int> subblossoms;
deba@326
  1538
        _blossom_set->split(blossom, std::back_inserter(subblossoms));
deba@326
  1539
        int b = _blossom_set->find(base);
deba@326
  1540
        int ib = -1;
deba@326
  1541
        for (int i = 0; i < int(subblossoms.size()); ++i) {
deba@326
  1542
          if (subblossoms[i] == b) { ib = i; break; }
deba@326
  1543
        }
deba@326
  1544
deba@326
  1545
        for (int i = 1; i < int(subblossoms.size()); i += 2) {
deba@326
  1546
          int sb = subblossoms[(ib + i) % subblossoms.size()];
deba@326
  1547
          int tb = subblossoms[(ib + i + 1) % subblossoms.size()];
deba@326
  1548
deba@326
  1549
          Arc m = (*_blossom_data)[tb].next;
deba@326
  1550
          extractBlossom(sb, _graph.target(m), _graph.oppositeArc(m));
deba@326
  1551
          extractBlossom(tb, _graph.source(m), m);
deba@326
  1552
        }
deba@326
  1553
        extractBlossom(subblossoms[ib], base, matching);
deba@326
  1554
deba@326
  1555
        int en = _blossom_node_list.size();
deba@326
  1556
deba@326
  1557
        _blossom_potential.push_back(BlossomVariable(bn, en, pot));
deba@326
  1558
      }
deba@326
  1559
    }
deba@326
  1560
deba@326
  1561
    void extractMatching() {
deba@326
  1562
      std::vector<int> blossoms;
deba@326
  1563
      for (typename BlossomSet::ClassIt c(*_blossom_set); c != INVALID; ++c) {
deba@326
  1564
        blossoms.push_back(c);
deba@326
  1565
      }
deba@326
  1566
deba@326
  1567
      for (int i = 0; i < int(blossoms.size()); ++i) {
deba@868
  1568
        if ((*_blossom_data)[blossoms[i]].next != INVALID) {
deba@326
  1569
deba@326
  1570
          Value offset = (*_blossom_data)[blossoms[i]].offset;
deba@326
  1571
          (*_blossom_data)[blossoms[i]].pot += 2 * offset;
deba@326
  1572
          for (typename BlossomSet::ItemIt n(*_blossom_set, blossoms[i]);
deba@326
  1573
               n != INVALID; ++n) {
deba@326
  1574
            (*_node_data)[(*_node_index)[n]].pot -= offset;
deba@326
  1575
          }
deba@326
  1576
deba@326
  1577
          Arc matching = (*_blossom_data)[blossoms[i]].next;
deba@326
  1578
          Node base = _graph.source(matching);
deba@326
  1579
          extractBlossom(blossoms[i], base, matching);
deba@326
  1580
        } else {
deba@326
  1581
          Node base = (*_blossom_data)[blossoms[i]].base;
deba@326
  1582
          extractBlossom(blossoms[i], base, INVALID);
deba@326
  1583
        }
deba@326
  1584
      }
deba@326
  1585
    }
deba@326
  1586
deba@326
  1587
  public:
deba@326
  1588
deba@326
  1589
    /// \brief Constructor
deba@326
  1590
    ///
deba@326
  1591
    /// Constructor.
deba@326
  1592
    MaxWeightedMatching(const Graph& graph, const WeightMap& weight)
deba@326
  1593
      : _graph(graph), _weight(weight), _matching(0),
deba@326
  1594
        _node_potential(0), _blossom_potential(), _blossom_node_list(),
deba@326
  1595
        _node_num(0), _blossom_num(0),
deba@326
  1596
deba@326
  1597
        _blossom_index(0), _blossom_set(0), _blossom_data(0),
deba@326
  1598
        _node_index(0), _node_heap_index(0), _node_data(0),
deba@326
  1599
        _tree_set_index(0), _tree_set(0),
deba@326
  1600
deba@326
  1601
        _delta1_index(0), _delta1(0),
deba@326
  1602
        _delta2_index(0), _delta2(0),
deba@326
  1603
        _delta3_index(0), _delta3(0),
deba@326
  1604
        _delta4_index(0), _delta4(0),
deba@326
  1605
deba@870
  1606
        _delta_sum(), _unmatched(0),
deba@870
  1607
deba@870
  1608
        _fractional(0)
deba@870
  1609
    {}
deba@326
  1610
deba@326
  1611
    ~MaxWeightedMatching() {
deba@326
  1612
      destroyStructures();
deba@870
  1613
      if (_fractional) {
deba@870
  1614
        delete _fractional;
deba@870
  1615
      }
deba@326
  1616
    }
deba@326
  1617
kpeter@590
  1618
    /// \name Execution Control
alpar@330
  1619
    /// The simplest way to execute the algorithm is to use the
kpeter@590
  1620
    /// \ref run() member function.
deba@326
  1621
deba@326
  1622
    ///@{
deba@326
  1623
deba@326
  1624
    /// \brief Initialize the algorithm
deba@326
  1625
    ///
kpeter@590
  1626
    /// This function initializes the algorithm.
deba@326
  1627
    void init() {
deba@326
  1628
      createStructures();
deba@326
  1629
deba@867
  1630
      _blossom_node_list.clear();
deba@867
  1631
      _blossom_potential.clear();
deba@867
  1632
deba@326
  1633
      for (ArcIt e(_graph); e != INVALID; ++e) {
kpeter@581
  1634
        (*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP;
deba@326
  1635
      }
deba@326
  1636
      for (NodeIt n(_graph); n != INVALID; ++n) {
kpeter@581
  1637
        (*_delta1_index)[n] = _delta1->PRE_HEAP;
deba@326
  1638
      }
deba@326
  1639
      for (EdgeIt e(_graph); e != INVALID; ++e) {
kpeter@581
  1640
        (*_delta3_index)[e] = _delta3->PRE_HEAP;
deba@326
  1641
      }
deba@326
  1642
      for (int i = 0; i < _blossom_num; ++i) {
kpeter@581
  1643
        (*_delta2_index)[i] = _delta2->PRE_HEAP;
kpeter@581
  1644
        (*_delta4_index)[i] = _delta4->PRE_HEAP;
deba@326
  1645
      }
alpar@877
  1646
deba@870
  1647
      _unmatched = _node_num;
deba@870
  1648
deba@867
  1649
      _delta1->clear();
deba@867
  1650
      _delta2->clear();
deba@867
  1651
      _delta3->clear();
deba@867
  1652
      _delta4->clear();
deba@867
  1653
      _blossom_set->clear();
deba@867
  1654
      _tree_set->clear();
deba@326
  1655
deba@326
  1656
      int index = 0;
deba@326
  1657
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@326
  1658
        Value max = 0;
deba@326
  1659
        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
deba@326
  1660
          if (_graph.target(e) == n) continue;
deba@326
  1661
          if ((dualScale * _weight[e]) / 2 > max) {
deba@326
  1662
            max = (dualScale * _weight[e]) / 2;
deba@326
  1663
          }
deba@326
  1664
        }
kpeter@581
  1665
        (*_node_index)[n] = index;
deba@867
  1666
        (*_node_data)[index].heap_index.clear();
deba@867
  1667
        (*_node_data)[index].heap.clear();
deba@326
  1668
        (*_node_data)[index].pot = max;
deba@326
  1669
        _delta1->push(n, max);
deba@326
  1670
        int blossom =
deba@326
  1671
          _blossom_set->insert(n, std::numeric_limits<Value>::max());
deba@326
  1672
deba@326
  1673
        _tree_set->insert(blossom);
deba@326
  1674
deba@326
  1675
        (*_blossom_data)[blossom].status = EVEN;
deba@326
  1676
        (*_blossom_data)[blossom].pred = INVALID;
deba@326
  1677
        (*_blossom_data)[blossom].next = INVALID;
deba@326
  1678
        (*_blossom_data)[blossom].pot = 0;
deba@326
  1679
        (*_blossom_data)[blossom].offset = 0;
deba@326
  1680
        ++index;
deba@326
  1681
      }
deba@326
  1682
      for (EdgeIt e(_graph); e != INVALID; ++e) {
deba@326
  1683
        int si = (*_node_index)[_graph.u(e)];
deba@326
  1684
        int ti = (*_node_index)[_graph.v(e)];
deba@326
  1685
        if (_graph.u(e) != _graph.v(e)) {
deba@326
  1686
          _delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot -
deba@326
  1687
                            dualScale * _weight[e]) / 2);
deba@326
  1688
        }
deba@326
  1689
      }
deba@326
  1690
    }
deba@326
  1691
deba@870
  1692
    /// \brief Initialize the algorithm with fractional matching
deba@870
  1693
    ///
deba@870
  1694
    /// This function initializes the algorithm with a fractional
deba@870
  1695
    /// matching. This initialization is also called jumpstart heuristic.
deba@870
  1696
    void fractionalInit() {
deba@870
  1697
      createStructures();
deba@876
  1698
deba@876
  1699
      _blossom_node_list.clear();
deba@876
  1700
      _blossom_potential.clear();
alpar@877
  1701
deba@870
  1702
      if (_fractional == 0) {
deba@870
  1703
        _fractional = new FractionalMatching(_graph, _weight, false);
deba@870
  1704
      }
deba@870
  1705
      _fractional->run();
deba@870
  1706
deba@870
  1707
      for (ArcIt e(_graph); e != INVALID; ++e) {
deba@870
  1708
        (*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP;
deba@870
  1709
      }
deba@870
  1710
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@870
  1711
        (*_delta1_index)[n] = _delta1->PRE_HEAP;
deba@870
  1712
      }
deba@870
  1713
      for (EdgeIt e(_graph); e != INVALID; ++e) {
deba@870
  1714
        (*_delta3_index)[e] = _delta3->PRE_HEAP;
deba@870
  1715
      }
deba@870
  1716
      for (int i = 0; i < _blossom_num; ++i) {
deba@870
  1717
        (*_delta2_index)[i] = _delta2->PRE_HEAP;
deba@870
  1718
        (*_delta4_index)[i] = _delta4->PRE_HEAP;
deba@870
  1719
      }
deba@870
  1720
deba@870
  1721
      _unmatched = 0;
deba@870
  1722
deba@876
  1723
      _delta1->clear();
deba@876
  1724
      _delta2->clear();
deba@876
  1725
      _delta3->clear();
deba@876
  1726
      _delta4->clear();
deba@876
  1727
      _blossom_set->clear();
deba@876
  1728
      _tree_set->clear();
deba@876
  1729
deba@870
  1730
      int index = 0;
deba@870
  1731
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@870
  1732
        Value pot = _fractional->nodeValue(n);
deba@870
  1733
        (*_node_index)[n] = index;
deba@870
  1734
        (*_node_data)[index].pot = pot;
deba@876
  1735
        (*_node_data)[index].heap_index.clear();
deba@876
  1736
        (*_node_data)[index].heap.clear();
deba@870
  1737
        int blossom =
deba@870
  1738
          _blossom_set->insert(n, std::numeric_limits<Value>::max());
deba@870
  1739
deba@870
  1740
        (*_blossom_data)[blossom].status = MATCHED;
deba@870
  1741
        (*_blossom_data)[blossom].pred = INVALID;
deba@870
  1742
        (*_blossom_data)[blossom].next = _fractional->matching(n);
deba@870
  1743
        if (_fractional->matching(n) == INVALID) {
deba@870
  1744
          (*_blossom_data)[blossom].base = n;
deba@870
  1745
        }
deba@870
  1746
        (*_blossom_data)[blossom].pot = 0;
deba@870
  1747
        (*_blossom_data)[blossom].offset = 0;
deba@870
  1748
        ++index;
deba@870
  1749
      }
deba@870
  1750
deba@870
  1751
      typename Graph::template NodeMap<bool> processed(_graph, false);
deba@870
  1752
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@870
  1753
        if (processed[n]) continue;
deba@870
  1754
        processed[n] = true;
deba@870
  1755
        if (_fractional->matching(n) == INVALID) continue;
deba@870
  1756
        int num = 1;
deba@870
  1757
        Node v = _graph.target(_fractional->matching(n));
deba@870
  1758
        while (n != v) {
deba@870
  1759
          processed[v] = true;
deba@870
  1760
          v = _graph.target(_fractional->matching(v));
deba@870
  1761
          ++num;
deba@870
  1762
        }
deba@870
  1763
deba@870
  1764
        if (num % 2 == 1) {
deba@870
  1765
          std::vector<int> subblossoms(num);
deba@870
  1766
deba@870
  1767
          subblossoms[--num] = _blossom_set->find(n);
deba@870
  1768
          _delta1->push(n, _fractional->nodeValue(n));
deba@870
  1769
          v = _graph.target(_fractional->matching(n));
deba@870
  1770
          while (n != v) {
deba@870
  1771
            subblossoms[--num] = _blossom_set->find(v);
deba@870
  1772
            _delta1->push(v, _fractional->nodeValue(v));
alpar@877
  1773
            v = _graph.target(_fractional->matching(v));
deba@870
  1774
          }
alpar@877
  1775
alpar@877
  1776
          int surface =
deba@870
  1777
            _blossom_set->join(subblossoms.begin(), subblossoms.end());
deba@870
  1778
          (*_blossom_data)[surface].status = EVEN;
deba@870
  1779
          (*_blossom_data)[surface].pred = INVALID;
deba@870
  1780
          (*_blossom_data)[surface].next = INVALID;
deba@870
  1781
          (*_blossom_data)[surface].pot = 0;
deba@870
  1782
          (*_blossom_data)[surface].offset = 0;
alpar@877
  1783
deba@870
  1784
          _tree_set->insert(surface);
deba@870
  1785
          ++_unmatched;
deba@870
  1786
        }
deba@870
  1787
      }
deba@870
  1788
deba@870
  1789
      for (EdgeIt e(_graph); e != INVALID; ++e) {
deba@870
  1790
        int si = (*_node_index)[_graph.u(e)];
deba@870
  1791
        int sb = _blossom_set->find(_graph.u(e));
deba@870
  1792
        int ti = (*_node_index)[_graph.v(e)];
deba@870
  1793
        int tb = _blossom_set->find(_graph.v(e));
deba@870
  1794
        if ((*_blossom_data)[sb].status == EVEN &&
deba@870
  1795
            (*_blossom_data)[tb].status == EVEN && sb != tb) {
deba@870
  1796
          _delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot -
deba@870
  1797
                            dualScale * _weight[e]) / 2);
deba@870
  1798
        }
deba@870
  1799
      }
deba@870
  1800
deba@870
  1801
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@870
  1802
        int nb = _blossom_set->find(n);
deba@870
  1803
        if ((*_blossom_data)[nb].status != MATCHED) continue;
deba@870
  1804
        int ni = (*_node_index)[n];
deba@870
  1805
deba@870
  1806
        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
deba@870
  1807
          Node v = _graph.target(e);
deba@870
  1808
          int vb = _blossom_set->find(v);
deba@870
  1809
          int vi = (*_node_index)[v];
deba@870
  1810
deba@870
  1811
          Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot -
deba@870
  1812
            dualScale * _weight[e];
deba@870
  1813
deba@870
  1814
          if ((*_blossom_data)[vb].status == EVEN) {
deba@870
  1815
deba@870
  1816
            int vt = _tree_set->find(vb);
deba@870
  1817
deba@870
  1818
            typename std::map<int, Arc>::iterator it =
deba@870
  1819
              (*_node_data)[ni].heap_index.find(vt);
deba@870
  1820
deba@870
  1821
            if (it != (*_node_data)[ni].heap_index.end()) {
deba@870
  1822
              if ((*_node_data)[ni].heap[it->second] > rw) {
deba@870
  1823
                (*_node_data)[ni].heap.replace(it->second, e);
deba@870
  1824
                (*_node_data)[ni].heap.decrease(e, rw);
deba@870
  1825
                it->second = e;
deba@870
  1826
              }
deba@870
  1827
            } else {
deba@870
  1828
              (*_node_data)[ni].heap.push(e, rw);
deba@870
  1829
              (*_node_data)[ni].heap_index.insert(std::make_pair(vt, e));
deba@870
  1830
            }
deba@870
  1831
          }
deba@870
  1832
        }
alpar@877
  1833
deba@870
  1834
        if (!(*_node_data)[ni].heap.empty()) {
deba@870
  1835
          _blossom_set->decrease(n, (*_node_data)[ni].heap.prio());
deba@870
  1836
          _delta2->push(nb, _blossom_set->classPrio(nb));
deba@870
  1837
        }
deba@870
  1838
      }
deba@870
  1839
    }
deba@870
  1840
kpeter@590
  1841
    /// \brief Start the algorithm
deba@326
  1842
    ///
kpeter@590
  1843
    /// This function starts the algorithm.
kpeter@590
  1844
    ///
deba@870
  1845
    /// \pre \ref init() or \ref fractionalInit() must be called
deba@870
  1846
    /// before using this function.
deba@326
  1847
    void start() {
deba@326
  1848
      enum OpType {
deba@326
  1849
        D1, D2, D3, D4
deba@326
  1850
      };
deba@326
  1851
deba@870
  1852
      while (_unmatched > 0) {
deba@326
  1853
        Value d1 = !_delta1->empty() ?
deba@326
  1854
          _delta1->prio() : std::numeric_limits<Value>::max();
deba@326
  1855
deba@326
  1856
        Value d2 = !_delta2->empty() ?
deba@326
  1857
          _delta2->prio() : std::numeric_limits<Value>::max();
deba@326
  1858
deba@326
  1859
        Value d3 = !_delta3->empty() ?
deba@326
  1860
          _delta3->prio() : std::numeric_limits<Value>::max();
deba@326
  1861
deba@326
  1862
        Value d4 = !_delta4->empty() ?
deba@326
  1863
          _delta4->prio() : std::numeric_limits<Value>::max();
deba@326
  1864
deba@868
  1865
        _delta_sum = d3; OpType ot = D3;
deba@868
  1866
        if (d1 < _delta_sum) { _delta_sum = d1; ot = D1; }
deba@326
  1867
        if (d2 < _delta_sum) { _delta_sum = d2; ot = D2; }
deba@326
  1868
        if (d4 < _delta_sum) { _delta_sum = d4; ot = D4; }
deba@326
  1869
deba@326
  1870
        switch (ot) {
deba@326
  1871
        case D1:
deba@326
  1872
          {
deba@326
  1873
            Node n = _delta1->top();
deba@326
  1874
            unmatchNode(n);
deba@870
  1875
            --_unmatched;
deba@326
  1876
          }
deba@326
  1877
          break;
deba@326
  1878
        case D2:
deba@326
  1879
          {
deba@326
  1880
            int blossom = _delta2->top();
deba@326
  1881
            Node n = _blossom_set->classTop(blossom);
deba@868
  1882
            Arc a = (*_node_data)[(*_node_index)[n]].heap.top();
deba@868
  1883
            if ((*_blossom_data)[blossom].next == INVALID) {
deba@868
  1884
              augmentOnArc(a);
deba@870
  1885
              --_unmatched;
deba@868
  1886
            } else {
deba@868
  1887
              extendOnArc(a);
deba@868
  1888
            }
deba@326
  1889
          }
deba@326
  1890
          break;
deba@326
  1891
        case D3:
deba@326
  1892
          {
deba@326
  1893
            Edge e = _delta3->top();
deba@326
  1894
deba@326
  1895
            int left_blossom = _blossom_set->find(_graph.u(e));
deba@326
  1896
            int right_blossom = _blossom_set->find(_graph.v(e));
deba@326
  1897
deba@326
  1898
            if (left_blossom == right_blossom) {
deba@326
  1899
              _delta3->pop();
deba@326
  1900
            } else {
deba@868
  1901
              int left_tree = _tree_set->find(left_blossom);
deba@868
  1902
              int right_tree = _tree_set->find(right_blossom);
deba@326
  1903
deba@326
  1904
              if (left_tree == right_tree) {
deba@327
  1905
                shrinkOnEdge(e, left_tree);
deba@326
  1906
              } else {
deba@327
  1907
                augmentOnEdge(e);
deba@870
  1908
                _unmatched -= 2;
deba@326
  1909
              }
deba@326
  1910
            }
deba@326
  1911
          } break;
deba@326
  1912
        case D4:
deba@326
  1913
          splitBlossom(_delta4->top());
deba@326
  1914
          break;
deba@326
  1915
        }
deba@326
  1916
      }
deba@326
  1917
      extractMatching();
deba@326
  1918
    }
deba@326
  1919
kpeter@590
  1920
    /// \brief Run the algorithm.
deba@326
  1921
    ///
kpeter@590
  1922
    /// This method runs the \c %MaxWeightedMatching algorithm.
deba@326
  1923
    ///
deba@326
  1924
    /// \note mwm.run() is just a shortcut of the following code.
deba@326
  1925
    /// \code
deba@870
  1926
    ///   mwm.fractionalInit();
deba@326
  1927
    ///   mwm.start();
deba@326
  1928
    /// \endcode
deba@326
  1929
    void run() {
deba@870
  1930
      fractionalInit();
deba@326
  1931
      start();
deba@326
  1932
    }
deba@326
  1933
deba@326
  1934
    /// @}
deba@326
  1935
kpeter@590
  1936
    /// \name Primal Solution
deba@868
  1937
    /// Functions to get the primal solution, i.e. the maximum weighted
kpeter@590
  1938
    /// matching.\n
kpeter@590
  1939
    /// Either \ref run() or \ref start() function should be called before
kpeter@590
  1940
    /// using them.
deba@326
  1941
deba@326
  1942
    /// @{
deba@326
  1943
kpeter@590
  1944
    /// \brief Return the weight of the matching.
deba@326
  1945
    ///
kpeter@590
  1946
    /// This function returns the weight of the found matching.
kpeter@590
  1947
    ///
kpeter@590
  1948
    /// \pre Either run() or start() must be called before using this function.
kpeter@593
  1949
    Value matchingWeight() const {
deba@326
  1950
      Value sum = 0;
deba@326
  1951
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@326
  1952
        if ((*_matching)[n] != INVALID) {
deba@326
  1953
          sum += _weight[(*_matching)[n]];
deba@326
  1954
        }
deba@326
  1955
      }
deba@868
  1956
      return sum / 2;
deba@326
  1957
    }
deba@326
  1958
kpeter@590
  1959
    /// \brief Return the size (cardinality) of the matching.
deba@326
  1960
    ///
kpeter@590
  1961
    /// This function returns the size (cardinality) of the found matching.
kpeter@590
  1962
    ///
kpeter@590
  1963
    /// \pre Either run() or start() must be called before using this function.
deba@327
  1964
    int matchingSize() const {
deba@327
  1965
      int num = 0;
deba@327
  1966
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@327
  1967
        if ((*_matching)[n] != INVALID) {
deba@327
  1968
          ++num;
deba@327
  1969
        }
deba@327
  1970
      }
deba@327
  1971
      return num /= 2;
deba@327
  1972
    }
deba@327
  1973
kpeter@590
  1974
    /// \brief Return \c true if the given edge is in the matching.
deba@327
  1975
    ///
deba@868
  1976
    /// This function returns \c true if the given edge is in the found
kpeter@590
  1977
    /// matching.
kpeter@590
  1978
    ///
kpeter@590
  1979
    /// \pre Either run() or start() must be called before using this function.
deba@327
  1980
    bool matching(const Edge& edge) const {
deba@327
  1981
      return edge == (*_matching)[_graph.u(edge)];
deba@326
  1982
    }
deba@326
  1983
kpeter@590
  1984
    /// \brief Return the matching arc (or edge) incident to the given node.
deba@326
  1985
    ///
kpeter@590
  1986
    /// This function returns the matching arc (or edge) incident to the
deba@868
  1987
    /// given node in the found matching or \c INVALID if the node is
kpeter@590
  1988
    /// not covered by the matching.
kpeter@590
  1989
    ///
kpeter@590
  1990
    /// \pre Either run() or start() must be called before using this function.
deba@326
  1991
    Arc matching(const Node& node) const {
deba@326
  1992
      return (*_matching)[node];
deba@326
  1993
    }
deba@326
  1994
kpeter@593
  1995
    /// \brief Return a const reference to the matching map.
kpeter@593
  1996
    ///
kpeter@593
  1997
    /// This function returns a const reference to a node map that stores
kpeter@593
  1998
    /// the matching arc (or edge) incident to each node.
kpeter@593
  1999
    const MatchingMap& matchingMap() const {
kpeter@593
  2000
      return *_matching;
kpeter@593
  2001
    }
kpeter@593
  2002
kpeter@590
  2003
    /// \brief Return the mate of the given node.
deba@326
  2004
    ///
deba@868
  2005
    /// This function returns the mate of the given node in the found
kpeter@590
  2006
    /// matching or \c INVALID if the node is not covered by the matching.
kpeter@590
  2007
    ///
kpeter@590
  2008
    /// \pre Either run() or start() must be called before using this function.
deba@326
  2009
    Node mate(const Node& node) const {
deba@326
  2010
      return (*_matching)[node] != INVALID ?
deba@326
  2011
        _graph.target((*_matching)[node]) : INVALID;
deba@326
  2012
    }
deba@326
  2013
deba@326
  2014
    /// @}
deba@326
  2015
kpeter@590
  2016
    /// \name Dual Solution
kpeter@590
  2017
    /// Functions to get the dual solution.\n
kpeter@590
  2018
    /// Either \ref run() or \ref start() function should be called before
kpeter@590
  2019
    /// using them.
deba@326
  2020
deba@326
  2021
    /// @{
deba@326
  2022
kpeter@590
  2023
    /// \brief Return the value of the dual solution.
deba@326
  2024
    ///
deba@868
  2025
    /// This function returns the value of the dual solution.
deba@868
  2026
    /// It should be equal to the primal value scaled by \ref dualScale
kpeter@590
  2027
    /// "dual scale".
kpeter@590
  2028
    ///
kpeter@590
  2029
    /// \pre Either run() or start() must be called before using this function.
deba@326
  2030
    Value dualValue() const {
deba@326
  2031
      Value sum = 0;
deba@326
  2032
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@326
  2033
        sum += nodeValue(n);
deba@326
  2034
      }
deba@326
  2035
      for (int i = 0; i < blossomNum(); ++i) {
deba@326
  2036
        sum += blossomValue(i) * (blossomSize(i) / 2);
deba@326
  2037
      }
deba@326
  2038
      return sum;
deba@326
  2039
    }
deba@326
  2040
kpeter@590
  2041
    /// \brief Return the dual value (potential) of the given node.
deba@326
  2042
    ///
kpeter@590
  2043
    /// This function returns the dual value (potential) of the given node.
kpeter@590
  2044
    ///
kpeter@590
  2045
    /// \pre Either run() or start() must be called before using this function.
deba@326
  2046
    Value nodeValue(const Node& n) const {
deba@326
  2047
      return (*_node_potential)[n];
deba@326
  2048
    }
deba@326
  2049
kpeter@590
  2050
    /// \brief Return the number of the blossoms in the basis.
deba@326
  2051
    ///
kpeter@590
  2052
    /// This function returns the number of the blossoms in the basis.
kpeter@590
  2053
    ///
kpeter@590
  2054
    /// \pre Either run() or start() must be called before using this function.
deba@326
  2055
    /// \see BlossomIt
deba@326
  2056
    int blossomNum() const {
deba@326
  2057
      return _blossom_potential.size();
deba@326
  2058
    }
deba@326
  2059
kpeter@590
  2060
    /// \brief Return the number of the nodes in the given blossom.
deba@326
  2061
    ///
kpeter@590
  2062
    /// This function returns the number of the nodes in the given blossom.
kpeter@590
  2063
    ///
kpeter@590
  2064
    /// \pre Either run() or start() must be called before using this function.
kpeter@590
  2065
    /// \see BlossomIt
deba@326
  2066
    int blossomSize(int k) const {
deba@326
  2067
      return _blossom_potential[k].end - _blossom_potential[k].begin;
deba@326
  2068
    }
deba@326
  2069
kpeter@590
  2070
    /// \brief Return the dual value (ptential) of the given blossom.
deba@326
  2071
    ///
kpeter@590
  2072
    /// This function returns the dual value (ptential) of the given blossom.
kpeter@590
  2073
    ///
kpeter@590
  2074
    /// \pre Either run() or start() must be called before using this function.
deba@326
  2075
    Value blossomValue(int k) const {
deba@326
  2076
      return _blossom_potential[k].value;
deba@326
  2077
    }
deba@326
  2078
kpeter@590
  2079
    /// \brief Iterator for obtaining the nodes of a blossom.
deba@326
  2080
    ///
deba@868
  2081
    /// This class provides an iterator for obtaining the nodes of the
kpeter@590
  2082
    /// given blossom. It lists a subset of the nodes.
deba@868
  2083
    /// Before using this iterator, you must allocate a
kpeter@590
  2084
    /// MaxWeightedMatching class and execute it.
deba@326
  2085
    class BlossomIt {
deba@326
  2086
    public:
deba@326
  2087
deba@326
  2088
      /// \brief Constructor.
deba@326
  2089
      ///
kpeter@590
  2090
      /// Constructor to get the nodes of the given variable.
kpeter@590
  2091
      ///
deba@868
  2092
      /// \pre Either \ref MaxWeightedMatching::run() "algorithm.run()" or
deba@868
  2093
      /// \ref MaxWeightedMatching::start() "algorithm.start()" must be
kpeter@590
  2094
      /// called before initializing this iterator.
deba@326
  2095
      BlossomIt(const MaxWeightedMatching& algorithm, int variable)
deba@326
  2096
        : _algorithm(&algorithm)
deba@326
  2097
      {
deba@326
  2098
        _index = _algorithm->_blossom_potential[variable].begin;
deba@326
  2099
        _last = _algorithm->_blossom_potential[variable].end;
deba@326
  2100
      }
deba@326
  2101
kpeter@590
  2102
      /// \brief Conversion to \c Node.
deba@326
  2103
      ///
kpeter@590
  2104
      /// Conversion to \c Node.
deba@326
  2105
      operator Node() const {
deba@327
  2106
        return _algorithm->_blossom_node_list[_index];
deba@326
  2107
      }
deba@326
  2108
deba@326
  2109
      /// \brief Increment operator.
deba@326
  2110
      ///
deba@326
  2111
      /// Increment operator.
deba@326
  2112
      BlossomIt& operator++() {
deba@326
  2113
        ++_index;
deba@326
  2114
        return *this;
deba@326
  2115
      }
deba@326
  2116
deba@327
  2117
      /// \brief Validity checking
deba@327
  2118
      ///
deba@327
  2119
      /// Checks whether the iterator is invalid.
deba@327
  2120
      bool operator==(Invalid) const { return _index == _last; }
deba@327
  2121
deba@327
  2122
      /// \brief Validity checking
deba@327
  2123
      ///
deba@327
  2124
      /// Checks whether the iterator is valid.
deba@327
  2125
      bool operator!=(Invalid) const { return _index != _last; }
deba@326
  2126
deba@326
  2127
    private:
deba@326
  2128
      const MaxWeightedMatching* _algorithm;
deba@326
  2129
      int _last;
deba@326
  2130
      int _index;
deba@326
  2131
    };
deba@326
  2132
deba@326
  2133
    /// @}
deba@326
  2134
deba@326
  2135
  };
deba@326
  2136
deba@326
  2137
  /// \ingroup matching
deba@326
  2138
  ///
deba@326
  2139
  /// \brief Weighted perfect matching in general graphs
deba@326
  2140
  ///
deba@326
  2141
  /// This class provides an efficient implementation of Edmond's
deba@327
  2142
  /// maximum weighted perfect matching algorithm. The implementation
deba@326
  2143
  /// is based on extensive use of priority queues and provides
kpeter@559
  2144
  /// \f$O(nm\log n)\f$ time complexity.
deba@326
  2145
  ///
deba@868
  2146
  /// The maximum weighted perfect matching problem is to find a subset of
deba@868
  2147
  /// the edges in an undirected graph with maximum overall weight for which
kpeter@590
  2148
  /// each node has exactly one incident edge.
kpeter@590
  2149
  /// It can be formulated with the following linear program.
deba@326
  2150
  /// \f[ \sum_{e \in \delta(u)}x_e = 1 \quad \forall u\in V\f]
deba@327
  2151
  /** \f[ \sum_{e \in \gamma(B)}x_e \le \frac{\vert B \vert - 1}{2}
deba@327
  2152
      \quad \forall B\in\mathcal{O}\f] */
deba@326
  2153
  /// \f[x_e \ge 0\quad \forall e\in E\f]
deba@326
  2154
  /// \f[\max \sum_{e\in E}x_ew_e\f]
deba@327
  2155
  /// where \f$\delta(X)\f$ is the set of edges incident to a node in
deba@327
  2156
  /// \f$X\f$, \f$\gamma(X)\f$ is the set of edges with both ends in
deba@327
  2157
  /// \f$X\f$ and \f$\mathcal{O}\f$ is the set of odd cardinality
deba@327
  2158
  /// subsets of the nodes.
deba@326
  2159
  ///
deba@326
  2160
  /// The algorithm calculates an optimal matching and a proof of the
deba@326
  2161
  /// optimality. The solution of the dual problem can be used to check
deba@327
  2162
  /// the result of the algorithm. The dual linear problem is the
kpeter@590
  2163
  /// following.
deba@327
  2164
  /** \f[ y_u + y_v + \sum_{B \in \mathcal{O}, uv \in \gamma(B)}z_B \ge
deba@327
  2165
      w_{uv} \quad \forall uv\in E\f] */
deba@326
  2166
  /// \f[z_B \ge 0 \quad \forall B \in \mathcal{O}\f]
deba@327
  2167
  /** \f[\min \sum_{u \in V}y_u + \sum_{B \in \mathcal{O}}
deba@327
  2168
      \frac{\vert B \vert - 1}{2}z_B\f] */
deba@326
  2169
  ///
deba@868
  2170
  /// The algorithm can be executed with the run() function.
kpeter@590
  2171
  /// After it the matching (the primal solution) and the dual solution
deba@868
  2172
  /// can be obtained using the query functions and the
deba@868
  2173
  /// \ref MaxWeightedPerfectMatching::BlossomIt "BlossomIt" nested class,
deba@868
  2174
  /// which is able to iterate on the nodes of a blossom.
kpeter@590
  2175
  /// If the value type is integer, then the dual solution is multiplied
kpeter@590
  2176
  /// by \ref MaxWeightedMatching::dualScale "4".
kpeter@590
  2177
  ///
kpeter@593
  2178
  /// \tparam GR The undirected graph type the algorithm runs on.
deba@868
  2179
  /// \tparam WM The type edge weight map. The default type is
kpeter@590
  2180
  /// \ref concepts::Graph::EdgeMap "GR::EdgeMap<int>".
kpeter@590
  2181
#ifdef DOXYGEN
kpeter@590
  2182
  template <typename GR, typename WM>
kpeter@590
  2183
#else
kpeter@559
  2184
  template <typename GR,
kpeter@559
  2185
            typename WM = typename GR::template EdgeMap<int> >
kpeter@590
  2186
#endif
deba@326
  2187
  class MaxWeightedPerfectMatching {
deba@326
  2188
  public:
deba@326
  2189
kpeter@590
  2190
    /// The graph type of the algorithm
kpeter@559
  2191
    typedef GR Graph;
kpeter@590
  2192
    /// The type of the edge weight map
kpeter@559
  2193
    typedef WM WeightMap;
kpeter@590
  2194
    /// The value type of the edge weights
deba@326
  2195
    typedef typename WeightMap::Value Value;
deba@326
  2196
deba@326
  2197
    /// \brief Scaling factor for dual solution
deba@326
  2198
    ///
deba@326
  2199
    /// Scaling factor for dual solution, it is equal to 4 or 1
deba@326
  2200
    /// according to the value type.
deba@326
  2201
    static const int dualScale =
deba@326
  2202
      std::numeric_limits<Value>::is_integer ? 4 : 1;
deba@326
  2203
kpeter@593
  2204
    /// The type of the matching map
deba@326
  2205
    typedef typename Graph::template NodeMap<typename Graph::Arc>
deba@326
  2206
    MatchingMap;
deba@326
  2207
deba@326
  2208
  private:
deba@326
  2209
deba@326
  2210
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@326
  2211
deba@326
  2212
    typedef typename Graph::template NodeMap<Value> NodePotential;
deba@326
  2213
    typedef std::vector<Node> BlossomNodeList;
deba@326
  2214
deba@326
  2215
    struct BlossomVariable {
deba@326
  2216
      int begin, end;
deba@326
  2217
      Value value;
deba@326
  2218
deba@326
  2219
      BlossomVariable(int _begin, int _end, Value _value)
deba@326
  2220
        : begin(_begin), end(_end), value(_value) {}
deba@326
  2221
deba@326
  2222
    };
deba@326
  2223
deba@326
  2224
    typedef std::vector<BlossomVariable> BlossomPotential;
deba@326
  2225
deba@326
  2226
    const Graph& _graph;
deba@326
  2227
    const WeightMap& _weight;
deba@326
  2228
deba@326
  2229
    MatchingMap* _matching;
deba@326
  2230
deba@326
  2231
    NodePotential* _node_potential;
deba@326
  2232
deba@326
  2233
    BlossomPotential _blossom_potential;
deba@326
  2234
    BlossomNodeList _blossom_node_list;
deba@326
  2235
deba@326
  2236
    int _node_num;
deba@326
  2237
    int _blossom_num;
deba@326
  2238
deba@326
  2239
    typedef RangeMap<int> IntIntMap;
deba@326
  2240
deba@326
  2241
    enum Status {
deba@326
  2242
      EVEN = -1, MATCHED = 0, ODD = 1
deba@326
  2243
    };
deba@326
  2244
deba@327
  2245
    typedef HeapUnionFind<Value, IntNodeMap> BlossomSet;
deba@326
  2246
    struct BlossomData {
deba@326
  2247
      int tree;
deba@326
  2248
      Status status;
deba@326
  2249
      Arc pred, next;
deba@326
  2250
      Value pot, offset;
deba@326
  2251
    };
deba@326
  2252
deba@327
  2253
    IntNodeMap *_blossom_index;
deba@326
  2254
    BlossomSet *_blossom_set;
deba@326
  2255
    RangeMap<BlossomData>* _blossom_data;
deba@326
  2256
deba@327
  2257
    IntNodeMap *_node_index;
deba@327
  2258
    IntArcMap *_node_heap_index;
deba@326
  2259
deba@326
  2260
    struct NodeData {
deba@326
  2261
deba@327
  2262
      NodeData(IntArcMap& node_heap_index)
deba@326
  2263
        : heap(node_heap_index) {}
deba@326
  2264
deba@326
  2265
      int blossom;
deba@326
  2266
      Value pot;
deba@327
  2267
      BinHeap<Value, IntArcMap> heap;
deba@326
  2268
      std::map<int, Arc> heap_index;
deba@326
  2269
deba@326
  2270
      int tree;
deba@326
  2271
    };
deba@326
  2272
deba@326
  2273
    RangeMap<NodeData>* _node_data;
deba@326
  2274
deba@326
  2275
    typedef ExtendFindEnum<IntIntMap> TreeSet;
deba@326
  2276
deba@326
  2277
    IntIntMap *_tree_set_index;
deba@326
  2278
    TreeSet *_tree_set;
deba@326
  2279
deba@326
  2280
    IntIntMap *_delta2_index;
deba@326
  2281
    BinHeap<Value, IntIntMap> *_delta2;
deba@326
  2282
deba@327
  2283
    IntEdgeMap *_delta3_index;
deba@327
  2284
    BinHeap<Value, IntEdgeMap> *_delta3;
deba@326
  2285
deba@326
  2286
    IntIntMap *_delta4_index;
deba@326
  2287
    BinHeap<Value, IntIntMap> *_delta4;
deba@326
  2288
deba@326
  2289
    Value _delta_sum;
deba@870
  2290
    int _unmatched;
deba@870
  2291
alpar@877
  2292
    typedef MaxWeightedPerfectFractionalMatching<Graph, WeightMap>
deba@870
  2293
    FractionalMatching;
deba@870
  2294
    FractionalMatching *_fractional;
deba@326
  2295
deba@326
  2296
    void createStructures() {
deba@326
  2297
      _node_num = countNodes(_graph);
deba@326
  2298
      _blossom_num = _node_num * 3 / 2;
deba@326
  2299
deba@326
  2300
      if (!_matching) {
deba@326
  2301
        _matching = new MatchingMap(_graph);
deba@326
  2302
      }
deba@867
  2303
deba@326
  2304
      if (!_node_potential) {
deba@326
  2305
        _node_potential = new NodePotential(_graph);
deba@326
  2306
      }
deba@867
  2307
deba@326
  2308
      if (!_blossom_set) {
deba@327
  2309
        _blossom_index = new IntNodeMap(_graph);
deba@326
  2310
        _blossom_set = new BlossomSet(*_blossom_index);
deba@326
  2311
        _blossom_data = new RangeMap<BlossomData>(_blossom_num);
deba@867
  2312
      } else if (_blossom_data->size() != _blossom_num) {
deba@867
  2313
        delete _blossom_data;
deba@867
  2314
        _blossom_data = new RangeMap<BlossomData>(_blossom_num);
deba@326
  2315
      }
deba@326
  2316
deba@326
  2317
      if (!_node_index) {
deba@327
  2318
        _node_index = new IntNodeMap(_graph);
deba@327
  2319
        _node_heap_index = new IntArcMap(_graph);
deba@326
  2320
        _node_data = new RangeMap<NodeData>(_node_num,
deba@327
  2321
                                            NodeData(*_node_heap_index));
deba@867
  2322
      } else if (_node_data->size() != _node_num) {
deba@867
  2323
        delete _node_data;
deba@867
  2324
        _node_data = new RangeMap<NodeData>(_node_num,
deba@867
  2325
                                            NodeData(*_node_heap_index));
deba@326
  2326
      }
deba@326
  2327
deba@326
  2328
      if (!_tree_set) {
deba@326
  2329
        _tree_set_index = new IntIntMap(_blossom_num);
deba@326
  2330
        _tree_set = new TreeSet(*_tree_set_index);
deba@867
  2331
      } else {
deba@867
  2332
        _tree_set_index->resize(_blossom_num);
deba@326
  2333
      }
deba@867
  2334
deba@326
  2335
      if (!_delta2) {
deba@326
  2336
        _delta2_index = new IntIntMap(_blossom_num);
deba@326
  2337
        _delta2 = new BinHeap<Value, IntIntMap>(*_delta2_index);
deba@867
  2338
      } else {
deba@867
  2339
        _delta2_index->resize(_blossom_num);
deba@326
  2340
      }
deba@867
  2341
deba@326
  2342
      if (!_delta3) {
deba@327
  2343
        _delta3_index = new IntEdgeMap(_graph);
deba@327
  2344
        _delta3 = new BinHeap<Value, IntEdgeMap>(*_delta3_index);
deba@326
  2345
      }
deba@867
  2346
deba@326
  2347
      if (!_delta4) {
deba@326
  2348
        _delta4_index = new IntIntMap(_blossom_num);
deba@326
  2349
        _delta4 = new BinHeap<Value, IntIntMap>(*_delta4_index);
deba@867
  2350
      } else {
deba@867
  2351
        _delta4_index->resize(_blossom_num);
deba@326
  2352
      }
deba@326
  2353
    }
deba@326
  2354
deba@326
  2355
    void destroyStructures() {
deba@326
  2356
      if (_matching) {
deba@326
  2357
        delete _matching;
deba@326
  2358
      }
deba@326
  2359
      if (_node_potential) {
deba@326
  2360
        delete _node_potential;
deba@326
  2361
      }
deba@326
  2362
      if (_blossom_set) {
deba@326
  2363
        delete _blossom_index;
deba@326
  2364
        delete _blossom_set;
deba@326
  2365
        delete _blossom_data;
deba@326
  2366
      }
deba@326
  2367
deba@326
  2368
      if (_node_index) {
deba@326
  2369
        delete _node_index;
deba@326
  2370
        delete _node_heap_index;
deba@326
  2371
        delete _node_data;
deba@326
  2372
      }
deba@326
  2373
deba@326
  2374
      if (_tree_set) {
deba@326
  2375
        delete _tree_set_index;
deba@326
  2376
        delete _tree_set;
deba@326
  2377
      }
deba@326
  2378
      if (_delta2) {
deba@326
  2379
        delete _delta2_index;
deba@326
  2380
        delete _delta2;
deba@326
  2381
      }
deba@326
  2382
      if (_delta3) {
deba@326
  2383
        delete _delta3_index;
deba@326
  2384
        delete _delta3;
deba@326
  2385
      }
deba@326
  2386
      if (_delta4) {
deba@326
  2387
        delete _delta4_index;
deba@326
  2388
        delete _delta4;
deba@326
  2389
      }
deba@326
  2390
    }
deba@326
  2391
deba@326
  2392
    void matchedToEven(int blossom, int tree) {
deba@326
  2393
      if (_delta2->state(blossom) == _delta2->IN_HEAP) {
deba@326
  2394
        _delta2->erase(blossom);
deba@326
  2395
      }
deba@326
  2396
deba@326
  2397
      if (!_blossom_set->trivial(blossom)) {
deba@326
  2398
        (*_blossom_data)[blossom].pot -=
deba@326
  2399
          2 * (_delta_sum - (*_blossom_data)[blossom].offset);
deba@326
  2400
      }
deba@326
  2401
deba@326
  2402
      for (typename BlossomSet::ItemIt n(*_blossom_set, blossom);
deba@326
  2403
           n != INVALID; ++n) {
deba@326
  2404
deba@326
  2405
        _blossom_set->increase(n, std::numeric_limits<Value>::max());
deba@326
  2406
        int ni = (*_node_index)[n];
deba@326
  2407
deba@326
  2408
        (*_node_data)[ni].heap.clear();
deba@326
  2409
        (*_node_data)[ni].heap_index.clear();
deba@326
  2410
deba@326
  2411
        (*_node_data)[ni].pot += _delta_sum - (*_blossom_data)[blossom].offset;
deba@326
  2412
deba@326
  2413
        for (InArcIt e(_graph, n); e != INVALID; ++e) {
deba@326
  2414
          Node v = _graph.source(e);
deba@326
  2415
          int vb = _blossom_set->find(v);
deba@326
  2416
          int vi = (*_node_index)[v];
deba@326
  2417
deba@326
  2418
          Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot -
deba@326
  2419
            dualScale * _weight[e];
deba@326
  2420
deba@326
  2421
          if ((*_blossom_data)[vb].status == EVEN) {
deba@326
  2422
            if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) {
deba@326
  2423
              _delta3->push(e, rw / 2);
deba@326
  2424
            }
deba@326
  2425
          } else {
deba@326
  2426
            typename std::map<int, Arc>::iterator it =
deba@326
  2427
              (*_node_data)[vi].heap_index.find(tree);
deba@326
  2428
deba@326
  2429
            if (it != (*_node_data)[vi].heap_index.end()) {
deba@326
  2430
              if ((*_node_data)[vi].heap[it->second] > rw) {
deba@326
  2431
                (*_node_data)[vi].heap.replace(it->second, e);
deba@326
  2432
                (*_node_data)[vi].heap.decrease(e, rw);
deba@326
  2433
                it->second = e;
deba@326
  2434
              }
deba@326
  2435
            } else {
deba@326
  2436
              (*_node_data)[vi].heap.push(e, rw);
deba@326
  2437
              (*_node_data)[vi].heap_index.insert(std::make_pair(tree, e));
deba@326
  2438
            }
deba@326
  2439
deba@326
  2440
            if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) {
deba@326
  2441
              _blossom_set->decrease(v, (*_node_data)[vi].heap.prio());
deba@326
  2442
deba@326
  2443
              if ((*_blossom_data)[vb].status == MATCHED) {
deba@326
  2444
                if (_delta2->state(vb) != _delta2->IN_HEAP) {
deba@326
  2445
                  _delta2->push(vb, _blossom_set->classPrio(vb) -
deba@326
  2446
                               (*_blossom_data)[vb].offset);
deba@326
  2447
                } else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) -
deba@326
  2448
                           (*_blossom_data)[vb].offset){
deba@326
  2449
                  _delta2->decrease(vb, _blossom_set->classPrio(vb) -
deba@326
  2450
                                   (*_blossom_data)[vb].offset);
deba@326
  2451
                }
deba@326
  2452
              }
deba@326
  2453
            }
deba@326
  2454
          }
deba@326
  2455
        }
deba@326
  2456
      }
deba@326
  2457
      (*_blossom_data)[blossom].offset = 0;
deba@326
  2458
    }
deba@326
  2459
deba@326
  2460
    void matchedToOdd(int blossom) {
deba@326
  2461
      if (_delta2->state(blossom) == _delta2->IN_HEAP) {
deba@326
  2462
        _delta2->erase(blossom);
deba@326
  2463
      }
deba@326
  2464
      (*_blossom_data)[blossom].offset += _delta_sum;
deba@326
  2465
      if (!_blossom_set->trivial(blossom)) {
deba@326
  2466
        _delta4->push(blossom, (*_blossom_data)[blossom].pot / 2 +
deba@326
  2467
                     (*_blossom_data)[blossom].offset);
deba@326
  2468
      }
deba@326
  2469
    }
deba@326
  2470
deba@326
  2471
    void evenToMatched(int blossom, int tree) {
deba@326
  2472
      if (!_blossom_set->trivial(blossom)) {
deba@326
  2473
        (*_blossom_data)[blossom].pot += 2 * _delta_sum;
deba@326
  2474
      }
deba@326
  2475
deba@326
  2476
      for (typename BlossomSet::ItemIt n(*_blossom_set, blossom);
deba@326
  2477
           n != INVALID; ++n) {
deba@326
  2478
        int ni = (*_node_index)[n];
deba@326
  2479
        (*_node_data)[ni].pot -= _delta_sum;
deba@326
  2480
deba@326
  2481
        for (InArcIt e(_graph, n); e != INVALID; ++e) {
deba@326
  2482
          Node v = _graph.source(e);
deba@326
  2483
          int vb = _blossom_set->find(v);
deba@326
  2484
          int vi = (*_node_index)[v];
deba@326
  2485
deba@326
  2486
          Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot -
deba@326
  2487
            dualScale * _weight[e];
deba@326
  2488
deba@326
  2489
          if (vb == blossom) {
deba@326
  2490
            if (_delta3->state(e) == _delta3->IN_HEAP) {
deba@326
  2491
              _delta3->erase(e);
deba@326
  2492
            }
deba@326
  2493
          } else if ((*_blossom_data)[vb].status == EVEN) {
deba@326
  2494
deba@326
  2495
            if (_delta3->state(e) == _delta3->IN_HEAP) {
deba@326
  2496
              _delta3->erase(e);
deba@326
  2497
            }
deba@326
  2498
deba@326
  2499
            int vt = _tree_set->find(vb);
deba@326
  2500
deba@326
  2501
            if (vt != tree) {
deba@326
  2502
deba@326
  2503
              Arc r = _graph.oppositeArc(e);
deba@326
  2504
deba@326
  2505
              typename std::map<int, Arc>::iterator it =
deba@326
  2506
                (*_node_data)[ni].heap_index.find(vt);
deba@326
  2507
deba@326
  2508
              if (it != (*_node_data)[ni].heap_index.end()) {
deba@326
  2509
                if ((*_node_data)[ni].heap[it->second] > rw) {
deba@326
  2510
                  (*_node_data)[ni].heap.replace(it->second, r);
deba@326
  2511
                  (*_node_data)[ni].heap.decrease(r, rw);
deba@326
  2512
                  it->second = r;
deba@326
  2513
                }
deba@326
  2514
              } else {
deba@326
  2515
                (*_node_data)[ni].heap.push(r, rw);
deba@326
  2516
                (*_node_data)[ni].heap_index.insert(std::make_pair(vt, r));
deba@326
  2517
              }
deba@326
  2518
deba@326
  2519
              if ((*_blossom_set)[n] > (*_node_data)[ni].heap.prio()) {
deba@326
  2520
                _blossom_set->decrease(n, (*_node_data)[ni].heap.prio());
deba@326
  2521
deba@326
  2522
                if (_delta2->state(blossom) != _delta2->IN_HEAP) {
deba@326
  2523
                  _delta2->push(blossom, _blossom_set->classPrio(blossom) -
deba@326
  2524
                               (*_blossom_data)[blossom].offset);
deba@326
  2525
                } else if ((*_delta2)[blossom] >
deba@326
  2526
                           _blossom_set->classPrio(blossom) -
deba@326
  2527
                           (*_blossom_data)[blossom].offset){
deba@326
  2528
                  _delta2->decrease(blossom, _blossom_set->classPrio(blossom) -
deba@326
  2529
                                   (*_blossom_data)[blossom].offset);
deba@326
  2530
                }
deba@326
  2531
              }
deba@326
  2532
            }
deba@326
  2533
          } else {
deba@326
  2534
deba@326
  2535
            typename std::map<int, Arc>::iterator it =
deba@326
  2536
              (*_node_data)[vi].heap_index.find(tree);
deba@326
  2537
deba@326
  2538
            if (it != (*_node_data)[vi].heap_index.end()) {
deba@326
  2539
              (*_node_data)[vi].heap.erase(it->second);
deba@326
  2540
              (*_node_data)[vi].heap_index.erase(it);
deba@326
  2541
              if ((*_node_data)[vi].heap.empty()) {
deba@326
  2542
                _blossom_set->increase(v, std::numeric_limits<Value>::max());
deba@326
  2543
              } else if ((*_blossom_set)[v] < (*_node_data)[vi].heap.prio()) {
deba@326
  2544
                _blossom_set->increase(v, (*_node_data)[vi].heap.prio());
deba@326
  2545
              }
deba@326
  2546
deba@326
  2547
              if ((*_blossom_data)[vb].status == MATCHED) {
deba@326
  2548
                if (_blossom_set->classPrio(vb) ==
deba@326
  2549
                    std::numeric_limits<Value>::max()) {
deba@326
  2550
                  _delta2->erase(vb);
deba@326
  2551
                } else if ((*_delta2)[vb] < _blossom_set->classPrio(vb) -
deba@326
  2552
                           (*_blossom_data)[vb].offset) {
deba@326
  2553
                  _delta2->increase(vb, _blossom_set->classPrio(vb) -
deba@326
  2554
                                   (*_blossom_data)[vb].offset);
deba@326
  2555
                }
deba@326
  2556
              }
deba@326
  2557
            }
deba@326
  2558
          }
deba@326
  2559
        }
deba@326
  2560
      }
deba@326
  2561
    }
deba@326
  2562
deba@326
  2563
    void oddToMatched(int blossom) {
deba@326
  2564
      (*_blossom_data)[blossom].offset -= _delta_sum;
deba@326
  2565
deba@326
  2566
      if (_blossom_set->classPrio(blossom) !=
deba@326
  2567
          std::numeric_limits<Value>::max()) {
deba@326
  2568
        _delta2->push(blossom, _blossom_set->classPrio(blossom) -
deba@326
  2569
                       (*_blossom_data)[blossom].offset);
deba@326
  2570
      }
deba@326
  2571
deba@326
  2572
      if (!_blossom_set->trivial(blossom)) {
deba@326
  2573
        _delta4->erase(blossom);
deba@326
  2574
      }
deba@326
  2575
    }
deba@326
  2576
deba@326
  2577
    void oddToEven(int blossom, int tree) {
deba@326
  2578
      if (!_blossom_set->trivial(blossom)) {
deba@326
  2579
        _delta4->erase(blossom);
deba@326
  2580
        (*_blossom_data)[blossom].pot -=
deba@326
  2581
          2 * (2 * _delta_sum - (*_blossom_data)[blossom].offset);
deba@326
  2582
      }
deba@326
  2583
deba@326
  2584
      for (typename BlossomSet::ItemIt n(*_blossom_set, blossom);
deba@326
  2585
           n != INVALID; ++n) {
deba@326
  2586
        int ni = (*_node_index)[n];
deba@326
  2587
deba@326
  2588
        _blossom_set->increase(n, std::numeric_limits<Value>::max());
deba@326
  2589
deba@326
  2590
        (*_node_data)[ni].heap.clear();
deba@326
  2591
        (*_node_data)[ni].heap_index.clear();
deba@326
  2592
        (*_node_data)[ni].pot +=
deba@326
  2593
          2 * _delta_sum - (*_blossom_data)[blossom].offset;
deba@326
  2594
deba@326
  2595
        for (InArcIt e(_graph, n); e != INVALID; ++e) {
deba@326
  2596
          Node v = _graph.source(e);
deba@326
  2597
          int vb = _blossom_set->find(v);
deba@326
  2598
          int vi = (*_node_index)[v];
deba@326
  2599
deba@326
  2600
          Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot -
deba@326
  2601
            dualScale * _weight[e];
deba@326
  2602
deba@326
  2603
          if ((*_blossom_data)[vb].status == EVEN) {
deba@326
  2604
            if (_delta3->state(e) != _delta3->IN_HEAP && blossom != vb) {
deba@326
  2605
              _delta3->push(e, rw / 2);
deba@326
  2606
            }
deba@326
  2607
          } else {
deba@326
  2608
deba@326
  2609
            typename std::map<int, Arc>::iterator it =
deba@326
  2610
              (*_node_data)[vi].heap_index.find(tree);
deba@326
  2611
deba@326
  2612
            if (it != (*_node_data)[vi].heap_index.end()) {
deba@326
  2613
              if ((*_node_data)[vi].heap[it->second] > rw) {
deba@326
  2614
                (*_node_data)[vi].heap.replace(it->second, e);
deba@326
  2615
                (*_node_data)[vi].heap.decrease(e, rw);
deba@326
  2616
                it->second = e;
deba@326
  2617
              }
deba@326
  2618
            } else {
deba@326
  2619
              (*_node_data)[vi].heap.push(e, rw);
deba@326
  2620
              (*_node_data)[vi].heap_index.insert(std::make_pair(tree, e));
deba@326
  2621
            }
deba@326
  2622
deba@326
  2623
            if ((*_blossom_set)[v] > (*_node_data)[vi].heap.prio()) {
deba@326
  2624
              _blossom_set->decrease(v, (*_node_data)[vi].heap.prio());
deba@326
  2625
deba@326
  2626
              if ((*_blossom_data)[vb].status == MATCHED) {
deba@326
  2627
                if (_delta2->state(vb) != _delta2->IN_HEAP) {
deba@326
  2628
                  _delta2->push(vb, _blossom_set->classPrio(vb) -
deba@326
  2629
                               (*_blossom_data)[vb].offset);
deba@326
  2630
                } else if ((*_delta2)[vb] > _blossom_set->classPrio(vb) -
deba@326
  2631
                           (*_blossom_data)[vb].offset) {
deba@326
  2632
                  _delta2->decrease(vb, _blossom_set->classPrio(vb) -
deba@326
  2633
                                   (*_blossom_data)[vb].offset);
deba@326
  2634
                }
deba@326
  2635
              }
deba@326
  2636
            }
deba@326
  2637
          }
deba@326
  2638
        }
deba@326
  2639
      }
deba@326
  2640
      (*_blossom_data)[blossom].offset = 0;
deba@326
  2641
    }
deba@326
  2642
deba@326
  2643
    void alternatePath(int even, int tree) {
deba@326
  2644
      int odd;
deba@326
  2645
deba@326
  2646
      evenToMatched(even, tree);
deba@326
  2647
      (*_blossom_data)[even].status = MATCHED;
deba@326
  2648
deba@326
  2649
      while ((*_blossom_data)[even].pred != INVALID) {
deba@326
  2650
        odd = _blossom_set->find(_graph.target((*_blossom_data)[even].pred));
deba@326
  2651
        (*_blossom_data)[odd].status = MATCHED;
deba@326
  2652
        oddToMatched(odd);
deba@326
  2653
        (*_blossom_data)[odd].next = (*_blossom_data)[odd].pred;
deba@326
  2654
deba@326
  2655
        even = _blossom_set->find(_graph.target((*_blossom_data)[odd].pred));
deba@326
  2656
        (*_blossom_data)[even].status = MATCHED;
deba@326
  2657
        evenToMatched(even, tree);
deba@326
  2658
        (*_blossom_data)[even].next =
deba@326
  2659
          _graph.oppositeArc((*_blossom_data)[odd].pred);
deba@326
  2660
      }
deba@326
  2661
deba@326
  2662
    }
deba@326
  2663
deba@326
  2664
    void destroyTree(int tree) {
deba@326
  2665
      for (TreeSet::ItemIt b(*_tree_set, tree); b != INVALID; ++b) {
deba@326
  2666
        if ((*_blossom_data)[b].status == EVEN) {
deba@326
  2667
          (*_blossom_data)[b].status = MATCHED;
deba@326
  2668
          evenToMatched(b, tree);
deba@326
  2669
        } else if ((*_blossom_data)[b].status == ODD) {
deba@326
  2670
          (*_blossom_data)[b].status = MATCHED;
deba@326
  2671
          oddToMatched(b);
deba@326
  2672
        }
deba@326
  2673
      }
deba@326
  2674
      _tree_set->eraseClass(tree);
deba@326
  2675
    }
deba@326
  2676
deba@327
  2677
    void augmentOnEdge(const Edge& edge) {
deba@327
  2678
deba@327
  2679
      int left = _blossom_set->find(_graph.u(edge));
deba@327
  2680
      int right = _blossom_set->find(_graph.v(edge));
deba@326
  2681
deba@326
  2682
      int left_tree = _tree_set->find(left);
deba@326
  2683
      alternatePath(left, left_tree);
deba@326
  2684
      destroyTree(left_tree);
deba@326
  2685
deba@326
  2686
      int right_tree = _tree_set->find(right);
deba@326
  2687
      alternatePath(right, right_tree);
deba@326
  2688
      destroyTree(right_tree);
deba@326
  2689
deba@327
  2690
      (*_blossom_data)[left].next = _graph.direct(edge, true);
deba@327
  2691
      (*_blossom_data)[right].next = _graph.direct(edge, false);
deba@326
  2692
    }
deba@326
  2693
deba@326
  2694
    void extendOnArc(const Arc& arc) {
deba@326
  2695
      int base = _blossom_set->find(_graph.target(arc));
deba@326
  2696
      int tree = _tree_set->find(base);
deba@326
  2697
deba@326
  2698
      int odd = _blossom_set->find(_graph.source(arc));
deba@326
  2699
      _tree_set->insert(odd, tree);
deba@326
  2700
      (*_blossom_data)[odd].status = ODD;
deba@326
  2701
      matchedToOdd(odd);
deba@326
  2702
      (*_blossom_data)[odd].pred = arc;
deba@326
  2703
deba@326
  2704
      int even = _blossom_set->find(_graph.target((*_blossom_data)[odd].next));
deba@326
  2705
      (*_blossom_data)[even].pred = (*_blossom_data)[even].next;
deba@326
  2706
      _tree_set->insert(even, tree);
deba@326
  2707
      (*_blossom_data)[even].status = EVEN;
deba@326
  2708
      matchedToEven(even, tree);
deba@326
  2709
    }
deba@326
  2710
deba@327
  2711
    void shrinkOnEdge(const Edge& edge, int tree) {
deba@326
  2712
      int nca = -1;
deba@326
  2713
      std::vector<int> left_path, right_path;
deba@326
  2714
deba@326
  2715
      {
deba@326
  2716
        std::set<int> left_set, right_set;
deba@326
  2717
        int left = _blossom_set->find(_graph.u(edge));
deba@326
  2718
        left_path.push_back(left);
deba@326
  2719
        left_set.insert(left);
deba@326
  2720
deba@326
  2721
        int right = _blossom_set->find(_graph.v(edge));
deba@326
  2722
        right_path.push_back(right);
deba@326
  2723
        right_set.insert(right);
deba@326
  2724
deba@326
  2725
        while (true) {
deba@326
  2726
deba@326
  2727
          if ((*_blossom_data)[left].pred == INVALID) break;
deba@326
  2728
deba@326
  2729
          left =
deba@326
  2730
            _blossom_set->find(_graph.target((*_blossom_data)[left].pred));
deba@326
  2731
          left_path.push_back(left);
deba@326
  2732
          left =
deba@326
  2733
            _blossom_set->find(_graph.target((*_blossom_data)[left].pred));
deba@326
  2734
          left_path.push_back(left);
deba@326
  2735
deba@326
  2736
          left_set.insert(left);
deba@326
  2737
deba@326
  2738
          if (right_set.find(left) != right_set.end()) {
deba@326
  2739
            nca = left;
deba@326
  2740
            break;
deba@326
  2741
          }
deba@326
  2742
deba@326
  2743
          if ((*_blossom_data)[right].pred == INVALID) break;
deba@326
  2744
deba@326
  2745
          right =
deba@326
  2746
            _blossom_set->find(_graph.target((*_blossom_data)[right].pred));
deba@326
  2747
          right_path.push_back(right);
deba@326
  2748
          right =
deba@326
  2749
            _blossom_set->find(_graph.target((*_blossom_data)[right].pred));
deba@326
  2750
          right_path.push_back(right);
deba@326
  2751
deba@326
  2752
          right_set.insert(right);
deba@326
  2753
deba@326
  2754
          if (left_set.find(right) != left_set.end()) {
deba@326
  2755
            nca = right;
deba@326
  2756
            break;
deba@326
  2757
          }
deba@326
  2758
deba@326
  2759
        }
deba@326
  2760
deba@326
  2761
        if (nca == -1) {
deba@326
  2762
          if ((*_blossom_data)[left].pred == INVALID) {
deba@326
  2763
            nca = right;
deba@326
  2764
            while (left_set.find(nca) == left_set.end()) {
deba@326
  2765
              nca =
deba@326
  2766
                _blossom_set->find(_graph.target((*_blossom_data)[nca].pred));
deba@326
  2767
              right_path.push_back(nca);
deba@326
  2768
              nca =
deba@326
  2769
                _blossom_set->find(_graph.target((*_blossom_data)[nca].pred));
deba@326
  2770
              right_path.push_back(nca);
deba@326
  2771
            }
deba@326
  2772
          } else {
deba@326
  2773
            nca = left;
deba@326
  2774
            while (right_set.find(nca) == right_set.end()) {
deba@326
  2775
              nca =
deba@326
  2776
                _blossom_set->find(_graph.target((*_blossom_data)[nca].pred));
deba@326
  2777
              left_path.push_back(nca);
deba@326
  2778
              nca =
deba@326
  2779
                _blossom_set->find(_graph.target((*_blossom_data)[nca].pred));
deba@326
  2780
              left_path.push_back(nca);
deba@326
  2781
            }
deba@326
  2782
          }
deba@326
  2783
        }
deba@326
  2784
      }
deba@326
  2785
deba@326
  2786
      std::vector<int> subblossoms;
deba@326
  2787
      Arc prev;
deba@326
  2788
deba@326
  2789
      prev = _graph.direct(edge, true);
deba@326
  2790
      for (int i = 0; left_path[i] != nca; i += 2) {
deba@326
  2791
        subblossoms.push_back(left_path[i]);
deba@326
  2792
        (*_blossom_data)[left_path[i]].next = prev;
deba@326
  2793
        _tree_set->erase(left_path[i]);
deba@326
  2794
deba@326
  2795
        subblossoms.push_back(left_path[i + 1]);
deba@326
  2796
        (*_blossom_data)[left_path[i + 1]].status = EVEN;
deba@326
  2797
        oddToEven(left_path[i + 1], tree);
deba@326
  2798
        _tree_set->erase(left_path[i + 1]);
deba@326
  2799
        prev = _graph.oppositeArc((*_blossom_data)[left_path[i + 1]].pred);
deba@326
  2800
      }
deba@326
  2801
deba@326
  2802
      int k = 0;
deba@326
  2803
      while (right_path[k] != nca) ++k;
deba@326
  2804
deba@326
  2805
      subblossoms.push_back(nca);
deba@326
  2806
      (*_blossom_data)[nca].next = prev;
deba@326
  2807
deba@326
  2808
      for (int i = k - 2; i >= 0; i -= 2) {
deba@326
  2809
        subblossoms.push_back(right_path[i + 1]);
deba@326
  2810
        (*_blossom_data)[right_path[i + 1]].status = EVEN;
deba@326
  2811
        oddToEven(right_path[i + 1], tree);
deba@326
  2812
        _tree_set->erase(right_path[i + 1]);
deba@326
  2813
deba@326
  2814
        (*_blossom_data)[right_path[i + 1]].next =
deba@326
  2815
          (*_blossom_data)[right_path[i + 1]].pred;
deba@326
  2816
deba@326
  2817
        subblossoms.push_back(right_path[i]);
deba@326
  2818
        _tree_set->erase(right_path[i]);
deba@326
  2819
      }
deba@326
  2820
deba@326
  2821
      int surface =
deba@326
  2822
        _blossom_set->join(subblossoms.begin(), subblossoms.end());
deba@326
  2823
deba@326
  2824
      for (int i = 0; i < int(subblossoms.size()); ++i) {
deba@326
  2825
        if (!_blossom_set->trivial(subblossoms[i])) {
deba@326
  2826
          (*_blossom_data)[subblossoms[i]].pot += 2 * _delta_sum;
deba@326
  2827
        }
deba@326
  2828
        (*_blossom_data)[subblossoms[i]].status = MATCHED;
deba@326
  2829
      }
deba@326
  2830
deba@326
  2831
      (*_blossom_data)[surface].pot = -2 * _delta_sum;
deba@326
  2832
      (*_blossom_data)[surface].offset = 0;
deba@326
  2833
      (*_blossom_data)[surface].status = EVEN;
deba@326
  2834
      (*_blossom_data)[surface].pred = (*_blossom_data)[nca].pred;
deba@326
  2835
      (*_blossom_data)[surface].next = (*_blossom_data)[nca].pred;
deba@326
  2836
deba@326
  2837
      _tree_set->insert(surface, tree);
deba@326
  2838
      _tree_set->erase(nca);
deba@326
  2839
    }
deba@326
  2840
deba@326
  2841
    void splitBlossom(int blossom) {
deba@326
  2842
      Arc next = (*_blossom_data)[blossom].next;
deba@326
  2843
      Arc pred = (*_blossom_data)[blossom].pred;
deba@326
  2844
deba@326
  2845
      int tree = _tree_set->find(blossom);
deba@326
  2846
deba@326
  2847
      (*_blossom_data)[blossom].status = MATCHED;
deba@326
  2848
      oddToMatched(blossom);
deba@326
  2849
      if (_delta2->state(blossom) == _delta2->IN_HEAP) {
deba@326
  2850
        _delta2->erase(blossom);
deba@326
  2851
      }
deba@326
  2852
deba@326
  2853
      std::vector<int> subblossoms;
deba@326
  2854
      _blossom_set->split(blossom, std::back_inserter(subblossoms));
deba@326
  2855
deba@326
  2856
      Value offset = (*_blossom_data)[blossom].offset;
deba@326
  2857
      int b = _blossom_set->find(_graph.source(pred));
deba@326
  2858
      int d = _blossom_set->find(_graph.source(next));
deba@326
  2859
deba@326
  2860
      int ib = -1, id = -1;
deba@326
  2861
      for (int i = 0; i < int(subblossoms.size()); ++i) {
deba@326
  2862
        if (subblossoms[i] == b) ib = i;
deba@326
  2863
        if (subblossoms[i] == d) id = i;
deba@326
  2864
deba@326
  2865
        (*_blossom_data)[subblossoms[i]].offset = offset;
deba@326
  2866
        if (!_blossom_set->trivial(subblossoms[i])) {
deba@326
  2867
          (*_blossom_data)[subblossoms[i]].pot -= 2 * offset;
deba@326
  2868
        }
deba@326
  2869
        if (_blossom_set->classPrio(subblossoms[i]) !=
deba@326
  2870
            std::numeric_limits<Value>::max()) {
deba@326
  2871
          _delta2->push(subblossoms[i],
deba@326
  2872
                        _blossom_set->classPrio(subblossoms[i]) -
deba@326
  2873
                        (*_blossom_data)[subblossoms[i]].offset);
deba@326
  2874
        }
deba@326
  2875
      }
deba@326
  2876
deba@326
  2877
      if (id > ib ? ((id - ib) % 2 == 0) : ((ib - id) % 2 == 1)) {
deba@326
  2878
        for (int i = (id + 1) % subblossoms.size();
deba@326
  2879
             i != ib; i = (i + 2) % subblossoms.size()) {
deba@326
  2880
          int sb = subblossoms[i];
deba@326
  2881
          int tb = subblossoms[(i + 1) % subblossoms.size()];
deba@326
  2882
          (*_blossom_data)[sb].next =
deba@326
  2883
            _graph.oppositeArc((*_blossom_data)[tb].next);
deba@326
  2884
        }
deba@326
  2885
deba@326
  2886
        for (int i = ib; i != id; i = (i + 2) % subblossoms.size()) {
deba@326
  2887
          int sb = subblossoms[i];
deba@326
  2888
          int tb = subblossoms[(i + 1) % subblossoms.size()];
deba@326
  2889
          int ub = subblossoms[(i + 2) % subblossoms.size()];
deba@326
  2890
deba@326
  2891
          (*_blossom_data)[sb].status = ODD;
deba@326
  2892
          matchedToOdd(sb);
deba@326
  2893
          _tree_set->insert(sb, tree);
deba@326
  2894
          (*_blossom_data)[sb].pred = pred;
deba@326
  2895
          (*_blossom_data)[sb].next =
deba@326
  2896
                           _graph.oppositeArc((*_blossom_data)[tb].next);
deba@326
  2897
deba@326
  2898
          pred = (*_blossom_data)[ub].next;
deba@326
  2899
deba@326
  2900
          (*_blossom_data)[tb].status = EVEN;
deba@326
  2901
          matchedToEven(tb, tree);
deba@326
  2902
          _tree_set->insert(tb, tree);
deba@326
  2903
          (*_blossom_data)[tb].pred = (*_blossom_data)[tb].next;
deba@326
  2904
        }
deba@326
  2905
deba@326
  2906
        (*_blossom_data)[subblossoms[id]].status = ODD;
deba@326
  2907
        matchedToOdd(subblossoms[id]);
deba@326
  2908
        _tree_set->insert(subblossoms[id], tree);
deba@326
  2909
        (*_blossom_data)[subblossoms[id]].next = next;
deba@326
  2910
        (*_blossom_data)[subblossoms[id]].pred = pred;
deba@326
  2911
deba@326
  2912
      } else {
deba@326
  2913
deba@326
  2914
        for (int i = (ib + 1) % subblossoms.size();
deba@326
  2915
             i != id; i = (i + 2) % subblossoms.size()) {
deba@326
  2916
          int sb = subblossoms[i];
deba@326
  2917
          int tb = subblossoms[(i + 1) % subblossoms.size()];
deba@326
  2918
          (*_blossom_data)[sb].next =
deba@326
  2919
            _graph.oppositeArc((*_blossom_data)[tb].next);
deba@326
  2920
        }
deba@326
  2921
deba@326
  2922
        for (int i = id; i != ib; i = (i + 2) % subblossoms.size()) {
deba@326
  2923
          int sb = subblossoms[i];
deba@326
  2924
          int tb = subblossoms[(i + 1) % subblossoms.size()];
deba@326
  2925
          int ub = subblossoms[(i + 2) % subblossoms.size()];
deba@326
  2926
deba@326
  2927
          (*_blossom_data)[sb].status = ODD;
deba@326
  2928
          matchedToOdd(sb);
deba@326
  2929
          _tree_set->insert(sb, tree);
deba@326
  2930
          (*_blossom_data)[sb].next = next;
deba@326
  2931
          (*_blossom_data)[sb].pred =
deba@326
  2932
            _graph.oppositeArc((*_blossom_data)[tb].next);
deba@326
  2933
deba@326
  2934
          (*_blossom_data)[tb].status = EVEN;
deba@326
  2935
          matchedToEven(tb, tree);
deba@326
  2936
          _tree_set->insert(tb, tree);
deba@326
  2937
          (*_blossom_data)[tb].pred =
deba@326
  2938
            (*_blossom_data)[tb].next =
deba@326
  2939
            _graph.oppositeArc((*_blossom_data)[ub].next);
deba@326
  2940
          next = (*_blossom_data)[ub].next;
deba@326
  2941
        }
deba@326
  2942
deba@326
  2943
        (*_blossom_data)[subblossoms[ib]].status = ODD;
deba@326
  2944
        matchedToOdd(subblossoms[ib]);
deba@326
  2945
        _tree_set->insert(subblossoms[ib], tree);
deba@326
  2946
        (*_blossom_data)[subblossoms[ib]].next = next;
deba@326
  2947
        (*_blossom_data)[subblossoms[ib]].pred = pred;
deba@326
  2948
      }
deba@326
  2949
      _tree_set->erase(blossom);
deba@326
  2950
    }
deba@326
  2951
deba@326
  2952
    void extractBlossom(int blossom, const Node& base, const Arc& matching) {
deba@326
  2953
      if (_blossom_set->trivial(blossom)) {
deba@326
  2954
        int bi = (*_node_index)[base];
deba@326
  2955
        Value pot = (*_node_data)[bi].pot;
deba@326
  2956
kpeter@581
  2957
        (*_matching)[base] = matching;
deba@326
  2958
        _blossom_node_list.push_back(base);
kpeter@581
  2959
        (*_node_potential)[base] = pot;
deba@326
  2960
      } else {
deba@326
  2961
deba@326
  2962
        Value pot = (*_blossom_data)[blossom].pot;
deba@326
  2963
        int bn = _blossom_node_list.size();
deba@326
  2964
deba@326
  2965
        std::vector<int> subblossoms;
deba@326
  2966
        _blossom_set->split(blossom, std::back_inserter(subblossoms));
deba@326
  2967
        int b = _blossom_set->find(base);
deba@326
  2968
        int ib = -1;
deba@326
  2969
        for (int i = 0; i < int(subblossoms.size()); ++i) {
deba@326
  2970
          if (subblossoms[i] == b) { ib = i; break; }
deba@326
  2971
        }
deba@326
  2972
deba@326
  2973
        for (int i = 1; i < int(subblossoms.size()); i += 2) {
deba@326
  2974
          int sb = subblossoms[(ib + i) % subblossoms.size()];
deba@326
  2975
          int tb = subblossoms[(ib + i + 1) % subblossoms.size()];
deba@326
  2976
deba@326
  2977
          Arc m = (*_blossom_data)[tb].next;
deba@326
  2978
          extractBlossom(sb, _graph.target(m), _graph.oppositeArc(m));
deba@326
  2979
          extractBlossom(tb, _graph.source(m), m);
deba@326
  2980
        }
deba@326
  2981
        extractBlossom(subblossoms[ib], base, matching);
deba@326
  2982
deba@326
  2983
        int en = _blossom_node_list.size();
deba@326
  2984
deba@326
  2985
        _blossom_potential.push_back(BlossomVariable(bn, en, pot));
deba@326
  2986
      }
deba@326
  2987
    }
deba@326
  2988
deba@326
  2989
    void extractMatching() {
deba@326
  2990
      std::vector<int> blossoms;
deba@326
  2991
      for (typename BlossomSet::ClassIt c(*_blossom_set); c != INVALID; ++c) {
deba@326
  2992
        blossoms.push_back(c);
deba@326
  2993
      }
deba@326
  2994
deba@326
  2995
      for (int i = 0; i < int(blossoms.size()); ++i) {
deba@326
  2996
deba@326
  2997
        Value offset = (*_blossom_data)[blossoms[i]].offset;
deba@326
  2998
        (*_blossom_data)[blossoms[i]].pot += 2 * offset;
deba@326
  2999
        for (typename BlossomSet::ItemIt n(*_blossom_set, blossoms[i]);
deba@326
  3000
             n != INVALID; ++n) {
deba@326
  3001
          (*_node_data)[(*_node_index)[n]].pot -= offset;
deba@326
  3002
        }
deba@326
  3003
deba@326
  3004
        Arc matching = (*_blossom_data)[blossoms[i]].next;
deba@326
  3005
        Node base = _graph.source(matching);
deba@326
  3006
        extractBlossom(blossoms[i], base, matching);
deba@326
  3007
      }
deba@326
  3008
    }
deba@326
  3009
deba@326
  3010
  public:
deba@326
  3011
deba@326
  3012
    /// \brief Constructor
deba@326
  3013
    ///
deba@326
  3014
    /// Constructor.
deba@326
  3015
    MaxWeightedPerfectMatching(const Graph& graph, const WeightMap& weight)
deba@326
  3016
      : _graph(graph), _weight(weight), _matching(0),
deba@326
  3017
        _node_potential(0), _blossom_potential(), _blossom_node_list(),
deba@326
  3018
        _node_num(0), _blossom_num(0),
deba@326
  3019
deba@326
  3020
        _blossom_index(0), _blossom_set(0), _blossom_data(0),
deba@326
  3021
        _node_index(0), _node_heap_index(0), _node_data(0),
deba@326
  3022
        _tree_set_index(0), _tree_set(0),
deba@326
  3023
deba@326
  3024
        _delta2_index(0), _delta2(0),
deba@326
  3025
        _delta3_index(0), _delta3(0),
deba@326
  3026
        _delta4_index(0), _delta4(0),
deba@326
  3027
deba@870
  3028
        _delta_sum(), _unmatched(0),
deba@870
  3029
deba@870
  3030
        _fractional(0)
deba@870
  3031
    {}
deba@326
  3032
deba@326
  3033
    ~MaxWeightedPerfectMatching() {
deba@326
  3034
      destroyStructures();
deba@870
  3035
      if (_fractional) {
deba@870
  3036
        delete _fractional;
deba@870
  3037
      }
deba@326
  3038
    }
deba@326
  3039
kpeter@590
  3040
    /// \name Execution Control
alpar@330
  3041
    /// The simplest way to execute the algorithm is to use the
kpeter@590
  3042
    /// \ref run() member function.
deba@326
  3043
deba@326
  3044
    ///@{
deba@326
  3045
deba@326
  3046
    /// \brief Initialize the algorithm
deba@326
  3047
    ///
kpeter@590
  3048
    /// This function initializes the algorithm.
deba@326
  3049
    void init() {
deba@326
  3050
      createStructures();
deba@326
  3051
deba@867
  3052
      _blossom_node_list.clear();
deba@867
  3053
      _blossom_potential.clear();
deba@867
  3054
deba@326
  3055
      for (ArcIt e(_graph); e != INVALID; ++e) {
kpeter@581
  3056
        (*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP;
deba@326
  3057
      }
deba@326
  3058
      for (EdgeIt e(_graph); e != INVALID; ++e) {
kpeter@581
  3059
        (*_delta3_index)[e] = _delta3->PRE_HEAP;
deba@326
  3060
      }
deba@326
  3061
      for (int i = 0; i < _blossom_num; ++i) {
kpeter@581
  3062
        (*_delta2_index)[i] = _delta2->PRE_HEAP;
kpeter@581
  3063
        (*_delta4_index)[i] = _delta4->PRE_HEAP;
deba@326
  3064
      }
deba@326
  3065
deba@870
  3066
      _unmatched = _node_num;
deba@870
  3067
deba@867
  3068
      _delta2->clear();
deba@867
  3069
      _delta3->clear();
deba@867
  3070
      _delta4->clear();
deba@867
  3071
      _blossom_set->clear();
deba@867
  3072
      _tree_set->clear();
deba@867
  3073
deba@326
  3074
      int index = 0;
deba@326
  3075
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@326
  3076
        Value max = - std::numeric_limits<Value>::max();
deba@326
  3077
        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
deba@326
  3078
          if (_graph.target(e) == n) continue;
deba@326
  3079
          if ((dualScale * _weight[e]) / 2 > max) {
deba@326
  3080
            max = (dualScale * _weight[e]) / 2;
deba@326
  3081
          }
deba@326
  3082
        }
kpeter@581
  3083
        (*_node_index)[n] = index;
deba@867
  3084
        (*_node_data)[index].heap_index.clear();
deba@867
  3085
        (*_node_data)[index].heap.clear();
deba@326
  3086
        (*_node_data)[index].pot = max;
deba@326
  3087
        int blossom =
deba@326
  3088
          _blossom_set->insert(n, std::numeric_limits<Value>::max());
deba@326
  3089
deba@326
  3090
        _tree_set->insert(blossom);
deba@326
  3091
deba@326
  3092
        (*_blossom_data)[blossom].status = EVEN;
deba@326
  3093
        (*_blossom_data)[blossom].pred = INVALID;
deba@326
  3094
        (*_blossom_data)[blossom].next = INVALID;
deba@326
  3095
        (*_blossom_data)[blossom].pot = 0;
deba@326
  3096
        (*_blossom_data)[blossom].offset = 0;
deba@326
  3097
        ++index;
deba@326
  3098
      }
deba@326
  3099
      for (EdgeIt e(_graph); e != INVALID; ++e) {
deba@326
  3100
        int si = (*_node_index)[_graph.u(e)];
deba@326
  3101
        int ti = (*_node_index)[_graph.v(e)];
deba@326
  3102
        if (_graph.u(e) != _graph.v(e)) {
deba@326
  3103
          _delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot -
deba@326
  3104
                            dualScale * _weight[e]) / 2);
deba@326
  3105
        }
deba@326
  3106
      }
deba@326
  3107
    }
deba@326
  3108
deba@870
  3109
    /// \brief Initialize the algorithm with fractional matching
deba@870
  3110
    ///
deba@870
  3111
    /// This function initializes the algorithm with a fractional
deba@870
  3112
    /// matching. This initialization is also called jumpstart heuristic.
deba@870
  3113
    void fractionalInit() {
deba@870
  3114
      createStructures();
deba@876
  3115
deba@876
  3116
      _blossom_node_list.clear();
deba@876
  3117
      _blossom_potential.clear();
alpar@877
  3118
deba@870
  3119
      if (_fractional == 0) {
deba@870
  3120
        _fractional = new FractionalMatching(_graph, _weight, false);
deba@870
  3121
      }
deba@870
  3122
      if (!_fractional->run()) {
deba@870
  3123
        _unmatched = -1;
deba@870
  3124
        return;
deba@870
  3125
      }
deba@870
  3126
deba@870
  3127
      for (ArcIt e(_graph); e != INVALID; ++e) {
deba@870
  3128
        (*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP;
deba@870
  3129
      }
deba@870
  3130
      for (EdgeIt e(_graph); e != INVALID; ++e) {
deba@870
  3131
        (*_delta3_index)[e] = _delta3->PRE_HEAP;
deba@870
  3132
      }
deba@870
  3133
      for (int i = 0; i < _blossom_num; ++i) {
deba@870
  3134
        (*_delta2_index)[i] = _delta2->PRE_HEAP;
deba@870
  3135
        (*_delta4_index)[i] = _delta4->PRE_HEAP;
deba@870
  3136
      }
deba@870
  3137
deba@870
  3138
      _unmatched = 0;
deba@870
  3139
deba@876
  3140
      _delta2->clear();
deba@876
  3141
      _delta3->clear();
deba@876
  3142
      _delta4->clear();
deba@876
  3143
      _blossom_set->clear();
deba@876
  3144
      _tree_set->clear();
deba@876
  3145
deba@870
  3146
      int index = 0;
deba@870
  3147
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@870
  3148
        Value pot = _fractional->nodeValue(n);
deba@870
  3149
        (*_node_index)[n] = index;
deba@870
  3150
        (*_node_data)[index].pot = pot;
deba@876
  3151
        (*_node_data)[index].heap_index.clear();
deba@876
  3152
        (*_node_data)[index].heap.clear();
deba@870
  3153
        int blossom =
deba@870
  3154
          _blossom_set->insert(n, std::numeric_limits<Value>::max());
deba@870
  3155
deba@870
  3156
        (*_blossom_data)[blossom].status = MATCHED;
deba@870
  3157
        (*_blossom_data)[blossom].pred = INVALID;
deba@870
  3158
        (*_blossom_data)[blossom].next = _fractional->matching(n);
deba@870
  3159
        (*_blossom_data)[blossom].pot = 0;
deba@870
  3160
        (*_blossom_data)[blossom].offset = 0;
deba@870
  3161
        ++index;
deba@870
  3162
      }
deba@870
  3163
deba@870
  3164
      typename Graph::template NodeMap<bool> processed(_graph, false);
deba@870
  3165
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@870
  3166
        if (processed[n]) continue;
deba@870
  3167
        processed[n] = true;
deba@870
  3168
        if (_fractional->matching(n) == INVALID) continue;
deba@870
  3169
        int num = 1;
deba@870
  3170
        Node v = _graph.target(_fractional->matching(n));
deba@870
  3171
        while (n != v) {
deba@870
  3172
          processed[v] = true;
deba@870
  3173
          v = _graph.target(_fractional->matching(v));
deba@870
  3174
          ++num;
deba@870
  3175
        }
deba@870
  3176
deba@870
  3177
        if (num % 2 == 1) {
deba@870
  3178
          std::vector<int> subblossoms(num);
deba@870
  3179
deba@870
  3180
          subblossoms[--num] = _blossom_set->find(n);
deba@870
  3181
          v = _graph.target(_fractional->matching(n));
deba@870
  3182
          while (n != v) {
deba@870
  3183
            subblossoms[--num] = _blossom_set->find(v);
alpar@877
  3184
            v = _graph.target(_fractional->matching(v));
deba@870
  3185
          }
alpar@877
  3186
alpar@877
  3187
          int surface =
deba@870
  3188
            _blossom_set->join(subblossoms.begin(), subblossoms.end());
deba@870
  3189
          (*_blossom_data)[surface].status = EVEN;
deba@870
  3190
          (*_blossom_data)[surface].pred = INVALID;
deba@870
  3191
          (*_blossom_data)[surface].next = INVALID;
deba@870
  3192
          (*_blossom_data)[surface].pot = 0;
deba@870
  3193
          (*_blossom_data)[surface].offset = 0;
alpar@877
  3194
deba@870
  3195
          _tree_set->insert(surface);
deba@870
  3196
          ++_unmatched;
deba@870
  3197
        }
deba@870
  3198
      }
deba@870
  3199
deba@870
  3200
      for (EdgeIt e(_graph); e != INVALID; ++e) {
deba@870
  3201
        int si = (*_node_index)[_graph.u(e)];
deba@870
  3202
        int sb = _blossom_set->find(_graph.u(e));
deba@870
  3203
        int ti = (*_node_index)[_graph.v(e)];
deba@870
  3204
        int tb = _blossom_set->find(_graph.v(e));
deba@870
  3205
        if ((*_blossom_data)[sb].status == EVEN &&
deba@870
  3206
            (*_blossom_data)[tb].status == EVEN && sb != tb) {
deba@870
  3207
          _delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot -
deba@870
  3208
                            dualScale * _weight[e]) / 2);
deba@870
  3209
        }
deba@870
  3210
      }
deba@870
  3211
deba@870
  3212
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@870
  3213
        int nb = _blossom_set->find(n);
deba@870
  3214
        if ((*_blossom_data)[nb].status != MATCHED) continue;
deba@870
  3215
        int ni = (*_node_index)[n];
deba@870
  3216
deba@870
  3217
        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
deba@870
  3218
          Node v = _graph.target(e);
deba@870
  3219
          int vb = _blossom_set->find(v);
deba@870
  3220
          int vi = (*_node_index)[v];
deba@870
  3221
deba@870
  3222
          Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot -
deba@870
  3223
            dualScale * _weight[e];
deba@870
  3224
deba@870
  3225
          if ((*_blossom_data)[vb].status == EVEN) {
deba@870
  3226
deba@870
  3227
            int vt = _tree_set->find(vb);
deba@870
  3228
deba@870
  3229
            typename std::map<int, Arc>::iterator it =
deba@870
  3230
              (*_node_data)[ni].heap_index.find(vt);
deba@870
  3231
deba@870
  3232
            if (it != (*_node_data)[ni].heap_index.end()) {
deba@870
  3233
              if ((*_node_data)[ni].heap[it->second] > rw) {
deba@870
  3234
                (*_node_data)[ni].heap.replace(it->second, e);
deba@870
  3235
                (*_node_data)[ni].heap.decrease(e, rw);
deba@870
  3236
                it->second = e;
deba@870
  3237
              }
deba@870
  3238
            } else {
deba@870
  3239
              (*_node_data)[ni].heap.push(e, rw);
deba@870
  3240
              (*_node_data)[ni].heap_index.insert(std::make_pair(vt, e));
deba@870
  3241
            }
deba@870
  3242
          }
deba@870
  3243
        }
alpar@877
  3244
deba@870
  3245
        if (!(*_node_data)[ni].heap.empty()) {
deba@870
  3246
          _blossom_set->decrease(n, (*_node_data)[ni].heap.prio());
deba@870
  3247
          _delta2->push(nb, _blossom_set->classPrio(nb));
deba@870
  3248
        }
deba@870
  3249
      }
deba@870
  3250
    }
deba@870
  3251
kpeter@590
  3252
    /// \brief Start the algorithm
deba@326
  3253
    ///
kpeter@590
  3254
    /// This function starts the algorithm.
kpeter@590
  3255
    ///
deba@870
  3256
    /// \pre \ref init() or \ref fractionalInit() must be called before
deba@870
  3257
    /// using this function.
deba@326
  3258
    bool start() {
deba@326
  3259
      enum OpType {
deba@326
  3260
        D2, D3, D4
deba@326
  3261
      };
deba@326
  3262
deba@870
  3263
      if (_unmatched == -1) return false;
deba@870
  3264
deba@870
  3265
      while (_unmatched > 0) {
deba@326
  3266
        Value d2 = !_delta2->empty() ?
deba@326
  3267
          _delta2->prio() : std::numeric_limits<Value>::max();
deba@326
  3268
deba@326
  3269
        Value d3 = !_delta3->empty() ?
deba@326
  3270
          _delta3->prio() : std::numeric_limits<Value>::max();
deba@326
  3271
deba@326
  3272
        Value d4 = !_delta4->empty() ?
deba@326
  3273
          _delta4->prio() : std::numeric_limits<Value>::max();
deba@326
  3274
deba@868
  3275
        _delta_sum = d3; OpType ot = D3;
deba@868
  3276
        if (d2 < _delta_sum) { _delta_sum = d2; ot = D2; }
deba@326
  3277
        if (d4 < _delta_sum) { _delta_sum = d4; ot = D4; }
deba@326
  3278
deba@326
  3279
        if (_delta_sum == std::numeric_limits<Value>::max()) {
deba@326
  3280
          return false;
deba@326
  3281
        }
deba@326
  3282
deba@326
  3283
        switch (ot) {
deba@326
  3284
        case D2:
deba@326
  3285
          {
deba@326
  3286
            int blossom = _delta2->top();
deba@326
  3287
            Node n = _blossom_set->classTop(blossom);
deba@326
  3288
            Arc e = (*_node_data)[(*_node_index)[n]].heap.top();
deba@326
  3289
            extendOnArc(e);
deba@326
  3290
          }
deba@326
  3291
          break;
deba@326
  3292
        case D3:
deba@326
  3293
          {
deba@326
  3294
            Edge e = _delta3->top();
deba@326
  3295
deba@326
  3296
            int left_blossom = _blossom_set->find(_graph.u(e));
deba@326
  3297
            int right_blossom = _blossom_set->find(_graph.v(e));
deba@326
  3298
deba@326
  3299
            if (left_blossom == right_blossom) {
deba@326
  3300
              _delta3->pop();
deba@326
  3301
            } else {
deba@326
  3302
              int left_tree = _tree_set->find(left_blossom);
deba@326
  3303
              int right_tree = _tree_set->find(right_blossom);
deba@326
  3304
deba@326
  3305
              if (left_tree == right_tree) {
deba@327
  3306
                shrinkOnEdge(e, left_tree);
deba@326
  3307
              } else {
deba@327
  3308
                augmentOnEdge(e);
deba@870
  3309
                _unmatched -= 2;
deba@326
  3310
              }
deba@326
  3311
            }
deba@326
  3312
          } break;
deba@326
  3313
        case D4:
deba@326
  3314
          splitBlossom(_delta4->top());
deba@326
  3315
          break;
deba@326
  3316
        }
deba@326
  3317
      }
deba@326
  3318
      extractMatching();
deba@326
  3319
      return true;
deba@326
  3320
    }
deba@326
  3321
kpeter@590
  3322
    /// \brief Run the algorithm.
deba@326
  3323
    ///
kpeter@590
  3324
    /// This method runs the \c %MaxWeightedPerfectMatching algorithm.
deba@326
  3325
    ///
kpeter@590
  3326
    /// \note mwpm.run() is just a shortcut of the following code.
deba@326
  3327
    /// \code
deba@870
  3328
    ///   mwpm.fractionalInit();
kpeter@590
  3329
    ///   mwpm.start();
deba@326
  3330
    /// \endcode
deba@326
  3331
    bool run() {
deba@870
  3332
      fractionalInit();
deba@326
  3333
      return start();
deba@326
  3334
    }
deba@326
  3335
deba@326
  3336
    /// @}
deba@326
  3337
kpeter@590
  3338
    /// \name Primal Solution
deba@868
  3339
    /// Functions to get the primal solution, i.e. the maximum weighted
kpeter@590
  3340
    /// perfect matching.\n
kpeter@590
  3341
    /// Either \ref run() or \ref start() function should be called before
kpeter@590
  3342
    /// using them.
deba@326
  3343
deba@326
  3344
    /// @{
deba@326
  3345
kpeter@590
  3346
    /// \brief Return the weight of the matching.
deba@326
  3347
    ///
kpeter@590
  3348
    /// This function returns the weight of the found matching.
kpeter@590
  3349
    ///
kpeter@590
  3350
    /// \pre Either run() or start() must be called before using this function.
kpeter@593
  3351
    Value matchingWeight() const {
deba@326
  3352
      Value sum = 0;
deba@326
  3353
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@326
  3354
        if ((*_matching)[n] != INVALID) {
deba@326
  3355
          sum += _weight[(*_matching)[n]];
deba@326
  3356
        }
deba@326
  3357
      }
deba@868
  3358
      return sum / 2;
deba@326
  3359
    }
deba@326
  3360
kpeter@590
  3361
    /// \brief Return \c true if the given edge is in the matching.
deba@326
  3362
    ///
deba@868
  3363
    /// This function returns \c true if the given edge is in the found
kpeter@590
  3364
    /// matching.
kpeter@590
  3365
    ///
kpeter@590
  3366
    /// \pre Either run() or start() must be called before using this function.
deba@327
  3367
    bool matching(const Edge& edge) const {
deba@327
  3368
      return static_cast<const Edge&>((*_matching)[_graph.u(edge)]) == edge;
deba@326
  3369
    }
deba@326
  3370
kpeter@590
  3371
    /// \brief Return the matching arc (or edge) incident to the given node.
deba@326
  3372
    ///
kpeter@590
  3373
    /// This function returns the matching arc (or edge) incident to the
deba@868
  3374
    /// given node in the found matching or \c INVALID if the node is
kpeter@590
  3375
    /// not covered by the matching.
kpeter@590
  3376
    ///
kpeter@590
  3377
    /// \pre Either run() or start() must be called before using this function.
deba@326
  3378
    Arc matching(const Node& node) const {
deba@326
  3379
      return (*_matching)[node];
deba@326
  3380
    }
deba@326
  3381
kpeter@593
  3382
    /// \brief Return a const reference to the matching map.
kpeter@593
  3383
    ///
kpeter@593
  3384
    /// This function returns a const reference to a node map that stores
kpeter@593
  3385
    /// the matching arc (or edge) incident to each node.
kpeter@593
  3386
    const MatchingMap& matchingMap() const {
kpeter@593
  3387
      return *_matching;
kpeter@593
  3388
    }
kpeter@593
  3389
kpeter@590
  3390
    /// \brief Return the mate of the given node.
deba@326
  3391
    ///
deba@868
  3392
    /// This function returns the mate of the given node in the found
kpeter@590
  3393
    /// matching or \c INVALID if the node is not covered by the matching.
kpeter@590
  3394
    ///
kpeter@590
  3395
    /// \pre Either run() or start() must be called before using this function.
deba@326
  3396
    Node mate(const Node& node) const {
deba@326
  3397
      return _graph.target((*_matching)[node]);
deba@326
  3398
    }
deba@326
  3399
deba@326
  3400
    /// @}
deba@326
  3401
kpeter@590
  3402
    /// \name Dual Solution
kpeter@590
  3403
    /// Functions to get the dual solution.\n
kpeter@590
  3404
    /// Either \ref run() or \ref start() function should be called before
kpeter@590
  3405
    /// using them.
deba@326
  3406
deba@326
  3407
    /// @{
deba@326
  3408
kpeter@590
  3409
    /// \brief Return the value of the dual solution.
deba@326
  3410
    ///
deba@868
  3411
    /// This function returns the value of the dual solution.
deba@868
  3412
    /// It should be equal to the primal value scaled by \ref dualScale
kpeter@590
  3413
    /// "dual scale".
kpeter@590
  3414
    ///
kpeter@590
  3415
    /// \pre Either run() or start() must be called before using this function.
deba@326
  3416
    Value dualValue() const {
deba@326
  3417
      Value sum = 0;
deba@326
  3418
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@326
  3419
        sum += nodeValue(n);
deba@326
  3420
      }
deba@326
  3421
      for (int i = 0; i < blossomNum(); ++i) {
deba@326
  3422
        sum += blossomValue(i) * (blossomSize(i) / 2);
deba@326
  3423
      }
deba@326
  3424
      return sum;
deba@326
  3425
    }
deba@326
  3426
kpeter@590
  3427
    /// \brief Return the dual value (potential) of the given node.
deba@326
  3428
    ///
kpeter@590
  3429
    /// This function returns the dual value (potential) of the given node.
kpeter@590
  3430
    ///
kpeter@590
  3431
    /// \pre Either run() or start() must be called before using this function.
deba@326
  3432
    Value nodeValue(const Node& n) const {
deba@326
  3433
      return (*_node_potential)[n];
deba@326
  3434
    }
deba@326
  3435
kpeter@590
  3436
    /// \brief Return the number of the blossoms in the basis.
deba@326
  3437
    ///
kpeter@590
  3438
    /// This function returns the number of the blossoms in the basis.
kpeter@590
  3439
    ///
kpeter@590
  3440
    /// \pre Either run() or start() must be called before using this function.
deba@326
  3441
    /// \see BlossomIt
deba@326
  3442
    int blossomNum() const {
deba@326
  3443
      return _blossom_potential.size();
deba@326
  3444
    }
deba@326
  3445
kpeter@590
  3446
    /// \brief Return the number of the nodes in the given blossom.
deba@326
  3447
    ///
kpeter@590
  3448
    /// This function returns the number of the nodes in the given blossom.
kpeter@590
  3449
    ///
kpeter@590
  3450
    /// \pre Either run() or start() must be called before using this function.
kpeter@590
  3451
    /// \see BlossomIt
deba@326
  3452
    int blossomSize(int k) const {
deba@326
  3453
      return _blossom_potential[k].end - _blossom_potential[k].begin;
deba@326
  3454
    }
deba@326
  3455
kpeter@590
  3456
    /// \brief Return the dual value (ptential) of the given blossom.
deba@326
  3457
    ///
kpeter@590
  3458
    /// This function returns the dual value (ptential) of the given blossom.
kpeter@590
  3459
    ///
kpeter@590
  3460
    /// \pre Either run() or start() must be called before using this function.
deba@326
  3461
    Value blossomValue(int k) const {
deba@326
  3462
      return _blossom_potential[k].value;
deba@326
  3463
    }
deba@326
  3464
kpeter@590
  3465
    /// \brief Iterator for obtaining the nodes of a blossom.
deba@326
  3466
    ///
deba@868
  3467
    /// This class provides an iterator for obtaining the nodes of the
kpeter@590
  3468
    /// given blossom. It lists a subset of the nodes.
deba@868
  3469
    /// Before using this iterator, you must allocate a
kpeter@590
  3470
    /// MaxWeightedPerfectMatching class and execute it.
deba@326
  3471
    class BlossomIt {
deba@326
  3472
    public:
deba@326
  3473
deba@326
  3474
      /// \brief Constructor.
deba@326
  3475
      ///
kpeter@590
  3476
      /// Constructor to get the nodes of the given variable.
kpeter@590
  3477
      ///
deba@868
  3478
      /// \pre Either \ref MaxWeightedPerfectMatching::run() "algorithm.run()"
deba@868
  3479
      /// or \ref MaxWeightedPerfectMatching::start() "algorithm.start()"
kpeter@590
  3480
      /// must be called before initializing this iterator.
deba@326
  3481
      BlossomIt(const MaxWeightedPerfectMatching& algorithm, int variable)
deba@326
  3482
        : _algorithm(&algorithm)
deba@326
  3483
      {
deba@326
  3484
        _index = _algorithm->_blossom_potential[variable].begin;
deba@326
  3485
        _last = _algorithm->_blossom_potential[variable].end;
deba@326
  3486
      }
deba@326
  3487
kpeter@590
  3488
      /// \brief Conversion to \c Node.
deba@326
  3489
      ///
kpeter@590
  3490
      /// Conversion to \c Node.
deba@326
  3491
      operator Node() const {
deba@327
  3492
        return _algorithm->_blossom_node_list[_index];
deba@326
  3493
      }
deba@326
  3494
deba@326
  3495
      /// \brief Increment operator.
deba@326
  3496
      ///
deba@326
  3497
      /// Increment operator.
deba@326
  3498
      BlossomIt& operator++() {
deba@326
  3499
        ++_index;
deba@326
  3500
        return *this;
deba@326
  3501
      }
deba@326
  3502
deba@327
  3503
      /// \brief Validity checking
deba@327
  3504
      ///
kpeter@590
  3505
      /// This function checks whether the iterator is invalid.
deba@327
  3506
      bool operator==(Invalid) const { return _index == _last; }
deba@327
  3507
deba@327
  3508
      /// \brief Validity checking
deba@327
  3509
      ///
kpeter@590
  3510
      /// This function checks whether the iterator is valid.
deba@327
  3511
      bool operator!=(Invalid) const { return _index != _last; }
deba@326
  3512
deba@326
  3513
    private:
deba@326
  3514
      const MaxWeightedPerfectMatching* _algorithm;
deba@326
  3515
      int _last;
deba@326
  3516
      int _index;
deba@326
  3517
    };
deba@326
  3518
deba@326
  3519
    /// @}
deba@326
  3520
deba@326
  3521
  };
deba@326
  3522
deba@326
  3523
} //END OF NAMESPACE LEMON
deba@326
  3524
deba@868
  3525
#endif //LEMON_MATCHING_H