56
Comparison Networks Sorting • Sorting binary values • Sorting arbitrary numbers • Implementing symmetric functions

Comparison Networks

Embed Size (px)

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

Page 1: Comparison Networks

Comparison Networks

Sorting• Sorting binary values• Sorting arbitrary numbers• Implementing symmetric functions

Page 2: Comparison Networks

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

Page 3: Comparison Networks

Sorting Networks

Order of comparisons fixed in advance.Readily implementable in hardware.

)log ( scomparison ofnumber ,)(logdepth 22 nnOnO

CDBA

ABCD

SortingNetwork

Page 4: Comparison Networks

Sorting Networks (binary values)

SortingNetwork

10010011

00001111

inputs outputs

sorted

Page 5: Comparison Networks

Comparator (2-sorter)

x

y

min(x, y)

max(x, y)

inputs outputs

C

Page 6: Comparison Networks

Comparator (2-sorter)

inputs

x

y

min(x, y)

max(x, y)

AON Implementation

outputs

Page 7: Comparison Networks

Comparator (2-sorter)

x

y

min(x, y)

max(x, y)

inputs outputs

Page 8: Comparison Networks

Comparison Network

0

0

1

1

0

0

1

1

0

0

1

1

1

1

0

0

depth d

width n

Page 9: Comparison Networks

Comparison Network

0

0

1

1

0

0

1

1

0

0

1

1

1

1

0

0

n / 2comparisonsper stage

d stages

Page 10: Comparison Networks

Sorting NetworkAny ideas?

Page 11: Comparison Networks

Sorting Networkinputs outputs

n 1Sorting

Network...... n

...

Page 12: Comparison Networks

Insertion Sort Networkinputs outputs

depth 2n 3

Page 13: Comparison Networks

Batcher Sorting NetworkNext Lecture

)(logdepth 2 nO

Page 14: Comparison Networks

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)

Page 15: Comparison Networks

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

Page 16: Comparison Networks

Integer ComparatorX

C(X, Y)

YC(X, Y)

min(X, Y)

XC(X, Y)

YC(X, Y)

max(X, Y)

Page 17: Comparison Networks

Sorting Arbitrary Numbers

9

6

2

2

6

9

2

9

6

sorted6

9

2

Page 18: Comparison Networks

Sorting Arbitrary Numbers

1

4

5

1

4

5

1

5

4

sorted4

5

1

Page 19: Comparison Networks

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?

Page 20: Comparison Networks

Sorting Arbitrary Numbersinputs outputs

Try all possible 0/1 sequences.

Page 21: Comparison Networks

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.

Page 22: Comparison Networks

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

Page 23: Comparison Networks

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

Page 24: Comparison Networks

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

Page 25: Comparison Networks

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

Page 26: Comparison Networks

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

Page 27: Comparison Networks

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!

Page 28: Comparison Networks

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!

Page 29: Comparison Networks

Sorting Arbitrary Numbers

Page 30: Comparison Networks

Sorting Arbitrary Numbersinputs outputs

Try all possible 0/1 sequences.

Page 31: Comparison Networks

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.

Page 32: Comparison Networks

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

Page 33: Comparison Networks

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

Page 34: Comparison Networks

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

Page 35: Comparison Networks

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

Page 36: Comparison Networks

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

Page 37: Comparison Networks

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

Page 38: Comparison Networks

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

Page 39: Comparison Networks

Sorting Arbitrary Numbersinputs outputs000 000

Try all possible 0/1 sequences.

001 001010 001011 011100 001101 011110 011111 111

all sorted!

Page 40: Comparison Networks

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.

Page 41: Comparison Networks

Lemma

0a

1a

Given

0b

1b

For a monotonically increasing function f,

Page 42: Comparison Networks

Lemma

0a

1a

Given

0b

1b

For a monotonically increasing function f,

)( 0af

)( 1af

)( 0bf

)( 1bf

Page 43: Comparison Networks

Proof: Lemma

),min( 10 aa

),max( 10 aa

0a

1a

Page 44: Comparison Networks

Proof: Lemma

))(),(min( 10 afaf

))(),(max( 10 afaf

)( 0af

)( 1af

Page 45: Comparison Networks

Proof: Lemma

f is monotonically increasing:yx )()( yfxf

)( 0af

)( 1af

))(),(min( 10 afaf

))(),(max( 10 afaf

Page 46: Comparison Networks

Proof: Lemma

f is monotonically increasing:yx )()( yfxf

)),(min( 10 aaf

)),(max( 10 aaf

)( 0af

)( 1af

Page 47: Comparison Networks

Proof: Lemma

f is monotonically increasing:

)( 0bf

)( 1bf

)( 0af

)( 1af

yx )()( yfxf

Page 48: Comparison Networks

Generalization

0a

1a

2a

0b

1b

2b

0c

1c

2c

0d

1d

2d

Given

Page 49: Comparison Networks

For a monotonically increasing function f,

)( 0af

)( 1af

)( 2af

)( 0bf

)( 1bf

)( 2bf

)( 0cf

)( 1cf

)( 2cf

)( 0df

)( 1df

)( 2df

Generalization

(by induction)

Page 50: Comparison Networks

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

Page 51: Comparison Networks

Proof: Zero-One Principle

0a

1a

1na

0a

1a

1na

ja

ia

SortingNetwork

......

...

Page 52: Comparison Networks

Proof: Zero-One Principle

SortingNetwork

)( 0af)( 1af

)( 1naf

)( 0af )( 1af

)( 1naf

)( jaf)( iaf

......

...

Page 53: Comparison Networks

Proof: Zero-One Principle

SortingNetwork

)( 0af)( 1af

)( 1naf

)( 0af )( 1af

)( 1naf

......

...

10

contradiction!

Page 54: Comparison Networks

Implementing XOR

SortingNetwork

00000001

00000111

00011111

X

01111111

XOR(X) =1 if odd0 otherwise

X

Page 55: Comparison Networks

Implementing XOR

SortingNetworkX

XOR(X) =1 if odd0 otherwise

X

Page 56: Comparison Networks

Symmetric Functions

SortingNetworkX

f (X) =1 if0 otherwise

Xfor some subset },...,0{ nof