Algorithm Search⌘K

Proofs: Green's filter

These are the human proofs of Green's filter, at full length.

Green's network sorts each input of 16 items full

This proof shows: correctness.

Call the network NN. It is a fixed list of standard comparators, so its output is a permutation of its input. By the zero-one principle it is enough to show that NN sorts each x∈{0,1}16x \in \{0, 1\}^{16}. For wires w,w′∈{0,…,15}w, w' \in \{0, \ldots, 15\} write w⊆w′w \subseteq w' when each bit of ww is also a bit of w′w'.

Lemma 1 (the filter). After lines L1 to L4, for each input over a totally ordered set, the item on wire ww is at most the item on wire w′w' whenever w⊆w′w \subseteq w'.

Proof. Let BB be a set of bits, and say that xx is monotone in BB when xw≤xw+2bx_w \le x_{w + 2^b} for each b∈Bb \in B and each ww with bit bb equal to 0. The layer for bit cc makes xx monotone in cc. We show that it keeps xx monotone in each other bit bb. Take ww with bits bb and cc equal to 0, and let β=2b\beta = 2^b and γ=2c\gamma = 2^c. The layer for cc replaces the pairs (xw,xw+γ)(x_w, x_{w+\gamma}) and (xw+β,xw+β+γ)(x_{w+\beta}, x_{w+\beta+\gamma}) by their minimum and maximum. If xw≤xw+βx_w \le x_{w+\beta} and xw+γ≤xw+β+γx_{w+\gamma} \le x_{w+\beta+\gamma} before, then

min⁡(xw,xw+γ)≤min⁡(xw+β,xw+β+γ),max⁡(xw,xw+γ)≤max⁡(xw+β,xw+β+γ),\min(x_w, x_{w+\gamma}) \le \min(x_{w+\beta}, x_{w+\beta+\gamma}), \qquad \max(x_w, x_{w+\gamma}) \le \max(x_{w+\beta}, x_{w+\beta+\gamma}),

so the order in bit bb holds after the layer. So after the four layers xx is monotone in all four bits. If w⊆w′w \subseteq w', add the bits of w′w' that are not in ww one at a time; each step does not decrease the item. □\square

So for x∈{0,1}16x \in \{0, 1\}^{16}, the set ZZ of wires that hold a 0 after line L4 is a down-set: if w′∈Zw' \in Z and w⊆w′w \subseteq w', then w∈Zw \in Z. Let z=∣Z∣z = |Z|. The network sorts xx if and only if, at the end, the zeros are on the wires 0,…,z−10, \ldots, z - 1.

Lemma 2 (symmetry). Let φ(x)w=1−x15−w\varphi(x)_w = 1 - x_{15 - w}. Then N(φ(x))=φ(N(x))N(\varphi(x)) = \varphi(N(x)), and φ\varphi maps sorted sequences to sorted sequences.

Proof. For a comparator [i:j][i{:}j] let c′c' be [15−j:15−i][15 - j : 15 - i]. On φ(x)\varphi(x), the comparator c′c' writes min⁡(1−xj,1−xi)=1−max⁡(xi,xj)\min(1 - x_j, 1 - x_i) = 1 - \max(x_i, x_j) to wire 15−j15 - j and 1−min⁡(xi,xj)1 - \min(x_i, x_j) to wire 15−i15 - i, which is φ\varphi of the result of [i:j][i{:}j] on xx. Each layer is mapped to itself by c↦c′c \mapsto c': in lines L1 to L4, [i:i+2b][i : i + 2^b] maps to [15−i−2b:15−i][15 - i - 2^b : 15 - i], which is in the same layer; in lines L5 to L10 the pairs map as [1:2]↔[13:14][1{:}2] \leftrightarrow [13{:}14], [4:8]↔[7:11][4{:}8] \leftrightarrow [7{:}11], [1:4]↔[11:14][1{:}4] \leftrightarrow [11{:}14], [2:8]↔[7:13][2{:}8] \leftrightarrow [7{:}13], [10:12]↔[3:5][10{:}12] \leftrightarrow [3{:}5], [2:4]↔[11:13][2{:}4] \leftrightarrow [11{:}13], [7:9]↔[6:8][7{:}9] \leftrightarrow [6{:}8], [7:10]↔[5:8][7{:}10] \leftrightarrow [5{:}8], [9:12]↔[3:6][9{:}12] \leftrightarrow [3{:}6], [9:10]↔[5:6][9{:}10] \leftrightarrow [5{:}6], [11:12]↔[3:4][11{:}12] \leftrightarrow [3{:}4], [6:7]↔[8:9][6{:}7] \leftrightarrow [8{:}9], and [3:12][3{:}12], [5:10][5{:}10], [6:9][6{:}9], [7:8][7{:}8] map to themselves. The comparators of one layer have disjoint wires, so their order does not matter. □\square

The input φ(x)\varphi(x) has 16−z′16 - z' zeros if xx has z′z' zeros. So by Lemma 2 it is enough to show that NN sorts each 0-1 input with at most 8 zeros. From now on z≤8z \le 8.

The groups. Let I={1,2,4,8}I = \{1, 2, 4, 8\}, M={3,5,6,9,10,12}M = \{3, 5, 6, 9, 10, 12\} and III={7,11,13,14}III = \{7, 11, 13, 14\}, and let z1=∣Z∩I∣z_1 = |Z \cap I|, z2=∣Z∩M∣z_2 = |Z \cap M| and z3=∣Z∩III∣z_3 = |Z \cap III|. If z≥1z \ge 1 then 0∈Z0 \in Z. A wire of IIIIII in ZZ brings the three wires of MM and the three wires of II below it, and wire 0, so z3≥1z_3 \ge 1 forces z≥8z \ge 8. Hence z3≤1z_3 \le 1, z3=1z_3 = 1 only if z=8z = 8, z1=z2=3z_1 = z_2 = 3, and wire 15∉Z15 \notin Z. Also a wire of MM in ZZ brings its two wires of II, so z2≤(z12)z_2 \le \binom{z_1}{2}.

Lemma 3 (the four sorts). Wires 0 and 15 are not touched after line L4. In lines L5 to L7, the comparators on the wires of II are exactly [1:2],[4:8],[1:4],[2:8],[2:4][1{:}2], [4{:}8], [1{:}4], [2{:}8], [2{:}4], and they sort II in the order 1,2,4,81, 2, 4, 8; wire 8 holds the largest item of II after line L6, and wires 1 and 2 are not touched after line L7. In the same way IIIIII is sorted in the order 7,11,13,147, 11, 13, 14, wire 7 holds its smallest item after line L6, and wires 13 and 14 are not touched after line L7. After line L5, the comparators [3:5][3{:}5] (L6), [6:8][6{:}8] (L7), [3:6],[5:8][3{:}6], [5{:}8] (L8), [5:6][5{:}6] (L9) sort the wires D=(3,5,6,8)D = (3, 5, 6, 8) in this order, and [10:12][10{:}12] (L6), [7:9][7{:}9] (L7), [7:10],[9:12][7{:}10], [9{:}12] (L8), [9:10][9{:}10] (L9) sort the wires U=(7,9,10,12)U = (7, 9, 10, 12). After line L8, wire 3 holds the smallest and wire 8 the largest item of DD, and wire 7 the smallest and wire 12 the largest item of UU.

Proof. Read the layers. Each of the named groups of five comparators is the standard sorter [a:b],[c:d],[a:c],[b:d],[b:c][a{:}b], [c{:}d], [a{:}c], [b{:}d], [b{:}c] on four wires a,b,c,da, b, c, d, which sorts each input, and after its third and fourth comparators aa holds the minimum and dd the maximum. No other comparator touches these wires between the comparators of the group, except that the comparator [6:8][6{:}8] of DD comes after wire 8 has its final value from II (line L6), and [7:9][7{:}9] of UU comes after wire 7 has its final value from IIIIII. In line L9 the comparators [3:4][3{:}4], [7:8][7{:}8] and [11:12][11{:}12] run at the same time as [5:6][5{:}6] and [9:10][9{:}10], on other wires. □\square

Now we follow a 0-1 input with z≤8z \le 8 zeros. Write xˉw\bar{x}_w for the bit on wire ww after line L4.

Line L5 compares the complementary pairs (3,12)(3, 12), (5,10)(5, 10), (6,9)(6, 9) of MM. Both wires of such a pair in ZZ would bring all four wires of II into ZZ. Let pp be the number of these pairs with two zeros and qq the number with one zero, so z2=2p+qz_2 = 2p + q, and p≥1p \ge 1 only if z1=4z_1 = 4. After line L5, wires 3, 5, 6 hold p+qp + q zeros and wires 9, 10, 12 hold pp zeros. With Lemma 3, the group DD holds d=p+q+[z1=4]d = p + q + [z_1 = 4] zeros, because wire 8 holds a zero only if all of II is zero, and UU holds u=p+z3u = p + z_3 zeros. After the sort of II, wires 1,2,4,81, 2, 4, 8 hold 0z114−z10^{z_1} 1^{4 - z_1}, and after the sort of IIIIII, wire 7 holds a 0 if and only if z3=1z_3 = 1, and wires 11, 13, 14 hold 1.

In each case below u≤1u \le 1, so wire 12, which holds the largest item of the four wires of UU, holds 1, and the comparator [11:12][11{:}12] of line L9 changes nothing. It remains to follow [3:4][3{:}4], [7:8][7{:}8] (line L9) and [6:7][6{:}7], [8:9][8{:}9] (line L10), with DD and UU sorted. We use z=1+z1+z2+z3z = 1 + z_1 + z_2 + z_3 for z≥1z \ge 1.

Case z≤3z \le 3, so z1≤2z_1 \le 2 and z2=z3=0z_2 = z_3 = 0. The zeros are on wire 0 and on the first z−1z - 1 wires of II, that is, on wires 11 and 22, and no other wire holds a 0. Every later comparator sees two ones or a 0 on its lower wire. So the output has the zeros on wires 0,…,z−10, \ldots, z - 1.

Case z1=2z_1 = 2, z2=1z_2 = 1, so z=4z = 4, p=0p = 0, q=1q = 1, d=1d = 1, u=0u = 0. Wire 3 holds the only zero of DD, wires 4 and 8 hold 1, and UU holds no zero. The comparators [3:4][3{:}4], [7:8][7{:}8], [6:7][6{:}7], [8:9][8{:}9] change nothing. The zeros are on wires 0 to 3.

Case z1=3z_1 = 3. Then p=0p = 0, q=z2≤3q = z_2 \le 3, d=z2d = z_2, u=z3u = z_3, wire 4 holds 0 and wire 8 holds 1. So DD holds zeros on its first z2z_2 wires in the order 3,5,6,83, 5, 6, 8, and wire 8 holds 1.

  • z2=0z_2 = 0: wire 3 holds 1 and wire 4 holds 0, so [3:4][3{:}4] exchanges them. The zeros are on wires 0 to 3, and z=4z = 4.
  • z2=1z_2 = 1: zeros on wires 0, 1, 2, 3, 4, and z=5z = 5.
  • z2=2z_2 = 2: zeros on wires 0 to 5, and z=6z = 6.
  • z2=3z_2 = 3, z3=0z_3 = 0: zeros on wires 0 to 6, and wire 7 holds 1. So z=7z = 7.
  • z2=3z_2 = 3, z3=1z_3 = 1: zeros on wires 0 to 6, and wire 7 holds the smallest item of UU, a 0. So the zeros are on wires 0 to 7, and z=8z = 8.

In each subcase, [7:8][7{:}8], [6:7][6{:}7] and [8:9][8{:}9] see a 0 on the lower wire or two equal bits, so they change nothing.

Case z1=4z_1 = 4. Then z=5+z2+z3≤8z = 5 + z_2 + z_3 \le 8, so z2≤3z_2 \le 3, and z3=0z_3 = 0 because z3=1z_3 = 1 needs z1=3z_1 = 3. So (p,q)(p, q) is one of (0,0),(0,1),(0,2),(0,3),(1,0),(1,1)(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), with d=p+q+1d = p + q + 1 and u=pu = p. Wires 1, 2, 4 hold 0, so [3:4][3{:}4] changes nothing.

  • (0,0)(0, 0): d=1d = 1, a zero on wire 3. Zeros on wires 0 to 4, and z=5z = 5.
  • (0,1)(0, 1): d=2d = 2, zeros on wires 3 and 5. Zeros on wires 0 to 5, and z=6z = 6.
  • (0,2)(0, 2): d=3d = 3, zeros on wires 3, 5, 6. Zeros on wires 0 to 6, and z=7z = 7.
  • (0,3)(0, 3): d=4d = 4, zeros on wires 3, 5, 6, 8, and u=0u = 0, so wire 7 holds 1. The comparator [7:8][7{:}8] exchanges them. Then the zeros are on wires 0 to 7, wire 8 holds 1, and [6:7][6{:}7] and [8:9][8{:}9] change nothing. So z=8z = 8.
  • (1,0)(1, 0): d=2d = 2, zeros on wires 3 and 5; u=1u = 1, a zero on wire 7. Wire 8 holds 1, so [7:8][7{:}8] changes nothing. Before line L10 the zeros are on wires 0 to 5 and 7, and wire 6 holds 1. The comparator [6:7][6{:}7] exchanges them, and [8:9][8{:}9] sees two ones. The zeros are on wires 0 to 6, and z=5+2=7z = 5 + 2 = 7.
  • (1,1)(1, 1): d=3d = 3, zeros on wires 3, 5, 6; u=1u = 1, a zero on wire 7. Zeros on wires 0 to 7, and z=5+3=8z = 5 + 3 = 8.

In each case the zeros end on the wires 0,…,z−10, \ldots, z - 1, so NN sorts each 0-1 input with at most 8 zeros. By Lemma 2 it sorts each 0-1 input, and by the zero-one principle it sorts each input. ■\blacksquare

Back to Green's filter