Maths & numbers

Spanning tree count calculator using a graph Laplacian

Count spanning trees of your undirected simple graph exactly from its edge list using the Matrix–Tree theorem.

Your numbers

Number vertices from 1. Each row is an undirected edge such as 1,2. Leave empty for a graph without 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

A spanning tree chooses edges from the supplied graph so that every declared vertex remains connected and no cycle remains. For n vertices it has n minus one edges. The calculation depends on the actual adjacency pattern, not just the vertex count. Build the graph Laplacian, remove one row and its matching column, and take the determinant of the remaining matrix. The calculator evaluates that determinant with exact integer arithmetic.

THE FORMULAτ(G) = det(L with one matching row and column removed); L = degree matrix − adjacency matrix.

A worked example

A triangle with edges 1–2, 2–3 and 1–3 has three spanning trees: remove any one edge. Its Laplacian has diagonal entries 2 and off-diagonal entries −1. Removing row and column 3 leaves [[2, −1], [−1, 2]], whose determinant is 4 − 1 = 3.

Good to know

Supports one through twelve declared vertices in an undirected, unweighted simple graph. Self-loops, duplicate edges, parallel edges and directed edges are rejected. Isolated vertices must still be included in the declared count. An empty edge list is valid; a one-vertex graph has one empty spanning tree by convention.

Count trees inside the supplied graph

For the triangle example, any two edges connect all three vertices and cannot contain a cycle. There are three possible pairs. If edge 1–3 is removed from the input graph before the calculation, the graph becomes a path and only one spanning tree remains: the graph itself. A vertex count alone cannot distinguish these cases.

The existing labeled-tree calculator uses Cayley’s formula to count all possible labeled trees on n vertices. That coincides with this result only when the supplied graph is complete. For the complete graph on four vertices, both counts are 4² = 16. For a four-vertex cycle, this calculator instead returns four, because only edges of the cycle are available.

A triangle has three spanning trees by deleting any one edge123123123Three choices for the missing edge
Every spanning tree connects all three vertices with two edges. Removing each of the three triangle edges gives a different spanning tree.

The matrix determinant calculator can check the smaller Laplacian cofactor directly. Use the cofactor, since the full Laplacian is singular.

An isolated declared vertex makes the count zero

Declaring four vertices but entering only the triangle edges leaves vertex 4 isolated. The graph then has no spanning tree, even though one connected component contains a cycle. Do not omit isolated vertices from the declared count. A spanning tree must cover every vertex, not only the vertices that appear in the edge rows.

The full Laplacian always has zero row sums and is singular, so taking its determinant would incorrectly return zero for every nonempty graph. The theorem requires a cofactor formed by deleting one row and its matching column. Any such principal cofactor gives the same count. The result table shows the complete Laplacian, and the calculation uses fraction-free integer elimination on its cofactor. This avoids treating a rounded determinant such as 2.999999999 as an exact count.

A few questions, answered

Are edges 1,2 and 2,1 different?

No. They are the same undirected edge, so entering both is rejected as a duplicate rather than counted twice.

Why does one vertex have one spanning tree?

The empty edge set connects the sole vertex and contains no cycle. The associated zero-by-zero determinant is defined as one.

Find your calculator

Start typing to explore the library.