Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions
Sorting Algorithms comparisons to sort n elements comparisons required to merge two arrays of size m/2 Order of comparisons not fixed in advance. Mergesort(array[1,…,n] of Integers): begin Mergesort(array[1,…,n/2]); Mergesort(array[n/2+1,…,n]); Merge(array[1,…,n/2], array[n/2+1,…,n]); end Not readily implementable in hardware. Example
Sorting Networks Order of comparisons fixed in advance. Readily implementable in hardware. C D B A A B C D Sorting Network
Sorting Networks (binary values) Sorting Network inputs outputs sorted
Comparator (2-sorter) x y min(x, y) max(x, y) inputs outputs C
Comparator (2-sorter) x y min(x, y) max(x, y) inputs outputs
Comparison Network depth d width n
Comparison Network n / 2 comparisons per stage d stages
Sorting Network Any ideas?
Sorting Network inputs outputs n 1 Sorting Network n...
Insertion Sort Network inputs outputs depth 2n 3
Batcher Sorting Network Next Lecture
Sorting Arbitrary Numbers x, y can be values from any linearly ordered set, e.g., integers, reals, etc. inputs outputs x y min(x, y) max(x, y)
Comparison function: C(X,Y) = 1 if X > Y, 0 otherwise. Idea: use C(X,Y) to select the min and the max of X and Y. X, Y: integers represented as m-bit binary strings. Integer Comparator
Sorting Arbitrary Numbers sorted 6 9 2
Sorting Arbitrary Numbers sorted 4 5 1
Sorting Arbitrary Numbers not sorted How can we verify if a network sorts all possible input sequences?
Sorting Arbitrary Numbers inputsoutputs Try all possible 0/1 sequences.
Sorting Arbitrary Numbers inputsoutputs Try all possible 0/1 sequences.
Sorting Arbitrary Numbers inputsoutputs 000 Try all possible 0/1 sequences. 001
Sorting Arbitrary Numbers inputsoutputs 000 Try all possible 0/1 sequences
Sorting Arbitrary Numbers inputsoutputs 000 Try all possible 0/1 sequences
Sorting Arbitrary Numbers inputsoutputs 000 Try all possible 0/1 sequences
Sorting Arbitrary Numbers inputsoutputs 000 Try all possible 0/1 sequences
Sorting Arbitrary Numbers inputsoutputs 000 Try all possible 0/1 sequences not sorted!
Sorting Arbitrary Numbers inputsoutputs 000 Try all possible 0/1 sequences not sorted!
Sorting Arbitrary Numbers
inputsoutputs Try all possible 0/1 sequences.
Sorting Arbitrary Numbers inputsoutputs Try all possible 0/1 sequences.
Sorting Arbitrary Numbers inputsoutputs 000 Try all possible 0/1 sequences. 001
Sorting Arbitrary Numbers inputsoutputs 000 Try all possible 0/1 sequences
Sorting Arbitrary Numbers inputsoutputs 000 Try all possible 0/1 sequences
Sorting Arbitrary Numbers inputsoutputs 000 Try all possible 0/1 sequences
Sorting Arbitrary Numbers inputsoutputs 000 Try all possible 0/1 sequences
Sorting Arbitrary Numbers inputsoutputs 000 Try all possible 0/1 sequences
Sorting Arbitrary Numbers inputsoutputs 000 Try all possible 0/1 sequences
Sorting Arbitrary Numbers inputsoutputs 000 Try all possible 0/1 sequences all sorted!
Zero-One Principle If a comparison network sorts all possible sequences of 0’s and 1’s correctly, then it sorts all sequences of arbitrary numbers correctly.
Lemma Given For a monotonically increasing function f,
Lemma Given For a monotonically increasing function f,
Proof: Lemma
f is monotonically increasing:
Proof: Lemma f is monotonically increasing:
Proof: Lemma f is monotonically increasing:
Generalization Given
For a monotonically increasing function f, Generalization (by induction)
Proof: Zero-One Principle Suppose b) there exists a sequence that it doesn’t sort, i.e., such that but is placed before in the output. a) the network sorts all sequences of 0’s and 1’s, Define f (x) = 0 if 1 otherwise
Proof: Zero-One Principle Sorting Network
Proof: Zero-One Principle Sorting Network
Proof: Zero-One Principle Sorting Network contradiction!