Sparsification by Effective Resistances
A spectral sparsifier of a graph is a sparse weighted graph on the same vertices whose Laplacian quadratic form is within a factor of 's for every vector. The lesson Spectral Sparsifiers by Effective Resistances in the streaming course stated the Spielman-Srivastava theorem and ran it. Here we open the box: the linear algebra that makes effective resistances the right sampling weights, the matrix concentration bound behind the theorem, how to estimate all resistances quickly with a random projection, and the deterministic linear-size construction of Batson, Spielman and Srivastava, implemented on a small example.
Spectral approximation
Write if for all (the Loewner order). is a -spectral sparsifier of if
By Courant-Fischer this implies that every eigenvalue, every cut (), every effective resistance (up to ) and the solution of every Laplacian system are approximately preserved; and is a preconditioner with relative condition number . Spielman and Teng (2004) introduced the notion, with sparsifiers of edges, as an ingredient of their solver. For a sparsifier is an expander: the Ramanujan graphs of the constructions lesson are, after scaling, sparsifiers of the complete graph with .
Leverage scores and the projection
Let be the incidence matrix and the diagonal edge weights, so . Define the matrix
is the orthogonal projection onto the column space of (check: ), which has dimension . Its diagonal entries are the leverage scores , and is Foster's theorem again. In statistics language, sparsification is row sampling of the matrix by leverage scores, a technique that also underlies fast least-squares solvers.
Whitening makes the role of the leverage scores clear. With ,
is a -sparsifier exactly when its reweighted sum has all eigenvalues (on the range) in . So sparsification is the problem of approximating the identity by a few of the rank-one matrices , and an edge's leverage score is its "share" of the identity.
import numpy as np
# two 5-cliques joined by a bridge (4-5) plus one extra edge (0-9) between them
edges = [(a + s, b + s) for s in (0, 5) for a in range(5) for b in range(a + 1, 5)] + [(4, 5), (0, 9)]
n, m = 10, len(edges)
B = np.zeros((m, n))
for i, (u, v) in enumerate(edges):
B[i, u], B[i, v] = 1, -1
L = B.T @ B
Pi = B @ np.linalg.pinv(L) @ B.T
lev = np.diag(Pi)
print("Pi is a projection:", bool(np.allclose(Pi @ Pi, Pi)), " trace =", round(float(np.trace(Pi)), 6), "= n - 1")
print("leverage scores inside a clique:", np.round(lev[:3], 4) + 0.0)
print("leverage of the two connecting edges 4-5 and 0-9:", np.round(lev[-2:], 4) + 0.0)Pi is a projection: True trace = 9.0 = n - 1
leverage scores inside a clique: [0.3857 0.3857 0.3857]
leverage of the two connecting edges 4-5 and 0-9: [0.6429 0.6429]
The two connecting edges each carry leverage about , against for an edge inside a clique, whose ends are joined by many parallel routes. A sparsifier that dropped both connecting edges would disconnect the halves, so they must be sampled with high probability; uniform sampling would treat all 22 edges alike.
The Spielman-Srivastava theorem, with the concentration step
Theorem (Spielman and Srivastava, 2008; journal version 2011). Sample edges independently with replacement, edge with probability , and give each sampled copy weight . If for a suitable absolute constant , then is a -sparsifier of with probability at least .
Proof outline. The -th sample contributes the random matrix (in whitened coordinates). Then , and because is proportional to , every possible has the same norm . The matrix Chernoff bound (Tropp, 2012, building on Rudelson, 1999, and Ahlswede and Winter, 2002) says: for independent positive semidefinite matrices with and ,
With and , the failure probability is below once .
Sampling by any upper bounds also works, with multiplied by . Koutis, Miller and Peng used tree stretch as such an upper bound, which needs no solver at all.
Estimating all resistances at once
Computing costs . Spielman and Srivastava observed that
a squared distance between columns of the matrix . By the Johnson-Lindenstrauss lemma, multiplying on the left by a random matrix with entries , , preserves all these distances within . Computing needs only Laplacian solves (one per row: ), so all resistances are approximated in time with a nearly-linear solver.
import numpy as np
rng = np.random.default_rng(2008)
n = 60
edges = [(u, v) for u in range(n) for v in range(u + 1, n) if rng.random() < 0.15]
m = len(edges)
B = np.zeros((m, n))
for i, (u, v) in enumerate(edges):
B[i, u], B[i, v] = 1, -1
L = B.T @ B
Lp = np.linalg.pinv(L)
exact = np.array([Lp[u, u] + Lp[v, v] - 2 * Lp[u, v] for u, v in edges])
print(f"n = {n}, m = {m}; exact resistances from {exact.min():.4f} to {exact.max():.4f}")
for k in [24, 96, 384]:
Q = rng.choice([-1.0, 1.0], size=(k, m)) / np.sqrt(k)
Z = (Q @ B) @ Lp # in a real solver: k Laplacian solves, one per row
approx = np.array([float(np.sum((Z[:, u] - Z[:, v]) ** 2)) for u, v in edges])
rel = np.abs(approx / exact - 1)
print(f"k = {k:3} random rows: median relative error {np.median(rel):.3f}, worst {rel.max():.3f}")n = 60, m = 258; exact resistances from 0.1466 to 0.5592
k = 24 random rows: median relative error 0.211, worst 0.920
k = 96 random rows: median relative error 0.113, worst 0.486
k = 384 random rows: median relative error 0.053, worst 0.207
The error shrinks like , as the Johnson-Lindenstrauss lemma predicts. For sampling, a constant-factor approximation of each resistance is enough (it only changes by a constant), so a small suffices.
Linear-size sparsifiers: Batson-Spielman-Srivastava
Sampling needs edges because of the coupon-collector effect. Batson, Spielman and Srivastava (2009; journal 2012) removed the logarithm with a deterministic barrier-potential argument.
Theorem (Batson, Spielman and Srivastava, 2009). For every , every graph has a weighted subgraph with at most edges such that (after scaling), with
and can be found in polynomial time. For this gives graphs with average degree and condition number about : comparable to Ramanujan graphs, which achieve it with half as many edges. Hence the name twice-Ramanujan sparsifiers. Lee and Sun (2017) found a nearly-linear-time construction of linear-size sparsifiers.
The algorithm adds one rank-one matrix at a time to (starting from ) while keeping all eigenvalues of strictly between a lower barrier and an upper barrier that both move right by fixed amounts each step. The potentials and measure how close the eigenvalues are to the barriers; a short averaging argument shows that some edge and weight always keep both potentials from growing. After steps the ratio is at most .
import numpy as np
rng = np.random.default_rng(1)
n = 30
edges = [(u, v) for u in range(n) for v in range(u + 1, n) if rng.random() < 0.8] # a dense random graph
m, N = len(edges), n - 1
L = np.zeros((n, n))
for u, v in edges:
L[u, u] += 1; L[v, v] += 1; L[u, v] -= 1; L[v, u] -= 1
vals, vecs = np.linalg.eigh(L)
Qw = vecs[:, 1:] / np.sqrt(vals[1:]) # whitening on the range of L
Y = np.array([Qw[u] - Qw[v] for u, v in edges]) # y_e, with sum y_e y_e^T = I
def condition(weights):
LH = np.zeros((n, n))
for (u, v), w in zip(edges, weights):
LH[u, u] += w; LH[v, v] += w; LH[u, v] -= w; LH[v, u] -= w
ev = np.linalg.eigvalsh(Qw.T @ LH @ Qw)
return float(ev[-1] / ev[0]) if ev[0] > 1e-9 else float("inf")
def bss(d):
eps_l, eps_u = 1 / np.sqrt(d), (np.sqrt(d) - 1) / (d + np.sqrt(d))
step_l, step_u = 1.0, (np.sqrt(d) + 1) / (np.sqrt(d) - 1)
low, up = -N / eps_l, N / eps_u
A, s, I = np.zeros((N, N)), np.zeros(m), np.eye(N)
for _ in range(int(d * N)):
up2, low2 = up + step_u, low + step_l
Ui, Li = np.linalg.inv(up2 * I - A), np.linalg.inv(A - low2 * I)
dphi_u = np.trace(np.linalg.inv(up * I - A)) - np.trace(Ui)
dphi_l = np.trace(Li) - np.trace(np.linalg.inv(A - low * I))
quad = lambda M: np.einsum("ij,jk,ik->i", Y, M, Y)
U = quad(Ui @ Ui) / dphi_u + quad(Ui) # need 1/t >= U(y_e)
Lo = quad(Li @ Li) / dphi_l - quad(Li) # and 1/t <= L(y_e)
e = int(np.argmax(np.round(Lo - U, 9))) # an edge with U <= L exists
t = 2 / (U[e] + Lo[e])
A += t * np.outer(Y[e], Y[e]); s[e] += t
up, low = up2, low2
return s
lev = (Y ** 2).sum(axis=1) # leverage scores, sum n - 1
p = lev / lev.sum()
print(f"G(30, 0.8): {m} edges")
for d in [3, 6]:
s = bss(d)
k = int((s > 0).sum())
w = np.zeros(m)
for e in rng.choice(m, size=k, p=p): # same budget, Spielman-Srivastava
w[e] += 1 / (k * p[e])
bound = (d + 1 + 2 * np.sqrt(d)) / (d + 1 - 2 * np.sqrt(d))
print(f"d = {d}: BSS keeps {k} edges, condition number {condition(s):.3f} (guarantee {bound:.3f}); "
f"resistance sampling with {k} draws: {condition(w):.3f}")G(30, 0.8): 353 edges
d = 3: BSS keeps 87 edges, condition number 3.072 (guarantee 13.928); resistance sampling with 87 draws: inf
d = 6: BSS keeps 174 edges, condition number 2.023 (guarantee 5.663); resistance sampling with 174 draws: 5.705
With the same edge budget, the barrier method's sparsifier is better conditioned than resistance sampling, and far better than its own worst-case guarantee. With only 87 draws, sampling even leaves the graph disconnected (condition number infinite): some vertex lost all its edges, the coupon-collector effect behind the . Sampling with replacement also wastes part of its budget on repeated edges.
Test yourself
What is the leverage score of edge in sparsification?
- Its weight
- , the diagonal entry of the projection
- The degree of its endpoints
Why does sampling proportionally to suit the matrix Chernoff bound?
- It minimizes the number of distinct edges
- Every sampled whitened term then has the same norm , the smallest possible maximum norm
- It makes the samples deterministic
- It avoids computing
How can all effective resistances be approximated quickly?
- By computing exactly
- By projecting with a random matrix, , which needs only Laplacian solves
- By BFS
- By counting spanning trees
What did Batson, Spielman and Srivastava (2009) achieve?
- Sparsifiers with edges by sampling
- Deterministic sparsifiers with edges and condition number , i.e. edges
- Exact cut sparsifiers
- Sparsifiers of directed graphs