Maximum General Graph Matching
Blossom Algorithm
The Blossom Algorithm (also known as the Blossom Tree Algorithm) can solve the maximum cardinality matching problem in general graphs. This algorithm was proposed by Jack Edmonds in 1961. With some modifications, it can also solve the maximum weight matching problem in general graphs. This algorithm was the first to prove that maximum matching has polynomial complexity.
The difference between general graph matching and bipartite matching is that the graph may contain odd cycles.

Taking this graph as an example, if we directly invert (swap matched and unmatched edges), the resulting \(M\) would be invalid, as some vertices would appear in two matched edges. The problem lies in the odd cycle.
Now consider the augmenting path algorithm for general graphs. From the perspective of bipartite graphs, each time we enumerate an unmatched vertex, set it as the root, and mark it as "o". Then alternately mark "o" and "i". It is not difficult to see that the edges from "i" to "o" are matched edges.
Assume the current vertex is \(v\), and its adjacent vertex is \(u\). There are two cases:
- \(u\) has not been visited. If \(u\) is unmatched, then an augmenting path is found; otherwise, search for an augmenting path from \(u\)'s partner.
- \(u\) has been visited. If the mark is "o", it indicates the need for blossom contraction; otherwise, it indicates an even cycle, which can be skipped.
For even cycles, treat them as bipartite graphs, so they can be ignored. After blossom contraction, continue searching for augmenting paths in the new graph.

Let the original graph be \(G\), and the graph after blossom contraction be \(G'\). We only need to prove:
- If \(G\) has an augmenting path, then \(G'\) also has one.
- If \(G'\) has an augmenting path, then \(G\) also has one.

Let the non-tree edge (the edge forming the cycle) be \((u,v)\), and define the blossom root \(h=LCA(u,v)\). The odd cycle alternates, and only the two edges adjacent to \(h\) are of the same type, both unmatched edges. Thus, the tree edge entering \(h\) must be a matched edge, and all vertices on the cycle except \(h\) have unmatched edges connecting to the outside of the cycle.
Observing the graph, there are two ways to exit the cycle from an external edge: clockwise or counterclockwise.

Therefore, whether blossom contraction is performed or not does not affect correctness.
In practice, after finding a blossom, we do not need to actually contract it. Instead, we can use an array to record which blossom root each vertex belongs to.
Complexity Analysis
Each time an augmenting path is searched, all edges are traversed. When encountering a blossom, the vertices on the blossom are maintained, resulting in \(O(|E|^2)\).
Enumerating all unmatched vertices to find augmenting paths yields a total complexity of \(O(|V||E|^2)\).
Reference Code
Reference Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
UOJ #79. General Graph Maximum Matching
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
General Graph Matching Algorithm Based on Gaussian Elimination
Hint
Before reading the following content, you may need to read the sections on matrices in the "Linear Algebra" part:
This section introduces a general graph matching algorithm based on Gaussian elimination. Compared to the traditional Blossom Algorithm, its advantages lie in being easier to understand and implement, and it is convenient for solving problems such as "necessary vertices in maximum matching." The disadvantage is that it has a relatively large constant because Gaussian elimination's \(O(n^3)\) is typically fully utilized, whereas the Blossom Algorithm usually does not run at full complexity.
Prerequisite: Tutte Matrix
Definition: For an undirected graph \(G = (V, E)\) with \(n\) vertices, its Tutte matrix \(\tilde{A}(G)\) is an \(n \times n\) matrix defined as:
where \(x_{i, j}\) is a variable, so \(\tilde{A}(G)\) contains \(|E|\) variables.
When there is no ambiguity, \(\tilde{A}(G)\) is abbreviated as \(\tilde{A}\).
Theorem (Tutte's Theorem): \(G\) has a perfect matching if and only if \(\det \tilde{A} \ne 0\).
Proof
Here, the concept of "even cycle cover" is introduced: an even cycle cover of an undirected graph \(G\) refers to covering all vertices with several even cycles (including 2-cycles) without overlap or omission.
It is easy to prove that \(G\) has a perfect matching if and only if \(G\) has an even cycle cover.
- If \(G\) has an even cycle cover, we can take every other edge in each cycle to obtain a perfect matching.
- If \(G\) has a perfect matching, we can take the 2-cycles corresponding to the matched edges to obtain an even cycle cover.
Next, we prove that \(G\) has an even cycle cover if and only if \(\tilde{A} \ne 0\).
Consider the definition of the determinant:
where \(\pi\) is any permutation, and \((-1)^{\pi}\) takes \(-1\) if the number of inversions in \(\pi\) is odd, and \(1\) otherwise.
It is not difficult to see that each permutation can be viewed as a cycle cover of \(G\). If this cycle cover contains an odd cycle, then the sum after flipping this cycle will be \(0\). Therefore, only even cycle covers can make the determinant non-zero, completing the proof.
Theorem: \(\operatorname{rank}\tilde{A}\) must be even, and the size of the maximum matching in \(G\) is equal to half of \(\operatorname{rank}\tilde{A}\).
Proof
The rank of a skew-symmetric matrix can only be even; the latter is left for the reader to think about.
In practice, it is impossible to compute with \(|E|\) variables. However, we can take a number field, such as the residue system \(\mathcal{Z}_p\) of a prime \(p\), and randomly replace the variables with numbers from \(\mathcal{Z}_p\) before computation. For convenience, when there is no ambiguity, \(\tilde{A}\) directly refers to the matrix after substitution.
Theorem: \(\operatorname{rank}\tilde{A}\) is at most twice the size of the maximum matching in \(G\), and the probability that they are equal is at least \(1 - \frac n p\).
Considering that \(n\) in general graph maximum matching problems typically does not exceed \(10^3\), in practice, taking \(p\) as a prime of the order \(10^9\) is sufficient.
From the theorem, if only the maximum matching size is required and not the matching scheme, then only one Gaussian elimination is needed to compute \(\operatorname{rank}\tilde{A}\), which is much simpler than the Blossom Algorithm. However, if the scheme needs to be output, it becomes slightly more complex and requires the algorithm introduced below.
Constructing a Perfect Matching
From Tutte's theorem and the above theorem, if \(G\) has a perfect matching, then \(\tilde{A}\) is very likely to be full rank. For convenience, the phrase "very likely" is omitted in the following discussion.
Denote the vertex labeled \(i\) in \(G\) as \(v_i\). Further, we have the following theorem:
Theorem: \(\tilde{A}^{-1}_{j,i} \ne 0 \iff G - \{v_i, v_j\}\) has a perfect matching.
Inverse Matrix and Adjugate Matrix
For any \(n \times n\) square matrix \(A\), define its adjugate matrix as \(A^*_{i, j} = (-1)^{i + j} M_{j, i}\), where \(M_{j, i}\) is the minor obtained by deleting the \(j\)-th row and \(i\)-th column. In other words, if the cofactor matrix of \(A\) is \(M\), then \(A^* = M^T\).
Theorem: If \(A\) is invertible, then \(A^{-1} = \frac 1 {\det A} A^*\).
Therefore, \(A^{-1}_{j, i} \ne 0 \iff M_{i, j} \ne 0\), meaning the submatrix of \(A\) after deleting the \(i\)-th row and \(j\)-th column is full rank.
In other words, if \((v_i, v_j) \in E\) and \(\tilde{A}^{-1}_{j, i} \ne 0\), it indicates that there exists a perfect matching scheme containing the edge \((v_i, v_j)\). Such edges are called feasible edges.
From the above theorem, for an undirected graph \(G\) with a perfect matching, we can derive a straightforward brute-force algorithm to find a perfect matching: each time enumerate \(i, j\). If \((v_i, v_j)\) is a feasible edge (the edge exists and \(\tilde{A}^{-1}_{j, i} \ne 0\)), add \((v_i, v_j)\) to the matching scheme, delete these two vertices from \(G\), and recompute the new \(\tilde{A}^{-1}\).
A total of \(\frac n 2\) rounds are needed, each round is \(O(n^3)\), resulting in an overall complexity of \(O(n ^ 4)\), which is somewhat slow. In fact, when recomputing \(\tilde{A}^{-1}\), we do not need to recompute the inverse matrix from scratch using Gaussian elimination each time. Instead, we can use the following theorem:
Theorem (Elimination Theorem): Let
and \(\hat a_{1, 1} \ne 0\). Then we have
The theorem describes eliminating the first row and first column. In fact, it can be generalized to eliminating any row and column. Therefore, we only need to compute \(\tilde{A}^{-1}\) once at the beginning of the algorithm. Later, each time two vertices are deleted, we only need to perform two \(O(n^2)\) elimination processes.
The description is somewhat abstract; refer to the C++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
A total of \(\frac n 2\) rounds are needed, each with complexity \(O(n^2)\). Therefore, the above algorithm can find a perfect matching in \(O(n^3)\) time.
Constructing Maximum Matching
We have just solved the problem of constructing a perfect matching, but in practice, we usually need the maximum matching.
As mentioned earlier, the size of the maximum matching in \(G\) is equal to half of \(\operatorname{rank}\tilde{A}\). If we can find a maximum full-rank submatrix of \(\tilde{A}\), then by finding a perfect matching in the induced subgraph corresponding to this submatrix, we can obtain a maximum matching for \(G\).
From another perspective, if \(G\) has a perfect matching, then \(\tilde{A}\) is full rank, meaning \(\tilde{A}\) is linearly independent. If \(\tilde{A}\) is not full rank, we can compute a basis for \(\tilde{A}\), then retain only the rows and columns corresponding to this basis to obtain a maximum full-rank submatrix of \(\tilde{A}\).
After obtaining the maximum full-rank submatrix, use the above algorithm to find a perfect matching in the induced subgraph, which gives a maximum matching for the original graph. Note that since row swaps may occur during Gaussian elimination, care must be taken to maintain vertex indices in the implementation.
UOJ #79. General Graph Maximum Matching
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
Exercises
References
- Mucha M, Sankowski P. Maximum matchings via Gaussian elimination
- Zhou Zixin, Yang Jiaqi. "General Graph Matching Based on Linear Algebra"
- ZYQN. "General Graph Matching Algorithm Based on Linear Algebra"
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用