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 . The constants are , and . Let and let the leaves be the bags at depth .
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 . Each such input is for a permutation of and a non-decreasing map , and the network commutes with . So it is enough to show that it sorts each permutation of the ranks . From now on the items are these ranks, all different.
Write for the interval of a node . An item in the bag of is a -stranger, for , if for the ancestor of that is levels up (or for the root, if there is no such ancestor). Let be the capacity of at the current time , so a child has capacity and the parent .
Invariant. Before each stage:
- the bags on alternate levels are empty;
- all bags on one level hold the same number of items;
- each bag holds at most items;
- each bag holds at most 1-strangers and no -strangers for .
Before the first stage all items are in the root, of capacity , 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 be a bag that is not the root, and let the invariant hold. Then each 1-stranger of is among the items that sends to its parent in lines L6 and L7.
Proof. A 1-stranger of is either smaller than each item of or larger than each item of . Let be the number of small ones. By part 4, , and is an integer, so . The exact sorter puts the small strangers in the first positions, and line L6 sends the first positions up (or all items, if there are fewer than ). The large strangers are handled in the same way at the other end.
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 that holds items after the stage was empty before it, and it receives items only from its parent and its two children. Let before the stage. The parent holds at most items and sends at most half of them to . Each child has capacity and sends at most items up. So receives at most items.
- If , this is at most .
- If , each bag above has capacity at most , so by part 3 it is empty. The level of is empty too, so all items are in the subtrees rooted at the level of the children of . By part 2 these subtrees hold the same number of items, divided by their number, which is a power of 2 and at least 2. Let be a child of . The two subtrees below hold the same number of items by part 2, so itself holds an even number of items. After line L6 an even number remains, so line L7 sends nothing, and receives at most items.
In both cases receives at most items. For the constants above, , and the new capacity of is . So part 3 holds again.
Part 4, strangers of order . Let be a bag that receives items, with parent . An item that comes from a child of and is a -stranger in is not native to the ancestor levels above , which is levels above . So it was a -stranger in , and there are none. An item that comes from and is a -stranger in was a -stranger in . For there are none, and for these are 1-strangers of , which Lemma 1 sends to the parent of , not to . (If is the root, it has no strangers.) So has no -strangers for .
Part 4, 1-strangers. The root never has strangers. Let be a bag that receives items, of capacity before the stage, with parent and sibling , and let hold items. Say is the left child; the other case is symmetric. By the case , no item from a child of and no 1-stranger of is a 1-stranger in after the stage. So the only 1-strangers that gets are items of that are native to .
The exact sorter puts the items of in order. Line L8 gives only positions below , because the rest after lines L6 and L7 is symmetric around the middle. Above the items native to there are only the large 1-strangers of ; let be their number, so . If is the number of items of native to , then those at positions below number at most . So gets at most 1-strangers.
To bound , count the items native to . Let the level of have bags . By part 2, the subtree of each holds the same number of items, and each parent holds items. The bag is on the level of , so it is empty by part 1. An item at depth below that is not native to is a -stranger, and there are none. So the subtree of holds items native to , and the other items of include the items in . So
All intervals on the level of have the same length, so is the same number for each . Their sum over the bags is , which is the number of items on the levels above the level of . By parts 1 and 3, the levels above that hold items are levels above , with bags of capacity . So
So gets at most 1-strangers. This is times the new capacity of , 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 leaves hold the same number of items, so each holds at most 2. Line L2 runs a stage only if the leaf capacity satisfies , so and line L6 sends all items of a leaf up.
The final step. When the loop stops, the leaf capacity satisfies . Let be the smallest integer with , and let be the depth of the roots of the final subtrees. A bag at depth below has capacity at most , so by part 3 it is empty. If , a bag at depth has capacity , so by part 4 it holds fewer than 1-strangers, that is none. An item in the subtree of at depth below that is not native to is a -stranger with , and there are none. (If , all items are native to the root.) So each item lies in the subtree of a depth- node to whose interval it belongs. By part 2 all these subtrees hold the same number of items, . So the subtree of holds exactly the items whose ranks are in .
Line L10 sorts the registers of each subtree and lists the subtrees from left to right. The intervals are in increasing order from left to right, so the output lists the ranks in order.