Post · 2 min read· pp. 650–651

Kirchhoff's theorem. Finding the number of spanning trees

Problem: You are given a connected undirected graph (with possible multiple edges) represented using an adjacency matrix. Find the number of different spanning trees of this graph.

The following formula was proven by Kirchhoff in 1847.

Kirchhoff's matrix tree theorem

Let AA be the adjacency matrix of the graph: Au,vA_{u,v} is the number of edges between uu and vv. Let DD be the degree matrix of the graph: a diagonal matrix with Du,uD_{u,u} being the degree of vertex uu (including multiple edges and loops - edges which connect vertex uu with itself).

The Laplacian matrix of the graph is defined as L=D−AL = D - A. According to Kirchhoff's theorem, all cofactors of this matrix are equal to each other, and they are equal to the number of spanning trees of the graph. The (i,j)(i,j) cofactor of a matrix is the product of (−1)i+j(-1)^{i + j} with the determinant of the matrix that you get after removing the ii-th row and jj-th column. So you can, for example, delete the last row and last column of the matrix LL, and the absolute value of the determinant of the resulting matrix will give you the number of spanning trees.

The determinant of the matrix can be found in O(N3)O(N^3) by using the Gaussian method.

The proof of this theorem is quite difficult and is not presented here; for an outline of the proof and variations of the theorem for graphs without multiple edges and for directed graphs refer to Wikipedia.

Relation to Kirchhoff's circuit laws

Kirchhoff's matrix tree theorem and Kirchhoff's laws for electrical circuit are related in a beautiful way. It is possible to show (using Ohm's law and Kirchhoff's first law) that resistance RijR_{ij} between two points of the circuit ii and jj is

Rij=∣L(i,j)∣∣Lj∣.R_{ij} = \frac{ \left| L^{(i,j)} \right| }{ | L^j | }.

Here the matrix LL is obtained from the matrix of inverse resistances AA (Ai,jA_{i,j} is inverse of the resistance of the conductor between points ii and jj) using the procedure described in Kirchhoff's matrix tree theorem. TjT^j is the matrix with row and column jj removed, T(i,j)T^{(i,j)} is the matrix with two rows and two columns ii and jj removed.

Kirchhoff's theorem gives this formula geometric meaning.

Practice Problems