|
ADS
Algorithms and Data Structures
|
The matchings of a general graph by the blossoms of Edmonds, beyond the book, whose matchings of a bipartite graph and branch and bound are in Matching.h. More...
Files | |
| file | ADS/graph/BlossomMatching.h |
| The matchings of a general graph by the blossoms of Edmonds, beyond the book, whose matchings of a bipartite graph and branch and bound are in Matching.h [see Matching.h, which includes this header]: | |
Functions | |
| template<Graph G> | |
| std::vector< int > | ADS::maxCardinalityMatching (const G &g) |
| the mate of each node in a matching of largest size of the unoriented graph \( g \), NIL for a node left unmatched and for a name that is not a node | |
| template<class W> requires std::is_arithmetic_v< W > && std::is_signed_v< W > | |
| std::pair< W, std::vector< std::size_t > > | ADS::maxWeightMatching (int n, const std::vector< std::tuple< int, int, W > > &E, bool maxCardinality=false) |
| the largest total weight of a matching of the graph of \( n \) nodes and of the edges \( E = ( u , v , w ) \), or, if maxCardinality, the largest weight of a matching of largest size, and the places in \( E \) of its edges, in increasing order | |
The matchings of a general graph by the blossoms of Edmonds, beyond the book, whose matchings of a bipartite graph and branch and bound are in Matching.h.
The definitions, the invariants and the costs of the area are in Theory of graphs.
| std::vector< int > ADS::maxCardinalityMatching | ( | const G & | g | ) |
the mate of each node in a matching of largest size of the unoriented graph \( g \), NIL for a node left unmatched and for a name that is not a node
In pseudocode (Edmonds' algorithm, beyond the book, with the blossoms as the sets of a union-find): \( B( v ) \) is the base of the outermost blossom of \( v \), a node is even if it is the root, the mate of an odd node or in a blossom, and an odd node \( y \) keeps the even node \( p[ y ] \) that reached it; a node of a cycle that was odd keeps the edge \( ( a , b ) \) that closed it, its side first, from which the path to the root is rebuilt (see Flows, cuts and matchings).
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maxCardinalityMatching}( G ) \cr 2 & \quad M \gets \text{a greedy matching} \cr 3 & \quad \textbf{foreach}\ r \in V\ \textbf{with}\ r \text{ free } \textbf{do} \cr 4 & \qquad \textbf{if not}\ \mathrm{search}( r )\ \textbf{then} \ \text{delete the nodes of its tree from } G \cr 5 & \quad \textbf{return}\ M \cr 6 & \textbf{function}\ \mathrm{search}( r ) \cr 7 & \quad Q \gets ( r );\ r \text{ even} \cr 8 & \quad \textbf{foreach}\ x \in Q ,\ y \in G.\mathrm{adj}( x ) \ \textbf{with}\ y \text{ not odd}\ \textbf{do} \cr 9 & \qquad \textbf{if}\ y \text{ is free and unlabeled } \ \textbf{then}\ \text{augment along } y , P( x ); \ \textbf{return true} \cr 10 & \qquad \textbf{if}\ y \text{ is unlabeled }\ \textbf{then} \ y \text{ odd};\ p[ y ] \gets x;\ \text{append } M( y ) \text{ to } Q \text{, even} \cr 11 & \qquad \textbf{else if}\ B( x ) \ne B( y )\ \textbf{then} \cr 12 & \qquad\quad b \gets \text{the first common base on the paths } \text{from } B( x ) \text{ and } B( y ) \text{ to } r \cr 13 & \qquad\quad \mathrm{shrink}( x , y , b );\ \mathrm{shrink}( y , x , b ) \cr 14 & \quad \textbf{return false} \cr 15 & \textbf{procedure}\ \mathrm{shrink}( x , y , b ) \cr 16 & \quad v \gets B( x ) \cr 17 & \quad \textbf{while}\ v \ne b\ \textbf{do} \cr 18 & \qquad o \gets M( v );\ o \text{ even, with the edge } ( x , y ); \ \text{append } o \text{ to } Q \cr 19 & \qquad v' \gets B( p[ o ] );\ \text{merge the blossoms of } v \text{ and } o \text{ into that of } b;\ v \gets v' \end{array}} \]
The path \( P( v ) \) from an even node to the root is \( v , M( v ) , P( p[ M( v ) ] ) \) for a node even from the start, and the part of \( P( a ) \) from \( v \) back to \( a \) followed by \( P( b ) \) for a node that was odd, with the edge \( ( a , b ) \); the augmentation rematches it with a stack of the pairs (node, new mate), stopping each part where a node has a new mate already:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{augment}( x , y ) \cr 2 & \quad M( y ) \gets x;\ K \gets ( ( x , y ) ) \cr 3 & \quad \textbf{while}\ K \ne ()\ \textbf{do} \cr 4 & \qquad ( v , w ) \gets \text{pop } K \cr 5 & \qquad \textbf{loop} \cr 6 & \qquad\quad o \gets M( v );\ M( v ) \gets w \cr 7 & \qquad\quad \textbf{if}\ o = \textbf{nil}\ \textbf{or} \ M( o ) \ne v\ \textbf{then break}\quad \triangleright \ \text{the root, or a part done} \cr 8 & \qquad\quad \textbf{if}\ v \text{ is even from the start} \ \textbf{then}\ M( o ) \gets p[ o ];\ w \gets o; \ v \gets p[ o ] \cr 9 & \qquad\quad \textbf{else}\ ( a , b ) \gets \text{the edge that} \text{ made } v \text{ even, } a \text{ on its side}; \ \text{push } ( b , a ) \text{ on } K;\ w \gets b;\ v \gets a \end{array}} \]
| std::pair< W, std::vector< std::size_t > > ADS::maxWeightMatching | ( | int | n, |
| const std::vector< std::tuple< int, int, W > > & | E, | ||
| bool | maxCardinality = false ) |
the largest total weight of a matching of the graph of \( n \) nodes and of the edges \( E = ( u , v , w ) \), or, if maxCardinality, the largest weight of a matching of largest size, and the places in \( E \) of its edges, in increasing order
In pseudocode (the primal-dual algorithm of Edmonds in the form of Galil, beyond the book; see Flows, cuts and matchings): \( y_v \) is the dual of a node, \( z_B \) that of a blossom, \( \pi( e ) \) the slack of an edge, \( y_u + y_v - 2 w( e ) \) plus \( 2 z_B \) for each blossom that holds both ends (the duals doubled), and \( S \) and \( T \) are the outer and the inner blossoms of the trees.
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maxWeightMatching}( n , E ) \cr 2 & \quad M \gets \emptyset;\ y_v \gets \max\{ 0 , \max_e w( e ) \} \ \forall v \cr 3 & \quad \textbf{repeat}\quad \triangleright\ \text{a stage} \cr 4 & \qquad \text{every free node is the root of a tree, label } S \cr 5 & \qquad \textbf{loop} \cr 6 & \qquad\quad \textbf{foreach}\ \text{tight edge } ( u , v ) \text{ from an } S\text{-node } u \text{ in another blossom} \ \textbf{do} \cr 7 & \qquad\qquad \textbf{if}\ v \text{ is unlabeled }\ \textbf{then} \ \text{label its blossom } T \text{ and the mate of its base } S \cr 8 & \qquad\qquad \textbf{else if}\ v \text{ is } S \text{ in the } \text{same tree }\ \textbf{then}\ \text{shrink the blossom, } S \cr 9 & \qquad\qquad \textbf{else if}\ v \text{ is } S\ \textbf{then} \ \text{augment } M \text{ along the two paths};\ \textbf{break} \text{ the stage} \cr 10 & \qquad\quad \delta \gets \min\{ \min_S y_v , \min \pi( S , \text{free} ) , \min \pi( S , S ) / 2 , \min_T z_B \} \cr 11 & \qquad\quad y_v \gets y_v \mp \delta \text{ on } S \text{ and } T \text{ nodes};\ z_B \gets z_B \pm \delta \text{ on } S \text{ and } T \text{ blossoms} \cr 12 & \qquad\quad \textbf{if}\ \delta = \min_S y_v\ \textbf{then} \ \textbf{return}\ M\quad \triangleright\ \text{optimal} \cr 13 & \qquad\quad \textbf{if}\ \delta = z_B \text{ for a } T \text{-blossom } B\ \textbf{then}\ \text{expand } B \cr 14 & \qquad \text{expand the } S\text{-blossoms with } z_B = 0 \cr 15 & \quad \textbf{until}\ \text{a stage does not augment} \end{array}} \]
With maxCardinality the first term of \( \delta \) is left out, and a stage that finds no augmenting path ends the search. The edges of weight at most 0 are left out of the matching of largest weight, which they cannot make heavier.