Exercise: spanning trees, single linkage and FH
Before this unit: The random walker (previous unit); Day 27: Graph cuts; Day 15: Watershed.
See also: Day 28: Ising model and Markov random fields; Day 35: Gaussian mixtures.
Goal
By the end of this exercise you should be able to:
- run Kruskal’s algorithm by hand and explain each “join” or “skip” with union–find,
- compute single-linkage regions and explain chaining,
- run Felzenszwalb–Huttenlocher by hand on a 1D signal, and predict what k, contrast and noise do,
- (math) prove the cut property, the minimax property of MST paths, and FH’s “not too fine” property on an example.
Background
| Idea | What it means |
|---|---|
| Dissimilarity | d_{pq} = \lvert I_p - I_q\rvert (or a colour distance): small inside regions |
| Minimum spanning tree | cheapest set of edges connecting all nodes without cycles |
| Kruskal | edges in increasing order; join unless both ends are already in the same component (union–find) |
| Single linkage at t | join across every edge with d < t = MST minus its edges \ge t |
| Minimax distance | the smallest possible largest edge on a path between two nodes; the MST path achieves it |
| \mathrm{Int}(C) | the heaviest edge in C’s spanning tree (0 for a single pixel) |
| FH test | merge C_1, C_2 across d iff d \le \min(\mathrm{Int}(C_1) + k/\lvert C_1\rvert,\ \mathrm{Int}(C_2) + k/\lvert C_2\rvert) |
In FH, edges of equal weight are processed in index order (left to right in 1D).
By hand
B1. Kruskal. Six nodes A–F with edges AB 3, AD 1, BC 5.5, BD 2.5, BE 2, CF 4, DE 5, EF 7. (a) Sort the edges and go through them: join or skip? (b) Draw the tree; what is its total weight? (c) Which single-linkage regions do you get for t = 3? For t = 5?
B2. Texture next to a faint step. The signal [0, 6, 0, 6, 20, 20, 20, 20, 24, 24, 24, 24] on a chain, d = absolute difference. (a) List the 11 edge weights. (b) Show that no single-linkage threshold gives the three regions “texture”, “20s”, “24s”. (c) Run FH with k = 8; at each non-zero edge write the two thresholds and the verdict. (d) For which k do the 20s and 24s merge?
B3. The signal [10, 11, 13, 12, 22, 23, 21, 24], k = 4. (a) Sort the edges and run FH. Which regions do you get? (b) What are \mathrm{Int} and the threshold of each final region? (c) What is the smallest k that gives one region? Check: what happens at k = 31.9?
B4. Predict. (a) In B3, every value is doubled. What happens to the regions at k = 4? Why is doubling the values the same as halving k? (b) Noise of ±1 is added to B3’s values. Would you expect the two regions to survive? (c) A 256 \times 256 image gets 4 times larger (512 \times 512, same content, upsampled). How should k change to give about the same regions?
B5. Chaining. A 4 \times 8 image whose columns are 0, 0, 3, 6, 9, 12, 15, 15 (all rows equal). (a) How many single-linkage regions for t = 3 and t = 4? (b) Run FH with k = 8 in your head: do the first two columns merge with the third? (c) Which method is fooled by the ramp?
B1. (a) AD 1 join; BE 2 join; BD 2.5 join ({A, D} with {B, E}); AB 3 skip (A and B are now in the same component); CF 4 join; DE 5 skip; BC 5.5 join ({A, B, D, E} with {C, F}); EF 7 skip. (b) Edges AD, BE, BD, CF, BC: total 1 + 2 + 2.5 + 4 + 5.5 = 15. (c) t = 3: edges below 3 are AD, BE, BD: regions {A, B, D, E}, {C}, {F}. t = 5: add AB and CF: {A, B, D, E}, {C, F} (DE 5 is not below 5, and it would join nothing new).
B2. (a) 6, 6, 6, 14, 0, 0, 0, 4, 0, 0, 0. (b) The texture holds together only if t > 6; but then the 4-edge between the 20s and the 24s is also below t and they merge. For t \le 6 the texture breaks into single pixels. So you get either 5 or more regions (t \le 4: 6 regions; 4 < t \le 6: 5) or at most 2. (c) The six 0-edges come first: within the 20s, singletons (threshold 0 + 8 = 8) merge, then \{5, 6\} (threshold 0 + 8/2 = 4) with {7}, then \{5, 6, 7\} (8/3 = 2.67) with {8}; the same for the 24s. Both flat parts: \mathrm{Int} = 0, size 4, threshold 2. The 4-edge: 4 > \min(2, 2): keep apart. The 6-edges: {1}, {2} (8 and 8): merge, \mathrm{Int} = 6, threshold 6 + 8/2 = 10; {1, 2} with {3}: \min(10, 8) = 8 \ge 6: merge, threshold 6 + 8/3 = 8.67; with {4}: \min(8.67, 8) \ge 6: merge, threshold 6 + 2 = 8. The 14-edge: \min(8, 2) = 2 < 14: keep. Three regions. (d) The flat parts’ threshold is k/4; they merge across the 4-edge when k/4 \ge 4, i.e. k \ge 16 (the demo shows 111122222222 at k = 16 and 111122223333 at 15.9).
B3. (a) Edges 1, 2, 1, 10, 1, 2, 3; sorted: 1 (1–2), 1 (3–4), 1 (5–6), 2 (2–3), 2 (6–7), 3 (7–8), 10. The three 1-edges join singletons (threshold 4): {1, 2}, {3, 4}, {5, 6}, each with Int 1 and threshold 1 + 4/2 = 3. 2 (2–3): \min(3, 3) \ge 2: merge {1..4}, Int 2. 2 (6–7): \min(3, 4) \ge 2: merge {5, 6, 7}, Int 2, threshold 2 + 4/3 = 3.33. 3 (7–8): \min(3.33, 4) \ge 3: merge {5..8}, Int 3. 10: thresholds 2 + 4/4 = 3 and 3 + 4/4 = 4: keep. Regions {1..4}, {5..8}. (b) {1..4}: Int 2, threshold 3; {5..8}: Int 3, threshold 4. (c) The merge order of the smaller edges does not change for larger k, so the last test is 10 \le \min(2 + k/4, 3 + k/4) = 2 + k/4, i.e. k \ge 32. At k = 31.9: 2 + 7.975 = 9.975 < 10: still two regions.
B4. (a) Doubled edges 2, 4, 2, 20, 2, 4, 6. Every threshold has the form \mathrm{Int} + k/|C|; doubling all weights doubles every Int and every d, which is the same as keeping the weights and using k/2. At k = 4 (= original data with k = 2): {1..4} forms (the 4-edge meets \min(4, 4) exactly); {5, 6, 7} forms with Int 4, threshold 4 + 4/3 = 5.33; the 6-edge to node 8 fails against \min(5.33, 0 + 4) = 4: node 8 becomes its own region: {1..4}, {5, 6, 7}, {8}. (b) Yes: noise of ±1 changes the inner edges by up to 2 and raises Int a little, but the 10-step (now 8 to 12) stays far above the thresholds (about 3–5). (c) Regions have 4 times as many pixels, so k/|C| drops by 4 for the same region: use about 4k.
B5. (a) t = 3: the steps of 3 are not below 3: columns 1–2 form one region, columns 7–8 another, and columns 3, 4, 5, 6 one each: 6 regions. t = 4: everything joins into one region, although the grey level goes from 0 to 15: chaining. (b) The vertical and 0-edges come first: the first two columns become one component of 8 pixels with Int 0 and threshold 8/8 = 1; each middle column gets 4 pixels and threshold 0 + 8/4 = 2. The 3-steps exceed both: nothing merges across the ramp (FH gives 6 regions). (c) Single linkage at t = 4 is fooled; FH is not here (its flat components quickly get small thresholds), but with larger k it chains too.
Math questions
B6. (math) Prove the cut property: for any split of the nodes into S and \bar S, the cheapest edge across the split belongs to some minimum spanning tree.
B7. (math) Show that the path between p and q in a minimum spanning tree minimises the largest edge weight over all paths from p to q.
B8. (math) Show that for every t the following give the same partition: the components of \{e : d_e < t\}, and the components of the MST after removing all edges with d \ge t.
B9. (math) FH’s result is “not too fine”: for every pair of neighbouring regions A, B, the cheapest edge between them exceeds \mathrm{MInt}(A, B) = \min(\mathrm{Int}(A) + k/|A|, \mathrm{Int}(B) + k/|B|). Check this for the result of B2(c).
B6. Let e be the cheapest edge across the split and T an MST. If e \in T, done. Otherwise T + e contains a cycle through e; the cycle crosses the split once at e and must cross back at another edge e'. d_{e'} \ge d_e (since e is the cheapest across). T' = T + e - e' is again a spanning tree, with weight \le that of T; so T' is an MST containing e.
B7. Let P be the tree path and e its heaviest edge. Removing e from the tree splits the nodes into S \ni p and \bar S \ni q. If some edge f across this split had d_f < d_e, then T - e + f would be a lighter spanning tree, a contradiction; so every edge across the split weighs at least d_e. Any path from p to q crosses the split, so its largest edge is \ge d_e = the largest edge of P.
B8. If p and q are joined in the MST without edges \ge t, they are joined in \{d < t\} (those tree edges are in it). Conversely, if a path with all edges < t joins them, their minimax distance is < t, so by B7 the tree path between them has all edges < t: they are joined in the pruned tree. Same “joined” relation, same partition.
B9. Final regions: T = texture (Int 6, size 4, threshold 8), F_1 = 20s (Int 0, size 4, threshold 2), F_2 = 24s (threshold 2). T–F_1: the only edge between them is 14 > \min(8, 2) = 2. F_1–F_2: the edge 4 > \min(2, 2) = 2. Both boundaries have evidence: not too fine.
Explore: spanning trees step by step
A small grid of numbers (pixels) with 4-neighbour edges; the number between two cells is the edge weight. Choose an algorithm and step through it (▶ step, ◀, play, ⏮, ⏭). The list on the right shows the edges in sorted order with the verdicts (or, for Prim, the queue). Click a cell to change its value (upper half +1, lower half −1), or switch the click mode to place markers or path ends.
G1. Preset Kruskal on a small image (4 \times 5, 31 edges). Step to the end. How many edges are skipped, how many joined, and what is the tree’s weight? Why must exactly 19 edges be joined?
G2. Choose minimax path and click the top-left and the bottom-right cell. What is the heaviest edge on the tree path? Find by eye a different path between the two cells: can its largest edge be smaller?
G3. Preset the 1D example (FH, k = 8): step through and compare with B2(c). Then set k = 16 and k = 4: what changes, and why?
G4. Presets texture next to a faint step (single linkage, t = 7) and the same with FH (k = 8): count the regions. Set t = 5 in single linkage. Then raise k in FH until the 20s and 24s merge.
G5. Preset chaining: a ramp (single linkage, t = 4): how many regions? Switch to FH with k = 8 and step through: where does it stop merging, and why?
G1. 19 edges joined and 12 skipped; tree weight 16. A spanning tree on 20 nodes always has 20 - 1 = 19 edges: each join reduces the number of components by one, from 20 to 1.
G2. The tree path from top-left to bottom-right has edges 1, 1, 4, 1, 0, 1, 0: the heaviest is 4. No path does better: the minimax distance is 4 (somewhere every path has to climb from the low values on the left to the high ones on the right through a step of at least 4).
G3. As in B2(c): three regions. At k = 16 the flat parts have threshold 16/4 = 4, and the 4-edge merges them (4 \le 4): two regions. At k = 4 singletons have threshold 4 < 6: the texture never forms; the result is 123455556666 (each texture pixel alone, two flat parts).
G4. Single linkage, t = 7: 2 regions (texture and the whole flat part: the 20s and 24s merge across the 4-steps). t = 5: 17 regions (16 texture pixels alone, the flat part still one). FH, k = 8: 3 regions (texture, 20s, 24s). The 20s and 24s (8 pixels each, Int 0) merge when k/8 \ge 4, i.e. k \ge 32 (2 regions).
G5. Single linkage at t = 4: 1 region, from 0 to 15 in steps of 3. FH, k = 8: 6 regions. The vertical edges (0) come first and build columns of 4 pixels (threshold 8/4 = 2), the two equal columns at each end become components of 8 pixels (threshold 1). Every step of 3 is above these thresholds, so FH stops before the ramp: it is protected from this particular chaining because components grow before the ramp edges are reached.
Explore: FH and single linkage on an image
A 256 \times 256 image: a synthetic test scene (a textured disc; two flat squares of 120 and 140; two bright halves separated by a dark line with a 3-pixel gap; five small bright spots; a ramp at the bottom), the micrograph, or a colour scene (Lab distances). Edges connect 8 neighbours after Gaussian pre-smoothing σ. The left panel shows the regions in random or mean colours, the right one their boundaries on the image. Click a region for its size, Int and FH threshold.
F1. Preset single linkage chains through the ramp (t = 9; the ramp is clicked). How big is the clicked region, and what else belongs to it? Why?
F2. Preset single linkage: texture or faint step, not both (t = 5). Count the regions; raise t to 7, 9, 12, 16. At which t is the disc one region, and what happens to the two squares at that t?
F3. Preset FH: both (k = 3000). Read Int and the threshold of the disc (clicked). Click the left square: its Int and threshold. Explain with these numbers why the disc holds together while the squares stay apart.
F4. Preset k sets scale, not size (k = 10\,000; a bright spot is clicked). How many pixels does the spot have, and what is its threshold? Why does it survive?
F5. Preset minimum size cleanup (noise 10, k = 300, minimum size 60). How many regions? Set the minimum size to 0.
F6. Preset leak through a narrow gap. Click the left and the right bright half. Try k = 300. Can any k separate them?
F7. On the test scene with k = 3000, set σ to 0 and then to 2.5. How does the number of regions change, and why?
F1. The clicked region has 45 138 pixels: it is the background. The ramp starts at 40 (darker than the background, 70) and rises to 200; near x \approx 58 it equals the background, so the steps across its upper and lower borders are tiny there. One chain of small steps is enough: the whole ramp joins the background.
F2. t = 5: 935 regions (the disc shattered, squares separate). t = 7: 398, t = 9: 242, t = 12: 140, t = 16: 97. At t = 9 99% of the disc is one region (a few small pieces remain; its smoothed neighbour steps reach about 8–10), and by t = 16 it is one region; already at t = 9 the two squares merge, because the smoothed faint step (20 grey levels spread over two or three pixels) has steps below 9.
F3. The disc: |C| = 6898, Int = 14.9, threshold 15.31: it has already accepted steps of 15. The left square: |C| = 4598, Int = 3.4, threshold 4.05. The steps across the faint step (about 7–9 after smoothing) are far above the square’s threshold, so the squares stay apart; the steps inside the disc are below the disc’s own Int. Each region is judged against its own variation.
F4. 45 pixels (the spot and its smoothed rim), Int 47.5, threshold 269.7. The spot’s own threshold would accept the merge, but the background (huge, small Int) has a tiny threshold, and the test needs both. Its edge to the background (more than 100 grey levels in total, tens per step after smoothing) is far above that.
F5. 17 regions with minimum size 60; 428 with minimum size 0 (most of them small noise regions and thin rims along the boundaries).
F6. Both clicks give the same region (3558 pixels at k = 3000: both halves without their thin smoothed rims). At k = 300 still one region. No k separates them: through the gap, bright pixels of the two halves are neighbours with differences of a few grey levels, below every threshold. FH only looks at the cheapest edge between two components.
F7. σ = 0: 88 regions (pixel noise makes many small regions, especially along edges); σ = 2.5: 13 regions (edges are blurred into ramps of small steps; weaker boundaries such as the faint step are crossed).
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 graphseg/src/mst_seg/exercise.rs. A graph is a number of nodes 0 \dots n-1 and a list of edges (a, b, d). For an image, grid_edges (given) makes the pixel graph with d = |I_p - I_q|, and sorted (given) sorts edges by weight, keeping the order of equal weights (so in 1D, left to right).
C1. UnionFind: new(n), find(a) and union(a, b), as in the labelling unit:
find(a): while parent[a] ≠ a:
parent[a] = parent[parent[a]] # path halving: shorter next time
a = parent[a]
return a
union(a, b): ra = find(a), rb = find(b)
if ra = rb: return "already joined"
hang the smaller of the two under the larger; add up the sizes
C2. kruskal(n, edges): the edges of a minimum spanning tree, in the order they are joined:
for each edge (a, b, d), sorted by d:
if union(a, b) succeeds: keep the edge # else it would close a cycle
The check is B1: AD, BE, BD, CF, BC, total weight 15.
C3. single_linkage(n, edges, t): join across every edge with d < t; the regions are the components. The checks are B1(c) and B5(a) (6 regions at t = 3, one at t = 4).
C4. felzenszwalb(n, edges, k): Kruskal’s order with FH’s test:
Int = 0 for every node
for each edge (a, b, d), sorted by d:
A = find(a), B = find(b)
if A ≠ B and d ≤ min(Int[A] + k/size[A], Int[B] + k/size[B]):
R = union(A, B); Int[R] = d
The checks are B2(c, d), G3, B3, B4(a) and B5(b).
C5. merge_small(n, edges, labels, min_size): the minimum-size clean-up: rebuild the given regions, then go through the edges in increasing order and join the two regions of an edge if either has fewer than min_size nodes.
Then cargo run -p graphseg -- mst-seg and open the pictures in out/mst-seg/. The program works on the micrograph with grey levels 0–255, smoothed with σ = 0.8, with 8 neighbours, as in the lecture.
C6. How many regions does single linkage give at t = 3, 6 and 12? Compare with the slide “Single linkage: thresholding the edges”. The program also prints the heaviest edge of the minimum spanning tree. Without running anything else: from which t on is the whole image one region? Why?
C7. How many regions does FH give at k = 50, 300 and 1500, before and after the clean-up? At which k is the nucleus one region of its own (fh-300.png), and what happens to it at k = 1500?
C8. The program times the minimum spanning tree (building the edges, sorting, one pass) and FH. How does the cost grow with the number of pixels?
The model solution is graphseg/src/mst_seg/solution.rs in the download.
C6. 25 514, 9996 and 1072 regions: within a few regions of the lecture’s numbers (the smoothing differs slightly). At t = 6 the nucleus is one region, but the background is in many pieces; at t = 12 one region covers almost everything. The heaviest tree edge is 28.7: for every t > 28.7 the whole image is one region, because the tree path between any two pixels then has no edge \ge t (single linkage = the tree minus its edges \ge t).
C7. k = 50: 2004 regions, 584 after the clean-up; k = 300: 681 and 135; k = 1500: 302 and 30 (the lecture: 583, 136, 30). At k = 300 the nucleus is one region with an irregular border; at k = 1500 it has merged with its surroundings through a weak part of its envelope, and almost the whole image is one region apart from small dark specks.
C8. In our run the tree took about 56 ms for 65 536 pixels and 260 610 edges, and FH with the clean-up about 100 ms per k (it sorts the edges again). Sorting m edges costs about m \log m, the pass with union–find is nearly linear: the whole method grows only a little faster than the number of pixels.
Where this leads
Kruskal grows a forest without seeds. Start it from markers instead, and use the altitude of a landscape as the weights, and you get the watershed: watershed as a minimum spanning forest. Keeping the whole merge order gives a hierarchy.