Eigenvalue Zero and Connected Components
The first theorem of spectral graph theory connects the bottom of the Laplacian spectrum to the most basic graph property, connectivity: the number of zero eigenvalues equals the number of connected components. In particular exactly when the graph is connected. This lesson proves it, turns "how far from zero" into a quantitative bound involving the diameter, and looks at what goes wrong when eigenvalues are computed in floating point.
The theorem
Theorem. Let be a graph with positive edge weights and Laplacian . The multiplicity of the eigenvalue 0 of equals the number of connected components of . The same holds for the normalized Laplacian (when there are no isolated vertices).
Proof. Because is symmetric, the multiplicity of 0 is . Since is positive semidefinite, if and only if (one direction is trivial; for the other, write , then forces and so ). Now
holds if and only if along every edge, that is, is constant on each connected component. If the components are , the kernel is therefore spanned by the indicator vectors , which are nonzero and have disjoint supports, hence linearly independent. So . For the kernel is , of the same dimension.
Corollary. is connected if and only if . The quantity is called the algebraic connectivity (Fiedler, 1973).
The proof also says what the eigenvectors of 0 look like: any orthonormal basis of the kernel is a rotation of the normalized indicator vectors. A numerical solver may return any such rotation, so the kernel vectors themselves are not unique, but the rows of the matrix of kernel vectors are equal for vertices in the same component and orthogonal across components. That observation is the seed of spectral clustering: with well-separated clusters, the bottom eigenvectors are close to a rotation of the cluster indicators.
from collections import deque
import numpy as np
def laplacian(n, edges):
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
return L
def bfs_components(n, edges):
adj = [[] for _ in range(n)]
for u, v in edges:
adj[u].append(v); adj[v].append(u)
comp, c = [-1] * n, 0
for s in range(n):
if comp[s] < 0:
comp[s], queue = c, deque([s])
while queue:
u = queue.popleft()
for v in adj[u]:
if comp[v] < 0:
comp[v] = c; queue.append(v)
c += 1
return comp
# three pieces: a triangle {0, 3, 6}, a path 1-4-7, and the pair {2, 5}; vertex 8 is isolated
n = 9
edges = [(0, 3), (3, 6), (6, 0), (1, 4), (4, 7), (2, 5)]
vals, vecs = np.linalg.eigh(laplacian(n, edges))
zero = vals < 1e-9
print("eigenvalues:", np.round(vals, 4) + 0.0)
print("zero eigenvalues:", int(zero.sum()), " BFS components:", max(bfs_components(n, edges)) + 1)
K = vecs[:, zero] # an orthonormal basis of the kernel
rows = np.round(K @ K.T, 4) + 0.0 # equal rows <=> same component
labels, seen = [], {}
for v in range(n):
key = tuple(rows[v])
labels.append(seen.setdefault(key, len(seen)))
print("components read from the kernel:", labels)
print("components from BFS: ", bfs_components(n, edges))eigenvalues: [0. 0. 0. 0. 1. 2. 3. 3. 3.]
zero eigenvalues: 4 BFS components: 4
components read from the kernel: [0, 1, 2, 0, 1, 2, 0, 1, 3]
components from BFS: [0, 1, 2, 0, 1, 2, 0, 1, 3]
The kernel has dimension 4 (three pieces plus the isolated vertex), and grouping vertices by their rows in the kernel basis recovers the same partition as BFS. The product is used because it does not depend on which orthonormal basis the solver returned: it is the projection onto the kernel.
How large is for a connected graph?
Connectivity says ; a bound in terms of the graph's shape says how far from 0.
Claim. If is connected, unweighted, with diameter , then .
Proof. Let be a unit eigenvector for , so and . Some entry has , and since the entries sum to zero there are entries of both signs, so . Take a shortest path from a vertex with to a vertex with ; . By Cauchy-Schwarz,
Mohar (1991) sharpened the constant to (stated here without proof). The path shows the order of magnitude is right: there and , between and .
import numpy as np
from collections import deque
def laplacian(n, edges):
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
return L
def diameter(n, edges):
adj = [[] for _ in range(n)]
for u, v in edges:
adj[u].append(v); adj[v].append(u)
best = 0
for s in range(n):
dist, queue = {s: 0}, deque([s])
while queue:
u = queue.popleft()
for v in adj[u]:
if v not in dist:
dist[v] = dist[u] + 1; queue.append(v)
best = max(best, max(dist.values()))
return best
graphs = {
"path P_20": (20, [(i, i + 1) for i in range(19)]),
"cycle C_20": (20, [(i, (i + 1) % 20) for i in range(20)]),
"star K_{1,19}": (20, [(0, i) for i in range(1, 20)]),
"two K_10 + bridge": (20, [(a + s, b + s) for s in (0, 10) for a in range(10) for b in range(a + 1, 10)] + [(9, 10)]),
}
print("graph diam lambda_2 1/(n diam) 4/(n diam)")
for name, (n, edges) in graphs.items():
d = diameter(n, edges)
lam2 = float(np.linalg.eigvalsh(laplacian(n, edges))[1])
print(f"{name:18} {d:5} {lam2:8.4f} {1 / (n * d):10.4f} {4 / (n * d):10.4f}")graph diam lambda_2 1/(n diam) 4/(n diam)
path P_20 19 0.0246 0.0026 0.0105
cycle C_20 10 0.0979 0.0050 0.0200
star K_{1,19} 2 1.0000 0.0250 0.1000
two K_10 + bridge 3 0.1690 0.0167 0.0667
The bound is within a factor of 10 for the path and the cycle. The graph "two joined by a bridge" has diameter only 3, as small as a dense graph's, yet its is more than a hundred times smaller than that of (which is 20). Diameter alone cannot see a bottleneck; that is the job of conductance and Cheeger's inequality in the next section.
Zero in floating point
Numerically, "is ?" is a question about a tolerance. A connected graph with a very weak link has a tiny but positive , and rounding errors of size about can make an exact zero look like . Counting components with eigenvalues is therefore a teaching device, not an algorithm: BFS answers the question exactly in time. Spectral information becomes valuable for questions BFS cannot answer, such as how well a connected graph is connected.
import numpy as np
def weighted_laplacian(n, wedges):
L = np.zeros((n, n))
for u, v, w in wedges:
L[u, u] += w; L[v, v] += w; L[u, v] -= w; L[v, u] -= w
return L
# two triangles joined by one edge of weight eps
for eps in [1e-2, 1e-6, 1e-10, 1e-14, 0.0]:
wedges = [(0, 1, 1), (1, 2, 1), (2, 0, 1), (3, 4, 1), (4, 5, 1), (5, 3, 1), (2, 3, eps)]
lam = np.linalg.eigvalsh(weighted_laplacian(6, wedges))
lam2 = float(lam[1])
if abs(lam2) < 1e-13:
print(f"eps = {eps:7.0e}: |lambda_2| < 1e-13, at the level of rounding error")
else:
print(f"eps = {eps:7.0e}: lambda_2 = {lam2:.2e} lambda_2 / eps = {lam2 / eps:.4f}")eps = 1e-02: lambda_2 = 6.64e-03 lambda_2 / eps = 0.6637
eps = 1e-06: lambda_2 = 6.67e-07 lambda_2 / eps = 0.6667
eps = 1e-10: lambda_2 = 6.67e-11 lambda_2 / eps = 0.6667
eps = 1e-14: |lambda_2| < 1e-13, at the level of rounding error
eps = 0e+00: |lambda_2| < 1e-13, at the level of rounding error
For moderate , exactly as perturbation theory predicts (the test vector on the two triangles has Rayleigh quotient ). At the connected graph and the disconnected one () both report a value at the level of rounding error (its exact digits depend on the machine and the library), and a threshold-based component count can no longer tell them apart.
Test yourself
A graph's Laplacian has eigenvalues . How many connected components does it have?
- 1
- 2
- 3
- 6
In the proof, why does force to be constant on each component?
- Because is invertible
- Because it is a sum of nonnegative terms , each of which must vanish
- Because
- Because has zero trace
Which lower bound on was proved for a connected unweighted graph with diameter ?
- always
Why is BFS, not eigenvalue counting, the right algorithm for counting components?
- Eigenvalues can be negative
- BFS is exact and takes time; numerically, a weakly connected graph's can be indistinguishable from 0
- Eigenvalues do not determine components
- BFS also computes