lecture 4 sorting networks. comparator comparator
TRANSCRIPT
Lecture 4Sorting Networks
Comparator
x ),min(' yxx
ycomparator
),max(' yxy
),min(' yxx
),max(' yxy
x
y
time)1(O
A Sorting Network9
5
2
6
5
9
2
6
2
5
6
9
2
5
6
9
A sorting network is a comparison network which output monotone nondecreasing sequence for every input.
Depth9
5
2
6
5
9
2
6
2
5
6
9
2
5
6
9
Depth is the maximum number of comparators on apath from an input wire to an output wire.
Depth = parallel time9
5
2
6
5
9
2
6
2
5
6
9
2
5
6
9
Depth is the maximum number of comparators on apath from an input wire to an output wire.
for.-end
;]1[
;1
and ][]1[ do
][ and 0 while
;1
];[begin do
][ to2for
keyiA
ii
iAiA
keyiAi
ji
jAkey
Alengthj
Insertion SortAarray input
3 1, ,6 5, 4, 2,
3 1, 6, 5, ,4 2,
3 1, 6, ,4 5, 2,
3 1, 6, 4, 5, ,2
3 1, 6, 4, ,2 5,
�
�
�
�
�
6 5, 4, ,3 2, 1,
6 5, ,3 4, 2, 1,
6 ,3 5, 4, 2, 1,
3 6, 5, 4, 2, 1,
�
�
�
�
3 6, 5, 4, 2, ,1
3 6, 5, 4, ,1 2,
3 6, 5, ,1 4, 2,
3 6, ,1 5, 4, 2,
3 ,1 6, 5, 4, 2,
�
�
�
�
�key
5a
1a
2a
6a
3a
4a
Sorting network constructed from insertion sort.
3-2n depth
How to construct a sortingnetwork from merging sort?
Divide and Conquer
• Divide the problem into subproblems.
• Conquer the subproblems by solving them recursively.
• Combine the solutions to subproblems into the solution for original problem.
Merge Sort
end
);,1,(Sort-Merge
begin
ProgramMain
]....[subarray in elements sortswhich
procedure a be ),,(Sort-MergeLet
nA
rpA
rpA
Procedure
then.-end
);,,,(Merge
);,1,(Sort-Merge
);,,(Sort-Merge
;2/)(begin then
if
),,(Sort-Merge
rqpA
rqA
qpA
rpq
rp
rpA
Structure
1a 1a
1a
Sortingnetwork
Sortingnetwork
Mergingnetwork
1a
2/na
2/1 na
na
Construction of Merging Network
• 0-1 principal.
• Bitonic sorter.
• Merging network.
0-1 principal
correctly.
numbersarbitrary of sequences all sortsit then
correctly, s1' and s0' of sequence possible 2
allsort input th network wi comparison a Ifn
n
Lemma
)).(),...,(),(( into ))(),...,(),((
ansformsnetwork tr the,function ingnondecreas
lly monotonicaany for then ,),...,,(
sequenceoutput theinto ),...,,( sequence
input theansformsnetwork tr comparison a If
2121
21
21
nn
n
n
bfbfbfafafaf
f
bbb
aaa
)),(min())(),(min( yxfyfxf
)(yf
)(xf
)),(max())(),(max( yxfyfxf
Proof of 0-1 Principal
ion.contradict a ,0)(
before 1)( putsnetwork Then the
. if 1
, if 0)(
Define . before putsnetwork the,
somefor is,That correctly.not ),...,,(
seqnce a is but there correctly, sequences 1-0 all
sortsnetwork heion that tcontradictfor Suppose
21
i
j
i
i
ijji
n
af
af
ax
axxf
aaaa
aaa
Bitonic Sequence
,8).(3,2,1,4,6 ,2),(1,4,6,8,3
.decreasing
monotone then and increasing monotone
first become toshifted circularly becan
or decreasing monotone then and increasing
monotonefirst isit if bitonic is ),...,,( 21 naaa
Bitonic 0-1 Sequence
kji 010
kji 101
Some Properties
).,...,,(),...,,(
is so bitonic, is ),...,,( If (2)
bitonic.
isit then ,increasing monotone then and
decreasing monotonefirst is ),...,,( If (1)
1121
21
21
aaaaaa
aaa
aaa
nnR
n
n
n
The half-cleaner
bitonic
bitonicclean
bitonic
0
0
0
0
1
0
1
1
0
0
1
1
1
0
0
0
The half-cleaner
bitonic
bitonicclean
bitonic
0
0
1
0
1
1
1
1
0
0
1
1
1
1
1
0
Lemma
(a)One of two halfs is bitonic clean.
(b) every number in the 1st half ≤ any element in the 2nd half.
Proof (case 1)
1
0
0
10
0 1
0
0
Proof (case 2)
1
0
0
1
0
0
1
0
0
Proof (case 3)
1
0
0
1
0
0
1
0
0
1
1
Proof (case 4)
1
0
0
1
0
01
0
01
1
Proof (case 5)
1
0
0
1
0
1
1
1
1
1
1
Proof (case 6)
1
0
0
1
0
1
1
1
1
1
1
Proof (case 7)
1
0
0
1
0
1
1
1
1
0
0
Proof (case 8)
1
0
0
1
0
1
1
11
00
Half cleaners
bitonic sorted
Half cleaners
sorted
sorted
sorted
0
0
11
0001
0
0000
11
1
Merging Network )(lg depth nO
Structure
1a 1a
1a
Sortingnetwork
Sortingnetwork
Mergingnetwork
1a
2/na
2/1 na
na
Merging Networks
Sorting Network
))((lg depth 2nO
What we learnt in this lecture?
• What is sorting network?
• Depth = parallel time.
• Sorting network from Merge sort.
Permutation Network
• Switching network
• Rearrangeability
• Nework with 2x2 crossbars
Crossbar Switch
A crossbar switch can realize any matching betweenInputs and outputs.
3-stage Clos Network
1
m
n
n
n
n
Rearrangeability
.
ifonly and if blerearrangea isnetwork Clos stage-3
outputs. and inputsbetween matchingany
realizecan it if blerearrangea isnetwork switchingA
nm
Theorem
Network with 2x2 crossbars
y.recursivel dconstructe becan crossbars
22only th network wi )lg3(-depth blerearrangeaA n
Puzzle
d?constructejust that we
network )(lg-depth for the thisdo can we ,Especially
?comparator ah switch witcrossbar 22
each replaceby network sorting a into switchscrossbar
22only th network win permutatioevery transformCan we
switch.crossbar 22 a with comparatorevery
replacingby network) switching lerearrageab a (i.e.,network
n permutatio a into ed transformbecan network sortingAny
nO