Lecture 4 Sorting Networks. Comparator comparator.

42
Lecture 4 Sorting Networks

Transcript of Lecture 4 Sorting Networks. Comparator comparator.

Page 1: Lecture 4 Sorting Networks. Comparator comparator.

Lecture 4Sorting Networks

Page 2: Lecture 4 Sorting Networks. Comparator comparator.

Comparator

x ),min(' yxx

ycomparator

),max(' yxy

),min(' yxx

),max(' yxy

x

y

time)1(O

Page 3: Lecture 4 Sorting Networks. Comparator comparator.

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.

Page 4: Lecture 4 Sorting Networks. Comparator comparator.

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.

Page 5: Lecture 4 Sorting Networks. Comparator comparator.

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.

Page 6: Lecture 4 Sorting Networks. Comparator comparator.

for.-end

;]1[

;1

and ][]1[ do

][ and 0 while

;1

];[begin do

][ to2for

keyiA

ii

iAiA

keyiAi

ji

jAkey

Alengthj

Insertion SortAarray input

Page 7: Lecture 4 Sorting Networks. Comparator comparator.

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

Page 8: Lecture 4 Sorting Networks. Comparator comparator.

5a

1a

2a

6a

3a

4a

Sorting network constructed from insertion sort.

3-2n depth

Page 9: Lecture 4 Sorting Networks. Comparator comparator.

How to construct a sortingnetwork from merging sort?

Page 10: Lecture 4 Sorting Networks. Comparator comparator.

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.

Page 11: Lecture 4 Sorting Networks. Comparator comparator.

Merge Sort

end

);,1,(Sort-Merge

begin

ProgramMain

]....[subarray in elements sortswhich

procedure a be ),,(Sort-MergeLet

nA

rpA

rpA

Page 12: Lecture 4 Sorting Networks. Comparator comparator.

Procedure

then.-end

);,,,(Merge

);,1,(Sort-Merge

);,,(Sort-Merge

;2/)(begin then

if

),,(Sort-Merge

rqpA

rqA

qpA

rpq

rp

rpA

Page 13: Lecture 4 Sorting Networks. Comparator comparator.

Structure

1a 1a

1a

Sortingnetwork

Sortingnetwork

Mergingnetwork

1a

2/na

2/1 na

na

Page 14: Lecture 4 Sorting Networks. Comparator comparator.

Construction of Merging Network

• 0-1 principal.

• Bitonic sorter.

• Merging network.

Page 15: Lecture 4 Sorting Networks. Comparator comparator.

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

Page 16: Lecture 4 Sorting Networks. Comparator comparator.

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

Page 17: Lecture 4 Sorting Networks. Comparator comparator.

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

Page 18: Lecture 4 Sorting Networks. Comparator comparator.

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

Page 19: Lecture 4 Sorting Networks. Comparator comparator.

Bitonic 0-1 Sequence

kji 010

kji 101

Page 20: Lecture 4 Sorting Networks. Comparator comparator.

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

Page 21: Lecture 4 Sorting Networks. Comparator comparator.

The half-cleaner

bitonic

bitonicclean

bitonic

0

0

0

0

1

0

1

1

0

0

1

1

1

0

0

0

Page 22: Lecture 4 Sorting Networks. Comparator comparator.

The half-cleaner

bitonic

bitonicclean

bitonic

0

0

1

0

1

1

1

1

0

0

1

1

1

1

1

0

Page 23: Lecture 4 Sorting Networks. Comparator comparator.

Lemma

(a)One of two halfs is bitonic clean.

(b) every number in the 1st half ≤ any element in the 2nd half.

Page 24: Lecture 4 Sorting Networks. Comparator comparator.

Proof (case 1)

1

0

0

10

0 1

0

0

Page 25: Lecture 4 Sorting Networks. Comparator comparator.

Proof (case 2)

1

0

0

1

0

0

1

0

0

Page 26: Lecture 4 Sorting Networks. Comparator comparator.

Proof (case 3)

1

0

0

1

0

0

1

0

0

1

1

Page 27: Lecture 4 Sorting Networks. Comparator comparator.

Proof (case 4)

1

0

0

1

0

01

0

01

1

Page 28: Lecture 4 Sorting Networks. Comparator comparator.

Proof (case 5)

1

0

0

1

0

1

1

1

1

1

1

Page 29: Lecture 4 Sorting Networks. Comparator comparator.

Proof (case 6)

1

0

0

1

0

1

1

1

1

1

1

Page 30: Lecture 4 Sorting Networks. Comparator comparator.

Proof (case 7)

1

0

0

1

0

1

1

1

1

0

0

Page 31: Lecture 4 Sorting Networks. Comparator comparator.

Proof (case 8)

1

0

0

1

0

1

1

11

00

Page 32: Lecture 4 Sorting Networks. Comparator comparator.

Half cleaners

bitonic sorted

Page 33: Lecture 4 Sorting Networks. Comparator comparator.

Half cleaners

sorted

sorted

sorted

0

0

11

0001

0

0000

11

1

Merging Network )(lg depth nO

Page 34: Lecture 4 Sorting Networks. Comparator comparator.

Structure

1a 1a

1a

Sortingnetwork

Sortingnetwork

Mergingnetwork

1a

2/na

2/1 na

na

Page 35: Lecture 4 Sorting Networks. Comparator comparator.

Merging Networks

Sorting Network

))((lg depth 2nO

Page 36: Lecture 4 Sorting Networks. Comparator comparator.

What we learnt in this lecture?

• What is sorting network?

• Depth = parallel time.

• Sorting network from Merge sort.

Page 37: Lecture 4 Sorting Networks. Comparator comparator.

Permutation Network

• Switching network

• Rearrangeability

• Nework with 2x2 crossbars

Page 38: Lecture 4 Sorting Networks. Comparator comparator.

Crossbar Switch

A crossbar switch can realize any matching betweenInputs and outputs.

Page 39: Lecture 4 Sorting Networks. Comparator comparator.

3-stage Clos Network

1

m

n

n

n

n

Page 40: Lecture 4 Sorting Networks. Comparator comparator.

Rearrangeability

.

ifonly and if blerearrangea isnetwork Clos stage-3

outputs. and inputsbetween matchingany

realizecan it if blerearrangea isnetwork switchingA

nm

Theorem

Page 41: Lecture 4 Sorting Networks. Comparator comparator.

Network with 2x2 crossbars

y.recursivel dconstructe becan crossbars

22only th network wi )lg3(-depth blerearrangeaA n

Page 42: Lecture 4 Sorting Networks. Comparator comparator.

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