TExcelTAD

Effective Resistance

Seen from two vertices ss and tt, a whole resistor network behaves like a single resistor. Its value, the effective resistance Reff(s,t)R_{\text{eff}}(s, t), 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 ss and tt, 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 ss to tt: solve Lx=bstLx = b_{st} with bst=es−etb_{st} = \mathbf{e}_s - \mathbf{e}_t (Graphs as Resistor Networks). The voltage needed is

Reff(s,t)=xs−xt=bstTL+bst=(L+)ss+(L+)tt−2(L+)st.R_{\text{eff}}(s, t) = x_s - x_t = b_{st}^{\mathsf{T}} L^+ b_{st} = (L^+)_{ss} + (L^+)_{tt} - 2(L^+)_{st} .

Equivalently, Reff(s,t)R_{\text{eff}}(s, t) is the energy of the unit electrical ss-tt flow, the minimum of ∑erege2\sum_e r_e g_e^2 over unit ss-tt flows gg (Thomson). The familiar rules follow: resistances in series add, conductances in parallel add. Another useful formula: Reff(s,t)=∥L+/2bst∥2R_{\text{eff}}(s, t) = \lVert L^{+/2} b_{st} \rVert^2, so the vertices embed in Rn\mathbb{R}^n with squared Euclidean distances equal to effective resistances.

It is a metric

Reff(u,v)≥0R_{\text{eff}}(u, v) \ge 0 with equality iff u=vu = v (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 Reff(u,w)≤Reff(u,v)+Reff(v,w)R_{\text{eff}}(u, w) \le R_{\text{eff}}(u, v) + R_{\text{eff}}(v, w). Since buw=buv+bvwb_{uw} = b_{uv} + b_{vw}, we have Reff(u,w)=buwTL+buv+buwTL+bvwR_{\text{eff}}(u, w) = b_{uw}^{\mathsf{T}} L^+ b_{uv} + b_{uw}^{\mathsf{T}} L^+ b_{vw}. Let x=L+buvx = L^+ b_{uv}, the potentials of a unit current from uu to vv. By the maximum principle for harmonic functions, xx is largest at uu and smallest at vv, so buwTx=xu−xw≤xu−xv=Reff(u,v)b_{uw}^{\mathsf{T}} x = x_u - x_w \le x_u - x_v = R_{\text{eff}}(u, v). The second term is bounded by Reff(v,w)R_{\text{eff}}(v, w) in the same way. □\square

Effective resistance is at most the shortest-path distance (with lengths rer_e), with equality on trees, and it is smaller when there are many disjoint paths: two vertices joined by kk edge-disjoint paths of length ℓ\ell have Reff≤ℓ/kR_{\text{eff}} \le \ell/k. Rayleigh's monotonicity law says that lowering any resistance (adding or strengthening an edge) cannot increase any effective resistance.

python · try it yourself
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 nn vertices, ∑e∈EweReff(e)=n−1\sum_{e \in E} w_e R_{\text{eff}}(e) = n - 1.

Proof. ∑ewebeTL+be=tr⁡(L+∑ewebebeT)=tr⁡(L+L)=rank⁡(L)=n−1\sum_e w_e b_e^{\mathsf{T}} L^+ b_e = \operatorname{tr}\big(L^+ \sum_e w_e b_eb_e^{\mathsf{T}}\big) = \operatorname{tr}(L^+ L) = \operatorname{rank}(L) = n - 1, since L+LL^+ L is the projection onto 1⊥\mathbf{1}^\perp. □\square

The quantity weReff(e)w_e R_{\text{eff}}(e) is the leverage score of edge ee, between 0 and 1. For an unweighted graph it has a beautiful meaning, due to Kirchhoff (1847): Reff(e)R_{\text{eff}}(e) is the probability that ee belongs to a uniformly random spanning tree. Foster's theorem is then the statement that a spanning tree has n−1n - 1 edges. A bridge has Reff=1R_{\text{eff}} = 1 (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 LL, and the number containing e=uve = uv equals the total minus the number of trees of G−eG - e.

python · try it yourself
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 κ(s,t)\kappa(s, t) is the expected number of steps for the random walk from ss to reach tt and return to ss.

Theorem (Chandra, Raghavan, Ruzzo, Smolensky and Tiwari, 1989). For an unweighted connected graph with mm edges, κ(s,t)=2m Reff(s,t)\kappa(s, t) = 2m\, R_{\text{eff}}(s, t). (With weights, 2m2m becomes the total weighted degree ∑vdv=2∑ewe\sum_v d_v = 2\sum_e w_e.)

Idea. Inject dvd_v units of current at every vertex and remove 2m2m at tt; the resulting potentials xv−xtx_v - x_t satisfy the same equations as the expected hitting times H(v,t)H(v, t) of tt. Doing the same with ss and adding the two systems gives H(s,t)+H(t,s)=2m bstTL+bstH(s, t) + H(t, s) = 2m\, b_{st}^{\mathsf{T}} L^+ b_{st}.

Consequences: the cover time (expected time to visit all vertices) is at most 2m(n−1)2m(n - 1) (walk around a spanning tree; Aleliunas et al., 1979) and lies between m Rmax⁡m\, R_{\max} and O(m Rmax⁡log⁡n)O(m\, R_{\max}\log n) with Rmax⁡=max⁡s,tReff(s,t)R_{\max} = \max_{s,t} R_{\text{eff}}(s, t). The lollipop graph (a clique with a long path attached) has cover time Θ(n3)\Theta(n^3) because the path end is far in resistance and the clique holds most of the edges.

python · try it yourself
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 tt, solve for a unit current from ss, and read xsx_s. The program checks Foster's theorem on the 4×44 \times 4 grid, whose boundary edges carry more of the tree weight than its central ones.

cpp · try it yourself
#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 Reff(s,t)R_{\text{eff}}(s,t)?

  1. (L−1)st(L^{-1})_{st}
  2. (es−et)TL+(es−et)(\mathbf{e}_s - \mathbf{e}_t)^{\mathsf{T}} L^+ (\mathbf{e}_s - \mathbf{e}_t)
  3. The shortest-path distance
  4. 1/λ21/\lambda_2

What does Foster's theorem say for a connected graph?

  1. ∑eReff(e)=m\sum_e R_{\text{eff}}(e) = m
  2. ∑eweReff(e)=n−1\sum_e w_e R_{\text{eff}}(e) = n - 1
  3. max⁡eReff(e)≤1/n\max_e R_{\text{eff}}(e) \le 1/n
  4. ∑s,tReff(s,t)=n2\sum_{s,t} R_{\text{eff}}(s,t) = n^2

In an unweighted graph, what is Reff(e)R_{\text{eff}}(e) for an edge ee?

  1. The length of the shortest cycle through ee
  2. The probability that ee lies in a uniformly random spanning tree
  3. The degree of its endpoints
  4. Always 1

What is the expected commute time between ss and tt in an unweighted graph with mm edges?

  1. m Reff(s,t)m\, R_{\text{eff}}(s,t)
  2. 2m Reff(s,t)2m\, R_{\text{eff}}(s,t)
  3. n Reff(s,t)n\, R_{\text{eff}}(s,t)
  4. Reff(s,t)2R_{\text{eff}}(s,t)^2