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)
outputs
inputs
min(x, y)
x
max(x, y)
y
7Comparison Network
8Comparison Network
n / 2 comparisons per stage
9Sorting Network
Any ideas?
10Sorting Network
inputs
outputs
11Insertion Sort Network
inputs
outputs
depth 2n 3
12Batcher Sorting Network
Next Lecture
13Batcher Sorting Network, n 4
14Batcher Sorting Network, n 8
n 4
n 4
15Lemma 1
Any subsequence of a sorted sequence is a sorted
sequence.
0
0
0
0
0
1
1
1
1
1
1
1
1
16Lemma 2
For a sorted sequence, the number of 0s in the
even subsequence is either equal to, or one
greater than, the number of 0s in the odd
subsequence.
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
even
odd
17Lemma 3
For two sorted sequences and
denotes the the number of 0s in
denotes the even subsequence of
denotes the odd subsequence of
18Lemma 3
19Lemma 3
For two sorted sequences and
(by Lemma 2)
(by Lemma 2)
20Merge Network
21Merge Network (pf.)
(by Lemma 1)
(by Lemma 1)
22Merge Network (pf.)
23Merge Network (pf.)
24Merge Network (pf.)
0
0
0
0
1
0
1
0
0
0
0
1
0
1
1
1
25Batcher Sorting Network
26Batcher Sorting Network, n 4
Sort2
Sort2
27Batcher Sorting Network, n 8
Sort4
Sort4
28Sorting Networks
Batcher
AKS (Chvátal)
29Sorting 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.
30Integer 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.
31Sorting Arbitrary Numbers
sorted
32Sorting Arbitrary Numbers
sorted
33Sorting Arbitrary Numbers
not sorted
How can we verify if a network sorts all possible
input sequences?
34Sorting Arbitrary Numbers
Try all possible 0/1 sequences.
35Sorting Arbitrary Numbers
000
000
Try all possible 0/1 sequences.
36Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
Try all possible 0/1 sequences.
37Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
Try all possible 0/1 sequences.
38Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
Try all possible 0/1 sequences.
39Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
100
001
Try all possible 0/1 sequences.
40Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
100
001
101
011
Try all possible 0/1 sequences.
41Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
100
001
101
011
110
101
Try all possible 0/1 sequences.
42Sorting 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.
43Sorting Arbitrary Numbers
44Sorting Arbitrary Numbers
Try all possible 0/1 sequences.
45Sorting Arbitrary Numbers
000
000
Try all possible 0/1 sequences.
46Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
Try all possible 0/1 sequences.
47Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
Try all possible 0/1 sequences.
48Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
Try all possible 0/1 sequences.
49Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
100
001
Try all possible 0/1 sequences.
50Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
100
001
101
011
Try all possible 0/1 sequences.
51Sorting Arbitrary Numbers
inputs
outputs
000
000
001
001
010
001
011
011
100
001
101
011
110
011
Try all possible 0/1 sequences.
52Sorting 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.
53Sorting 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.
54Zero-One Principle
If a comparison network sorts all possible
sequences of 0s and 1s correctly, then it
sorts all sequences of arbitrary numbers
correctly.
55Lemma
Given
For a monotonically increasing function f,
56Lemma
Given
For a monotonically increasing function f,
57Proof Lemma
58Proof Lemma
59Proof Lemma
f is monotonically increasing
60Proof Lemma
f is monotonically increasing
61Proof Lemma
f is monotonically increasing
62Generalization
Given
63Generalization
For a monotonically increasing function f,
(by induction)
64Proof Zero-One Principle
65Proof Zero-One Principle
66Proof Zero-One Principle
Sorting Network
.
.
.
67Proof Zero-One Principle
Sorting Network
.
.
.