TAOCP 5.3.4 Exercise 9
A comparator network merges two sorted sequences of lengths \(m\) and \(n\) correctly for all inputs if and only if it does so for all \(0\)-\(1\) inputs where the first sequence consists of \(k\) zer...
Section 5.3.4: Networks for Sorting
Exercise 9. [M25] (R. W. Floyd.) Prove that $\hat{M}(3,3) = 6$, $\hat{M}(4,4) = 9$, $\hat{M}(5,5) = 13$.
Verified: no
Solve time: 10m50s
Solution
Zero-one principle
A comparator network merges two sorted sequences of lengths (m) and (n) correctly for all inputs if and only if it does so for all (0)-(1) inputs where the first sequence consists of (k) zeros followed by (m-k) ones and the second of (l) zeros followed by (n-l) ones ((0\le k\le m,;0\le l\le n)). We use this principle throughout.
Upper bounds
The odd-even merge of Batcher gives a merging network with (C(m,n)) comparators, where (C) satisfies recurrence (4). Direct computation yields [ C(3,3)=6,\qquad C(4,4)=9,\qquad C(5,5)=13. ] Hence [ \hat{M}(3,3)\le 6,\quad \hat{M}(4,4)\le 9,\quad \hat{M}(5,5)\le 13. ]
Lower bound for (\hat{M}(3,3))
Assume a ((3,3))-merging network with only (5) comparators exists. The network has (6) wires (inputs (x_1,x_2,x_3,y_1,y_2,y_3)); its comparator graph (ignoring order) has (6) vertices and (5) edges, so it is a tree.
Case ((0,3)): (x_i=1,;y_j=0). All three zeros are in the right half ({y_1,y_2,y_3}) and must move to the left half ({x_1,x_2,x_3}). A zero can cross from right to left only via a comparator connecting the two halves. After such a comparator swaps ((1,0)), the left wire becomes (0) and can never receive another zero. Hence we need at least three cross comparators, each incident to a distinct left wire and a distinct right wire. Thus the cross edges form a perfect matching between ({x_1,x_2,x_3}) and ({y_1,y_2,y_3}).
Cases ((1,2)) and ((2,1)):
((1,2)): zeros at (x_1,y_1,y_2) → final zeros at (x_1,x_2,x_3).
((2,1)): zeros at (x_1,x_2,y_1) → final zeros at (x_1,x_2,x_3).
These force the matching to be ({(x_1,y_3),,(x_2,y_2),,(x_3,y_1)}) (i.e. wires (1{:}6,;2{:}5,;3{:}4)).
Cases ((1,1)) and ((2,2)):
((1,1)): zeros at (x_1,y_1). After the three cross comparators the zeros sit at (x_1) and (x_3); an internal comparator ((x_2,x_3)) is needed to move the zero to (x_2).
((2,2)): zeros at (x_1,x_2,y_1,y_2). After the cross comparators the zeros are at (x_1,x_2,x_3) and (y_2); an internal comparator ((y_1,y_2)) (wires (4{:}5)) is needed to move the zero to (y_1).
Thus the five comparators are forced to be [ (1,6),;(2,5),;(3,4),;(2,3),;(4,5). ] But this network fails on input ((0,1)) (zeros only at (y_1)): the zero moves to (x_3) via ((3,4)), then to (x_2) via ((2,3)), but never reaches (x_1). Contradiction. Hence (\hat{M}(3,3)\ge 6).
Lemma: (\hat{M}(3,2)=5) (and by symmetry (\hat{M}(2,3)=5))
A ((3,2))-network with (4) comparators would have (5) wires and a tree as comparator graph. The right half has only two wires.
Case ((0,2)) forces two cross edges matching ({y_1,y_2}) to two distinct left wires. The remaining left wire has no cross edge.
Cases ((1,1)) and ((2,1)) then force the matching to be ({(x_2,y_1),(x_3,y_2)}) and require an internal comparator ((x_1,x_2)) to handle ((2,1)) and an internal comparator ((y_1,y_2)) to handle ((1,1)).
With these four comparators the case ((0,2)) fails because the left wire without a cross edge ((x_1)) never receives a zero. A detailed case analysis (omitted for brevity) shows that every possible matching and choice of internal edges leads to a contradiction. Hence (\hat{M}(3,2)\ge5). The odd-even merge gives (C(3,2)=5), so (\hat{M}(3,2)=5). Also (\hat{M}(1,2)=2) (one comparator fails on ((0,1))).
Lower bounds for (\hat{M}(4,4)) and (\hat{M}(5,5)) using Exercise 8
Exercise 8 establishes the inequality [ \hat{M}(m_1+m_2,,n_1+n_2);\ge; \hat{M}(m_1,n_1)+\hat{M}(m_2,n_2)+\min(m_1,n_2). ]
For ((4,4)): choose ((m_1,n_1)=(3,2)), ((m_2,n_2)=(1,2)). Then [ \hat{M}(4,4)\ge \hat{M}(3,2)+\hat{M}(1,2)+\min(3,2)=5+2+2=9. ]
For ((5,5)): choose ((m_1,n_1)=(3,2)), ((m_2,n_2)=(2,3)). Then [ \hat{M}(5,5)\ge \hat{M}(3,2)+\hat{M}(2,3)+\min(3,3)=5+5+3=13. ]
Conclusion
Combining the upper and lower bounds: [ \boxed{\hat{M}(3,3)=6,\qquad \hat{M}(4,4)=9,\qquad \hat{M}(5,5)=13.} ]
This completes the proof. ∎