Exercise: spectral partitioning and normalized cuts

Goal

By the end of this exercise you should be able to:

  • write down the degree matrix and the Laplacian L = D - W of a small graph,
  • evaluate the quadratic form \mathbf{x}^\top L\mathbf{x} and relate it to the cut,
  • verify an eigenvector, and split a graph by the sign of its Fiedler vector,
  • compute Ncut and explain why it prefers balanced splits to the minimum cut.

Do the pen-and-paper part first, then the interactive part.

Background

Idea What it means
Affinity w_{pq} = w_{qp} \ge 0: how strongly p and q belong together
Degree, volume d_p = \sum_q w_{pq}; \text{vol}(A) = \sum_{p \in A} d_p
Laplacian L = D - W; \mathbf{x}^\top L\mathbf{x} = \sum_{\text{edges}} w_{pq}(x_p - x_q)^2
Cut \text{cut}(A, B) = \sum_{p \in A, q \in B} w_{pq}; for x = \pm1: \mathbf{x}^\top L\mathbf{x} = 4\,\text{cut}
Fiedler vector eigenvector of the second-smallest eigenvalue of L (unnormalized), or of L\mathbf{x} = \mu D\mathbf{x} (normalized)
Normalized cut \text{Ncut}(A, B) = \text{cut}/\text{vol}(A) + \text{cut}/\text{vol}(B)

By hand

B1. A path of 4 nodes, 1 - 2 - 3 - 4, all weights 1. (a) Write W, D and L. (b) Check that \mathbf{1} is an eigenvector with eigenvalue 0. (c) Check that \mathbf{v} = (1,\ \sqrt2 - 1,\ 1 - \sqrt2,\ -1) is an eigenvector of L with eigenvalue 2 - \sqrt2. Which split does its sign give?

B2. Same path. Compute \mathbf{x}^\top L\mathbf{x} for \mathbf{x} = (1, 1, -1, -1) and \mathbf{x} = (1, -1, 1, -1), once with the matrix and once with \sum_{\text{edges}}(x_p - x_q)^2. Which cuts do the two vectors describe, and what are their costs?

B3. Same path, now the normalized problem L\mathbf{x} = \mu D\mathbf{x}. (a) Check that \mathbf{x} = (1, \tfrac12, -\tfrac12, -1) solves it with \mu = \tfrac12. Check that \mathbf{x}^\top D\mathbf{1} = 0. (b) Compute Ncut for the splits \{1, 2\} \mid \{3, 4\} and \{1\} \mid \{2, 3, 4\}. Which is better?

B4. Seven nodes: a triangle 1, 2, 3 and a triangle 4, 5, 6, all triangle edges with weight 1; an edge 3 - 4 with weight 0.1; and a node 7 attached only to node 6, with weight 0.05. (a) Compute the degrees and the total volume. (b) What is the minimum cut over all splits? Which split is it? (c) Compute Ncut for \{7\} against the rest, for \{1, 2, 3\} against the rest, and for \{6, 7\} against the rest. Which split does Ncut prefer?

B5. Show that if a graph has two connected components A and B, the eigenvalue 0 of L has (at least) two independent eigenvectors. Give one that is orthogonal to \mathbf{1}. What does its sign do?

B1. (a) W = \begin{bmatrix} 0&1&0&0\\1&0&1&0\\0&1&0&1\\0&0&1&0 \end{bmatrix}, D = \operatorname{diag}(1, 2, 2, 1), L = \begin{bmatrix} 1&-1&0&0\\-1&2&-1&0\\0&-1&2&-1\\0&0&-1&1 \end{bmatrix}. (b) Every row of L sums to 0, so L\mathbf{1} = \mathbf{0}. (c) Write a = \sqrt2 - 1, so \mathbf{v} = (1, a, -a, -1). Row 1: 1 - a = 2 - \sqrt2, and \mu v_1 = 2 - \sqrt2. Row 2: -1 + 2a + a = 3a - 1 = 3\sqrt2 - 4, and \mu v_2 = (2 - \sqrt2)(\sqrt2 - 1) = 2\sqrt2 - 2 - 2 + \sqrt2 = 3\sqrt2 - 4. Rows 3 and 4 follow by symmetry (they are rows 2 and 1 with the signs flipped). The sign splits \{1, 2\} \mid \{3, 4\}, in the middle. (All eigenvalues: 0, 2 - \sqrt2 = 0.586, 2, 2 + \sqrt2 = 3.414.)

B2. \mathbf{x} = (1, 1, -1, -1): L\mathbf{x} = (0, 2, -2, 0), \mathbf{x}^\top L\mathbf{x} = 0 + 2 + 2 + 0 = 4; edge sum: (1-1)^2 + (1+1)^2 + (-1+1)^2 = 4. The cut \{1, 2\} \mid \{3, 4\} crosses one edge: cut 1, and 4 = 4 \cdot 1. \mathbf{x} = (1, -1, 1, -1): L\mathbf{x} = (2, -4, 4, -2), \mathbf{x}^\top L\mathbf{x} = 2 + 4 + 4 + 2 = 12; edge sum 4 + 4 + 4 = 12. The cut \{1, 3\} \mid \{2, 4\} crosses all 3 edges: cut 3, and 12 = 4 \cdot 3.

B3. (a) L\mathbf{x} = (1 - \tfrac12,\ -1 + 1 + \tfrac12,\ -\tfrac12 - 1 + 1,\ \tfrac12 - 1) = (\tfrac12, \tfrac12, -\tfrac12, -\tfrac12) and \mu D\mathbf{x} = \tfrac12(1, 1, -1, -1): equal. \mathbf{x}^\top D\mathbf{1} = 1 \cdot 1 + 2 \cdot \tfrac12 - 2 \cdot \tfrac12 - 1 \cdot 1 = 0. (b) \{1, 2\} \mid \{3, 4\}: cut 1, volumes 1 + 2 = 3 and 3: Ncut = \tfrac13 + \tfrac13 = 0.667. \{1\} \mid \{2, 3, 4\}: cut 1, volumes 1 and 5: Ncut = 1 + 0.2 = 1.2. The balanced split is better, although both cut one edge of weight 1.

B4. (a) d = (2, 2, 2.1, 2.1, 2, 2.05, 0.05), total volume 12.3. (b) The minimum cut is 0.05: \{7\} against the rest. (Separating the triangles costs 0.1; any split through a triangle costs at least 2.) (c) \{7\}: cut 0.05, volumes 0.05 and 12.25: Ncut = 1 + 0.004 = 1.004. \{1, 2, 3\}: cut 0.1, volumes 6.1 and 6.2: Ncut = 0.0164 + 0.0161 = 0.0325. \{6, 7\}: cut 2 (edges 4 - 6 and 5 - 6), volumes 2.1 and 10.2: Ncut = 0.952 + 0.196 = 1.149. Ncut prefers separating the two triangles, by a factor of 30; the minimum cut prefers cutting off node 7. (Computed numerically, the normalized Fiedler vector of this graph is negative on nodes 1–3 and positive on 4–7, with \mu_2 = 0.031: its sign gives the triangles.)

B5. The indicator vectors \mathbf{1}_A (1 on A, 0 on B) and \mathbf{1}_B both satisfy L\mathbf{x} = 0: each row of L only involves nodes of one component, and on that component the vector is constant. A combination orthogonal to \mathbf{1}: \mathbf{x} = |B|\,\mathbf{1}_A - |A|\,\mathbf{1}_B (sum |A||B| - |A||B| = 0). Its sign is + on A and - on B: it splits the graph exactly into its components, at cut 0.

Show that \mathbf{x}^\top L\mathbf{x} = \sum_{\text{edges}} w_{pq}(x_p - x_q)^2 for any graph. (Write \mathbf{x}^\top D\mathbf{x} = \sum_p d_p x_p^2 and \mathbf{x}^\top W\mathbf{x} = \sum_{p,q} w_{pq}x_p x_q, and expand (x_p - x_q)^2.) Conclude that all eigenvalues of L are \ge 0.

Solution: \sum_{\text{edges}} w_{pq}(x_p - x_q)^2 = \tfrac12\sum_{p,q} w_{pq}(x_p^2 - 2x_px_q + x_q^2) (the double sum counts each edge twice). The first and last terms each give \tfrac12\sum_p d_p x_p^2, together \mathbf{x}^\top D\mathbf{x}; the middle term gives -\sum_{p,q} w_{pq}x_p x_q = -\mathbf{x}^\top W\mathbf{x}. So the sum is \mathbf{x}^\top(D - W)\mathbf{x}. It is a sum of non-negative terms, so \mathbf{x}^\top L\mathbf{x} \ge 0 for every \mathbf{x}; for an eigenvector, \mathbf{x}^\top L\mathbf{x} = \mu\|\mathbf{x}\|^2, hence \mu \ge 0.

Show that L\mathbf{x} = \mu D\mathbf{x} is equivalent to D^{-1/2}LD^{-1/2}\mathbf{y} = \mu\mathbf{y} with \mathbf{y} = D^{1/2}\mathbf{x}, and that the constraint \mathbf{y} \perp D^{1/2}\mathbf{1} is the same as \mathbf{x}^\top D\mathbf{1} = 0.

Solution: Put \mathbf{x} = D^{-1/2}\mathbf{y} into L\mathbf{x} = \mu D\mathbf{x}: LD^{-1/2}\mathbf{y} = \mu D^{1/2}\mathbf{y}. Multiply on the left by D^{-1/2}: D^{-1/2}LD^{-1/2}\mathbf{y} = \mu\mathbf{y}. All steps can be reversed when every d_p > 0. The constraint: \mathbf{y}^\top D^{1/2}\mathbf{1} = \mathbf{x}^\top D^{1/2}D^{1/2}\mathbf{1} = \mathbf{x}^\top D\mathbf{1}.

Explore

The demo builds a graph on points (affinity w = e^{-d^2/2\sigma^2} between every pair) or on the pixels of a 32 \times 32 image (neighbours within 2 pixels, affinity from the grey-level difference with scale \sigma and from the distance). It computes the Fiedler vector of L\mathbf{x} = \mu D\mathbf{x} (normalized) or of L\mathbf{x} = \mu\mathbf{x}, colours the nodes by it (blue negative, orange positive) and splits them at the threshold (red lines: the cut edges; for images, a black line). The top plot shows the vector sorted; the bottom plot shows Ncut (purple) and the cut (green, scaled) for every split along the sorted vector, with the best Ncut marked. Show the minimum cut shows the smallest cut without balance instead (for points the exact one, for images the smallest along the sorted vector). Points can be dragged.

E1. Two blobs (\sigma = 0.08). Read \mu_2, the split at threshold 0, its cut and its Ncut. Tick show the minimum cut: is it the same split? Now raise \sigma to 0.15. What does the minimum cut do now, and what does the normalized cut do?

E2. Blobs + outlier. Which split does the normalized Fiedler vector give? Untick normalized: what does the unnormalized Fiedler vector look like, and which split does threshold 0 give now? Compare with B4.

E3. Three blobs. How does the Fiedler vector split the three blobs? How would you get all three?

E4. Two rings (\sigma = 0.05). Are the rings separated? Raise \sigma to 0.12 and use the best Ncut button. What happens, and why?

E5. Image: shapes (\sigma = 30). Which region does the split separate? Tick show the image to compare. Then raise \sigma to 60. What kind of split do you get, and why? Why is the square never separated as a third region?

E6. Image: micrograph (\sigma = 30). Does the split find the nucleus? Press best Ncut. What does the best normalized cut separate, and what does this tell you about unsupervised segmentation?

E1. \mu_2 = 0.0135; threshold 0 splits 25 | 25 (the two blobs), cut 2.19, volumes 173 and 185, Ncut 0.0245, which is also the best Ncut on the sweep. The minimum cut (any split) is the same here: 2.19, 25 | 25, because the blobs are well separated at this \sigma. At \sigma = 0.15 the blobs are more strongly connected: the minimum cut now cuts off a single point (cut 9.84, 1 | 49), while the normalized cut still splits the two groups (24 | 26, Ncut 0.143).

E2. Normalized: the two blobs are split (25 | 26: the outlier, almost unconnected, goes with one of them), Ncut 0.0245, as before. Unnormalized: \mu_2 = 4.3 \cdot 10^{-7}; the vector is +1 at the outlier and almost 0 everywhere else, and threshold 0 gives 50 | 1: the outlier alone, Ncut 1.00. That is also the minimum cut (cut 4.2 \cdot 10^{-7}). Without normalisation, cutting off a weakly connected node is cheap, exactly as for node 7 in B4. (The sweep still finds the blob split as the best Ncut, at k = 25, because the tiny differences between the blob values order the points correctly.)

E3. \mu_2 = 9.5 \cdot 10^{-4} and the split is 40 | 20: one blob against the other two (the two that are closest together stay together). For three segments, split again: compute the Fiedler vector of the subgraph of the 40 points. (Or use the third eigenvector as well.)

E4. At \sigma = 0.05 the rings are separated: 20 | 44, \mu_2 = 6.6 \cdot 10^{-6}; the cut between them is tiny (2.1 \cdot 10^{-4}), because the gap is many \sigma wide. At \sigma = 0.12 the affinity reaches across the gap. Threshold 0 now cuts both rings in half (32 | 32), and the best Ncut along the sweep (40 | 24, Ncut 0.214) does not follow the rings either: the “two rings” structure is no longer the dominant one at this scale. \sigma decides what counts as close.

E5. At \sigma = 30 the split is 875 | 149: the bright disk against everything else. (Ncut 6.4 \cdot 10^{-4}.) At \sigma = 60 the grey-level differences matter much less (the disk edge, 190 - 60 = 130, is only about 2\sigma), and the split becomes geometric, roughly in half (527 | 497), like the Fiedler vector of a plain grid. The square is never a third region because one eigenvector gives one split; it would take a second split of the background part (or more eigenvectors).

E6. No: at threshold 0 the image is split diagonally (434 | 590), into the lower left and the upper right. The best Ncut along the sweep (242 pixels, Ncut 0.021) separates the bright white area in the upper right from the rest. Normalized cuts find the dominant grouping of the whole image, here bright versus dark, not necessarily the object you are interested in. To get the nucleus you need more information: seeds (graph cuts), a model, or further splits.

Code

Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in graphcuts/src/ncut/exercise.rs. A graph is its symmetric matrix of affinities w[p][q].

C1. affinities(points, sigma): w_{pq} = e^{-d_{pq}^2/2\sigma^2} between all points, 0 on the diagonal.

C2. laplacian(w) and quadratic_form(l, x): the degrees d_p = \sum_q w_{pq}, L = D - W, and \mathbf x^\top L\mathbf x. The checks are your answers to B1 and B2.

C3. ncut(w, in_a): the cut and the normalized cut of a split:

cut = vol(A) = vol(B) = 0
for every pair (p, q):
    if p is in A:  vol(A) = vol(A) + w_pq;  if q is not in A: cut = cut + w_pq
    else:          vol(B) = vol(B) + w_pq
Ncut = cut / vol(A) + cut / vol(B)

The checks are your answers to B3(b) and B4.

C4. fiedler(w, iterations): the eigenvector of L\mathbf x = \mu D\mathbf x for the second-smallest μ, by power iteration (as in the PCA unit). Power iteration finds the eigenvector of the largest eigenvalue, so it works on M = I + D^{-1/2}WD^{-1/2}, whose eigenvalues are 2 - \mu; its largest eigenvalue, 2, belongs to the trivial vector \mathbf s = D^{1/2}\mathbf 1, which is removed in every step:

s = (√d_1, ..., √d_n), scaled to length 1
y = any vector, minus its part along s, scaled to length 1
repeat:
    z_p = y_p + Σ_q w_pq · y_q / √(d_p d_q)          # z = M y
    z = z − (z · s) s                                  # remove the trivial part
    λ = z · y                                          # eigenvalue of M
    y = z, scaled to length 1
μ₂ = 2 − λ;   x_p = y_p / √d_p

The checks are B3(a) (μ = ½, x ∝ (1, ½, −½, −1)) and B4 (μ₂ ≈ 0.031, signs splitting the triangles).

C5. sweep(w, x): the best split along the sorted vector:

sort the nodes by x
A = {}
for k = 1, ..., n − 1:
    add the k-th node to A
    keep A if its Ncut is the smallest so far

The check is B4 (the triangles, Ncut 0.0325).

Then cargo run -p graphcuts -- ncut and open the pictures in out/ncut/. The program runs the normalized Fiedler vector on point clouds (two blobs at σ = 0.08 and 0.15, blobs + outlier, three blobs, two rings at σ = 0.05 and 0.12) and on a 16 \times 16 image with a bright disk and a grey square (pixels within 2 of each other joined, grey-level scale σ = 30 and 60). For each it prints μ₂, the split at 0 with its cut and Ncut, the best split of the sweep, and the cheapest single node to cut off. The pictures colour the points by the vector (blue negative, orange positive) and show the vector of the image (0 = grey).

C6. Two blobs at σ = 0.08 and at σ = 0.15: compare the cut of the blob split with the cut of the cheapest single node. Which would the minimum cut choose, and which does Ncut choose? And with the outlier?

C7. Two rings at σ = 0.05 and 0.12, and the image at σ = 30 and 60: what does each split separate? What does σ decide?

C8. Count the cost. The program does 3000 steps of power iteration for the image. Change this number in src/main.rs to 100, 300 and 1000: when is the disk found by the sign of the vector, and by the sweep? (math) Power iteration on M shrinks the error by about (2 - \mu_3)/(2 - \mu_2) per step. Why is it slow for this image, and what does that mean for large images?

The model solution is graphcuts/src/ncut/solution.rs in the download.

C6. σ = 0.08: the blob split (25 | 25) cuts 0.151, Ncut 9.6 \cdot 10^{-4}; the cheapest single node costs 6.13 (Ncut 1.01): here the minimum cut and Ncut agree. σ = 0.15: the blob split now cuts 26.7 (Ncut 0.105), but a single node can be cut off for 16.6 (Ncut 1.02): the minimum cut would isolate that node, while Ncut still splits the blobs (as in E1). With the outlier, cutting it off costs only 1.6 \cdot 10^{-5}, the minimum cut by far, but its Ncut is 1.00; the Fiedler vector splits the blobs (26 | 25, the outlier joining one of them, Ncut 9.6 \cdot 10^{-4}), as in B4 and E2. Three blobs: 40 | 20, the two blobs closest together stay together (E3).

C7. Rings at σ = 0.05: 20 | 44, exactly the two rings (μ₂ = 2.8 \cdot 10^{-4}). At σ = 0.12 the affinities reach across the gap: the split at 0 cuts both rings in half (33 | 31, Ncut 0.386), and the best sweep split (19 | 45, Ncut 0.316) is not the rings either. Image at σ = 30: 38 | 218, the disk against the rest (Ncut 2.7 \cdot 10^{-4}; the vector is dark on the disk and nearly grey elsewhere). At σ = 60 grey-level differences matter little and the split is geometric, roughly in half along a diagonal (134 | 122). σ sets the scale of what counts as close or similar.

C8. At σ = 30, after 100 steps the vector is still far from the Fiedler vector (μ₂ estimated as 0.011; split 130 | 126 at 0, 132 | 124 in the sweep); after 300 steps the sweep finds the disk (38 | 218) but the sign does not yet (119 | 137); after 1000 steps both find it (μ₂ = 2.7 \cdot 10^{-4}). So slow convergence means that (2 - \mu_3)/(2 - \mu_2) is close to 1: μ₃ is small as well, because the image can be split cheaply in more than one way (the disk, the square, the background). Each step costs n^2 multiplications (3000 steps take about 0.8 s for 256 pixels). For a large image n^2 is huge and the small eigenvalues lie even closer together; real implementations use the sparsity of W (only neighbours are joined) and faster eigensolvers (Lanczos), as the slide “From eigenvector to segmentation” says.

Where this leads

Spectral partitioning is the basis of spectral clustering, used far beyond images, and of several superpixel methods that over-segment an image into many small coherent regions as a first step (superpixels). Graph cuts and normalized cuts both treat the image as a graph; other segmentation methods follow image gradients (watershed) or evolve a contour (level sets).

The energy and the pixel graph of this day return throughout Part VI and beyond: