gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Doc improvements in lemon/max_matching.h
0 1 0
default
1 file changed with 35 insertions and 36 deletions:
↑ Collapse diff ↑
Ignore white space 24 line context
... ...
@@ -30,53 +30,52 @@
30 30
#include <lemon/maps.h>
31 31

	
32 32
///\ingroup matching
33 33
///\file
34 34
///\brief Maximum matching algorithms in general graphs.
35 35

	
36 36
namespace lemon {
37 37

	
38 38
  /// \ingroup matching
39 39
  ///
40 40
  /// \brief Edmonds' alternating forest maximum matching algorithm.
41 41
  ///
42
  /// This class provides Edmonds' alternating forest matching
43
  /// algorithm. The starting matching (if any) can be passed to the
44
  /// algorithm using some of init functions.
42
  /// This class implements Edmonds' alternating forest matching
43
  /// algorithm. The algorithm can be started from an arbitrary initial
44
  /// matching (the default is the empty one)
45 45
  ///
46
  /// The dual side of a matching is a map of the nodes to
46
  /// The dual solution of the problem is a map of the nodes to
47 47
  /// MaxMatching::Status, having values \c EVEN/D, \c ODD/A and \c
48 48
  /// MATCHED/C showing the Gallai-Edmonds decomposition of the
49 49
  /// graph. The nodes in \c EVEN/D induce a graph with
50 50
  /// factor-critical components, the nodes in \c ODD/A form the
51 51
  /// barrier, and the nodes in \c MATCHED/C induce a graph having a
52
  /// perfect matching. The number of the fractor critical components
52
  /// perfect matching. The number of the factor-critical components
53 53
  /// minus the number of barrier nodes is a lower bound on the
54
  /// unmatched nodes, and if the matching is optimal this bound is
54
  /// unmatched nodes, and the matching is optimal if and only if this bound is
55 55
  /// tight. This decomposition can be attained by calling \c
56 56
  /// decomposition() after running the algorithm.
57 57
  ///
58 58
  /// \param _Graph The graph type the algorithm runs on.
59 59
  template <typename _Graph>
60 60
  class MaxMatching {
61 61
  public:
62 62

	
63 63
    typedef _Graph Graph;
64 64
    typedef typename Graph::template NodeMap<typename Graph::Arc>
65 65
    MatchingMap;
66 66

	
67 67
    ///\brief Indicates the Gallai-Edmonds decomposition of the graph.
68 68
    ///
69
    ///Indicates the Gallai-Edmonds decomposition of the graph, which
70
    ///shows an upper bound on the size of a maximum matching. The
69
    ///Indicates the Gallai-Edmonds decomposition of the graph. The
71 70
    ///nodes with Status \c EVEN/D induce a graph with factor-critical
72 71
    ///components, the nodes in \c ODD/A form the canonical barrier,
73 72
    ///and the nodes in \c MATCHED/C induce a graph having a perfect
74 73
    ///matching.
75 74
    enum Status {
76 75
      EVEN = 1, D = 1, MATCHED = 0, C = 0, ODD = -1, A = -1, UNMATCHED = -2
77 76
    };
78 77

	
79 78
    typedef typename Graph::template NodeMap<Status> StatusMap;
80 79

	
81 80
  private:
82 81

	
... ...
@@ -401,74 +400,74 @@
401 400
    ///
402 401
    /// Constructor.
403 402
    MaxMatching(const Graph& graph)
404 403
      : _graph(graph), _matching(0), _status(0), _ear(0),
405 404
        _blossom_set_index(0), _blossom_set(0), _blossom_rep(0),
406 405
        _tree_set_index(0), _tree_set(0) {}
407 406

	
408 407
    ~MaxMatching() {
409 408
      destroyStructures();
410 409
    }
411 410

	
412 411
    /// \name Execution control
413
    /// The simplest way to execute the algorithm is to use the member
412
    /// The simplest way to execute the algorithm is to use the
414 413
    /// \c run() member function.
415 414
    /// \n
416 415

	
417
    /// If you need more control on the execution, first you must call
416
    /// If you need better control on the execution, you must call
418 417
    /// \ref init(), \ref greedyInit() or \ref matchingInit()
419
    /// functions, then you can start the algorithm with the \ref
418
    /// functions first, then you can start the algorithm with the \ref
420 419
    /// startParse() or startDense() functions.
421 420

	
422 421
    ///@{
423 422

	
424 423
    /// \brief Sets the actual matching to the empty matching.
425 424
    ///
426 425
    /// Sets the actual matching to the empty matching.
427 426
    ///
428 427
    void init() {
429 428
      createStructures();
430 429
      for(NodeIt n(_graph); n != INVALID; ++n) {
431 430
        _matching->set(n, INVALID);
432 431
        _status->set(n, UNMATCHED);
433 432
      }
434 433
    }
435 434

	
436
    ///\brief Finds a greedy matching for initial matching.
435
    ///\brief Finds an initial matching in a greedy way
437 436
    ///
438
    ///For initial matchig it finds a maximal greedy matching.
437
    ///It finds an initial matching in a greedy way.
439 438
    void greedyInit() {
440 439
      createStructures();
441 440
      for (NodeIt n(_graph); n != INVALID; ++n) {
442 441
        _matching->set(n, INVALID);
443 442
        _status->set(n, UNMATCHED);
444 443
      }
445 444
      for (NodeIt n(_graph); n != INVALID; ++n) {
446 445
        if ((*_matching)[n] == INVALID) {
447 446
          for (OutArcIt a(_graph, n); a != INVALID ; ++a) {
448 447
            Node v = _graph.target(a);
449 448
            if ((*_matching)[v] == INVALID && v != n) {
450 449
              _matching->set(n, a);
451 450
              _status->set(n, MATCHED);
452 451
              _matching->set(v, _graph.oppositeArc(a));
453 452
              _status->set(v, MATCHED);
454 453
              break;
455 454
            }
456 455
          }
457 456
        }
458 457
      }
459 458
    }
460 459

	
461 460

	
462
    /// \brief Initialize the matching from the map containing a matching.
461
    /// \brief Initialize the matching from a map containing.
463 462
    ///
464 463
    /// Initialize the matching from a \c bool valued \c Edge map. This
465 464
    /// map must have the property that there are no two incident edges
466 465
    /// with true value, ie. it contains a matching.
467 466
    /// \return %True if the map contains a matching.
468 467
    template <typename MatchingMap>
469 468
    bool matchingInit(const MatchingMap& matching) {
470 469
      createStructures();
471 470

	
472 471
      for (NodeIt n(_graph); n != INVALID; ++n) {
473 472
        _matching->set(n, INVALID);
474 473
        _status->set(n, UNMATCHED);
... ...
@@ -498,25 +497,25 @@
498 497
        if ((*_status)[n] == UNMATCHED) {
499 498
          (*_blossom_rep)[_blossom_set->insert(n)] = n;
500 499
          _tree_set->insert(n);
501 500
          _status->set(n, EVEN);
502 501
          processSparse(n);
503 502
        }
504 503
      }
505 504
    }
506 505

	
507 506
    /// \brief Starts Edmonds' algorithm.
508 507
    ///
509 508
    /// It runs Edmonds' algorithm with a heuristic of postponing
510
    /// shrinks, giving a faster algorithm for dense graphs.
509
    /// shrinks, therefore resulting in a faster algorithm for dense graphs.
511 510
    void startDense() {
512 511
      for(NodeIt n(_graph); n != INVALID; ++n) {
513 512
        if ((*_status)[n] == UNMATCHED) {
514 513
          (*_blossom_rep)[_blossom_set->insert(n)] = n;
515 514
          _tree_set->insert(n);
516 515
          _status->set(n, EVEN);
517 516
          processDense(n);
518 517
        }
519 518
      }
520 519
    }
521 520

	
522 521

	
... ...
@@ -529,31 +528,31 @@
529 528
      if (countEdges(_graph) < 2 * countNodes(_graph)) {
530 529
        greedyInit();
531 530
        startSparse();
532 531
      } else {
533 532
        init();
534 533
        startDense();
535 534
      }
536 535
    }
537 536

	
538 537
    /// @}
539 538

	
540 539
    /// \name Primal solution
541
    /// Functions for get the primal solution, ie. the matching.
540
    /// Functions to get the primal solution, ie. the matching.
542 541

	
543 542
    /// @{
544 543

	
545
    ///\brief Returns the size of the actual matching stored.
544
    ///\brief Returns the size of the current matching.
546 545
    ///
547
    ///Returns the size of the actual matching stored. After \ref
546
    ///Returns the size of the current matching. After \ref
548 547
    ///run() it returns the size of the maximum matching in the graph.
549 548
    int matchingSize() const {
550 549
      int size = 0;
551 550
      for (NodeIt n(_graph); n != INVALID; ++n) {
552 551
        if ((*_matching)[n] != INVALID) {
553 552
          ++size;
554 553
        }
555 554
      }
556 555
      return size / 2;
557 556
    }
558 557

	
559 558
    /// \brief Returns true when the edge is in the matching.
... ...
@@ -574,25 +573,25 @@
574 573
    ///\brief Returns the mate of a node in the actual matching.
575 574
    ///
576 575
    ///Returns the mate of a \c node in the actual matching or
577 576
    ///INVALID if the \c node is not covered by the actual matching.
578 577
    Node mate(const Node& n) const {
579 578
      return (*_matching)[n] != INVALID ?
580 579
        _graph.target((*_matching)[n]) : INVALID;
581 580
    }
582 581

	
583 582
    /// @}
584 583

	
585 584
    /// \name Dual solution
586
    /// Functions for get the dual solution, ie. the decomposition.
585
    /// Functions to get the dual solution, ie. the decomposition.
587 586

	
588 587
    /// @{
589 588

	
590 589
    /// \brief Returns the class of the node in the Edmonds-Gallai
591 590
    /// decomposition.
592 591
    ///
593 592
    /// Returns the class of the node in the Edmonds-Gallai
594 593
    /// decomposition.
595 594
    Status decomposition(const Node& n) const {
596 595
      return (*_status)[n];
597 596
    }
598 597

	
... ...
@@ -636,25 +635,25 @@
636 635
  /** \f[ y_u + y_v + \sum_{B \in \mathcal{O}, uv \in \gamma(B)}
637 636
      z_B \ge w_{uv} \quad \forall uv\in E\f] */
638 637
  /// \f[y_u \ge 0 \quad \forall u \in V\f]
639 638
  /// \f[z_B \ge 0 \quad \forall B \in \mathcal{O}\f]
640 639
  /** \f[\min \sum_{u \in V}y_u + \sum_{B \in \mathcal{O}}
641 640
      \frac{\vert B \vert - 1}{2}z_B\f] */
642 641
  ///
643 642
  /// The algorithm can be executed with \c run() or the \c init() and
644 643
  /// then the \c start() member functions. After it the matching can
645 644
  /// be asked with \c matching() or mate() functions. The dual
646 645
  /// solution can be get with \c nodeValue(), \c blossomNum() and \c
647 646
  /// blossomValue() members and \ref MaxWeightedMatching::BlossomIt
648
  /// "BlossomIt" nested class which is able to iterate on the nodes
647
  /// "BlossomIt" nested class, which is able to iterate on the nodes
649 648
  /// of a blossom. If the value type is integral then the dual
650 649
  /// solution is multiplied by \ref MaxWeightedMatching::dualScale "4".
651 650
  template <typename _Graph,
652 651
            typename _WeightMap = typename _Graph::template EdgeMap<int> >
653 652
  class MaxWeightedMatching {
654 653
  public:
655 654

	
656 655
    typedef _Graph Graph;
657 656
    typedef _WeightMap WeightMap;
658 657
    typedef typename WeightMap::Value Value;
659 658

	
660 659
    /// \brief Scaling factor for dual solution
... ...
@@ -1621,25 +1620,25 @@
1621 1620
        _delta1_index(0), _delta1(0),
1622 1621
        _delta2_index(0), _delta2(0),
1623 1622
        _delta3_index(0), _delta3(0),
1624 1623
        _delta4_index(0), _delta4(0),
1625 1624

	
1626 1625
        _delta_sum() {}
1627 1626

	
1628 1627
    ~MaxWeightedMatching() {
1629 1628
      destroyStructures();
1630 1629
    }
1631 1630

	
1632 1631
    /// \name Execution control
1633
    /// The simplest way to execute the algorithm is to use the member
1632
    /// The simplest way to execute the algorithm is to use the
1634 1633
    /// \c run() member function.
1635 1634

	
1636 1635
    ///@{
1637 1636

	
1638 1637
    /// \brief Initialize the algorithm
1639 1638
    ///
1640 1639
    /// Initialize the algorithm
1641 1640
    void init() {
1642 1641
      createStructures();
1643 1642

	
1644 1643
      for (ArcIt e(_graph); e != INVALID; ++e) {
1645 1644
        _node_heap_index->set(e, BinHeap<Value, IntArcMap>::PRE_HEAP);
... ...
@@ -1782,31 +1781,31 @@
1782 1781
    /// \code
1783 1782
    ///   mwm.init();
1784 1783
    ///   mwm.start();
1785 1784
    /// \endcode
1786 1785
    void run() {
1787 1786
      init();
1788 1787
      start();
1789 1788
    }
1790 1789

	
1791 1790
    /// @}
1792 1791

	
1793 1792
    /// \name Primal solution
1794
    /// Functions for get the primal solution, ie. the matching.
1793
    /// Functions to get the primal solution, ie. the matching.
1795 1794

	
1796 1795
    /// @{
1797 1796

	
1798
    /// \brief Returns the matching value.
1797
    /// \brief Returns the weight of the matching.
1799 1798
    ///
1800
    /// Returns the matching value.
1799
    /// Returns the weight of the matching.
1801 1800
    Value matchingValue() const {
1802 1801
      Value sum = 0;
1803 1802
      for (NodeIt n(_graph); n != INVALID; ++n) {
1804 1803
        if ((*_matching)[n] != INVALID) {
1805 1804
          sum += _weight[(*_matching)[n]];
1806 1805
        }
1807 1806
      }
1808 1807
      return sum /= 2;
1809 1808
    }
1810 1809

	
1811 1810
    /// \brief Returns the cardinality of the matching.
1812 1811
    ///
... ...
@@ -1839,25 +1838,25 @@
1839 1838
    /// \brief Returns the mate of the node.
1840 1839
    ///
1841 1840
    /// Returns the adjancent node in a mathcing arc. If the node is
1842 1841
    /// not matched then it gives back \c INVALID.
1843 1842
    Node mate(const Node& node) const {
1844 1843
      return (*_matching)[node] != INVALID ?
1845 1844
        _graph.target((*_matching)[node]) : INVALID;
1846 1845
    }
1847 1846

	
1848 1847
    /// @}
1849 1848

	
1850 1849
    /// \name Dual solution
1851
    /// Functions for get the dual solution.
1850
    /// Functions to get the dual solution.
1852 1851

	
1853 1852
    /// @{
1854 1853

	
1855 1854
    /// \brief Returns the value of the dual solution.
1856 1855
    ///
1857 1856
    /// Returns the value of the dual solution. It should be equal to
1858 1857
    /// the primal value scaled by \ref dualScale "dual scale".
1859 1858
    Value dualValue() const {
1860 1859
      Value sum = 0;
1861 1860
      for (NodeIt n(_graph); n != INVALID; ++n) {
1862 1861
        sum += nodeValue(n);
1863 1862
      }
... ...
@@ -1889,35 +1888,35 @@
1889 1888
    int blossomSize(int k) const {
1890 1889
      return _blossom_potential[k].end - _blossom_potential[k].begin;
1891 1890
    }
1892 1891

	
1893 1892
    /// \brief Returns the value of the blossom.
1894 1893
    ///
1895 1894
    /// Returns the the value of the blossom.
1896 1895
    /// \see BlossomIt
1897 1896
    Value blossomValue(int k) const {
1898 1897
      return _blossom_potential[k].value;
1899 1898
    }
1900 1899

	
1901
    /// \brief Lemon iterator for get the items of the blossom.
1900
    /// \brief Iterator for obtaining the nodes of the blossom.
1902 1901
    ///
1903
    /// Lemon iterator for get the nodes of the blossom. This class
1904
    /// provides a common style lemon iterator which gives back a
1902
    /// Iterator for obtaining the nodes of the blossom. This class
1903
    /// provides a common lemon style iterator for listing a
1905 1904
    /// subset of the nodes.
1906 1905
    class BlossomIt {
1907 1906
    public:
1908 1907

	
1909 1908
      /// \brief Constructor.
1910 1909
      ///
1911
      /// Constructor for get the nodes of the variable.
1910
      /// Constructor to get the nodes of the variable.
1912 1911
      BlossomIt(const MaxWeightedMatching& algorithm, int variable)
1913 1912
        : _algorithm(&algorithm)
1914 1913
      {
1915 1914
        _index = _algorithm->_blossom_potential[variable].begin;
1916 1915
        _last = _algorithm->_blossom_potential[variable].end;
1917 1916
      }
1918 1917

	
1919 1918
      /// \brief Conversion to node.
1920 1919
      ///
1921 1920
      /// Conversion to node.
1922 1921
      operator Node() const {
1923 1922
        return _algorithm->_blossom_node_list[_index];
... ...
@@ -2808,25 +2807,25 @@
2808 2807

	
2809 2808
        _delta2_index(0), _delta2(0),
2810 2809
        _delta3_index(0), _delta3(0),
2811 2810
        _delta4_index(0), _delta4(0),
2812 2811

	
2813 2812
        _delta_sum() {}
2814 2813

	
2815 2814
    ~MaxWeightedPerfectMatching() {
2816 2815
      destroyStructures();
2817 2816
    }
2818 2817

	
2819 2818
    /// \name Execution control
2820
    /// The simplest way to execute the algorithm is to use the member
2819
    /// The simplest way to execute the algorithm is to use the
2821 2820
    /// \c run() member function.
2822 2821

	
2823 2822
    ///@{
2824 2823

	
2825 2824
    /// \brief Initialize the algorithm
2826 2825
    ///
2827 2826
    /// Initialize the algorithm
2828 2827
    void init() {
2829 2828
      createStructures();
2830 2829

	
2831 2830
      for (ArcIt e(_graph); e != INVALID; ++e) {
2832 2831
        _node_heap_index->set(e, BinHeap<Value, IntArcMap>::PRE_HEAP);
... ...
@@ -2946,25 +2945,25 @@
2946 2945
    /// \code
2947 2946
    ///   mwm.init();
2948 2947
    ///   mwm.start();
2949 2948
    /// \endcode
2950 2949
    bool run() {
2951 2950
      init();
2952 2951
      return start();
2953 2952
    }
2954 2953

	
2955 2954
    /// @}
2956 2955

	
2957 2956
    /// \name Primal solution
2958
    /// Functions for get the primal solution, ie. the matching.
2957
    /// Functions to get the primal solution, ie. the matching.
2959 2958

	
2960 2959
    /// @{
2961 2960

	
2962 2961
    /// \brief Returns the matching value.
2963 2962
    ///
2964 2963
    /// Returns the matching value.
2965 2964
    Value matchingValue() const {
2966 2965
      Value sum = 0;
2967 2966
      for (NodeIt n(_graph); n != INVALID; ++n) {
2968 2967
        if ((*_matching)[n] != INVALID) {
2969 2968
          sum += _weight[(*_matching)[n]];
2970 2969
        }
... ...
@@ -2987,25 +2986,25 @@
2987 2986
    }
2988 2987

	
2989 2988
    /// \brief Returns the mate of the node.
2990 2989
    ///
2991 2990
    /// Returns the adjancent node in a mathcing arc.
2992 2991
    Node mate(const Node& node) const {
2993 2992
      return _graph.target((*_matching)[node]);
2994 2993
    }
2995 2994

	
2996 2995
    /// @}
2997 2996

	
2998 2997
    /// \name Dual solution
2999
    /// Functions for get the dual solution.
2998
    /// Functions to get the dual solution.
3000 2999

	
3001 3000
    /// @{
3002 3001

	
3003 3002
    /// \brief Returns the value of the dual solution.
3004 3003
    ///
3005 3004
    /// Returns the value of the dual solution. It should be equal to
3006 3005
    /// the primal value scaled by \ref dualScale "dual scale".
3007 3006
    Value dualValue() const {
3008 3007
      Value sum = 0;
3009 3008
      for (NodeIt n(_graph); n != INVALID; ++n) {
3010 3009
        sum += nodeValue(n);
3011 3010
      }
... ...
@@ -3037,35 +3036,35 @@
3037 3036
    int blossomSize(int k) const {
3038 3037
      return _blossom_potential[k].end - _blossom_potential[k].begin;
3039 3038
    }
3040 3039

	
3041 3040
    /// \brief Returns the value of the blossom.
3042 3041
    ///
3043 3042
    /// Returns the the value of the blossom.
3044 3043
    /// \see BlossomIt
3045 3044
    Value blossomValue(int k) const {
3046 3045
      return _blossom_potential[k].value;
3047 3046
    }
3048 3047

	
3049
    /// \brief Lemon iterator for get the items of the blossom.
3048
    /// \brief Iterator for obtaining the nodes of the blossom.
3050 3049
    ///
3051
    /// Lemon iterator for get the nodes of the blossom. This class
3052
    /// provides a common style lemon iterator which gives back a
3050
    /// Iterator for obtaining the nodes of the blossom. This class
3051
    /// provides a common lemon style iterator for listing a
3053 3052
    /// subset of the nodes.
3054 3053
    class BlossomIt {
3055 3054
    public:
3056 3055

	
3057 3056
      /// \brief Constructor.
3058 3057
      ///
3059
      /// Constructor for get the nodes of the variable.
3058
      /// Constructor to get the nodes of the variable.
3060 3059
      BlossomIt(const MaxWeightedPerfectMatching& algorithm, int variable)
3061 3060
        : _algorithm(&algorithm)
3062 3061
      {
3063 3062
        _index = _algorithm->_blossom_potential[variable].begin;
3064 3063
        _last = _algorithm->_blossom_potential[variable].end;
3065 3064
      }
3066 3065

	
3067 3066
      /// \brief Conversion to node.
3068 3067
      ///
3069 3068
      /// Conversion to node.
3070 3069
      operator Node() const {
3071 3070
        return _algorithm->_blossom_node_list[_index];
0 comments (0 inline)