ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Matching.h File Reference

Matchings, the sets of edges of a graph no two of which share an end beyond the capacities of the ends: More...

#include "ADS/Contract.h"
#include "ADS/graph/Graph.h"
#include "ADS/graph/MaxFlow.h"
#include "ADS/graph/Visit.h"
#include <algorithm>
#include <cstddef>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/graph/impl/Matching.tpp"
#include "ADS/graph/BlossomMatching.h"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

std::vector< std::pair< int, int > > ADS::bipartiteBMatching (const std::vector< std::vector< int > > &A, int nr, const std::vector< long long > &capL, const std::vector< long long > &capR)
 the pairs \( ( u , v ) \) of a \( b \)-matching of largest size, \( A[ u ] \) the nodes on the right adjacent to the node \( u \) on the left, \( u \) in at most \( \mathit{capL}[ u ] \) pairs and \( v \) in at most \( \mathit{capR}[ v ] \)
template<class W>
std::pair< W, std::vector< std::size_t > > ADS::branchAndBoundMatching (int n, const std::vector< std::tuple< int, int, W > > &E)
 the largest total weight of a matching of the graph of \( n \) nodes and of the edges \( E = ( u , v , w ) \) by a branch and bound, and the places in \( E \) of its edges, in increasing order

Detailed description

Matchings, the sets of edges of a graph no two of which share an end beyond the capacities of the ends:

  • bipartiteBMatching(), the b-matching of largest size of a bipartite graph, each node u on the left in at most capL[ u ] pairs and each v on the right in at most capR[ v ], each edge used once: a maximum flow of section 15.2 from a source joined to each u by capL[ u ], through the edges of capacity 1, to a sink joined from each v by capR[ v ]. The flow is integral, and its edges of flow 1 between the two sides are the pairs; with DINIC of MaxFlow.h it takes \( O( N^2 M ) \) for the N = n + 2 nodes and the M = m + n edges of the network, and less on such layered networks. With all the capacities 1 it is the bipartiteMatching() of EasyCases.h, for which the algorithm of Hopcroft and Karp takes \( O( m \sqrt{n} ) \); copying each node as many times as its capacity reduces the general case to it, and Gabow shows how to do so without the blow up of the copies. It decides, e.g., whether the tasks can all be given to workers who can do them, each worker taking a bounded number of them;
  • branchAndBoundMatching(), the matching of largest weight of a graph of a few nodes by a branch and bound: the first node not yet decided is left unmatched or matched to each free neighbor, and a branch whose weight plus half the best weight at each node left cannot beat the best matching found is cut. It takes time proportional to the number of matchings of the graph in the worst case, the telephone numbers \( \sum_k \binom{n}{2k} ( 2k - 1 )!! \) on the complete graph, i.e., it suits graphs of some tens of nodes; it is kept as an independent check of the blossoms, which share none of its code.

The matchings of largest size and of largest weight of a general graph, maxCardinalityMatching() and maxWeightMatching() by the blossoms of Edmonds (beyond the book), are in BlossomMatching.h, which this header includes, so that an include of either gives all the matchings.

The contracts [see Contract.h]: the nodes on the right of the edges are in [ 0 , nr ), the capacities are not negative, an edge joins two distinct nodes of the graph; at the level of the audits the matchings given respect the capacities of the ends. The functions are defined in Matching.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 15.2, https://algoritmicamente.it/materiali/libro/capitoli/cap15.pdf
J. E. Hopcroft, R. M. Karp, An n^{5/2} algorithm for maximum matchings in bipartite graphs, SIAM Journal on Computing 2(4), 225-231, 1973, doi:10.1137/0202019
H. N. Gabow, An efficient reduction technique for degree-constrained subgraph and bidirected network flow problems, Proceedings of the 15th ACM Symposium on Theory of Computing, 448-456, 1983, doi:10.1145/800061.808776
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa