Technique · CP 2000
Gaussian Elimination (mod p / reals) & Matrix-Tree Theorem
Solve n linear equations, or compute a rank or determinant, in O(n3) by row reduction. Kirchhoff's theorem counts spanning trees as any cofactor of the Laplacian. Expected-value systems on cyclic state graphs are also solved this way.
0/3 solved
Recognise it
The signal
system of linear equations modulo a prime
1Brute force
Cramer's rule / permutation expansion: O(n!).
2The observation
Mutual dependencies between unknowns rule out a DP order.
3Why it works
Row operations preserve the solution set. The determinant flips sign on a row swap and scales with row scaling.
Reach for it when
- +count spanning trees of a graph with n <= 500
- +expected steps on a graph with cycles (states depend on each other)
- +determinant of a matrix
Not when
- ×equations over GF(2) (xor-basis / bitset-opt)
- ×the state graph is a DAG (plain DP)
Practice
- Broken robotCodeforces2400
- System of Linear EquationsCSESHard
- Stranger TreesCodeforces2600
Pitfalls
Misconceptions
- !pivoting on zero (pick a non-zero pivot; use partial pivoting with reals)
- !forgetting to drop one row and column of the Laplacian
Go deeper2 combinations · resources
Combinations
Probability & Expected-Value DP (linearity of expectation)+Gaussian Elimination (mod p / reals) & Matrix-Tree Theorem
expected steps of a random walk with self-loops or sideways moves
- Probability & Expected-Value DP (linearity of expectation):
- E[state] = 1 + sum of p * E[next]
- Gaussian Elimination (mod p / reals) & Matrix-Tree Theorem:
- solve the system when states reference each other
Gaussian Elimination (mod p / reals) & Matrix-Tree Theorem+Lagrange Interpolation
count spanning trees sharing exactly k edges with a given tree, n <= 100
- Gaussian Elimination (mod p / reals) & Matrix-Tree Theorem:
- matrix-tree determinants for spanning trees with weighted edges
- Lagrange Interpolation:
- recover the polynomial in x from n evaluations