Comparator Networks for Binary Heap Construction. Binary Heap. â Williams 1964. 2 5 13 6 7 251412 8 9 1026321542. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 j2. 1.
Comparator Networks for Binary Heap Construction Gerth Stølting Brodal ¨ Informatik Max–Planck–Institut fur ¨ Saarbrucken, Germany
M. Cristina Pinotti Istituto di Elaborazione della Informazione CNR, Pisa, Italy July 1998
Comparator Networks for Binary Heap Construction
Binary Heap – Williams 1964
j H
j
5
2
1
HH
j
13
j j j j jA j jA j jA j jA j 6
;2@ ; @
4
7
25
5
6
;3@ ; @
14
7
12
8
9
10
26
32
15
42
8
9
10
11
12
13
14
15
2 5 13 6 7 25 14 12 8 9 10 26 32 15 42
Priority queue O(log n) – Insert, DeleteMin Sorting O(n log n) Construction O(n) Selecting the t smallest O(t) Implicit representation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
G. S. Brodal, M. C. Pinotti
2
8
5
6
9
7
10
32
13
26
25
15
2
– Floyd 1964
The Sequential Binary Heap Construction Algorithm
12
12
5
6
8
8
6
5
9
9
7
7
10
10
32
32
26
26
13
13
25
25
15
2
15
14
14
2
14
42
42
42
j H j HH j j;; @@ j j;; @@ j jA j jA j jA j jA j j H j HH j j;; @@ j j;; @@ j jA j jA j jA j jA j j H j HH j j;; @@ j j;; @@ j jA j jA j jA j jA j
12
Alg. For each node i = n : : : 1 apply SiftDown(i)
G. S. Brodal, M. C. Pinotti
3
Comparator Networks for Binary Heap Construction
Binary Heap Construction Sequential –
O(
– Floyd 1964
n)
Optimal parallel algorithms
O(log ) CRCW PRAM: O(log log
– EREW PRAM: –
– Olariu, Wen 1991
n
– Dietz, Raman 1994
n)
– Randomized parallel comparison tree model:
O(
(n))
– Dietz 1992
Question – Comparator networks (the simplest parallel model of computation) ?
G. S. Brodal, M. C. Pinotti
4
Comparator Networks for Binary Heap Construction
Comparator Networks x1 x2 x3 x4
8 7 5 9
7 ?8 5 ?9 s
s
s
s
1
5 8 ?7 ?9 s
s
s
s
2
5 7 ?8 9 s
s
3
An optimal sorting network for n = 4 Merging networks: O(n log n) Sorting networks: O(n log2 n) AKS sorting networks: O(n log n) Median selection networks: O(n log n)
– Batcher 1960’s – Batcher 1960’s – Ajtai, Koml´os, Szemer´edi 1983 – Ajtai, Koml´os, Szemer´edi 1983
Sorting, merging, and median require networks of size (n log n) – Alekseev 1969 – Miltersen, Paterson, 1996 Note Sequentially merging and median can be solved in linear time – Blum, Floyd, Pratt, Rivest, Tarjan 1973 G. S. Brodal, M. C. Pinotti
5
Comparator Networks for Binary Heap Construction
Binary Heaps and Comparator Networks Heap Construction Networks – Comparator networks that permute n inputs to an implicit heap –
H (n)
=
minimum size of a heap construction network
–
H (n)
=
O(
(n t)-Selection
n
log n)
because of O(n log n) sorting networks
Networks
– Comparator networks that select the t smallest elements from n inputs –
S (n t)
=
G. S. Brodal, M. C. Pinotti
minimum size (n t)-selection network
6
4
7
7
5
5
6
6
6
6
6
7
4
1
1
1
3
3
3
4
4
1
1
4
3
2
2
2
2
2
3
X
log n
O( i=0
x1 x2 x3 x4 x5 x6
7 4 1 5 6 3 2
i
n
2i
)=
1 7 ?4 5 6 3 2
n
O(
1 5 3 ?7 6 ?4 2
log n)
1 5 2 7 6 4
?3
ss s s s s s s s ? s s s 4 ?7 1 5 6 3 2
1 2 3 4 SiftDown network, n = 7
x7
– Depth O(log n)
2
Heap construction network – Size
SiftDown networks – Each tree edge comparator – Size n ; 1, depth O(log n)
O(n log n) Heap Construction Networks
7
5
5
5
j j;; @@ j jA j jA j j j;; @@ j jA j jA j j j;; @@ j jA j jA j j j;; @@ j jA j jA j j j;; @@ j jA j jA j
7
G. S. Brodal, M. C. Pinotti
7
Comparator Networks for Binary Heap Construction
O(n log n) Heap Construction Network x1
s
x2
s
x3 x4
s
x5
s
x6 x7 x8
s
s
s
s
? s
n
G. S. Brodal, M. C. Pinotti
= 15
s
? s
s
s
s
? s
?
?
s
? s
?
s
s
s
s
s
?
s
?
?
s
s
s
?
?
s
s
s
s
2
?
s
? s
?
s
1
s
s
s
?
?
?
s
?
s
?
s
s
s
?
s
?
s
s
s
x14
s
s
s
s
s
?
?
x13
s
s
?
x12
s
s
?
s
x11
?
s
?
s
s
s
x10
?
s
?
x9
x15
?
s
?
s
s
s
s
s
3
4
5 Size = 34
6
? s
? s
7
8
9
Depth = 10
10
n
Cn
n) C >
O(
3 log 3
– Jimbo, Maruoka 1996
log n +
O(n log log n) Heap Construction Networks n
b 2 c)
S (n t
S (n
Corollary
Lemma
n
t
n)
) = O ( log ) O( log log
H (n)
=
Theorem Proof of Theorem 1. Apply (log2 n log n)-selection networks to each green subtree O(n log log n) 2. Apply O(n log n)-heap construction O(n) network to the blue subtree 3. Apply O(n log n)-heap construction O(n log log n) network to each green subtree A A A A A log n ; log log n n= log n A A A A AA A A AA 2 log log n 2 A log nA A
G. S. Brodal, M. C. Pinotti
9
p
A A A A A A A A J A J A A A A B xAA A A AA A y A A C
O(n log log n) Heap Construction Networks
i) A is heap-ordered after Step 2 ii) BC is heap-ordered after Step 3
y
x
^ j jj j )
0
0
x
y
In the O(n log n)-heap construction networks tree edges comparators ) elements in x can only be replaced by smaller elements in Step 2
For Step 2 x
iii) ABC is heap-ordered after Step 3
G. S. Brodal, M. C. Pinotti
10
n
S (n
t
blog n
t
e
+ 1)
– Alekseev 1969
n
n)
c) ; O( ; O( )
( ; )dlog(
An (n log log n) Lower Bound S (n t) H (n
Lemma Theorem n
log log n
H (n)
)
Corollary Proof of Theorem
H (n)
Idea Reduce (n blog nc)-selection networks to heap construction networks 1. Construct a heap
2. Iteratively delete the blog nc smallest elements To find the t smallest elements only the t topmost levels of the heap need to be considered Sufficient to apply the linear size SiftDown networks to heaps of size n n=2 n=4 n=8 :::
X
log n i=0
n
2i
=
O(
n)
Total size of the SiftDown networks
G. S. Brodal, M. C. Pinotti
11
Comparator Networks for Binary Heap Construction
Conclusion – Heap construction networks with size O(n log log n) and depth O(log n) – An (n log log n) lower bound for the size of heap construction networks
Theorem If for constants C1 and C2 C1 n
log t
; O( ) n
S (n t)
C2 n
log t +
O(
n)
then C1 n
log log n
G. S. Brodal, M. C. Pinotti
; O( ) n
H (n)
C2 n
log log n +
O(
n
log log log n)
12