Video failed to load

Weighted Matrix-Tree: Make a Determinant Count the Structures You Want

587 words
3 minutes
Weighted Matrix-Tree: Make a Determinant Count the Structures You Want

The matrix-tree theorem becomes much more useful when edge weights are treated as algebraic markers. A determinant can encode a sum over spanning trees, where each tree contributes the product of its selected edge weights. This lets a graph-counting problem become polynomial algebra.

We consider undirected graphs, allow parallel edges, and ignore self-loops because they cannot belong to a spanning tree. All modular elimination below assumes a prime modulus.

Build the weighted Laplacian#

For an edge {u,v}\{u,v\} of weight ww, add ww to LuuL_{uu} and LvvL_{vv}, and subtract it from LuvL_{uv} and LvuL_{vu}. Delete the same row and column for any chosen root. The determinant of the remaining matrix is

Z=∑T spanning tree∏e∈Twe.Z=\sum_{T\text{ spanning tree}}\prod_{e\in T}w_e.

Parallel edges contribute separately when building the matrix. Adding their weights is correct because the determinant expansion distributes over these alternatives. Self-loops should be skipped explicitly rather than counted as ordinary degree contributions.

Why a determinant selects trees#

Orient each edge arbitrarily and build its incidence column: one entry +1+1, one entry −1-1. Remove the root row to form BB. Let WW be the diagonal matrix of edge weights. The reduced Laplacian is BWBTBWB^\mathsf T.

The Cauchy-Binet formula expands its determinant over sets SS of exactly n−1n-1 edges:

det⁡(BWBT)=∑∣S∣=n−1det⁡(BS)2∏e∈Swe.\det(BWB^\mathsf T)= \sum_{|S|=n-1}\det(B_S)^2\prod_{e\in S}w_e.

If those edges contain a cycle, their incidence columns are dependent. If they are disconnected, their incidence matrix lacks full rank. Either case contributes zero. For a tree, repeatedly remove a non-root leaf and expand along its row. Each expansion contributes +1+1 or −1-1, so the determinant is ±1\pm1 and its square is one. Exactly the spanning trees survive.

This proof also explains why the arbitrary orientation disappears. Reversing a column changes a sign that is squared away.

weighted triangle#

Give triangle edges weights two, three, and five. Its three spanning trees have products six, ten, and fifteen, totaling 31. Deleting the vertex shared by weights two and five leaves the reduced matrix

(5−3−38),\begin{pmatrix}5&-3\\-3&8\end{pmatrix},

whose determinant is 40−9=3140-9=31. Setting all three weights to one instead yields three ordinary spanning trees. This tiny graph is a useful check for both diagonal construction and off-diagonal signs.

Modular determinant without floating point#

At column ii, find a row with a nonzero pivot. If none exists, the determinant is zero in the field. Swap it into place and negate the accumulated answer if necessary. Multiply the answer by the pivot, then eliminate rows below it using the pivot’s modular inverse.

answer = 1
for column i:
find pivot row r with matrix[r][i] != 0
if none exists: return 0
if r != i: swap rows; answer = -answer
pivot = matrix[i][i]
answer *= pivot
for row j > i:
ratio = matrix[j][i] / pivot
row[j] -= ratio * row[i]
return answer

The empty determinant is one, matching the single-vertex graph’s empty spanning tree. A graph can have a positive integer number of spanning trees while its determinant is zero modulo pp; modular zero does not prove graph disconnection.

Mark selected edges with a variable#

Assign weight xx to red edges and one to other edges. Then Z(x)Z(x) is a polynomial whose coefficient of xkx^k counts trees containing exactly kk red edges. Its degree is at most n−1n-1.

A direct approach evaluates the determinant at nn distinct field points and interpolates, costing O(n4)O(n^4) with ordinary elimination. That is a valid baseline for small graphs, not a universal fast solution. Structural graph properties, determinant updates, or specialized polynomial-matrix methods are needed when nn is large.

Exercise. Color two edges of a triangle red and one blue. Derive Z(x)Z(x) by enumeration and by the symbolic 2×22\times2 determinant. Then explain how the answer changes if one red edge has two parallel copies.

Support & Share

If this article helped you, please share or support!

Sponsor
Weighted Matrix-Tree: Make a Determinant Count the Structures You Want
https://blog.wsh.moe/posts/weighted-matrix-tree/
Author
Wsh
Published at
2026-10-24

Comments

LEAVE A NOTE

Join the conversation

Markdown supported

Be kind. Good conversations start with you.

Conversation

Loading conversation…

Comments powered by Waline

Profile Image of the Author
Wsh
Nice to meet you! Hello, I'm Wsh.
Announcement
Any news or updates will be shared here, so check back anytime! Thanks for stopping by, meow >.<
Categories
Tags
Site Statistics
Posts
27
Categories
7
Tags
69
Total Words
124,615
Running Days
0 days
Last Activity
0 days ago
Site Info
Build Platform
Vercel
Blog Version
Firefly v6.16.8
License
CC BY-NC-SA 4.0
Table of Contents