Algorithm Search⌘K

Proofs: AKS sorting network

These are the human proofs of AKS sorting network, at full length.

The bag network sorts each input full

This proof shows: correctness.

This proof follows Seiferas (Sections 4 and 5) for the case of the code, where SEPARATE in line L5 is an exact sorter, so ε=0\varepsilon = 0. The constants are A=10A = 10, λ=1/100\lambda = 1/100 and ν=13/20\nu = 13/20. Let n=2Ln = 2^L and let the leaves be the bags at depth L−1L - 1.

Reduction. The construction is a fixed list of comparators, each of which puts the minimum on a named register. By the zero-one principle, which also holds for such general comparators, it is enough to show that the network sorts each input in {0,1}n\{0, 1\}^n. Each such input is f(π)f(\pi) for a permutation π\pi of 0,…,n−10, \ldots, n-1 and a non-decreasing map ff, and the network commutes with ff. So it is enough to show that it sorts each permutation of the ranks 0,…,n−10, \ldots, n-1. From now on the items are these ranks, all different.

Write IXI_X for the interval of a node XX. An item rr in the bag of XX is a jj-stranger, for j≥1j \ge 1, if r∉IYr \notin I_Y for the ancestor YY of XX that is j−1j - 1 levels up (or for the root, if there is no such ancestor). Let bXb_X be the capacity of XX at the current time tt, so a child has capacity AbXA b_X and the parent bX/Ab_X / A.

Invariant. Before each stage:

  1. the bags on alternate levels are empty;
  2. all bags on one level hold the same number of items;
  3. each bag XX holds at most bXb_X items;
  4. each bag XX holds at most λbX\lambda b_X 1-strangers and no jj-strangers for j≥2j \ge 2.

Before the first stage all nn items are in the root, of capacity nn, and all are native to the root. So the invariant holds with the odd levels empty.

Lemma 1 (strangers in a bag go back). Let DD be a bag that is not the root, and let the invariant hold. Then each 1-stranger of DD is among the items that DD sends to its parent in lines L6 and L7.

Proof. A 1-stranger of DD is either smaller than each item of IDI_D or larger than each item of IDI_D. Let ss be the number of small ones. By part 4, s≤λbDs \le \lambda b_D, and ss is an integer, so s≤⌊λbD⌋s \le \lfloor \lambda b_D \rfloor. The exact sorter puts the ss small strangers in the first ss positions, and line L6 sends the first ⌊λbD⌋\lfloor \lambda b_D \rfloor positions up (or all items, if there are fewer than 2⌊λbD⌋2 \lfloor \lambda b_D \rfloor). The large strangers are handled in the same way at the other end. □\square

Parts 1 and 2. By part 1, all items of the nonempty levels move one level up or down in a stage, so after it the nonempty levels are the ones that were empty. All bags on a level have the same capacity and, by part 2, the same number of items, so lines L6 to L8 send the same numbers of items from each of them. So the bags on each level receive the same numbers, and part 2 holds again.

Part 3. By part 1, a bag BB that holds items after the stage was empty before it, and it receives items only from its parent DD and its two children. Let b=bBb = b_B before the stage. The parent holds at most b/Ab/A items and sends at most half of them to BB. Each child has capacity AbAb and sends at most 2⌊λAb⌋+12 \lfloor \lambda A b \rfloor + 1 items up. So BB receives at most 4λAb+2+b/(2A)4 \lambda A b + 2 + b/(2A) items.

  • If b≥Ab \ge A, this is at most b(4λA+1/(2A)+2/A)b(4 \lambda A + 1/(2A) + 2/A).
  • If b<Ab < A, each bag above BB has capacity at most b/A<1b/A < 1, so by part 3 it is empty. The level of BB is empty too, so all nn items are in the subtrees rooted at the level of the children of BB. By part 2 these subtrees hold the same number of items, nn divided by their number, which is a power of 2 and at least 2. Let EE be a child of BB. The two subtrees below EE hold the same number of items by part 2, so EE itself holds an even number of items. After line L6 an even number remains, so line L7 sends nothing, and BB receives at most 4λAb+b/(2A)4 \lambda A b + b/(2A) items.

In both cases BB receives at most b(4λA+5/(2A))b(4 \lambda A + 5/(2A)) items. For the constants above, 4λA+5/(2A)=2/5+1/4=13/20=ν4 \lambda A + 5/(2A) = 2/5 + 1/4 = 13/20 = \nu, and the new capacity of BB is νb\nu b. So part 3 holds again.

Part 4, strangers of order j≥2j \ge 2. Let BB be a bag that receives items, with parent DD. An item that comes from a child EE of BB and is a jj-stranger in BB is not native to the ancestor j−1j - 1 levels above BB, which is jj levels above EE. So it was a (j+1)(j+1)-stranger in EE, and there are none. An item that comes from DD and is a jj-stranger in BB was a (j−1)(j-1)-stranger in DD. For j≥3j \ge 3 there are none, and for j=2j = 2 these are 1-strangers of DD, which Lemma 1 sends to the parent of DD, not to BB. (If DD is the root, it has no strangers.) So BB has no jj-strangers for j≥2j \ge 2.

Part 4, 1-strangers. The root never has strangers. Let BB be a bag that receives items, of capacity bb before the stage, with parent DD and sibling CC, and let DD hold dd items. Say BB is the left child; the other case is symmetric. By the case j≥2j \ge 2, no item from a child of BB and no 1-stranger of DD is a 1-stranger in BB after the stage. So the only 1-strangers that BB gets are items of DD that are native to CC.

The exact sorter puts the items of DD in order. Line L8 gives BB only positions below d/2d/2, because the rest after lines L6 and L7 is symmetric around the middle. Above the items native to CC there are only the large 1-strangers of DD; let sLs_L be their number, so sL≤λb/As_L \le \lambda b/A. If cc is the number of items of DD native to CC, then those at positions below d/2d/2 number at most max⁡(0,c+sL−d/2)\max(0, c + s_L - d/2). So BB gets at most max⁡(0,c−d/2)+λb/A\max(0, c - d/2) + \lambda b / A 1-strangers.

To bound c−d/2c - d/2, count the items native to CC. Let the level of CC have 2i2^i bags C′C'. By part 2, the subtree of each C′C' holds the same number mm of items, and each parent D′D' holds dd items. The bag CC is on the level of BB, so it is empty by part 1. An item at depth δ≥1\delta \ge 1 below CC that is not native to CC is a (δ+1)(\delta + 1)-stranger, and there are none. So the subtree of CC holds mm items native to CC, and the other items of ICI_C include the cc items in DD. So

c≤∣IC∣−m.c \le |I_C| - m.

All intervals on the level of CC have the same length, so ∣IC′∣−m−d/2|I_{C'}| - m - d/2 is the same number for each C′C'. Their sum over the 2i2^i bags C′C' is n−2im−2i−1dn - 2^i m - 2^{i-1} d, which is the number of items on the levels above the level of DD. By parts 1 and 3, the levels above DD that hold items are 2,4,6,…2, 4, 6, \ldots levels above DD, with 2i−3,2i−5,…2^{i-3}, 2^{i-5}, \ldots bags of capacity b/A3,b/A5,…b/A^3, b/A^5, \ldots. So

c−d2≤∣IC∣−m−d2≤12i∑r≥12i−2r−1bA2r+1=b∑r≥11(2A)2r+1=b8A3−2A.c - \frac d2 \le |I_C| - m - \frac d2 \le \frac{1}{2^i} \sum_{r \ge 1} 2^{i - 2r - 1} \frac{b}{A^{2r+1}} = b \sum_{r \ge 1} \frac{1}{(2A)^{2r+1}} = \frac{b}{8A^3 - 2A}.

So BB gets at most b(18A3−2A+λA)=b(17980+11000)<132000b=λνbb \left( \frac{1}{8A^3 - 2A} + \frac{\lambda}{A} \right) = b \left( \frac{1}{7980} + \frac{1}{1000} \right) < \frac{13}{2000} b = \lambda \nu b 1-strangers. This is λ\lambda times the new capacity of BB, so part 4 holds again.

Each stage can be done. The only rule that can fail is line L8 at a leaf, which has no children. By part 2 the n/2n/2 leaves hold the same number of items, so each holds at most 2. Line L2 runs a stage only if the leaf capacity bb satisfies b≥1/λb \ge 1/\lambda, so 2⌊λb⌋≥22 \lfloor \lambda b \rfloor \ge 2 and line L6 sends all items of a leaf up.

The final step. When the loop stops, the leaf capacity bb satisfies b<1/λb < 1/\lambda. Let kk be the smallest integer with (1/λ)(1/A)k+1<1(1/\lambda)(1/A)^{k+1} < 1, and let τ=max⁡(L−1−k,0)\tau = \max(L - 1 - k, 0) be the depth of the roots of the final subtrees. A bag at depth below τ\tau has capacity at most b/Ak+1<(1/λ)(1/A)k+1<1b / A^{k+1} < (1/\lambda) (1/A)^{k+1} < 1, so by part 3 it is empty. If τ>0\tau > 0, a bag RR at depth τ=L−1−k\tau = L - 1 - k has capacity bR=b/Ak<1/(λAk)b_R = b / A^k < 1/(\lambda A^k), so by part 4 it holds fewer than λbR<1/Ak≤1\lambda b_R < 1/A^k \le 1 1-strangers, that is none. An item in the subtree of RR at depth δ\delta below RR that is not native to RR is a (δ+1)(\delta + 1)-stranger with δ≥1\delta \ge 1, and there are none. (If τ=0\tau = 0, all items are native to the root.) So each item lies in the subtree of a depth-τ\tau node to whose interval it belongs. By part 2 all these 2τ2^\tau subtrees hold the same number of items, n/2τ=∣IR∣n / 2^\tau = |I_R|. So the subtree of RR holds exactly the items whose ranks are in IRI_R.

Line L10 sorts the registers of each subtree and lists the subtrees from left to right. The intervals IRI_R are in increasing order from left to right, so the output lists the ranks 0,…,n−10, \ldots, n-1 in order. ■\blacksquare

Back to AKS sorting network