Algorithm Search⌘K

Proofs: Force-directed layout

These are the human proofs of Force-directed layout, at full length.

Each round of the Fruchterman-Reingold layout is a capped descent step on an energy, and its fixed points are the balanced drawings full

This proof shows: correctness, termination.

Let G=(V,E)G = (V, E) be an undirected simple graph with nn vertices, let k>0k > 0, and let p=(pv)v∈Vp = (p_v)_{v \in V} be a drawing in R2\mathbb{R}^2 in which no two vertices share a point. Define

E(p)=∑{u,v}∈E∥pu−pv∥33k−∑{u,v}⊆V, u≠vk2ln⁡∥pu−pv∥.E(p) = \sum_{\{u,v\} \in E} \frac{\lVert p_u - p_v \rVert^3}{3k} - \sum_{\{u,v\} \subseteq V,\ u \ne v} k^2 \ln \lVert p_u - p_v \rVert .

Lemma 1 (the displacement is the force). After lines L3 to L5, disp[v]=−∇pvE(p)\mathrm{disp}[v] = -\nabla_{p_v} E(p) for every vertex vv.

Proof. For δ=pv−pu≠0\delta = p_v - p_u \ne 0 and d=∥δ∥d = \lVert \delta \rVert, the gradient of dd with respect to pvp_v is δ/d\delta/d. So the gradient with respect to pvp_v of the edge term d3/(3k)d^3/(3k) is (d2/k)(δ/d)(d^2/k)(\delta/d), and that of −k2ln⁡d-k^2 \ln d is −(k2/d)(δ/d)-(k^2/d)(\delta/d). With respect to pup_u the signs flip, since δ\delta changes sign. A pair term depends only on pup_u and pvp_v, so −∇pvE-\nabla_{p_v} E is the sum, over all u≠vu \ne v, of (k2/d)(k^2/d) times the unit vector from uu to vv, plus the sum, over the neighbours uu of vv, of −(d2/k)-(d^2/k) times the same unit vector. Line L4 adds exactly the first sum to disp[v]\mathrm{disp}[v], with the pair {u,v}\{u, v\} written in either order, and line L5 adds exactly the second. □\square

Lemma 2 (step length). In line L6 of round rr, each vertex moves by min⁡(∥−∇pvE∥,tr)\min(\lVert -\nabla_{p_v} E \rVert, t_r) in the direction of −∇pvE-\nabla_{p_v} E, where tr=t0(1−(r−1)/T)t_r = t_0 (1 - (r - 1)/T); a vertex with zero force does not move.

Proof. Line L1 sets t=t0t = t_0, and line L7 at the end of round r−1r - 1 sets t=t0(1−(r−1)/T)t = t_0(1 - (r-1)/T), so t=trt = t_r during round rr. Line L6 moves pvp_v by the unit vector of disp[v]\mathrm{disp}[v] times min⁡(∥disp[v]∥,t)\min(\lVert \mathrm{disp}[v] \rVert, t), and Lemma 1 identifies disp[v]\mathrm{disp}[v]. □\square

Lemma 3 (fixed points). Let t>0t > 0. A round leaves the drawing pp unchanged if and only if ∇E(p)=0\nabla E(p) = 0.

Proof. If ∇E(p)=0\nabla E(p) = 0, every disp[v]\mathrm{disp}[v] is 0 by Lemma 1 and line L6 moves nothing. If some ∇pvE(p)≠0\nabla_{p_v} E(p) \ne 0, Lemma 2 moves vv by min⁡(∥∇pvE∥,t)>0\min(\lVert \nabla_{p_v} E \rVert, t) > 0. □\square

Lemma 4 (two balance distances). A single edge {u,v}\{u, v\} with no other vertex is balanced exactly when ∥pu−pv∥=k\lVert p_u - p_v \rVert = k. A path a−b−ca - b - c with bb at the midpoint of aa and cc on the segment between them is balanced exactly when ∥pa−pb∥=(3/2)1/3k\lVert p_a - p_b \rVert = (3/2)^{1/3} k.

Proof. For the edge, the force on vv along the unit vector from uu is k2/d−d2/kk^2/d - d^2/k, which is zero exactly when d3=k3d^3 = k^3. For the path, let d=∥pa−pb∥=∥pb−pc∥d = \lVert p_a - p_b \rVert = \lVert p_b - p_c \rVert. By symmetry, the forces on bb from aa and from cc cancel. All forces on cc lie on the line, and along the unit vector from bb to cc they are k2/dk^2/d from bb, k2/(2d)k^2/(2d) from aa and −d2/k-d^2/k from the edge {b,c}\{b, c\}. The sum is zero exactly when d3=32k3d^3 = \frac32 k^3, and the case of aa is the mirror image. □\square

Theorem. FORCE-DIRECTED-LAYOUT stops after TT rounds. In round rr every vertex takes a step of length at most trt_r along the negative gradient of EE with respect to its own position, so it ends within distance ∑r=1Ttr\sum_{r=1}^{T} t_r of its start. A drawing with distinct points is left unchanged by a round with positive temperature exactly when it is a critical point of EE.

Proof. The loop of line L2 runs TT times, and each round does a finite amount of work. The step claims are Lemma 2, and the bound on the total distance follows by the triangle inequality. The last claim is Lemma 3. □\square

∎

Back to Force-directed layout