TExcelTAD
Home › Spectral Graph Theory & Modern Graph Algorithms › Graphs as Resistor Networks

Graphs as Resistor Networks

Replace every edge of a graph by a resistor, connect a battery between two vertices, and the currents that flow are the solution of a linear system in the Laplacian. This physical picture is the starting point of the second half of the course. Electrical flows are smooth (they spread over many paths), they are optimal for a quadratic energy, and they can be computed by solving Lx=bLx = b, which is possible in nearly linear time. Modern algorithms for cuts, flows and sampling use them as a primitive. This lesson sets up the dictionary between circuits and linear algebra; Resistor Networks in the linear-algebra course is the elementary version.

Ohm, Kirchhoff and the Laplacian

Give edge e={u,v}e = \{u, v\} a resistance re>0r_e > 0 and conductance we=1/rew_e = 1/r_e (the edge weight). Inject an external current bub_u at each vertex uu (positive: current enters, negative: leaves), with ∑ubu=0\sum_u b_u = 0. The unknowns are potentials (voltages) xux_u and currents fuvf_{uv} on edges, oriented so that fuv=−fvuf_{uv} = -f_{vu}.

  • Ohm's law: fuv=wuv(xu−xv)f_{uv} = w_{uv}(x_u - x_v): current flows from high to low potential.
  • Kirchhoff's current law: the current leaving uu through edges equals the current injected: ∑v∼ufuv=bu\sum_{v \sim u} f_{uv} = b_u.

Substituting the first into the second,

∑v∼uwuv(xu−xv)=duxu−∑v∼uwuvxv=(Lx)u=bu,that is,Lx=b.\sum_{v \sim u} w_{uv}(x_u - x_v) = d_u x_u - \sum_{v \sim u} w_{uv} x_v = (Lx)_u = b_u, \qquad\text{that is,}\qquad Lx = b .

In matrix form with the signed incidence matrix BB and W=diag⁡(we)W = \operatorname{diag}(w_e): currents are f=WBxf = WBx, Kirchhoff's law is BTf=bB^{\mathsf{T}} f = b, and L=BTWBL = B^{\mathsf{T}} W B.

Existence and uniqueness. For a connected graph, ker⁡L=span⁡(1)\ker L = \operatorname{span}(\mathbf{1}), so Lx=bLx = b is solvable iff b⊥1b \perp \mathbf{1} (total injection zero), and the solution is unique up to adding a constant: potentials are only defined relative to a reference. The canonical choice is x=L+bx = L^+ b, where L+L^+ is the pseudoinverse (L+=∑i≥2λi−1viviTL^+ = \sum_{i \ge 2} \lambda_i^{-1} v_iv_i^{\mathsf{T}}), giving the solution with ∑uxu=0\sum_u x_u = 0. In practice one grounds a vertex: set xt=0x_t = 0 and solve the reduced system with row and column tt deleted, which is positive definite for a connected graph.

The program sends 1 ampere from ss to tt in a small network, solves by grounding tt, and checks both laws.

python · try it yourself
import numpy as np

names = ["s", "a", "b", "c", "t"]
# (u, v, resistance in ohms)
resistors = [("s", "a", 1), ("s", "b", 2), ("a", "b", 1), ("a", "c", 2), ("b", "c", 1), ("c", "t", 1), ("b", "t", 3)]
idx = {v: i for i, v in enumerate(names)}
n = len(names)
L = np.zeros((n, n))
for u, v, r in resistors:
    i, j, w = idx[u], idx[v], 1 / r
    L[i, i] += w; L[j, j] += w; L[i, j] -= w; L[j, i] -= w
b = np.zeros(n); b[idx["s"]], b[idx["t"]] = 1, -1        # 1 A in at s, out at t

keep = [i for i in range(n) if i != idx["t"]]               # ground t: x_t = 0
x = np.zeros(n)
x[keep] = np.linalg.solve(L[np.ix_(keep, keep)], b[keep])
print("potentials:", {v: round(float(x[idx[v]]), 4) for v in names})
for u, v, r in resistors:
    print(f"  current {u}->{v}: {(x[idx[u]] - x[idx[v]]) / r:+.4f} A")
print("Kirchhoff residual L x - b:", np.round(L @ x - b, 12) + 0.0)
xp = np.linalg.pinv(L) @ b
print("pinv solution minus grounded solution is constant:", np.round(xp - x, 6) + 0.0)
energy = sum((x[idx[u]] - x[idx[v]]) ** 2 / r for u, v, r in resistors)
print(f"energy sum r_e f_e^2 = {energy:.4f} = x^T L x = {float(x @ L @ x):.4f} = x_s - x_t = {float(x[idx['s']]):.4f}")
potentials: {'s': 1.8592, 'a': 1.2817, 'b': 1.0141, 'c': 0.662, 't': 0.0}
  current s->a: +0.5775 A
  current s->b: +0.4225 A
  current a->b: +0.2676 A
  current a->c: +0.3099 A
  current b->c: +0.3521 A
  current c->t: +0.6620 A
  current b->t: +0.3380 A
Kirchhoff residual L x - b: [0. 0. 0. 0. 0.]
pinv solution minus grounded solution is constant: [-0.96338 -0.96338 -0.96338 -0.96338 -0.96338]
energy sum r_e f_e^2 = 1.8592 = x^T L x = 1.8592 = x_s - x_t = 1.8592

The energy dissipated equals the potential difference because the current is 1: in general E=xTLx=bTx\mathcal{E} = x^{\mathsf{T}} L x = b^{\mathsf{T}} x, power = voltage times current.

Harmonic functions and random walks

At a vertex with no external current (bu=0b_u = 0), Kirchhoff's law says xu=1du∑v∼uwuvxvx_u = \frac{1}{d_u}\sum_{v \sim u} w_{uv} x_v: the potential is the weighted average of its neighbours'. Such a function is harmonic at uu. Harmonic functions obey the maximum principle (maximum and minimum are attained on the boundary, where current is injected) and are determined uniquely by their boundary values.

That gives a probabilistic reading. Fix xs=1x_s = 1, xt=0x_t = 0 (a battery instead of a current source) and let h(u)h(u) be the probability that the random walk from uu (step to vv with probability wuv/duw_{uv}/d_u) reaches ss before tt. Then h(s)=1h(s) = 1, h(t)=0h(t) = 0, and conditioning on the first step shows hh is harmonic everywhere else. By uniqueness, xu=h(u)x_u = h(u): voltages are hitting probabilities (Kakutani, 1945; developed in Doyle and Snell's Random Walks and Electric Networks, 1984).

python · try it yourself
import random
import numpy as np

names = ["s", "a", "b", "c", "t"]
resistors = [("s", "a", 1), ("s", "b", 2), ("a", "b", 1), ("a", "c", 2), ("b", "c", 1), ("c", "t", 1), ("b", "t", 3)]
idx = {v: i for i, v in enumerate(names)}
n = len(names)
W = np.zeros((n, n))
for u, v, r in resistors:
    W[idx[u], idx[v]] = W[idx[v], idx[u]] = 1 / r
L = np.diag(W.sum(axis=1)) - W

# battery: x_s = 1, x_t = 0; solve for the interior vertices
inner = [idx[v] for v in "abc"]
rhs = W[np.ix_(inner, [idx["s"]])].ravel() * 1.0
x_inner = np.linalg.solve(L[np.ix_(inner, inner)], rhs)

rng = random.Random(1945)
def hits_s_first(start):
    u = start
    while u not in (idx["s"], idx["t"]):
        u = rng.choices(range(n), weights=W[u])[0]
    return u == idx["s"]

trials = 10000
for v, xv in zip("abc", x_inner):
    est = sum(hits_s_first(idx[v]) for _ in range(trials)) / trials
    print(f"vertex {v}: potential {xv:.4f}   walk reaches s before t in {est:.4f} of {trials} walks")
vertex a: potential 0.6894   walk reaches s before t in 0.6852 of 10000 walks
vertex b: potential 0.5455   walk reaches s before t in 0.5493 of 10000 walks
vertex c: potential 0.3561   walk reaches s before t in 0.3608 of 10000 walks

Thomson's principle

Among all flows gg that satisfy the demands (BTg=bB^{\mathsf{T}} g = b), the electrical flow minimizes the energy E(g)=∑erege2\mathcal{E}(g) = \sum_e r_e g_e^2.

Proof. Any feasible gg is f+cf + c with BTc=0B^{\mathsf{T}} c = 0 (a circulation). Then E(g)=E(f)+2∑erefece+E(c)\mathcal{E}(g) = \mathcal{E}(f) + 2\sum_e r_e f_e c_e + \mathcal{E}(c), and the middle term is 2∑e(Bx)ece=2xTBTc=02\sum_e (Bx)_e c_e = 2x^{\mathsf{T}} B^{\mathsf{T}} c = 0 by Ohm's law refe=(Bx)er_e f_e = (Bx)_e. So E(g)=E(f)+E(c)≥E(f)\mathcal{E}(g) = \mathcal{E}(f) + \mathcal{E}(c) \ge \mathcal{E}(f), with equality only for c=0c = 0. □\square

This variational form is what makes electrical flows useful in optimization: the max-flow algorithm of Christiano et al. minimizes a weighted energy as a smooth proxy for congestion. It also gives Rayleigh's monotonicity law: increasing any resistance cannot decrease the energy of the optimal flow, so effective resistances can only grow.

In C++

Gauss-Seidel solves Lx=bLx = b by sweeping over the vertices and setting each potential to the value that satisfies its own Kirchhoff equation, xu←1du(bu+∑v∼uwuvxv)x_u \leftarrow \frac{1}{d_u}\big(b_u + \sum_{v \sim u} w_{uv}x_v\big), using the newest values. For a grounded Laplacian (positive definite) it always converges, but slowly on large grids: the error shrinks by about 1−O(λ2/dmax⁡)1 - O(\lambda_2/d_{\max}) per sweep. The program solves the battery problem on a 20×2020 \times 20 grid with x=1x = 1 on the left column and x=0x = 0 on the right column.

cpp · try it yourself
#include <cmath>
#include <cstdio>
#include <vector>

int main() {
    const int R = 20, C = 20;
    auto id = [&](int i, int j) { return i * C + j; };
    std::vector<double> x(R * C, 0.0);
    for (int i = 0; i < R; ++i) x[id(i, 0)] = 1.0;           // left column held at 1 V, right at 0 V
    for (int sweep = 1; sweep <= 2000; ++sweep) {
        double change = 0;
        for (int i = 0; i < R; ++i)
            for (int j = 1; j + 1 < C; ++j) {                   // interior columns are free
                double s = 0;
                int deg = 0;
                if (i > 0) { s += x[id(i - 1, j)]; ++deg; }
                if (i + 1 < R) { s += x[id(i + 1, j)]; ++deg; }
                s += x[id(i, j - 1)] + x[id(i, j + 1)];
                deg += 2;
                double nx = s / deg;                            // harmonic: average of the neighbours
                change = std::fmax(change, std::fabs(nx - x[id(i, j)]));
                x[id(i, j)] = nx;
            }
        if (sweep == 10 || sweep == 100 || sweep == 1000 || change < 1e-12) {
            std::printf("sweep %4d: largest update %.2e, x at row 0: %.4f %.4f %.4f\n",
                        sweep, change, x[id(0, 5)], x[id(0, 10)], x[id(0, 15)]);
            if (change < 1e-12) break;
        }
    }
    double current = 0;                                          // current leaving the left column
    for (int i = 0; i < R; ++i) current += x[id(i, 0)] - x[id(i, 1)];
    std::printf("total current %.6f A, so the effective resistance is %.6f ohm\n", current, 1.0 / current);
}
sweep   10: largest update 2.36e-02, x at row 0: 0.1494 0.0089 0.0003
sweep  100: largest update 2.13e-03, x at row 0: 0.6200 0.3225 0.1213
sweep 1000: largest update 7.17e-09, x at row 0: 0.7368 0.4737 0.2105
sweep 1633: largest update 9.97e-13, x at row 0: 0.7368 0.4737 0.2105
total current 1.052632 A, so the effective resistance is 0.950000 ohm

With no vertical current at all (by symmetry every row is the same), the potential falls linearly, 1−j/191 - j/19, and the network is 20 parallel chains of 19 one-ohm resistors: 19/20=0.9519/20 = 0.95 ohm. Gauss-Seidel needs hundreds of sweeps to get there; conjugate gradient and the solvers after it do much better.

Test yourself

Ohm's and Kirchhoff's laws together give which equation for the potentials xx with injected currents bb?

  1. Ax=bAx = b
  2. Lx=bLx = b with L=D−AL = D - A (edge weights = conductances)
  3. Dx=bDx = b
  4. L2x=bL^2 x = b

When does Lx=bLx = b have a solution for a connected graph?

  1. Always
  2. Exactly when ∑ubu=0\sum_u b_u = 0
  3. When b≥0b \ge 0
  4. When LL is invertible

With xs=1x_s = 1 and xt=0x_t = 0, what is the potential xux_u of another vertex?

  1. The distance from uu to tt
  2. The probability that a random walk from uu hits ss before tt
  3. The degree of uu
  4. The expected time to reach tt

What does Thomson's principle say?

  1. The electrical flow maximizes the flow value
  2. Among all flows meeting the demands, the electrical flow has the least energy ∑erege2\sum_e r_e g_e^2
  3. Currents are integers
  4. Potentials are unique