Introduction
A matching, or independent edge set, is a set of edges in a graph that have no common endpoints. Graph matching algorithms are commonly used in informatics competitions and can be broadly divided into maximum matching and maximum-weight matching. Because matching in a bipartite graph is equivalent to a network-flow problem and has well-behaved properties, making it relatively easy to handle, this article first introduces the two types of algorithms for bipartite graphs and then discusses algorithms for general graphs.
Graph matching
Let be an undirected graph, where is the vertex set and is the edge set. If a set of edges contains no self-loops and no two of its edges share a vertex, then the edge set is called a matching (or independent edge set) of graph . An edge is called a matched edge if it appears in matching , and an unmatched edge otherwise. Correspondingly, a vertex is called a matched vertex if it is an endpoint of a matched edge, and an unmatched vertex otherwise.
The size of matching is the number of edges it contains. For matchings in (weighted) undirected graphs, the following concepts are often considered:
-
Maximal matching: a matching to which no more matched edges can be added. A maximal matching is not necessarily a maximum matching.
maximal matching -
Maximum matching (or maximum cardinality matching): a matching with the largest number of matched edges. There may be more than one maximum matching, but the number of edges in a maximum matching is fixed, and it cannot exceed half the number of vertices in the graph.
maximum cardinality matching -
Maximum-weight matching: a matching in a weighted graph whose sum of edge weights is largest.
maximum weight matching -
Maximum-weight maximum-cardinality matching: among matchings with the largest possible cardinality, one whose sum of edge weights is largest; that is, the maximum-weight matching among all maximum matchings.
maximum weight maximum cardinality matching -
Perfect matching: a matching in which every vertex is matched. A perfect matching is necessarily a maximum matching. A complete graph with an even number of vertices necessarily has a perfect matching.
-
Near-perfect matching: a matching with exactly one unmatched vertex. This can happen only when the graph has an odd number of vertices. A near-perfect matching is also necessarily a maximum matching. A complete graph with an odd number of vertices necessarily has a near-perfect matching.
The graph matching problems encountered in algorithmic competitions mainly refer to maximum matching and maximum-weight matching.
Augmenting paths
In graph matching algorithms, an augmenting path is the core structure used to improve a matching.
Definition
For a graph and one of its matchings , the following two types of (simple) paths can be defined:
- An alternating path is a path made up of matching edges and non-matching edges in alternation;
- An augmenting path is an alternating path that starts at an unmatched vertex and ends at an unmatched vertex.
Because an augmenting path contains more non-matching edge than matching edge, the number of its edges is necessarily odd. If the matching and non-matching edges on an augmenting path are swapped, it remains an alternating path, and the number of matched edges increases by . The process of finding an augmenting path and swapping its edges to increase the size of a matching is called augmentation. In mathematical terms, augmentation takes the symmetric difference of matching and augmenting path , yielding a new matching .
The figure below shows how one augmentation increases the number of matched edges from to .

Berge's lemma
Berge's lemma states that improving a matching by using augmenting paths is sufficient. In other words, when no augmenting path can be found, the matching obtained is maximum.
Berge's lemma
For a graph and one of its matchings , matching is a maximum matching if and only if there is no augmenting path with respect to matching .
Proof
As explained above, if an augmenting path exists, then matching is a larger matching than , so cannot be a maximum matching.
Conversely, we need to show that if there is a matching larger than matching , denoted by , then, with respect to , there must be an augmenting path . Consider the symmetric difference . Every vertex in graph has degree , , or ; therefore, every connected component of this graph must be a path, a cycle, or an isolated vertex. Moreover, the two edges incident to a vertex of degree must come from different matchings, so each such cycle contains the same number of edges from and . Since is larger than , at least one path contains more edges from than from ; call this path . The two endpoints of are unmatched vertices of , and is an alternating path with respect to , so must be an augmenting path with respect to . This completes the proof.
This theorem gives the central idea for finding a maximum matching:
- Enumerate all unmatched vertices and find augmenting paths until no augmenting path can be found.
In fact, after each augmentation, it is unnecessary to traverse all unmatched vertices again. During the entire process of finding a maximum matching, each vertex needs to be traversed only once.
Proof
It suffices to show that if, when the enumeration reaches vertex , there is no augmenting path starting at , then after any number of augmentations there is still no augmenting path starting at . This shows that even though augmentation changes the matching, previously enumerated unmatched vertices need not be checked again.
Suppose otherwise. That is, suppose is an unmatched vertex that has already been enumerated, and after augmenting along an augmenting path from to , denoted by in some round, a new augmenting path starting at , denoted by appears. Then must share an edge with ; otherwise, augmenting along would not change the matching status of any edge in , so could not have appeared as a result of this augmentation.
(In the figure, black denotes non-matching edges, while red and blue denote different matching states.)
Let be the first vertex reached from along , and let it be a vertex on . Since an alternating path from to already existed before this augmentation, must have been a matched vertex, so it cannot be either or . Therefore, on augmenting path , the two edges adjacent to have opposite matching states. This means that regardless of the matching state of the edge, an alternating path from to can be extended along to one of and . Thus, an augmenting path starting at already existed before the augmentation, contradicting the assumption.
Alternating trees
Another concept closely related to augmenting paths is the alternating tree. It is a tree produced while performing DFS or BFS from an unmatched vertex to search for an augmenting path.
For a graph and one of its matchings , if a subgraph is a tree rooted at an unmatched vertex , and the path connecting to any is an alternating path, then is called an alternating tree. A vertex at even depth in the tree is called an even vertex, and a vertex at odd depth is called an odd vertex.
The figure below shows an alternating tree that may be obtained by starting BFS from unmatched vertex . (In the figure, red edges are matching edges and black edges are non-matching edges; dark vertices are matched vertices and light vertices are unmatched vertices.)
Existence of perfect matchings
In graph matching theory, there are two important existence theorems that can be used to determine whether a perfect matching exists in a bipartite graph or a general graph.
Hall's theorem
Suppose is a bipartite graph and . For graph , let be a matching; if every vertex in is matched, then is called an -perfect matching, sometimes simply a perfect matching (of bipartite graph ). This is the largest matching that can be achieved in a bipartite graph. Hall's theorem gives a necessary and sufficient condition for such a matching to exist.
Hall's theorem says that for every subset of , there are enough vertices in to match it, and therefore an -perfect matching exists.
Hall's theorem
Suppose is a bipartite graph and . For any , let be the set of all vertices in graph adjacent to vertices in . Then an -perfect matching exists if and only if holds for every .
Proof
The condition is clearly necessary. Suppose an -perfect matching exists. Every vertex in is matched to a different vertex in . The set contains at least the vertices matched to vertices in , so its size is at least .
The condition is also sufficient. Suppose no -perfect matching exists. Then there must be a maximum matching in which a vertex remains unmatched. Let be the set of all vertices reachable from by alternating paths, and let and . Every vertex in must be matched; otherwise, an odd cycle would arise, contradicting the fact that is bipartite. Every vertex in must also be matched; otherwise, there would be an augmenting path, which by Berge's lemma would contradict the maximality of . Since all these vertices are matched, and matching can occur only between and , the vertices in correspond one-to-one to the vertices in , so . At the same time, every vertex in is matched to a vertex in , so at least . But contains no unmatched vertex : if it were adjacent to a vertex in , namely , the alternating path reaching could necessarily be extended to an alternating path reaching . Thus . These arguments show that , contradicting the condition of Hall's theorem. Therefore an -perfect matching exists.
Corollary
Every regular bipartite graph has a perfect matching.
Proof
In a regular bipartite graph, all vertices have the same degree, say . First verify that Hall's condition holds: for every , we have . The number of edges incident to vertices in is , while each vertex in can be incident to at most of these edges, so necessarily , that is, . In particular, ; since and are symmetric, . Therefore, in a regular bipartite graph, an -perfect matching is also a perfect matching. Since Hall's theorem guarantees the existence of an -perfect matching, a perfect matching must exist as well.
Tutte's theorem
Tutte's theorem gives a necessary and sufficient condition for the existence of a perfect matching in a general graph. This condition follows from a direct observation: a graph with an odd number of vertices cannot have a perfect matching.
Tutte's theorem
A graph has a perfect matching if and only if for every , , where is the subgraph obtained by deleting from graph the vertices in and the edges incident to them, and is the number of connected components of subgraph that have an odd number of vertices.
Proof
It suffices to consider simple graphs, because multiple edges and self-loops do not affect Tutte's condition or the existence of a perfect matching.
The necessity of the condition is relatively easy. Suppose a perfect matching exists. For any , after deleting from graph the vertices in , every connected component with an odd number of vertices has at least one vertex that cannot be matched with a vertex in the same connected component; these vertices can only seek matches with vertices in . The existence of such a matching requires at least . This is Tutte's condition.
The sufficiency of the condition is more complicated. Suppose satisfies Tutte's condition but has no perfect matching. Since adding any edge to preserves Tutte's condition, without loss of generality assume that is a maximal graph with this property: has no perfect matching, but adding to graph any edge that is not already present makes have a perfect matching. Let be the set of all vertices of degree . It can be proved that every connected component of is a complete graph. We can then construct a perfect matching of : first take a maximum matching in each connected component of ; this leaves an unmatched vertex only when the component has an odd number of vertices. Match these unmatched vertices to vertices in . Since has an even number of vertices (by taking in Tutte's condition), the number of as-yet-unmatched vertices remaining in is also even, so pair them up. This contradiction shows that there is no graph satisfying Tutte's condition but lacking a perfect matching.
The key is to prove that every connected component of is a complete graph. Suppose otherwise. Without loss of generality, let vertices belong to such a connected component, with , , and . Moreover, since , there must be some such that . By the maximality of , graphs and have perfect matchings and , respectively. Consider their symmetric difference . Every vertex in graph has degree either or , so is in fact a disjoint union of several even cycles, each consisting of alternating matching edges from and .
As shown in the figure, there are two cases:
- and lie on different cycles (as shown on the left): let the cycle containing be . Then the edge set is a perfect matching of graph ;
- and lie on the same cycle (as shown on the right): by symmetry, assume the cycle passes through in that order. Thus, take the path on the cycle from through to and denote it by , and let be the cycle . Then the edge set is likewise a perfect matching of graph .
In either case, this contradicts the choice of . The contradiction proves that every connected component of is a complete graph.
Corollary
Every bridgeless 3-regular graph has a perfect matching.
Proof
To verify Tutte's condition, take any and prove that . Let be all connected components of with an odd number of vertices. Let be the number of edges connecting vertices in to vertices in . Simple counting gives
Thus must be odd. Since has no bridge (that is, no cut edge), . Therefore
Thus Tutte's condition holds, and graph necessarily has a perfect matching.
Common algorithms
A basic problem in combinatorial optimization is to find a maximum matching and a maximum-weight matching of a graph.
Maximum matching in bipartite graphs
See the Maximum matching in bipartite graphs page for details.
In an unweighted bipartite graph, the Kuhn algorithm solves the problem in time, while the Hopcroft–Karp algorithm solves it in time.
Maximum-weight matching in bipartite graphs
See the Maximum-weight matching in bipartite graphs page for details.
In a weighted bipartite graph, the Hungarian algorithm can be used. If the Bellman–Ford algorithm is used when finding shortest paths, the time complexity is ; with Dijkstra's algorithm or a Fibonacci heap, the problem can be solved in time.
Maximum matching in general graphs
See the Maximum matching in general graphs page for details.
In an unweighted general graph, Edmonds' blossom algorithm solves the problem in time.
Maximum-weight matching in general graphs
See the Maximum-weight matching in general graphs page for details.
In a weighted general graph, Edmonds' blossom algorithm solves the problem in time.
Related problems
Maximum (weight) matching is closely related to other graph-theoretic problems. This section discusses only general graphs; for results concerning bipartite graphs, see the Maximum matching in bipartite graphs page.
Maximum-weight maximum-cardinality matching
The maximum-weight maximum-cardinality matching problem and the maximum-weight matching problem can be reduced to each other. One significant difference between them is that negative-weight edges may occur in maximum-weight maximum-cardinality matching, whereas maximum-weight matching never uses negative-weight edges.
First, the maximum-weight matching problem can be reduced to the maximum-weight maximum-cardinality matching problem. First, set the weights of all negative-weight edges in graph to ; then, by adding several edges of weight , extend the graph to a complete graph . Notice that in a complete graph with nonnegative edge weights, maximum-weight maximum-cardinality matching and maximum-weight matching coincide. Therefore, it suffices to compute the maximum-weight maximum-cardinality matching of , denoted by , then delete all zero-weight edges from ; the resulting edge set is a maximum-weight matching of graph .1
Conversely, the maximum-weight maximum-cardinality matching problem can also be reduced to the maximum-weight matching problem. Add to every edge of graph a sufficiently large positive number . This ensures that a maximum-weight matching of the resulting graph is also a maximum matching, and hence necessarily a maximum-weight maximum-cardinality matching. This is because finding a maximum-weight matching of graph is equivalent to maximizing over all matchings of graph :
When is sufficiently large, the gain from matching one additional edge exceeds the change in the weight sum of the latter term. Therefore, the algorithm first matches as many edges as possible and only then maximizes the sum of the weights of matched edges. The choice of constant only needs to ensure that it is strictly greater than the difference between the values of any two possible matchings. An obvious choice is
Minimum (weight) edge cover
Another problem closely related to maximum (weight) matching is minimum (weight) edge cover. The relationship between edge cover and matching (also called an independent edge set) is similar to the relationship between vertex cover and independent set.
In graph , a set of edges , if every vertex is an endpoint of an edge in , then is called an edge cover of graph . When discussing edge covers, we always assume that graph has no isolated vertices.
For an unweighted graph, the minimum edge cover problem is almost exactly the maximum matching problem. For graph , take any maximum matching , adding one incident edge for every unmatched vertex yields a minimum edge cover . Their sizes satisfy the simple relation . The figure below shows some examples of minimum edge covers:
For a weighted graph, the minimum-weight edge cover problem can be reduced to a minimum-weight perfect matching problem. First, copy graph to obtain , with the same edge weights; then connect every vertex to its copy , assigning it the minimum weight among the edges incident in graph to . Denote the resulting graph by . If graph is bipartite or sparse, then graph is likewise bipartite or sparse, respectively. Moreover, the minimum-weight edge cover problem of graph reduces to the minimum-weight perfect matching problem of graph 2: for graph , let be a minimum-weight perfect matching; keep only the edges in , and replace every matched with an edge of minimum weight in graph incident to ; the result is a minimum-weight edge cover of graph .
References
- Wikiwand - Matching (graph theory)
- Wikiwand - Blossom algorithm
- 2015, "A Brief Discussion of Graph Matching Algorithms and Their Applications" — Chen Yinbo
- Algorithm Notes - Matching
- the-tourist/algo
- Bill Yang's Blog - Study Notes on the Blossom Algorithm
- Maximum matching, perfect matching, and the Hungarian algorithm in bipartite graphs
- Wikiwand - Hopcroft–Karp algorithm
- Bondy, John Adrian, and Uppaluri Siva Ramachandra Murty. Graph theory with applications. Vol. 290. London: Macmillan, 1976.
Footnotes
-
Of course, this is not the only way to perform the reduction. For graph , one can instead connect a copy point by point to the original graph and set the weights of all edges newly added with respect to the original graph (including edges in the copy) to , obtaining graph . In other words, the vertex set of new graph is , and its edge set contains, in addition to the edges of graph , zero-weight edges connecting every vertex to its copy , and, for every edge , zero-weight edges connecting and . Every matching in graph , denoted by , corresponds to a perfect matching of the same total weight in graph : simply match the unmatched vertices of graph : each vertex with its copy , and for every matched edge , match with . Thus, the maximum-weight maximum-cardinality matching in graph , which is also a maximum-weight perfect matching, restricted to , yields a maximum-weight matching in graph . The advantage of this reduction is that if graph is bipartite or sparse, the expanded graph is also bipartite or sparse, respectively. ↩
-
For graph , every perfect matching can be used in the manner described here to obtain an edge cover of graph , denoted by , whose total weight is half that of the former; conversely, reversing this construction, for every edge cover of graph , denoted by , one can construct a perfect matching of graph , denoted by , whose total weight is no more than twice that of the former. This shows that the reduction is valid. ↩