Transcript
Page 1: Sorting Networks Uri Zwick Tel Aviv University May 2015

Sorting Networks

Uri ZwickTel Aviv University

May 2015

Page 2: Sorting Networks Uri Zwick Tel Aviv University May 2015

Comparators

๐‘ฅ๐‘ฆ

min (๐‘ฅ , ๐‘ฆ )max (๐‘ฅ , ๐‘ฆ)

Can we use comparators to build efficient sorting networks?

๐‘ฅ๐‘ฆ

min (๐‘ฅ , ๐‘ฆ )max (๐‘ฅ , ๐‘ฆ)

Page 3: Sorting Networks Uri Zwick Tel Aviv University May 2015

Comparator networks

A comparator network is a network composed of comparators.

There are input wires, each feeding a single comparator.

Each output of a comparator is either an output wire, or feeds a single comparator.

The network must be acyclic.

Number of output wires must also be .

Page 4: Sorting Networks Uri Zwick Tel Aviv University May 2015

โ€œStandard formโ€

Exercise: Show that any comparator networkis equivalent to a network in standard form.

Page 5: Sorting Networks Uri Zwick Tel Aviv University May 2015

A simple sorting network

5 comparators3 levels

Page 6: Sorting Networks Uri Zwick Tel Aviv University May 2015

Insertion sort

๐‘†๐‘œ๐‘Ÿ๐‘ก (๐‘›)

Page 7: Sorting Networks Uri Zwick Tel Aviv University May 2015

Selection/bubble sort

๐‘†๐‘œ๐‘Ÿ๐‘ก (๐‘›)

Page 8: Sorting Networks Uri Zwick Tel Aviv University May 2015

Selection/bubble Sort

Size = Depth =

Page 9: Sorting Networks Uri Zwick Tel Aviv University May 2015

Exercise: Any sorting network that only compares adjacent lines must be of size at least

Exercise: Prove that the network on the next slide, whose depth is , is a sorting network

Page 10: Sorting Networks Uri Zwick Tel Aviv University May 2015

Odd-Even Transposition Sort

Size = Depth =

Page 11: Sorting Networks Uri Zwick Tel Aviv University May 2015

The 0-1 principle

Theorem: If a network sort all 0-1 inputs,

then it sort all inputs

Page 12: Sorting Networks Uri Zwick Tel Aviv University May 2015

The 0-1 principle

Lemma: Let be a monotone non-decreasing function. Then,

if a network maps to ,then it maps to

Proof: By induction on the number of comparisons using

Page 13: Sorting Networks Uri Zwick Tel Aviv University May 2015

The 0-1 principle

Suppose that a network is not a sorting network.

Proof:

It then maps some to , where , for some .

Let , iff , 0 otherwise.

The network maps to

Thus, the network does not sort all 0-1 inputs.

Page 14: Sorting Networks Uri Zwick Tel Aviv University May 2015

Sorting by merging

Page 15: Sorting Networks Uri Zwick Tel Aviv University May 2015

Batcherโ€™s odd-even merge

๐‘›

๐‘š

Page 16: Sorting Networks Uri Zwick Tel Aviv University May 2015

Batcherโ€™s odd-even merge

๐‘›

๐‘š

Page 17: Sorting Networks Uri Zwick Tel Aviv University May 2015

Batcherโ€™s odd-even merge

To merge with :

Split into and

Split into and

Merge odd-indexed items to create

Merge even-indexed items to create

Compare/swap

Does it really work?

Page 18: Sorting Networks Uri Zwick Tel Aviv University May 2015

Batcherโ€™s odd-even merge

๐‘€ (1 ,1)

๐‘€ (2 ,2)

Page 19: Sorting Networks Uri Zwick Tel Aviv University May 2015

Batcherโ€™s odd-even merge -

Page 20: Sorting Networks Uri Zwick Tel Aviv University May 2015

Batcherโ€™s odd-even merge -

Page 21: Sorting Networks Uri Zwick Tel Aviv University May 2015

Odd-even merge Odd-even sort

Page 22: Sorting Networks Uri Zwick Tel Aviv University May 2015

Batcherโ€™s odd-even merge

Suppose that starts with 0โ€™s and that starts with 0โ€™s.

Then starts with 0โ€™s,and starts 0โ€™s.

The difference is either 0,1 or 2!

Proof using the 0-1 principle.

There is a problem only if difference is 2and last level of comparators fixes it.

Page 23: Sorting Networks Uri Zwick Tel Aviv University May 2015

Batcherโ€™s odd-even merge

0 0 0 0 0 1 1 10 0 0 0 0 1 1 1

๐‘’๐‘œ

0 0 0 0 1 1 1 10 0 0 0 0 1 1 1

๐‘’๐‘œ

0 0 0 1 1 1 1 10 0 0 0 0 1 1 1

๐‘’๐‘œ

Page 24: Sorting Networks Uri Zwick Tel Aviv University May 2015

Batcherโ€™s odd-even merge

Exercise: Justify the use of the 0-1 principle.

Exercise: Prove the correctness of the odd-even merging network directly without relying on the 0-1 principle.

Page 25: Sorting Networks Uri Zwick Tel Aviv University May 2015

Batcherโ€™s odd-even mergeSize โ€“ number of comparators

๐‘€ (๐‘› ,0 )=๐‘€ (0 ,๐‘š )=0

๐‘€ (๐‘› ,๐‘›)=2๐‘€ (๐‘›2 ,๐‘›2 )+(๐‘›โˆ’1)

๐‘€ ( 2๐‘˜ ,2๐‘˜ )=๐‘˜2๐‘˜+1

๐‘€ (๐‘› ,๐‘›)=๐‘› lg๐‘›+ยฟ๐‘‚ (๐‘›)ยฟ

๐‘€ (1 ,1 )=1

๐‘€ (๐‘› ,๐‘š )=๐‘€ (โŒˆ ๐‘›2 โŒ‰ , โŒˆ ๐‘š2โŒ‰ )+๐‘€ (โŒŠ ๐‘›2 โŒ‹ , โŒŠ ๐‘š

2โŒ‹ )+โŒŠ ๐‘›+๐‘šโˆ’1

2โŒ‹

No better merging networks are known for any !Are the odd-even merge networks optimal?

Page 26: Sorting Networks Uri Zwick Tel Aviv University May 2015

Bitonic sequences

A sequence is bitonic iff it is acyclic shift of a strict bitonic sequence

8 6 4 1 2 5 7 9

4 1 2 5 7 9 8 6

A sequence is strict bitonic iff it is aconcatenation of a decreasing sequence

and an increasing sequence

1 4 2 3

Page 27: Sorting Networks Uri Zwick Tel Aviv University May 2015

A Bitonic sorter

A (strict) bitonic sorter is a network that sorts every (strict) bitonic sequence.

I and are sorted,then is bitonic.

Thus, a strict bitonic sorter can serve as a merging network

Page 28: Sorting Networks Uri Zwick Tel Aviv University May 2015

Batcherโ€™s bitonic sorter

๐‘›

Is this different from odd-even merge?

Page 29: Sorting Networks Uri Zwick Tel Aviv University May 2015

A strict binary bitonic sequence โ€“

The odd and even subsequences are alsostrict binary bitonic sequences.

Simple proof using the 0-1 principle.

The difference between the number of 0โ€™s in the two sorted sequences is one of .

Batcherโ€™s bitonic sorter

By induction they are sorted correctly.

Final level of comparators fixes the problem.

Page 30: Sorting Networks Uri Zwick Tel Aviv University May 2015

Exercise: Show that the difference between the number of 0โ€™s in the odd

and even subsequences of is

Batcherโ€™s bitonic sorter

Note: We do not need this exact formulain the proof given in the previous slide.

Seeing that the difference is is immediate.

Page 31: Sorting Networks Uri Zwick Tel Aviv University May 2015

Very regular structure!

When , there are levels with

comparators each.

Lines and are compared at level iff

they differ only in the -th most significant bit

Batcherโ€™s bitonic sorter for n=

Page 32: Sorting Networks Uri Zwick Tel Aviv University May 2015

Batcherโ€™s bitonic sorter for n=

Alternative recursive definition for

Sorts general bitonic sequences

Page 33: Sorting Networks Uri Zwick Tel Aviv University May 2015

The AKS sorting networks[Ajtai-Komlรณs-Szemerรฉdi (1983)]

There are sorting networks of depth,and hence size.

The construction is fairly complicated.

The constant factors are very large.


Recommended