Advanced maths

Matching polynomial calculator with counts by matching size

Count matchings by edge count and construct the signed matching polynomial of a small simple undirected graph using exact integer arithmetic.

Your numbers

Labels are 0 through n−1; include isolated vertices.
One row: u v. No loops or duplicate unordered pairs; blank means no edges.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

Count matchings by edge count and construct the signed matching polynomial of a small simple undirected graph using exact integer arithmetic.

THE FORMULAμ(G, x)=Σk(−1)^k mₖx^(n−2k), where mₖ counts matchings containing k edges and m₀=1.

A worked example

For the four-cycle, m₀=1, m₁=4 and m₂=2. The polynomial is x⁴−4x²+2 and the total number of matchings is 7.

Good to know

0–18 vertices, distinct undirected edges, no loops or weights. Integer counts are returned exactly as decimal strings.

What a matching polynomial records

A matching is a set of edges with no shared endpoint. The matching polynomial compresses the number of such sets at each possible size into one algebraic expression. Its coefficients therefore count compatible edge selections, rather than walks, connected components or ways to assign colors to the graph.

This calculator uses the signed convention with exponent n minus twice the matching size. Other references sometimes call the unsigned generating function a matching polynomial too. Check the displayed convention before comparing coefficients: the same matching counts can appear with different signs and powers under another perfectly valid definition.

The square has two perfect matchings0123m₀ = 1m₁ = 4m₂ = 2μ(x) = x⁴ − 4x² + 2
Scroll across to see the full diagram.
Four single edges and two opposite-edge pairs give 1+4+2=7 matchings.

Compare this with the Maximum weight matching calculator for a general graph. Count every matching size and form the matching polynomial, instead of returning only one maximum-weight matching.

Representing vertices and edges

Declare the number of vertices separately from the edge list so isolated vertices are included in n. Each edge row contains two distinct zero-based labels. The input is a simple undirected graph, so a reversed duplicate is still a duplicate and multiple parallel edges are not alternative choices here.

An isolated vertex participates in no matching edge but still changes the polynomial's powers. Adding one isolated vertex multiplies the signed polynomial by x without changing any matching count. This is a useful reason to preserve isolated labels instead of inferring the graph's vertex count solely from its largest listed endpoint.

Working through the four-cycle

The default graph is a square with four edges. There is one empty matching and four ways to choose a single edge. A two-edge matching must use opposite sides of the square, giving two possibilities. No three-edge matching is possible because that would require six distinct endpoints in a four-vertex graph.

The resulting expression is x to the fourth minus four x squared plus two. Adding the unsigned counts gives seven matchings altogether. The constant term is positive because the perfect matchings have size two, an even number; it does not count seven, which is the sum across all sizes.

The vertex-removal counting recurrence

Choose a vertex v and divide all matchings into disjoint cases. In the first case v is unmatched, leaving a matching in the graph with v deleted. In each remaining case v is paired to one neighbor u, leaving a matching after deleting both v and u, with its size increased by one.

Every matching belongs to exactly one case, so adding these count arrays neither omits nor duplicates a selection. The implementation memoizes results for remaining vertex subsets and uses arbitrary-precision integers. The coefficients are assembled only after counting, so alternating signs cannot cause cancellation to obscure the underlying nonnegative matching counts.

Interpreting the coefficient table

The matching-count table lists size k and count mₖ. The polynomial table transforms that row into exponent n−2k and coefficient (−1)ᵏmₖ. A highest nonzero matching-size row also reveals the graph's maximum matching size, but this page counts all matchings rather than returning an explicit largest matching witness.

The total-matchings output sums the unsigned table, including the empty matching exactly once. Evaluating the signed polynomial at one is generally not that total because odd-size contributions are subtracted. If your purpose is enumerating valid edge selections, use the count table or total directly rather than removing signs from a numerical evaluation.

Disconnected graphs and special cases

For disconnected components, a matching consists of one independent matching choice in each component. Their generating functions multiply, and the signed matching polynomials multiply as well. This property supplies an independent check: calculate two disjoint edges and compare the result with the square of the single-edge expression.

The zero-vertex graph has polynomial one and one empty matching. A graph with n isolated vertices has polynomial x to the n and still only one matching. A perfect matching uses every vertex, so only an even-order graph can contribute a nonzero constant term through its largest matching-size row.

Exact counts within finite limits

The eighteen-vertex cap bounds the subset dynamic program; it is not a promise of efficient counting for unrestricted networks. Self-loops, parallel edges, weights and directed arcs are outside this model. Counts remain exact even when they exceed ordinary floating-point integer precision because the calculation stores them as arbitrary-precision integers.

For validation, every single input edge should contribute one to m₁. The empty matching count must be one, and no nonzero size can exceed half the vertex count rounded down. Adding an edge cannot reduce any unsigned matching count, although the signed polynomial's coefficients alternate and therefore should not be compared as ordinary magnitudes.

A few questions, answered

Is the total the polynomial evaluated at one?

No. The total sums unsigned matching counts; the displayed polynomial alternates their signs.

Does the empty matching count?

Yes. It contributes m₀=1, including when the graph has no vertices.

Are parallel edges supported?

No. Enter a simple undirected graph without loops or duplicate unordered edges.

Why do isolated vertices change the expression?

They increase n and therefore the exponents, while leaving the matching counts unchanged.

Find your calculator

Start typing to explore the library.