View
27
Download
0
Category
Tags:
Preview:
DESCRIPTION
Comparison Networks. Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions. 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. - PowerPoint PPT Presentation
Citation preview
Comparison Networks
Sorting• Sorting binary values• Sorting arbitrary numbers• Implementing symmetric functions
Sorting Algorithms
)log ( nnO comparisons to sort n elements
)(mO 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.
)log ( scomparison ofnumber ,)(logdepth 22 nnOnO
CDBA
ABCD
SortingNetwork
Sorting Networks (binary values)
SortingNetwork
10010011
00001111
inputs outputs
sorted
Comparator (2-sorter)
x
y
min(x, y)
max(x, y)
inputs outputs
C
Comparator (2-sorter)
inputs
x
y
min(x, y)
max(x, y)
AON Implementation
outputs
Comparator (2-sorter)
x
y
min(x, y)
max(x, y)
inputs outputs
Comparison Network
0
0
1
1
0
0
1
1
0
0
1
1
1
1
0
0
depth d
width n
Comparison Network
0
0
1
1
0
0
1
1
0
0
1
1
1
1
0
0
n / 2comparisonsper stage
d stages
Sorting NetworkAny ideas?
Sorting Networkinputs outputs
n 1Sorting
Network...... n
...
Insertion Sort Networkinputs outputs
depth 2n 3
Batcher Sorting NetworkNext Lecture
)(logdepth 2 nO
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
Integer ComparatorX
C(X, Y)
YC(X, Y)
min(X, Y)
XC(X, Y)
YC(X, Y)
max(X, Y)
Sorting Arbitrary Numbers
9
6
2
2
6
9
2
9
6
sorted6
9
2
Sorting Arbitrary Numbers
1
4
5
1
4
5
1
5
4
sorted4
5
1
Sorting Arbitrary Numbers
3
0
7
3
0
7
3
7
0
0
7
3
not sorted
How can we verify if a network sorts all possible input sequences?
Sorting Arbitrary Numbersinputs outputs
Try all possible 0/1 sequences.
Sorting Arbitrary Numbers
0
0
0
0
0
0
0
0
0
0
0
0 000 000
inputs outputs
Try all possible 0/1 sequences.
Sorting Arbitrary Numbers
0
1
0
0
0
1
0
0
1
0
1
0inputs outputs000 000
Try all possible 0/1 sequences.
001 001
Sorting Arbitrary Numbers
0
0
1
0
0
1
0
1
0
0
1
0inputs outputs000 000
Try all possible 0/1 sequences.
001 001010 001
Sorting Arbitrary Numbers
0
1
1
0
1
1
0
1
1
1
1
0inputs outputs000 000
Try all possible 0/1 sequences.
001 001010 001011 011
Sorting Arbitrary Numbers
1
0
0
0
0
1
0
1
0
0
1
0inputs outputs000 000
Try all possible 0/1 sequences.
001 001010 001011 011100 001
Sorting Arbitrary Numbers
1
1
0
0
1
1
0
1
1
1
1
0inputs outputs000 000
Try all possible 0/1 sequences.
001 001010 001011 011100 001101 011
Sorting Arbitrary Numbers
1
0
1
1
0
1
1
1
0
0
1
1inputs outputs000 000
Try all possible 0/1 sequences.
001 001010 001011 011100 001101 011110 101
not sorted!
Sorting Arbitrary Numbers
1
1
1
1
1
1
1
1
1
1
1
1inputs outputs000 000
Try all possible 0/1 sequences.
001 001010 001011 011100 001101 011110 101111 111
not sorted!
Sorting Arbitrary Numbers
Sorting Arbitrary Numbersinputs outputs
Try all possible 0/1 sequences.
Sorting Arbitrary Numbers
0
0
0
0
0
0
0
0
0
0
0
0 000 000
inputs outputs
Try all possible 0/1 sequences.
Sorting Arbitrary Numbers
0
1
0
0
0
1
0
0
1
0
1
0inputs outputs000 000
Try all possible 0/1 sequences.
001 001
Sorting Arbitrary Numbers
0
0
1
0
0
1
0
1
0
1
0
0inputs outputs000 000
Try all possible 0/1 sequences.
001 001010 001
Sorting Arbitrary Numbers
0
1
1
0
1
1
0
1
1
1
1
0inputs outputs000 000
Try all possible 0/1 sequences.
001 001010 001011 011
Sorting Arbitrary Numbers
1
0
0
0
0
1
0
1
0
1
0
0inputs outputs000 000
Try all possible 0/1 sequences.
001 001010 001011 011100 001
Sorting Arbitrary Numbers
1
1
0
0
1
1
0
1
1
1
1
0inputs outputs000 000
Try all possible 0/1 sequences.
001 001010 001011 011100 001101 011
Sorting Arbitrary Numbers
1
0
1
0
1
1
1
1
0
1
1
0inputs outputs000 000
Try all possible 0/1 sequences.
001 001010 001011 011100 001101 011110 011
Sorting Arbitrary Numbers
1
1
1
1
1
1
1
1
1
1
1
1inputs outputs000 000
Try all possible 0/1 sequences.
001 001010 001011 011100 001101 011110 011111 111
Sorting Arbitrary Numbersinputs outputs000 000
Try all possible 0/1 sequences.
001 001010 001011 011100 001101 011110 011111 111
all sorted!
Zero-One Principle
If a comparison network sorts all possible sequencesof 0’s and 1’s correctly, then it sorts all sequencesof arbitrary numbers correctly.
Lemma
0a
1a
Given
0b
1b
For a monotonically increasing function f,
Lemma
0a
1a
Given
0b
1b
For a monotonically increasing function f,
)( 0af
)( 1af
)( 0bf
)( 1bf
Proof: Lemma
),min( 10 aa
),max( 10 aa
0a
1a
Proof: Lemma
))(),(min( 10 afaf
))(),(max( 10 afaf
)( 0af
)( 1af
Proof: Lemma
f is monotonically increasing:yx )()( yfxf
)( 0af
)( 1af
))(),(min( 10 afaf
))(),(max( 10 afaf
Proof: Lemma
f is monotonically increasing:yx )()( yfxf
)),(min( 10 aaf
)),(max( 10 aaf
)( 0af
)( 1af
Proof: Lemma
f is monotonically increasing:
)( 0bf
)( 1bf
)( 0af
)( 1af
yx )()( yfxf
Generalization
0a
1a
2a
0b
1b
2b
0c
1c
2c
0d
1d
2d
Given
For a monotonically increasing function f,
)( 0af
)( 1af
)( 2af
)( 0bf
)( 1bf
)( 2bf
)( 0cf
)( 1cf
)( 2cf
)( 0df
)( 1df
)( 2df
Generalization
(by induction)
Proof: Zero-One PrincipleSuppose
b) there exists a sequence that it doesn’t sort, i.e., 110 ,...,, naaa
ji aa , such that ji aa but is placed before in the output.ja ia
a) the network sorts all sequences of 0’s and 1’s,
Define
f (x) =0 if1 otherwise
iax
Proof: Zero-One Principle
0a
1a
1na
0a
1a
1na
ja
ia
SortingNetwork
......
...
Proof: Zero-One Principle
SortingNetwork
)( 0af)( 1af
)( 1naf
)( 0af )( 1af
)( 1naf
)( jaf)( iaf
......
...
Proof: Zero-One Principle
SortingNetwork
)( 0af)( 1af
)( 1naf
)( 0af )( 1af
)( 1naf
......
...
10
contradiction!
Implementing XOR
SortingNetwork
00000001
00000111
00011111
X
01111111
XOR(X) =1 if odd0 otherwise
X
Implementing XOR
SortingNetworkX
XOR(X) =1 if odd0 otherwise
X
Symmetric Functions
SortingNetworkX
f (X) =1 if0 otherwise
Xfor some subset },...,0{ nof
Recommended