跳到正文

个人记录

矩阵树定理

Pengzt约 8 分钟
原文
文章信息与版本

文章信息

发布
2025年2月6日
收录
2025年12月12日
更新
2026年11月7日
篇幅
约 8 分钟

版本

  1. v1当前

luogu & cnblogs

矩阵树定理

给定带权无向图 GG,求出 ∑T∏e∈Twe\sum_T\prod_{e \in T}w_e 的值。

定义

  • 定义 [n]={1,2,3,⋯ ,n}[n] = \{1, 2, 3, \cdots, n\}

  • 对于无向图,定义 D(G)D(G) 为其度数矩阵,有:D(G)ij={deg⁡ii=j0i≠jD(G)_{ij} = \left\{\begin{matrix} \deg_i & i = j \\ 0 & i \ne j \end{matrix}\right..

  • 对于有向图,定义 Din(G)D^{\text{in}}(G) 和 Dout(G)D^{\text{out}}(G) 分别表示其入度矩阵和出度矩阵,有:Din(G)ij={deg⁡iini=j0i≠j,Dout(G)ij={deg⁡iouti=j0i≠jD^{\text{in}}(G)_{ij} = \left\{\begin{matrix} \deg^{\text{in}}_i & i = j \\ 0 & i \ne j \end{matrix}\right., D^{\text{out}}(G)_{ij} = \left\{\begin{matrix} \deg^{\text{out}}_i & i = j \\ 0 & i \ne j \end{matrix}\right..

  • 定义 A(G)A(G) 为图的邻接矩阵。A(G)ijA(G)_{ij} 表示 ii 连向 jj 的边的数量。

  • 定义无向图的 Laplace 矩阵 LL 为 L(G)=D(G)−A(G)L(G) = D(G) - A(G)。有向图的情况可以类似的定义 LinL^{\text{in}} 和 LoutL^{\text{out}}.

  • 定义图 GG 以 uu 为根的根向树和叶向树的数量分别为 troot(G,u)t^{\text{root}}(G, u) 和 tleaf(G,u)t^{\text{leaf}}(G, u)。

  • 定义矩阵 AA 的子矩阵 AS,TA_{S, T} 为选取 i∈Si \in S,j∈Tj \in T 构成的元素。

  • 定义有向图 GG 的关联矩阵为 M(G)M(G)(无向图可以随便定方向),有 Min(G)ij={w(ej)i is the starting point of edge j0otherwise.,Mout(G)ij={w(ej)i is the end of edge j0otherwise.M^{\text{in}}(G)_{ij} = \left\{\begin{matrix} \sqrt{w(e_j)} & \text{i is the starting point of edge j} \\ 0 & \text{otherwise.} \end{matrix}\right., M^{\text{out}}(G)_{ij} = \left\{\begin{matrix} \sqrt{w(e_j)} & \text{i is the end of edge j} \\ 0 & \text{otherwise.} \end{matrix}\right.,令 M(G)=Min(G)−Mout(G)M(G) = M^{\text{in}}(G) - M^{\text{out}}(G)。

不难发现

Din=Min⋅(Min)T,Dout=Mout⋅(Mout)T,A(G)=Min⋅(Mout)T.D^{\text{in}} = M^{\text{in}} \cdot (M^{\text{in}})^T, D^{\text{out}} = M^{\text{out}}\cdot (M^{\text{out}})^T, A(G) = M^{\text{in}} \cdot (M^{\text{out}})^T.

进而有

Lin(G)=Min⋅(Min−Mout)T,Lout(G)=(Mout−Min)⋅(Mout)T.L^{\text{in}}(G) = M^{\text{in}} \cdot (M^{\text{in}} - M^{\text{out}})^T, L^{\text{out}}(G) = (M^{\text{out}} - M^{\text{in}}) \cdot (M^{\text{out}})^T.

对于无向图有

L(G)=M⋅MTL(G) = M \cdot M^T

定理

  • 对于无向图 GG 和任意的 kk,有 t(G)=det⁡L(G)[n]∖{k},[n]∖{k}t(G) = \det L(G)_{[n] \setminus \{k\}, [n] \setminus \{k\}}
  • 对于有向图 GG 和根 uu,有 troot(G)=det⁡Lout(G)[n]∖{u},[n]∖{u}t^{\text{root}}(G) = \det L^{\text{out}}(G)_{[n] \setminus \{u\}, [n] \setminus \{u\}},tleaf(G)=det⁡Lin(G)[n]∖{u},[n]∖{u}t^{\text{leaf}}(G) = \det L^{\text{in}}(G)_{[n] \setminus \{u\}, [n] \setminus \{u\}}

证明

引理 1(Cauthy-Binet):

对于 n×mn \times m 的矩阵 AA 和 m×nm \times n 的矩阵 BB,有

det⁡(AB)=∑S⊆{1,2,⋯ ,m}∧∣S∣=ndet⁡A[n],S⋅det⁡BS,[n]\det(AB) = \sum\limits_{S \subseteq \{1, 2, \cdots, m\} \land |S| = n}\det A_{[n], S} \cdot \det B_{S, [n]}

如果 n>mn > m,则必有 det⁡(AB)=0\det(AB) = 0。

证明咕了。

引理 2:

对于图 GG 的子图 (V′,E′)(V', E'),若 ∣E′∣≤∣V′∣|E'| \le |V'|,则子图 TT 是一棵以 V∖V′V \setminus V' 为根的根向生成树当且仅当

det⁡(MV′,E′out)det⁡(MV′,E′out−MV′,E′in)≠0\det(M_{V', E'}^{\text{out}}) \det(M_{V', E'}^{\text{out}} - M_{V', E'}^{\text{in}}) \ne 0

且该式的值在不为 00 时,恰好为 ∏e∈E′we\prod_{e \in E'} w_e。

证明:

记 w(T)=∏e∈Twew(T) = \prod_{e \in T} w_e。先把行列式中每行的 we\sqrt{w_e} 提出来,最后的答案再乘上一个 w(E′)w(E') 就行了。

先考虑第一个行列式。如果 MV′,E′outM_{V', E'}^{\text{out}} 存在某行全是 00(该点集中存在点没有在边集中出现),那么 det⁡(MV′,E′out)\det(M_{V', E'}^{\text{out}}) 就是 00。然后又由于每条边只有一个出点,所以每行恰好有一个 11。所以这个式子保证了就有 ∣V′∣=∣E′∣|V'| = |E'|,但是没有保证 TT 是一棵根向生成树。

现在考虑第二个行列式:MV′,E′out−MV′,E′inM_{V', E'}^{\text{out}} - M_{V', E'}^{\text{in}}。这个矩阵中每行只有一个 11,零个或一个 −1-1。若 TT 中存在环,这些边为 e1→e2→⋯→eme_1 \to e_2 \to \cdots \to e_m。则行列式会长成形如这样的东西:

[0100−1000⋯10000−100⋯00−100100⋯0010000−1⋯0−1000001⋯−10001000⋯⋮⋮⋮⋮⋮⋮⋮⋮⋱]\begin{bmatrix} 0 & 1 & 0 & 0 & -1 & 0 & 0 & 0 & \cdots \\ 1 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & \cdots \\ 0 & 0 & -1 & 0 & 0 & 1 & 0 & 0 & \cdots \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & -1 & \cdots \\ 0 & -1 & 0 & 0 & 0 & 0 & 0 & 1 & \cdots \\ -1 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & \cdots \\ \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \ddots \\ \end{bmatrix}

记从第 ii 行的 −1-1 处进入点 ii,+1+1 处离开点 ii。回顾消元求解行列式的过程,最后一定会消出一行 00。不存在环,那么就可以从叶子处开始消(叶子的那一行有且仅有一个 11,其余的都是 00),可以把它的父亲所在行的 −1-1 消掉。最后消出来的结果其实就是 det⁡(MV′,E′out)\det(M_{V', E'}^{\text{out}})。

所以当且仅当 TT 是一棵以 V∖V′V \setminus V' 为根的根向生成树时 det⁡(MV′,E′out)det⁡(MV′,E′out−MV′,E′in)=det⁡(MV′,E′out)2=w(T)≠0\det(M_{V', E'}^{\text{out}}) \det(M_{V', E'}^{\text{out}} - M_{V', E'}^{\text{in}}) = \det(M_{V', E'}^{\text{out}})^2 = w(T) \ne 0。

□\square

根据刚刚的证明,有更通用的结论:

∑T∈τ(G,u)∏e∈Twe=det⁡Lout(G)[n]∖{u},[n]∖{u}\sum\limits_{T \in \tau(G, u)} \prod_{e \in T} w_e = \det L^{\text{out}}(G)_{[n]\setminus \{u\}, [n] \setminus \{u\}}

这里的 τ(G,u)\tau(G, u) 表示图 GG 以 uu 为根的根向生成树集合。

时间复杂度是求行列式的 O(n3)\mathcal{O}(n^3)。

例题先咕了。

原文