Title: Comparison Networks
1Comparison Networks
- Sorting
- Sorting binary values
- Sorting arbitrary numbers
- Implementing symmetric functions
2Sorting Algorithms
Example
Order of comparisons not fixed in advance.
Not readily implementable in hardware.
3Sorting Networks
Order of comparisons fixed in advance.
Readily implementable in hardware.
4Sorting Networks (binary values)
inputs
outputs
1
0
0
0
0
0
Sorting Network
1
0
0
1
0
1
1
1
1
1
5Comparator (2-sorter)
outputs
inputs
x
min(x, y)
C
y
max(x, y)
6Comparator (2-sorter)
AON Implementation
outputs
inputs
x
min(x, y)
max(x, y)
y
7Comparator (2-sorter)
outputs
inputs
min(x, y)
x
max(x, y)
y
8Comparison Network
9Comparison Network
n / 2 comparisons per stage
10Sorting Network
Any ideas?
11Sorting Network
inputs
outputs
12Insertion Sort Network
inputs
outputs
depth 2n 3
13Batcher Sorting Network
Next Lecture
14Sorting Arbitrary Numbers
outputs
inputs
min(x, y)
x
max(x, y)
y
x, y can be values from any linearly ordered
set, e.g., integers, reals, etc.
15Integer Comparator
X, Y integers represented as m-bit binary
strings.
Idea use C(X,Y) to select the min and the max
of X and Y.
16Integer Comparator
X
min(X, Y)
Y
C(X, Y)
X
C(X, Y)
max(X, Y)
Y
17Sorting Arbitrary Numbers
sorted
18Sorting Arbitrary Numbers
sorted
19Sorting Arbitrary Numbers
not sorted
How can we verify if a network sorts all possible
input sequences?
20Sorting Arbitrary Numbers
Try all possible 0/1 sequences.
21Sorting Arbitrary Numbers
000
000
Try all possible 0/1 sequences.
22Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
Try all possible 0/1 sequences.
23Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
Try all possible 0/1 sequences.
24Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
Try all possible 0/1 sequences.
25Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
100
001
Try all possible 0/1 sequences.
26Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
100
001
101
011
Try all possible 0/1 sequences.
27Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
100
001
101
011
110
101
Try all possible 0/1 sequences.
28Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
100
001
101
011
110
101
111
111
Try all possible 0/1 sequences.
29Sorting Arbitrary Numbers
30Sorting Arbitrary Numbers
Try all possible 0/1 sequences.
31Sorting Arbitrary Numbers
000
000
Try all possible 0/1 sequences.
32Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
Try all possible 0/1 sequences.
33Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
Try all possible 0/1 sequences.
34Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
Try all possible 0/1 sequences.
35Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
100
001
Try all possible 0/1 sequences.
36Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
100
001
101
011
Try all possible 0/1 sequences.
37Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
100
001
101
011
110
011
Try all possible 0/1 sequences.
38Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
100
001
101
011
110
011
111
111
Try all possible 0/1 sequences.
39Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
100
001
101
011
110
011
111
111
all sorted!
Try all possible 0/1 sequences.
40Zero-One Principle
If a comparison network sorts all possible
sequences of 0s and 1s correctly, then it
sorts all sequences of arbitrary numbers
correctly.
41Lemma
Given
For a monotonically increasing function f,
42Lemma
Given
For a monotonically increasing function f,
43Proof Lemma
44Proof Lemma
45Proof Lemma
f is monotonically increasing
46Proof Lemma
f is monotonically increasing
47Proof Lemma
f is monotonically increasing
48Generalization
Given
49Generalization
For a monotonically increasing function f,
(by induction)
50Proof Zero-One Principle
51Proof Zero-One Principle
52Proof Zero-One Principle
Sorting Network
.
.
.
53Proof Zero-One Principle
Sorting Network
.
.
.
54Implementing XOR
55Implementing XOR
Sorting Network
56Symmetric Functions
Sorting Network