Effective Resistance
Seen from two vertices and , a whole resistor network behaves like a single resistor. Its value, the effective resistance , turns out to be one of the most informative numbers attached to a pair of vertices: it is a distance (a metric on the vertices), it measures how many independent routes connect and , it gives the probability that an edge lies in a random spanning tree, and it gives the expected commute time of a random walk. It is also the sampling weight in spectral sparsification.
Definition
Send one unit of current from to : solve with (Graphs as Resistor Networks). The voltage needed is
Equivalently, is the energy of the unit electrical - flow, the minimum of over unit - flows (Thomson). The familiar rules follow: resistances in series add, conductances in parallel add. Another useful formula: , so the vertices embed in with squared Euclidean distances equal to effective resistances.
It is a metric
with equality iff (in a connected graph), and it is symmetric. The triangle inequality (Klein and Randić, 1993) needs an argument, because squared Euclidean distances are not metrics in general.
Proof of . Since , we have . Let , the potentials of a unit current from to . By the maximum principle for harmonic functions, is largest at and smallest at , so . The second term is bounded by in the same way.
Effective resistance is at most the shortest-path distance (with lengths ), with equality on trees, and it is smaller when there are many disjoint paths: two vertices joined by edge-disjoint paths of length have . Rayleigh's monotonicity law says that lowering any resistance (adding or strengthening an edge) cannot increase any effective resistance.
import itertools
import random
import numpy as np
def 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
def reff_matrix(L):
P = np.linalg.pinv(L)
dg = np.diag(P)
return dg[:, None] + dg[None, :] - 2 * P
# series and parallel: two 1-ohm resistors
print("series (path 0-1-2): R(0,2) =", round(float(reff_matrix(laplacian(3, [(0, 1, 1), (1, 2, 1)]))[0, 2]), 6))
print("parallel (two edges): R(0,1) =", round(float(reff_matrix(laplacian(2, [(0, 1, 1), (0, 1, 1)]))[0, 1]), 6))
print("cycle C_6, opposite: R(0,3) =", round(float(reff_matrix(laplacian(6, [(i, (i + 1) % 6, 1) for i in range(6)]))[0, 3]), 6),
"(two paths of 3 ohm in parallel)")
rng = random.Random(5)
n = 12
wedges = [(u, v, 1.0) for u in range(n) for v in range(u + 1, n) if rng.random() < 0.3]
R = reff_matrix(laplacian(n, wedges))
worst = max(R[u, w] - R[u, v] - R[v, w] for u, v, w in itertools.permutations(range(n), 3))
print(f"random graph, {len(wedges)} edges: max over all triples of R(u,w) - R(u,v) - R(v,w) = {round(float(worst), 4) + 0.0:.4f} (<= 0)")
before = R[0, 1]
R2 = reff_matrix(laplacian(n, wedges + [(5, 9, 1.0)]))
print(f"Rayleigh: adding edge 5-9 changes R(0,1) from {before:.4f} to {R2[0, 1]:.4f}; "
f"no pair's resistance increased: {bool((R2 - R).max() <= 1e-12)}")series (path 0-1-2): R(0,2) = 2.0
parallel (two edges): R(0,1) = 0.5
cycle C_6, opposite: R(0,3) = 1.5 (two paths of 3 ohm in parallel)
random graph, 22 edges: max over all triples of R(u,w) - R(u,v) - R(v,w) = 0.0000 (<= 0)
Rayleigh: adding edge 5-9 changes R(0,1) from 1.3917 to 1.3916; no pair's resistance increased: True
Foster's theorem and spanning trees
Theorem (Foster, 1949). For a connected graph on vertices, .
Proof. , since is the projection onto .
The quantity is the leverage score of edge , between 0 and 1. For an unweighted graph it has a beautiful meaning, due to Kirchhoff (1847): is the probability that belongs to a uniformly random spanning tree. Foster's theorem is then the statement that a spanning tree has edges. A bridge has (in every tree), an edge of a dense cluster a small value. The program checks both facts, counting spanning trees with the matrix-tree theorem: the number of spanning trees is any cofactor of , and the number containing equals the total minus the number of trees of .
import random
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 trees(n, edges):
return round(float(np.linalg.det(laplacian(n, edges)[1:, 1:]))) # matrix-tree theorem
rng = random.Random(1847)
n = 8
edges = [(u, v) for u in range(n) for v in range(u + 1, n) if rng.random() < 0.45] + [(7, 8)]
n = 9 # vertex 8 hangs off vertex 7
L = laplacian(n, edges)
P = np.linalg.pinv(L)
total = trees(n, edges)
print(f"{len(edges)} edges, {total} spanning trees")
print(" edge R_eff trees containing it / total")
for u, v in edges[:5] + edges[-1:]:
r = P[u, u] + P[v, v] - 2 * P[u, v]
with_e = total - trees(n, [e for e in edges if e != (u, v)])
print(f" {u}-{v} {r:.4f} {with_e}/{total} = {with_e / total:.4f}")
foster = sum(P[u, u] + P[v, v] - 2 * P[u, v] for u, v in edges)
print(f"Foster: sum of R_eff over edges = {foster:.4f} = n - 1 = {n - 1}")18 edges, 4014 spanning trees
edge R_eff trees containing it / total
0-1 0.4641 1863/4014 = 0.4641
0-3 0.4350 1746/4014 = 0.4350
0-4 0.4342 1743/4014 = 0.4342
1-3 0.3640 1461/4014 = 0.3640
1-4 0.3617 1452/4014 = 0.3617
7-8 1.0000 4014/4014 = 1.0000
Foster: sum of R_eff over edges = 8.0000 = n - 1 = 8
Commute times
The commute time is the expected number of steps for the random walk from to reach and return to .
Theorem (Chandra, Raghavan, Ruzzo, Smolensky and Tiwari, 1989). For an unweighted connected graph with edges, . (With weights, becomes the total weighted degree .)
Idea. Inject units of current at every vertex and remove at ; the resulting potentials satisfy the same equations as the expected hitting times of . Doing the same with and adding the two systems gives .
Consequences: the cover time (expected time to visit all vertices) is at most (walk around a spanning tree; Aleliunas et al., 1979) and lies between and with . The lollipop graph (a clique with a long path attached) has cover time because the path end is far in resistance and the clique holds most of the edges.
import random
import numpy as np
# a "lollipop": clique on 0..5, path 5-6-7-8-9
edges = [(a, b) for a in range(6) for b in range(a + 1, 6)] + [(5, 6), (6, 7), (7, 8), (8, 9)]
n, m = 10, len(edges)
L = np.zeros((n, n))
adj = [[] for _ in range(n)]
for u, v in edges:
L[u, u] += 1; L[v, v] += 1; L[u, v] -= 1; L[v, u] -= 1
adj[u].append(v); adj[v].append(u)
P = np.linalg.pinv(L)
rng = random.Random(1989)
def hitting_time(s, t):
u, steps = s, 0
while u != t:
u = rng.choice(adj[u]); steps += 1
return steps
for s, t in [(0, 5), (0, 9), (5, 9)]:
R = P[s, s] + P[t, t] - 2 * P[s, t]
runs = 2000
commute = sum(hitting_time(s, t) + hitting_time(t, s) for _ in range(runs)) / runs
print(f"s = {s}, t = {t}: R_eff = {R:.4f}, 2 m R_eff = {2 * m * R:7.2f}, simulated commute time {commute:7.2f}")s = 0, t = 5: R_eff = 0.3333, 2 m R_eff = 12.67, simulated commute time 12.64
s = 0, t = 9: R_eff = 4.3333, 2 m R_eff = 164.67, simulated commute time 165.02
s = 5, t = 9: R_eff = 4.0000, 2 m R_eff = 152.00, simulated commute time 149.81
In C++
All edge resistances of a small graph, by solving one grounded system per edge with Gaussian elimination (no pseudoinverse needed): ground , solve for a unit current from , and read . The program checks Foster's theorem on the grid, whose boundary edges carry more of the tree weight than its central ones.
#include <cmath>
#include <cstdio>
#include <utility>
#include <vector>
using Matrix = std::vector<std::vector<double>>;
// Solve M y = r by Gaussian elimination with partial pivoting (M is small and dense).
std::vector<double> solve(Matrix M, std::vector<double> r) {
int n = (int)M.size();
for (int c = 0; c < n; ++c) {
int p = c;
for (int i = c + 1; i < n; ++i) if (std::fabs(M[i][c]) > std::fabs(M[p][c])) p = i;
std::swap(M[c], M[p]); std::swap(r[c], r[p]);
for (int i = c + 1; i < n; ++i) {
double f = M[i][c] / M[c][c];
for (int j = c; j < n; ++j) M[i][j] -= f * M[c][j];
r[i] -= f * r[c];
}
}
std::vector<double> y(n);
for (int i = n - 1; i >= 0; --i) {
double s = r[i];
for (int j = i + 1; j < n; ++j) s -= M[i][j] * y[j];
y[i] = s / M[i][i];
}
return y;
}
// Effective resistance between s and t: ground t, inject 1 at s, return x_s.
double reff(const Matrix& L, int s, int t) {
int n = (int)L.size();
Matrix M;
std::vector<double> r;
for (int i = 0; i < n; ++i) {
if (i == t) continue;
std::vector<double> row;
for (int j = 0; j < n; ++j) if (j != t) row.push_back(L[i][j]);
M.push_back(row);
r.push_back(i == s ? 1.0 : 0.0);
}
std::vector<double> y = solve(M, r);
return y[s < t ? s : s - 1];
}
int main() {
const int k = 4, n = k * k;
Matrix L(n, std::vector<double>(n, 0.0));
std::vector<std::pair<int, int>> edges;
for (int i = 0; i < k; ++i)
for (int j = 0; j < k; ++j) {
if (j + 1 < k) edges.push_back({i * k + j, i * k + j + 1});
if (i + 1 < k) edges.push_back({i * k + j, (i + 1) * k + j});
}
for (auto [u, v] : edges) { L[u][u] += 1; L[v][v] += 1; L[u][v] -= 1; L[v][u] -= 1; }
double sum = 0;
for (auto [u, v] : edges) {
double r = reff(L, u, v);
sum += r;
if (u == 0 || u == 5) std::printf("edge %2d-%2d: R_eff = %.4f\n", u, v, r);
}
std::printf("%zu edges, sum of R_eff = %.6f, n - 1 = %d\n", edges.size(), sum, n - 1);
std::printf("R_eff between opposite corners 0 and %d: %.4f\n", n - 1, reff(L, 0, n - 1));
}edge 0- 1: R_eff = 0.7009
edge 0- 4: R_eff = 0.7009
edge 5- 6: R_eff = 0.5446
edge 5- 9: R_eff = 0.5446
24 edges, sum of R_eff = 15.000000, n - 1 = 15
R_eff between opposite corners 0 and 15: 1.8571
Test yourself
Which formula gives ?
- The shortest-path distance
What does Foster's theorem say for a connected graph?
In an unweighted graph, what is for an edge ?
- The length of the shortest cycle through
- The probability that lies in a uniformly random spanning tree
- The degree of its endpoints
- Always 1
What is the expected commute time between and in an unweighted graph with edges?