Perfect Matchings and the Tutte Matrix
A perfect matching of a graph is a set of edges that covers every vertex exactly once. For bipartite graphs it can be found with flows (Maximum Bipartite Matching); for general graphs Edmonds' blossom algorithm (1965) does it in polynomial time but is intricate. There is a completely different, algebraic route: a graph has a perfect matching exactly when a certain symbolic determinant is not identically zero. Combined with the Schwartz-Zippel lemma, that gives a randomized test that is a few lines of linear algebra, and that parallelises well. The idea is due to Tutte (1947), Edmonds (1967) and Lovász (1979).
Bipartite graphs: the Edmonds matrix
Let be bipartite with sides and . Its Edmonds matrix has an indeterminate in position if is an edge, and 0 otherwise. Expanding the determinant over permutations,
a term is nonzero exactly when every is an edge, that is, when describes a perfect matching. Different permutations give different monomials, so nothing cancels: if and only if has a perfect matching.
General graphs: the Tutte matrix
For a general graph on vertices ( even), the Tutte matrix is skew-symmetric:
Theorem (Tutte, 1947). as a polynomial if and only if has a perfect matching.
Proof sketch
A permutation contributes a nonzero term only if every pair is an edge (fixed points are excluded, since ). Decompose into cycles.
If has a perfect matching , let swap the endpoints of every edge of . Its term is up to sign. Any other permutation producing the same monomial must also use exactly the edges of in both directions, so it is itself; the monomial survives and .
Conversely, pair up the permutations that contain an odd cycle: reverse the odd cycle containing the smallest vertex that lies on an odd cycle. Reversing an odd cycle of a skew-symmetric matrix multiplies its product by and keeps the sign of the permutation, so the two terms cancel. The surviving terms come from permutations with only even cycles, and every even cycle of edges contains a perfect matching of its vertices (take every other edge). So a nonzero determinant implies a perfect matching.
Over a field, is the square of a polynomial called the Pfaffian of , whose terms correspond to perfect matchings one by one.
The randomized test
has total degree (every term has one entry per row). By Schwartz-Zippel, substituting independent uniform values from for the indeterminates and computing the determinant numerically, in time by Gaussian elimination, gives:
- if has no perfect matching, the result is always 0;
- if has one, the result is 0 with probability at most .
HAS-PERFECT-MATCHING(G, p) // Lovász 1979; one-sided error n/p
0 build T with T[i][j] = r_ij, T[j][i] = -r_ij for every edge ij (i < j), r_ij uniform in Z_p
1 return DET(T) mod p != 0A "yes" answer is always correct; a "no" answer can be wrong with probability at most , about for and in the thousands. More generally, the rank of the randomly substituted Tutte matrix is twice the size of a maximum matching with the same probability (Lovász, 1979), so the test also measures how large a matching is.
The program compares the test with brute-force matching search on small graphs: the Petersen graph (it has perfect matchings), two disjoint triangles (no perfect matching: each triangle has an odd number of vertices), a star with three leaves plus a separate edge (no perfect matching: the centre can match only one leaf), and a 6-cycle with a pendant path.
import random
P = 2 ** 61 - 1
def rank_mod(M, p=P):
M = [row[:] for row in M]
rank, rows, cols = 0, len(M), len(M[0])
for c in range(cols):
piv = next((r for r in range(rank, rows) if M[r][c] % p), None)
if piv is None:
continue
M[rank], M[piv] = M[piv], M[rank]
inv = pow(M[rank][c], p - 2, p)
for r in range(rows):
if r != rank and M[r][c] % p:
f = M[r][c] * inv % p
M[r] = [(a - f * b) % p for a, b in zip(M[r], M[rank])]
rank += 1
return rank
def tutte(n, edges, rng):
T = [[0] * n for _ in range(n)]
for i, j in edges:
r = rng.randrange(1, P)
T[i][j], T[j][i] = r, (-r) % P
return T
def max_matching_brute(n, edges):
best = 0
def grow(k, used, size):
nonlocal best
best = max(best, size)
for t in range(k, len(edges)):
i, j = edges[t]
if i not in used and j not in used:
grow(t + 1, used | {i, j}, size + 1)
grow(0, frozenset(), 0)
return best
petersen = [(i, (i + 1) % 5) for i in range(5)] + [(i, i + 5) for i in range(5)] + [(5 + i, 5 + (i + 2) % 5) for i in range(5)]
graphs = {
"Petersen graph (10 vertices)": (10, petersen),
"two disjoint triangles": (6, [(0, 1), (1, 2), (0, 2), (3, 4), (4, 5), (3, 5)]),
"star K_1,3 plus an edge": (6, [(0, 1), (0, 2), (0, 3), (4, 5)]),
"6-cycle with a 2-vertex tail": (8, [(i, (i + 1) % 6) for i in range(6)] + [(0, 6), (6, 7)]),
}
rng = random.Random(80)
for name, (n, edges) in graphs.items():
rk = rank_mod(tutte(n, edges, rng))
print(f"{name:30}: rank(T) = {rk:2} -> matching size {rk // 2}, perfect: {rk == n}; "
f"brute force: {max_matching_brute(n, edges)}")Petersen graph (10 vertices) : rank(T) = 10 -> matching size 5, perfect: True; brute force: 5
two disjoint triangles : rank(T) = 4 -> matching size 2, perfect: False; brute force: 2
star K_1,3 plus an edge : rank(T) = 4 -> matching size 2, perfect: False; brute force: 2
6-cycle with a 2-vertex tail : rank(T) = 8 -> matching size 4, perfect: True; brute force: 4
The rank halves exactly to the size of a maximum matching in every case.
The error is one-sided, and Schwartz-Zippel bounds it
With a small prime the test can miss a perfect matching: the random determinant may vanish by accident. The lemma bounds that probability by , since has degree . The program measures the false-"no" rate on the Petersen graph () for small primes, and confirms that graphs without a perfect matching are never reported as having one.
import random
def det_mod(M, p):
M = [row[:] for row in M]
n, det = len(M), 1
for c in range(n):
piv = next((r for r in range(c, n) if M[r][c] % p), None)
if piv is None:
return 0
if piv != c:
M[c], M[piv] = M[piv], M[c]
det = -det
det = det * M[c][c] % p
inv = pow(M[c][c], p - 2, p)
for r in range(c + 1, n):
f = M[r][c] * inv % p
M[r] = [(a - f * b) % p for a, b in zip(M[r], M[c])]
return det % p
def random_tutte(n, edges, p, rng):
T = [[0] * n for _ in range(n)]
for i, j in edges:
r = rng.randrange(p)
T[i][j], T[j][i] = r, (-r) % p
return T
petersen = [(i, (i + 1) % 5) for i in range(5)] + [(i, i + 5) for i in range(5)] + [(5 + i, 5 + (i + 2) % 5) for i in range(5)]
triangles = [(0, 1), (1, 2), (0, 2), (3, 4), (4, 5), (3, 5)]
rng = random.Random(81)
trials = 400
print(" p Petersen: false 'no' rate bound n/p two triangles: false 'yes'")
for p in [5, 11, 31, 101]:
miss = sum(det_mod(random_tutte(10, petersen, p, rng), p) == 0 for _ in range(trials))
fake = sum(det_mod(random_tutte(6, triangles, p, rng), p) != 0 for _ in range(trials))
print(f"{p:4} {miss / trials:26.3f} {min(1, 10 / p):9.3f} {fake:26}") p Petersen: false 'no' rate bound n/p two triangles: false 'yes'
5 0.292 1.000 0
11 0.105 0.909 0
31 0.025 0.323 0
101 0.007 0.099 0
Here the values are drawn from all of , including 0, so the lemma applies with . The measured error is well below , and graphs without a perfect matching never produce a nonzero determinant.
In C++
The test in C++: fill the Tutte matrix with random residues modulo (fixed-seed std::mt19937_64), and compute the determinant by Gaussian elimination, with 128-bit products. It checks random graphs on 40 vertices with a planted perfect matching, and the same graphs with one vertex's edges deleted (then no perfect matching exists).
#include <cstdint>
#include <cstdio>
#include <random>
#include <utility>
#include <vector>
using u64 = std::uint64_t;
const u64 P = (1ULL << 61) - 1;
std::mt19937_64 rng(80);
u64 mul(u64 a, u64 b) { return (unsigned __int128)a * b % P; }
u64 power(u64 a, u64 e) { u64 r = 1; for (; e; e >>= 1, a = mul(a, a)) if (e & 1) r = mul(r, a); return r; }
u64 det_mod(std::vector<std::vector<u64>> m) {
int n = m.size();
u64 det = 1;
for (int c = 0; c < n; ++c) {
int piv = -1;
for (int r = c; r < n; ++r) if (m[r][c]) { piv = r; break; }
if (piv < 0) return 0;
if (piv != c) { std::swap(m[piv], m[c]); det = (P - det) % P; }
det = mul(det, m[c][c]);
u64 inv = power(m[c][c], P - 2);
for (int r = c + 1; r < n; ++r) {
u64 f = mul(m[r][c], inv);
for (int k = c; k < n; ++k) m[r][k] = (m[r][k] + P - mul(f, m[c][k])) % P;
}
}
return det;
}
bool has_perfect_matching(int n, const std::vector<std::pair<int, int>>& edges) {
std::vector<std::vector<u64>> t(n, std::vector<u64>(n, 0));
for (auto [i, j] : edges) {
u64 r = 1 + rng() % (P - 1);
t[i][j] = r;
t[j][i] = P - r;
}
return det_mod(t) != 0;
}
int main() {
const int n = 40;
for (int g = 0; g < 3; ++g) {
std::vector<std::pair<int, int>> e;
for (int i = 0; i < n; i += 2) e.push_back({i, i + 1}); // planted matching
for (int k = 0; k < 60; ++k) {
int a = rng() % n, b = rng() % n;
if (a != b && (a | 1) != (b | 1)) e.push_back({a, b});
}
std::vector<std::pair<int, int>> cut; // isolate vertex 0
for (auto [a, b] : e) if (a != 0 && b != 0) cut.push_back({a, b});
std::printf("graph %d: %zu edges, perfect matching: %s; vertex 0 isolated: %s\n", g, e.size(),
has_perfect_matching(n, e) ? "yes" : "no", has_perfect_matching(n, cut) ? "yes" : "no");
}
}graph 0: 74 edges, perfect matching: yes; vertex 0 isolated: no
graph 1: 78 edges, perfect matching: yes; vertex 0 isolated: no
graph 2: 79 edges, perfect matching: yes; vertex 0 isolated: no
From testing to finding, and parallelism
The determinant test decides existence. To find a perfect matching, one can delete edges one at a time and keep the deletion whenever a matching still exists, at the cost of determinant computations; Rabin and Vazirani (1989) read the matching off the inverse of instead. Mucha and Sankowski (2004) found a perfect matching in time, where is the matrix-multiplication exponent, faster than the of Micali and Vazirani on dense graphs.
The algebraic approach also gave the first fast parallel algorithms. Determinants can be computed in parallel time, so perfect-matching existence is in randomized NC. Finding one in parallel needed an extra trick, the isolation lemma of Mulmuley, Vazirani and Vazirani (1987): if each edge gets a random weight from , then with probability at least the minimum-weight perfect matching is unique, and a unique matching can be extracted from a single determinant with weights . Whether perfect matching is in deterministic NC is still open (quasi-NC algorithms are known: Fenner, Gurjar and Thierauf, 2016, for bipartite graphs, and Svensson and Tarnawski, 2017, for general graphs).
Test yourself
In the Edmonds matrix of a bipartite graph, why can no two nonzero terms of the determinant cancel?
- Because all entries are positive
- Because distinct permutations give distinct monomials in the indeterminates
- Because the matrix is symmetric
- Because the field has characteristic 2
The randomly substituted Tutte determinant is nonzero. What can you conclude?
- The graph certainly has a perfect matching
- The graph has a perfect matching with probability at least
- Nothing
- The graph has no perfect matching
Why does the Tutte matrix need to be skew-symmetric for general graphs?
- To make the determinant easier to compute
- So that terms coming from permutations with odd cycles cancel in pairs
- Because graphs are undirected
- To keep the entries small
What does the rank of a randomly substituted Tutte matrix tell you, with high probability?
- The number of vertices
- Twice the size of a maximum matching
- The number of perfect matchings
- The chromatic number