Sorting Networks Uri Zwick Tel Aviv University May 2015.

33
Sorting Networks Uri Zwick Tel Aviv University May 2015

Transcript of Sorting Networks Uri Zwick Tel Aviv University May 2015.

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.