Graph matching

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 G=(V,E)G=(V,E) be an undirected graph, where VV is the vertex set and EE is the edge set. If a set of edges M⊆EM\subseteq E contains no self-loops and no two of its edges share a vertex, then the edge set MM is called a matching (or independent edge set) of graph GG. An edge e∈Ee\in E is called a matched edge if it appears in matching MM, and an unmatched edge otherwise. Correspondingly, a vertex v∈Vv\in V is called a matched vertex if it is an endpoint of a matched edge, and an unmatched vertex otherwise.

The size of matching MM 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
    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 cardinality matching
  • Maximum-weight matching: a matching in a weighted graph whose sum of edge weights is largest.

    maximum weight matching
    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
    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 G=(V,E)G=(V,E) and one of its matchings MM, 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 11 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 11. 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 MM and augmenting path PP, yielding a new matching M⊕PM\oplus P.

The figure below shows how one augmentation increases the number of matched edges from 22 to 33.

augment-1
augment-1

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 G=(V,E)G=(V,E) and one of its matchings MM, matching MM is a maximum matching if and only if there is no augmenting path with respect to matching MM.

Proof

As explained above, if an augmenting path PP exists, then matching M⊕PM\oplus P is a larger matching than MM, so MM cannot be a maximum matching.

Conversely, we need to show that if there is a matching larger than matching MM, denoted by M′M', then, with respect to MM, there must be an augmenting path PP. Consider the symmetric difference M⊕M′M\oplus M'. Every vertex in graph (V,M⊕M′)(V,M\oplus M') has degree 00, 11, or 22; 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 22 must come from different matchings, so each such cycle contains the same number of edges from MM and M′M'. Since M′M' is larger than MM, at least one path contains more edges from M′M' than from MM; call this path PP. The two endpoints of PP are unmatched vertices of MM, and PP is an alternating path with respect to MM, so PP must be an augmenting path with respect to MM. 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 vv, there is no augmenting path starting at vv, then after any number of augmentations there is still no augmenting path starting at vv. This shows that even though augmentation changes the matching, previously enumerated unmatched vertices need not be checked again.

Suppose otherwise. That is, suppose vv is an unmatched vertex that has already been enumerated, and after augmenting along an augmenting path from uu to ww, denoted by PP in some round, a new augmenting path starting at vv, denoted by P′P' appears. Then P′P' must share an edge with PP; otherwise, augmenting along PP would not change the matching status of any edge in P′P', so P′P' could not have appeared as a result of this augmentation.

augment-2
augment-2

(In the figure, black denotes non-matching edges, while red and blue denote different matching states.)

Let xx be the first vertex reached from vv along P′P', and let it be a vertex on PP. Since an alternating path from vv to xx already existed before this augmentation, xx must have been a matched vertex, so it cannot be either uu or ww. Therefore, on augmenting path PP, the two edges adjacent to xx have opposite matching states. This means that regardless of the matching state of the edge, an alternating path from vv to xx can be extended along PP to one of uu and ww. Thus, an augmenting path starting at vv 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 rr to search for an augmenting path.

For a graph G=(V,E)G=(V,E) and one of its matchings MM, if a subgraph H⊆GH\subseteq G is a tree rooted at an unmatched vertex rr, and the path connecting rr to any v∈Hv\in H is an alternating path, then HH 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 11. (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.)

Figure: alternating tree

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 G=(X,Y,E)G=(X,Y,E) is a bipartite graph and ∣X∣≤∣Y∣|X|\le |Y|. For graph GG, let MM be a matching; if every vertex in XX is matched, then MM is called an XX-perfect matching, sometimes simply a perfect matching (of bipartite graph GG). 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 XX, there are enough vertices in YY to match it, and therefore an XX-perfect matching exists.

Hall's theorem

Suppose G=(X,Y,E)G=(X,Y,E) is a bipartite graph and ∣X∣≤∣Y∣|X|\le |Y|. For any W⊆XW\subseteq X, let NG(W)N_G(W) be the set of all vertices in graph GG adjacent to vertices in WW. Then an XX-perfect matching exists if and only if ∣W∣≤∣NG(W)∣|W|\le |N_G(W)| holds for every W⊆XW\subseteq X.

Proof

The condition is clearly necessary. Suppose an XX-perfect matching MM exists. Every vertex in XX is matched to a different vertex in YY. The set NG(W)N_G(W) contains at least the vertices matched to vertices in WW, so its size is at least ∣W∣|W|.

The condition is also sufficient. Suppose no XX-perfect matching exists. Then there must be a maximum matching MM in which a vertex v∈Xv\in X remains unmatched. Let ZZ be the set of all vertices reachable from vv by alternating paths, and let S=Z∩XS=Z\cap X and T=Z∩YT=Z\cap Y. Every vertex in S∖{v}S\setminus\{v\} must be matched; otherwise, an odd cycle would arise, contradicting the fact that GG is bipartite. Every vertex in TT must also be matched; otherwise, there would be an augmenting path, which by Berge's lemma would contradict the maximality of MM. Since all these vertices are matched, and matching can occur only between XX and YY, the vertices in S∖{v}S\setminus\{v\} correspond one-to-one to the vertices in TT, so ∣T∣=∣S∣−1|T|=|S|-1. At the same time, every vertex in TT is matched to a vertex in SS, so at least T⊆NG(S)T\subseteq N_G(S). But NG(S)N_G(S) contains no unmatched vertex uu: if it were adjacent to a vertex in SS, namely v′v', the alternating path reaching v′v' could necessarily be extended to an alternating path reaching uu. Thus T=NG(S)T=N_G(S). These arguments show that ∣NG(S)∣<∣S∣|N_G(S)|<|S|, contradicting the condition of Hall's theorem. Therefore an XX-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 kk. First verify that Hall's condition holds: for every W⊆XW\subseteq X, we have ∣NG(W)∣≥∣W∣|N_G(W)|\ge |W|. The number of edges incident to vertices in WW is k∣W∣k|W|, while each vertex in NG(W)N_G(W) can be incident to at most kk of these edges, so necessarily k∣W∣≤k∣NG(W)∣k|W|\le k|N_G(W)|, that is, ∣W∣≤∣NG(W)∣|W|\le |N_G(W)|. In particular, ∣X∣≤∣Y∣|X|\le |Y|; since XX and YY are symmetric, ∣X∣=∣Y∣|X|=|Y|. Therefore, in a regular bipartite graph, an XX-perfect matching is also a perfect matching. Since Hall's theorem guarantees the existence of an XX-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 G=(V,E)G=(V,E) has a perfect matching if and only if for every U⊆VU\subseteq V, odd⁡(G−U)≤∣U∣\operatorname{odd}(G-U)\le |U|, where G−UG-U is the subgraph obtained by deleting from graph GG the vertices in UU and the edges incident to them, and odd⁡(G−U)\operatorname{odd}(G-U) is the number of connected components of subgraph G−UG-U 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 MM exists. For any U⊆VU\subseteq V, after deleting from graph GG the vertices in UU, 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 UU. The existence of such a matching requires at least odd⁡(G−U)≤∣U∣\operatorname{odd}(G-U)\le |U|. This is Tutte's condition.

The sufficiency of the condition is more complicated. Suppose GG satisfies Tutte's condition but has no perfect matching. Since adding any edge to GG preserves Tutte's condition, without loss of generality assume that GG is a maximal graph with this property: GG has no perfect matching, but adding to graph GG any edge ee that is not already present makes G+eG+e have a perfect matching. Let U⊆VU\subseteq V be the set of all vertices of degree ∣V∣−1|V|-1. It can be proved that every connected component of G−UG-U is a complete graph. We can then construct a perfect matching of GG: first take a maximum matching in each connected component of G−UG-U; this leaves an unmatched vertex only when the component has an odd number of vertices. Match these unmatched vertices to vertices in UU. Since GG has an even number of vertices (by taking U=∅U=\varnothing in Tutte's condition), the number of as-yet-unmatched vertices remaining in UU is also even, so pair them up. This contradiction shows that there is no graph GG satisfying Tutte's condition but lacking a perfect matching.

The key is to prove that every connected component of G−UG-U is a complete graph. Suppose otherwise. Without loss of generality, let vertices x,y,zx,y,z belong to such a connected component, with (x,y)∈E(x,y)\in E, (y,z)∈E(y,z)\in E, and (x,z)∉E(x,z)\notin E. Moreover, since y∉Uy\notin U, there must be some w∈V∖Uw\in V\setminus U such that (y,w)∉E(y,w)\notin E. By the maximality of GG, graphs G+(x,z)G+(x,z) and G+(y,w)G+(y,w) have perfect matchings M1M_1 and M2M_2, respectively. Consider their symmetric difference M1⊕M2M_1\oplus M_2. Every vertex in graph (V,M1⊕M2)(V,M_1\oplus M_2) has degree either 00 or 22, so M1⊕M2M_1\oplus M_2 is in fact a disjoint union of several even cycles, each consisting of alternating matching edges from M1M_1 and M2M_2.

Figure: tutte proof

As shown in the figure, there are two cases:

  • (x,z)(x,z) and (y,w)(y,w) lie on different cycles (as shown on the left): let the cycle containing (y,w)(y,w) be CC. Then the edge set M2⊕CM_2\oplus C is a perfect matching of graph GG;
  • (x,z)(x,z) and (y,w)(y,w) lie on the same cycle (as shown on the right): by symmetry, assume the cycle passes through x,y,w,zx,y,w,z in that order. Thus, take the path on the cycle from yy through ww to zz and denote it by PP, and let {(y,z)}∪P\{(y,z)\}\cup P be the cycle CC. Then the edge set M2⊕CM_2\oplus C is likewise a perfect matching of graph GG.

In either case, this contradicts the choice of GG. The contradiction proves that every connected component of G−UG-U is a complete graph.

Corollary

Every bridgeless 3-regular graph has a perfect matching.

Proof

To verify Tutte's condition, take any U⊆VU\subseteq V and prove that odd⁡(G−U)≤∣U∣\operatorname{odd}(G-U)\le |U|. Let G1,⋯ ,GnG_1,\cdots,G_n be all connected components of G−UG-U with an odd number of vertices. Let mim_i be the number of edges connecting vertices in GiG_i to vertices in UU. Simple counting gives

3∣V(Gi)∣=∑v∈V(Gi)d(v)=2∣E(Gi)∣+mi.3|V(G_i)| = \sum_{v\in V(G_i)} d(v) = 2|E(G_i)| + m_i.

Thus mim_i must be odd. Since GG has no bridge (that is, no cut edge), mi≥3m_i\ge 3. Therefore

odd⁡(G−U)=n≤13∑i=1nmi≤13∑v∈Ud(v)=∣U∣.\operatorname{odd}(G-U) = n \le \dfrac{1}{3}\sum_{i=1}^n m_i \le \dfrac{1}{3}\sum_{v\in U} d(v) = |U|.

Thus Tutte's condition holds, and graph GG 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 O(∣V∣∣E∣)O(|V||E|) time, while the Hopcroft–Karp algorithm solves it in O(∣V∣1/2∣E∣)O(|V|^{1/2}|E|) 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 O(∣V∣2∣E∣)O(|V|^2|E|); with Dijkstra's algorithm or a Fibonacci heap, the problem can be solved in O(∣V∣2log⁡∣V∣+∣V∣∣E∣)O(|V|^{2}\log {|V|}+|V||E|) 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 O(∣V∣2∣E∣)O(|V|^2|E|) 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 O(∣V∣2∣E∣)O(|V|^2|E|) time.

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 GG to 00; then, by adding several edges of weight 00, extend the graph to a complete graph G′G'. 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 G′G', denoted by M′M', then delete all zero-weight edges from M′M'; the resulting edge set MM is a maximum-weight matching of graph GG.1

graph-match
graph-match

Conversely, the maximum-weight maximum-cardinality matching problem can also be reduced to the maximum-weight matching problem. Add to every edge of graph GG a sufficiently large positive number KK. This ensures that a maximum-weight matching of the resulting graph G′G' is also a maximum matching, and hence necessarily a maximum-weight maximum-cardinality matching. This is because finding a maximum-weight matching of graph G′G' is equivalent to maximizing over all matchings of graph GG:

K∣M∣+∑e∈Mw(e).K|M| + \sum_{e\in M}w(e).

When KK is sufficiently large, the gain KK 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 KK only needs to ensure that it is strictly greater than the difference between the values of any two possible matchings. An obvious choice is

K=∑e∈E∣w(e)∣+1.K = \sum_{e\in E}|w(e)| + 1.
graph-match
graph-match

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 G=(V,E)G=(V,E), a set of edges C⊆EC\subseteq E, if every vertex v∈Vv\in V is an endpoint of an edge in CC, then CC is called an edge cover of graph GG. When discussing edge covers, we always assume that graph GG has no isolated vertices.

For an unweighted graph, the minimum edge cover problem is almost exactly the maximum matching problem. For graph GG, take any maximum matching MM, adding one incident edge for every unmatched vertex yields a minimum edge cover CC. Their sizes satisfy the simple relation ∣M∣+∣C∣=∣V∣|M|+|C|=|V|. The figure below shows some examples of minimum edge covers:

graph-match
graph-match

For a weighted graph, the minimum-weight edge cover problem can be reduced to a minimum-weight perfect matching problem. First, copy graph G=(V,E)G=(V,E) to obtain G~=(V~,E~)\tilde G=(\tilde V,\tilde E), with the same edge weights; then connect every vertex v∈Vv\in V to its copy v~∈V~\tilde v\in\tilde V, assigning it the minimum weight among the edges incident in graph GG to vv. Denote the resulting graph by G′=(V′,E′)G'=(V',E'). If graph GG is bipartite or sparse, then graph G′G' is likewise bipartite or sparse, respectively. Moreover, the minimum-weight edge cover problem of graph GG reduces to the minimum-weight perfect matching problem of graph G′G'2: for graph G′G', let M′M' be a minimum-weight perfect matching; keep only the edges in EE, and replace every matched (v,v′)(v,v') with an edge of minimum weight in graph GG incident to vv; the result is a minimum-weight edge cover of graph GG.

References

  1. Wikiwand - Matching (graph theory)
  2. Wikiwand - Blossom algorithm
  3. 2015, "A Brief Discussion of Graph Matching Algorithms and Their Applications" — Chen Yinbo
  4. Algorithm Notes - Matching
  5. the-tourist/algo
  6. Bill Yang's Blog - Study Notes on the Blossom Algorithm
  7. Maximum matching, perfect matching, and the Hungarian algorithm in bipartite graphs
  8. Wikiwand - Hopcroft–Karp algorithm
  9. Bondy, John Adrian, and Uppaluri Siva Ramachandra Murty. Graph theory with applications. Vol. 290. London: Macmillan, 1976.

Footnotes

  1. Of course, this is not the only way to perform the reduction. For graph G=(V,E)G=(V,E), one can instead connect a copy G~=(V~,E~)\tilde G=(\tilde V,\tilde E) point by point to the original graph and set the weights of all edges newly added with respect to the original graph GG (including edges in the copy) to 00, obtaining graph G′=(V′,E′)G'=(V',E'). In other words, the vertex set of new graph G′G' is V∪V′V\cup V', and its edge set contains, in addition to the edges of graph GG, zero-weight edges connecting every vertex v∈Vv\in V to its copy v~∈V\tilde v\in V, and, for every edge (u,v)∈E(u,v)\in E, zero-weight edges connecting u~\tilde u and v~\tilde v. Every matching in graph GG, denoted by MM, corresponds to a perfect matching of the same total weight in graph G′G': simply match the unmatched vertices of graph GG: each vertex vv with its copy v~\tilde v, and for every matched edge (u,v)(u,v), match u~\tilde u with v~\tilde v. Thus, the maximum-weight maximum-cardinality matching in graph G′G', which is also a maximum-weight perfect matching, restricted to EE, yields a maximum-weight matching in graph GG. The advantage of this reduction is that if graph GG is bipartite or sparse, the expanded graph G′G' is also bipartite or sparse, respectively. ↩

  2. For graph G′G', every perfect matching M′M' can be used in the manner described here to obtain an edge cover of graph GG, denoted by CC, whose total weight is half that of the former; conversely, reversing this construction, for every edge cover of graph GG, denoted by CC, one can construct a perfect matching of graph G′G', denoted by M′M', whose total weight is no more than twice that of the former. This shows that the reduction is valid. ↩

This content comes from OI Wiki: Graph matching. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.