Comparison Networks Sorting Sorting binary values Sorting arbitrary
Comparison Networks Sorting • Sorting binary values • Sorting arbitrary numbers • Implementing symmetric functions
Sorting Algorithms Example 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 comparisons required to merge two arrays of size m/2 comparisons to sort n elements Order of comparisons not fixed in advance. Not readily implementable in hardware.
Sorting Networks C D B A Sorting Network Order of comparisons fixed in advance. Readily implementable in hardware. A B C D
Sorting Networks (binary values) inputs outputs 1 0 0 1 1 Sorting Network 0 0 1 1 sorted
Comparator (2 -sorter) outputs inputs x y C min(x, y) max(x, y)
Comparator (2 -sorter) AON Implementation inputs x y outputs min(x, y) max(x, y)
Comparator (2 -sorter) inputs outputs x min(x, y) y max(x, y)
Comparison Network 1 0 0 1 1 0 1 1 1 width n depth d
Comparison Network n/2 comparisons per stage 1 0 0 1 1 0 1 1 1 d stages
Sorting Network Any ideas?
Sorting Network inputs . . . outputs . . . Sorting Network . . . n 1 n
Insertion Sort Network inputs outputs depth 2 n 3
Batcher Sorting Network Next Lecture
Sorting Arbitrary Numbers inputs outputs x min(x, y) y max(x, y) x, y can be values from any linearly ordered set, e. g. , integers, reals, etc.
Integer Comparator X, Y: integers represented as m-bit binary strings. 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.
Integer Comparator X C(X, Y) min(X, Y) Y C(X, Y) X C(X, Y) max(X, Y) Y C(X, Y)
Sorting Arbitrary Numbers 9 2 2 9 6 6 9 9 sorted
Sorting Arbitrary Numbers 1 1 5 5 4 4 5 5 sorted
Sorting Arbitrary Numbers 3 3 7 7 0 0 7 7 not sorted How can we verify if a network sorts all possible input sequences?
Sorting Arbitrary Numbers inputs Try all possible 0/1 sequences. outputs
Sorting Arbitrary Numbers 0 0 0 Try all possible 0/1 sequences. inputs outputs 000
Sorting Arbitrary Numbers 0 0 0 0 1 1 Try all possible 0/1 sequences. inputs outputs 000 001
Sorting Arbitrary Numbers 0 0 0 0 1 1 Try all possible 0/1 sequences. inputs outputs 000 001 010 001 001
Sorting Arbitrary Numbers 0 0 1 1 1 1 Try all possible 0/1 sequences. inputs outputs 000 001 010 011 000 001 011
Sorting Arbitrary Numbers 1 0 0 0 0 1 1 Try all possible 0/1 sequences. inputs outputs 000 001 010 011 100 001 011 001
Sorting Arbitrary Numbers 1 0 0 1 1 1 1 Try all possible 0/1 sequences. inputs outputs 000 001 010 011 100 101 000 001 011
Sorting Arbitrary Numbers 1 1 1 0 0 1 1 Try all possible 0/1 sequences. inputs outputs 000 001 010 011 100 101 110 001 001 011 101 not sorted!
Sorting Arbitrary Numbers 1 1 1 Try all possible 0/1 sequences. inputs outputs 000 001 010 011 100 101 110 111 000 001 011 101 111 not sorted!
Sorting Arbitrary Numbers
Sorting Arbitrary Numbers inputs Try all possible 0/1 sequences. outputs
Sorting Arbitrary Numbers 0 0 0 Try all possible 0/1 sequences. inputs outputs 000
Sorting Arbitrary Numbers 0 0 0 0 1 1 Try all possible 0/1 sequences. inputs outputs 000 001
Sorting Arbitrary Numbers 0 0 1 1 1 0 0 1 Try all possible 0/1 sequences. inputs outputs 000 001 010 001 001
Sorting Arbitrary Numbers 0 0 1 1 1 1 Try all possible 0/1 sequences. inputs outputs 000 001 010 011 000 001 011
Sorting Arbitrary Numbers 1 0 0 0 0 1 Try all possible 0/1 sequences. inputs outputs 000 001 010 011 100 001 011 001
Sorting Arbitrary Numbers 1 0 0 1 1 1 1 Try all possible 0/1 sequences. inputs outputs 000 001 010 011 100 101 000 001 011
Sorting Arbitrary Numbers 1 1 0 0 1 1 Try all possible 0/1 sequences. inputs outputs 000 001 010 011 100 101 110 001 001 011 011
Sorting Arbitrary Numbers 1 1 1 Try all possible 0/1 sequences. inputs outputs 000 001 010 011 100 101 110 111 000 001 011 111
Sorting Arbitrary Numbers Try all possible 0/1 sequences. inputs outputs 000 001 010 011 100 101 110 111 000 001 011 111 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
Proof: Lemma
Proof: Lemma f is monotonically increasing:
Proof: Lemma f is monotonically increasing:
Proof: Lemma f is monotonically increasing:
Generalization Given
Generalization For a monotonically increasing function f, (by induction)
Proof: Zero-One Principle Suppose a) the network sorts all sequences of 0’s and 1’s, b) there exists a sequence that it doesn’t sort, i. e. , but such that is placed before Define f (x) = 0 if 1 otherwise in the output.
Proof: Zero-One Principle . . . Sorting Network . . .
Proof: Zero-One Principle . . . Sorting Network . . .
Proof: Zero-One Principle . . . Sorting Network . . . 1 0 contradiction!
Implementing XOR(X) = 1 if odd 0 otherwise Sorting Network 0 0 0 0 1 1 1 0 0 0 1 1 1 1
Implementing XOR(X) = 1 if odd 0 otherwise Sorting Network
Symmetric Functions for some subset of Sorting Network f (X) = 1 if 0 otherwise
- Slides: 56