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 of weight , add to and , and subtract it from and . Delete the same row and column for any chosen root. The determinant of the remaining matrix is
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 , one entry . Remove the root row to form . Let be the diagonal matrix of edge weights. The reduced Laplacian is .
The Cauchy-Binet formula expands its determinant over sets of exactly edges:
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 or , so the determinant is 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
whose determinant is . 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 , 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 = 1for 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 answerThe 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 ; modular zero does not prove graph disconnection.
Mark selected edges with a variable
Assign weight to red edges and one to other edges. Then is a polynomial whose coefficient of counts trees containing exactly red edges. Its degree is at most .
A direct approach evaluates the determinant at distinct field points and interpolates, costing 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 is large.
Exercise. Color two edges of a triangle red and one blue. Derive by enumeration and by the symbolic 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!





























































Conversation
Comments powered by Waline