| ... | ... |
@@ -36,25 +36,25 @@ |
| 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 |
/// |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 {
|
| ... | ... |
@@ -63,14 +63,13 @@ |
| 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 |
| ... | ... |
@@ -407,19 +406,19 @@ |
| 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 |
|
| 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 |
|
| 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 |
/// |
| ... | ... |
@@ -430,15 +429,15 @@ |
| 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 |
|
| 435 |
///\brief Finds an initial matching in a greedy way |
|
| 437 | 436 |
/// |
| 438 |
/// |
|
| 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 |
} |
| ... | ... |
@@ -456,13 +455,13 @@ |
| 456 | 455 |
} |
| 457 | 456 |
} |
| 458 | 457 |
} |
| 459 | 458 |
} |
| 460 | 459 |
|
| 461 | 460 |
|
| 462 |
/// \brief Initialize the matching from |
|
| 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> |
| ... | ... |
@@ -504,13 +503,13 @@ |
| 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, |
|
| 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); |
| ... | ... |
@@ -535,19 +534,19 @@ |
| 535 | 534 |
} |
| 536 | 535 |
} |
| 537 | 536 |
|
| 538 | 537 |
/// @} |
| 539 | 538 |
|
| 540 | 539 |
/// \name Primal solution |
| 541 |
/// Functions |
|
| 540 |
/// Functions to get the primal solution, ie. the matching. |
|
| 542 | 541 |
|
| 543 | 542 |
/// @{
|
| 544 | 543 |
|
| 545 |
///\brief Returns the size of the |
|
| 544 |
///\brief Returns the size of the current matching. |
|
| 546 | 545 |
/// |
| 547 |
///Returns the size of the |
|
| 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; |
| ... | ... |
@@ -580,13 +579,13 @@ |
| 580 | 579 |
_graph.target((*_matching)[n]) : INVALID; |
| 581 | 580 |
} |
| 582 | 581 |
|
| 583 | 582 |
/// @} |
| 584 | 583 |
|
| 585 | 584 |
/// \name Dual solution |
| 586 |
/// Functions |
|
| 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 |
/// |
| ... | ... |
@@ -642,13 +641,13 @@ |
| 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: |
| ... | ... |
@@ -1627,13 +1626,13 @@ |
| 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 |
|
| 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 |
/// |
| ... | ... |
@@ -1788,19 +1787,19 @@ |
| 1788 | 1787 |
start(); |
| 1789 | 1788 |
} |
| 1790 | 1789 |
|
| 1791 | 1790 |
/// @} |
| 1792 | 1791 |
|
| 1793 | 1792 |
/// \name Primal solution |
| 1794 |
/// Functions |
|
| 1793 |
/// Functions to get the primal solution, ie. the matching. |
|
| 1795 | 1794 |
|
| 1796 | 1795 |
/// @{
|
| 1797 | 1796 |
|
| 1798 |
/// \brief Returns the |
|
| 1797 |
/// \brief Returns the weight of the matching. |
|
| 1799 | 1798 |
/// |
| 1800 |
/// Returns the |
|
| 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 |
} |
| ... | ... |
@@ -1845,13 +1844,13 @@ |
| 1845 | 1844 |
_graph.target((*_matching)[node]) : INVALID; |
| 1846 | 1845 |
} |
| 1847 | 1846 |
|
| 1848 | 1847 |
/// @} |
| 1849 | 1848 |
|
| 1850 | 1849 |
/// \name Dual solution |
| 1851 |
/// Functions |
|
| 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 |
| ... | ... |
@@ -1895,23 +1894,23 @@ |
| 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 |
|
| 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 |
|
| 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 |
} |
| ... | ... |
@@ -2814,13 +2813,13 @@ |
| 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 |
|
| 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 |
/// |
| ... | ... |
@@ -2952,13 +2951,13 @@ |
| 2952 | 2951 |
return start(); |
| 2953 | 2952 |
} |
| 2954 | 2953 |
|
| 2955 | 2954 |
/// @} |
| 2956 | 2955 |
|
| 2957 | 2956 |
/// \name Primal solution |
| 2958 |
/// Functions |
|
| 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. |
| ... | ... |
@@ -2993,13 +2992,13 @@ |
| 2993 | 2992 |
return _graph.target((*_matching)[node]); |
| 2994 | 2993 |
} |
| 2995 | 2994 |
|
| 2996 | 2995 |
/// @} |
| 2997 | 2996 |
|
| 2998 | 2997 |
/// \name Dual solution |
| 2999 |
/// Functions |
|
| 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 |
| ... | ... |
@@ -3043,23 +3042,23 @@ |
| 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 |
|
| 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 |
|
| 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 |
} |
0 comments (0 inline)