On the random construction of heaps

On the random construction of heaps

Information Processing North-Holland 29 February Letters 27 (1988) 103-109 ON THE RANDOM CONSTRUCTION 1988 OF HEAPS Alan M. FRIEZE Deportment o...

378KB Sizes 1 Downloads 53 Views

Information Processing North-Holland

29 February

Letters 27 (1988) 103-109

ON THE RANDOM

CONSTRUCTION

1988

OF HEAPS

Alan M. FRIEZE Deportment of Computer Science and Statistics. United Kingdom El 4NS

Queen MOT College, University

of London, Mile End Rood, London,

Communicated by P. Henderson Received 1 April 1987 Revised 1 June 1987

We give a simple proof of the result of Bollobb and Simon (1985) on the linear expected time construction heap. We also give bounds on the probability that the construction time is much more than this and generalise d-heaps, da 2.

Keywork

Binary

heap,

d-heap,

upper

of a binary the result to

bound

1. Introduction The binary

heap is one of the basic data structures

of Computer

Science.

An array A[O] = - co, A[l], [3]) that an originally unsorted array A can be put into heap order in O(n) time. On the other hand, it is also known that the ‘repeated insertion’ Algorithm RI below requires S2(n log n) time in the worst case.

A[n] is in heap order if A[i] a A[[+i]] for i z 1. It is well known (see, for example. API,...,

Algorithm RI. The input, the bottom. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

real array A[l..n],

is transformed

into a binary

heap by repeated

insertion

from

begin A[O]:= -00; for t = 1 to n do begin p := t; q := [it]; a := A[t]; while A[ q] > a do begin A[p]:=a[q]; p:=q; end A[ p] := a end

q:=[+pJ

end

Bollobds and Simon [l] analysed the average case time complexity of Algorithm RI under the assumption that the input array A[l..n] contains a random permutation of { 1, 2,. . . , n}. If Tm( A) denotes the (random) number of exchanges (= executions of line 8) for input A, then they proved the following theorem. 0020-0190/88/%3.50

6 1988, Elsevier Science

Publishers

B.V. (North-Holland)

103

INFORMATION

Volume 27, Number 2

PROCESSING

LETTERS

29 February 1988

Theorem A

E(T,,(A))d(l+++o(l))n,

where+=

E - 1 ,=I 1+2’

= 0.7645.

(I)

(Strictly speaking they prove the result for n + 1 a power of 2. We do the same. For arbitrary n one can only double the bound from what is proued.) This is a nice result but its proof is rather involved and the main purpose of this paper is to simplify the proof. (It also removes the o(1) term in inequality (1)). By using a different model of randomness we are able to cut out most of the ‘combinatorial computation’. We are also able to prove some related results. For example, we prove that Algorithm RI is unlikely to require much more than (1 + $)n exchanges (n + 1 = 29. Theorem B. Let 0 < E < 1 be fixed.

Then, for some constant c > 0,

Pr(T,,(A)>,(l+++&)n)

=o(e-C’Z”“‘O).

We also consider d-heaps, d > 2, where all we require is A[i] 2 A[[(i + d - 2)/d J] and analyse the obvious generalisation of Algorithm RI,, in which [itj in line 5 is replaced by ]( t + d - 2)/d ] and ]ip] in line 8 is replaced by ]( p + d - 2)/d 1. We prove the following. Theorem C

2. Model of randomness and notation The main device for simplifying the proof of Bollobas and Simon is a change of model. Instead of considering A[l..n] to contain a random permutation of (1, 2,..., n} we assume that A[l], A[2],..., A[n] are independent uniform [0, 1) random variables. All we have to observe is that the number of exchanges usedbyAlgorithmRIonlydependsoni,, i,,...,i,,whereA[i,]cA[i,]< ...
Ai,k =

C jEL,

104

“j.a

ad

Bi,k =

c aj.k j-0

forO
INFORMATION

Volume 27, Number 2 3. Proof of Theorem

29 February 1988

PROCESSING LETTERS

A

Fix k > 0 and consider the insertion of the elements in level k. For t E L, let P(t) = {[t/2’] : j = k - l} be the vertices on the path from vertex t to the root of T,. Let

1, 2,...,

X,= ]{iEP(t):u,,,_l>ut}

1

= the number of exchanges in the insertion of u, by Algorithm RI d I{ i E

p(t) I u,,,~_,>

u,}I = Y,

say,

since ~~,~+tQ uiTSfor s > i. We prove that E

c i

and

then

r,

<(l++)lLkl,

fGL,

1

inequality

(1)

(2)

k=l,2,...,

follows from X, G x, as observed in [l].

Now, E(Y, I U’.7t-,’ i E P(t)) =

C Pr( 4 < ui,sk_,I qTk_, i E p(t)) = C iaP(r) iSP(f)

ui.7k_1.

Hence, E

c

k-l y,lU,,,k_,~l
=

tGL,

IEL,

C ui.s,_,= ,Co2*-‘Aj,k-l

C

iCP(t)

‘2s

k-2

= 2k

c

2-(J+‘)BJ,k_, + 2-(k-1’Bk_,,k-,

j-0

by straightforward algebra. Removing the conditioning yields E( ,zkx)=2’(

x2-‘/i”h(Bj,,_,)

+2-“-“E(B~-~*k-*)).

(3)

All that is needed are upper bounds to E( Bj,i) for 0
(4)

E( Bo,i) = l/2’+’

since Bo_i is the minimum of 2’+’ - 1 independent

uniform [0, l] random variables.

(5)

E(B,,,)=2’-f since B, i is the sum of 2’+’ - 1 independent uniform [0, l] random variables. To estimate E( Bj,;) for j < i we observe (see [l, Lemma 51) that

Bj,i G Bj,i-1 -

C

max{O, u,,T,_, - VI}9

tcL,

where u, = mm{ U, : s E Li and t E P(S)}. 105

INFORMATION

Volume 27, Number 2

PROCESSING

29 February 1988

LETTERS

value u, will rise to level j at time s and To see this, note that if u, < Us.,,_,. then the corresponding push down the contents of t. Thus, by this time, when looking at the sum of the contents of the first j levels, u, has replaced u ,.,, _, and some other values may have been reduced. Hence, BJ.iGBJ,i-t-

C

(ur,T,_,-ut)=BJ-,.i-,

+

C

(6)

01

IEL,

IGL,

and so

Bj.i) < E(B,_,,,_,)+ 2’/(2’-J+

E(

since each u, is the minimum

1)

of 2’-J independent 2’+2’-‘+

E(Bj.i)

G E(BO,i-j)

+

uniform

variables.

Thus,

2J+‘-1

0.1 +2

2’-’

[0, l] random

G

+ 1

2’-‘+I

(7)



on using (4). Using (5) and (7) in (3) yields k-2 C

2-(i+l)

2if’-1 p-1-j

j=O

As 1L, 1= 2k we have (2) and Theorem

+

+ 1 - 1/2k

< 2”(1+

$8).

1

A.

4. Proof of Theorem B

8,

Again we consider n + 1 = 2k and the insertion of level k and the random We shall prove that be the event {Z, E LkY, > , (1 + e)(l + +) 1L, I}.

variables

K for t E I!+,. Let

(8)

Pr( 8,) = o(e-r’n”‘/25). Since C, E L, Y, dj I LjIalways, we then will have Pr j=lk/21

=o

(ke

tCL,

- e*n”‘“/25

1

and this will prove the theorem. Let g2 be the event k-2

c 2-"+1)Bjek_1 +2-(k-1)Bk_ -l.k-1

<

(1

+

t&)(1

+

d’)

. i

j-0

Then, Pr(8,)

d Pr(gl

l&2;)

+

P@J

We shall show later on that pr( z2) =

and we quickly 106

o(e-‘2n”‘/25)

dispose

of Pr(&t I d$).

(9)

INFORMATION

Volume 27, Number 2

PROCESSING

LE-ITERS

29 February 1988

Consider the following part of Theorem 1 by Hoeffding [2, inequality (2.3)]: let 2,. Z,,. . . , Z,,, be independent random variables where 0 < Z, d 1 for i = 1, 2,. . . , m and E(( Z, + Z, + . . . + Z,,,)/nz) = /-L. Then, for t > 0, Pr((Z,+Z,+ We now observe Also,

that, given

E 2-k i Applying yields

inequality

c

+ Z,)/m

...

2 p + t) Q e-2mr’.

ui,?_ ,,...,

r, 1g2

(10) Y,, t E L,, are independent

u,~_,+,~_,, the variables

d (1 + &)(l

and 0 G Y, G k.

++).

i

teL,

(10) with

m = 2k, Z, = y/k,

i E L,,

p G (1 + ie)(l

+ 9)/k

and

t = ~(1 + +)/(2k)

pr( g1 , g*;) Q e-n(4++v*b3~)* and the theorem follows once we have dealt with equation Consider r E Lj and j < i. We show that 24I.7, d (j + 1)st smallest

24,: s E

of

()

,

L,andrEP(s)

u-j+1

i i.e., u, 7 is at most the (j + 1)st smallest of 2’-j+’ To prove this we inductively show that u,.,,<(j+l)stsmallestof

(9).

1

- 2 2 2*-j independent

{u,:rEP(s)},

jz0,

rEL,,

uniform

[0, l] random

t>,O,

variables.

(11)

where u, = u, if t z s and u, = cc otherwise. Now, clearly (11) holds for j = 0 and t z 0; assume it holds for all r E Lj_, and t z 0 for some j > 0. Now, (11) trivially holds while 1{s : r E P(s) and u, < cc} 1 -c j + 1 and so we have a basis for assuming (11) true up to some value of t. Let S = {s : r E P(s) and u, < cc}. We must confirm (11) for the next case t’~tsuchthatr~P(t’).LetS’=S~{t’}.Wemustshowthatu,,~~(j+l)stsmallestof{u,:sES’}. This is clearly true if u,, 2 u,,~ and if u,, < u,,,, then u,.,~ = u,~,~):, which is at most the jth smallest of a set which includes S. Hence, (11) continues to hold and the inductive step is complete. It then follows that Pr( A,,; >, a2*j-‘)

Q Pr(3r G

2j

e-(a-n]2J-‘-j(i-j)logc2)

Since Aj,i < 2j always, we can remove Pr( Bjai 2 $a2*jmi)

E Lj: u,,,, 2 a2j-j)

< 2j(*‘T)(l G 2j

the restriction

- a2j-i)2’-‘-i

e-(a-aj2J-‘-kz)

if a d 2’-j,

if a < 2’-j.

on a in the above inequality.

Hence,

< Pr( gJ< j : A;,, >, a2*/-‘) j Q

e-(a-mj2’-‘-kZ)

c

2/<

2j+l

e-(a-aj2’-‘-k2).

(12)

J%

Putting

a = 2k’5 we see that (i+l)Bj,k_l

2 $2-k/s

< Pr(3j

Q +k : 2-(j+‘)Bj+k_l

Q zn3/5

e-2k’5-‘_

> $ X 2j-4k’5)

(13) 107

Volume 27, Number 2

INFORMATION

PROCESSING

LETTERS

29 February 1988

Next we give a simple consequence of Hoeffding’s Theorem 1 [2] (easily derivable from inequality (2.1) of that paper): if Z,, Z,,..., Z, are as in (10) and 0 < E < 1, then 2 (1 + e)~) < e-EZmp/3.

Pr(( Z, + Z, + . . . + Z,)/m

(14)

Now suppose that i > [:kJ. It follows from (6) and (14) that 2i-r

Pr Bi_,,k-l-r ( forOgr
-e222’-‘-‘/3

(1 + E) 2/c-l-i+1

- Bi-r_l,k_2_ra

(15)

‘e i

Thus, s-l

Pr 2-(i+1)Bi,k_1 - 2-(i+‘)B,_S,k_,_s

2 (1 + E) c

,x’J”,+T

1

r-0

s-l d

If [;k] <

_r222t-k-‘,3

Ce r=O

i G [$I?],

-,=2”=/3

(16)

then we can use (12) with a = 2k/5 to show that

Pr( Bj_s,k_l_s/2i+1 If


>, $2-k/‘)

< 2i-S+1e-2k’5-‘.

[jk], then we use Bi_s,k_l_s/2i+’ Q 2-” Q 2-k/5. In conjunction with (16) we get that, for i > ]ik],

i >

Pr( B,,,_,/2’+’

> (1 + ~)/(2~-‘-’

+ 1) + 4

x

Zmk/‘)

=

o( n e-e’n”‘/6)_

(17)

It follows next from (14) that Pr(2- +l)B,,

k_l a 1 + E) s

e&2&-‘-v2)/3

as BO,k_ 1 is the sum of 2k - 1 independent (18) then imply that

(18)

uniform [0, l] random variables. (1n)equalities (13), (17), and

k-2

Pr

C2(

(i+l)Bj,k_l +

2-(k-1)Bk_l,k_l

2 (1 + .e)(l + +) + 4

X

j-0

2-k/5

= o(n e-e2n”J/6). i 09)

Replacing E in (19) by E’ satisfying (1 + e’)(l + +) + 4 X 2-k’5 = (1 + $e>(l + 9) yields (9).

5. Proof of Theorem C The idea of the proof is the same as that of Theorem A. This time 7” denotes an infinite d-ary tree where vertex i has children (i - l)d+j, i= 2, 3, d + 1. The level sets L, and the Ai,k, Bi,k are defined analogously, as are the variables X,, Y,, t E L,. In place of (3) we have

E(,z,K) =dk( xd-“+“(d108

l)E(B&,)

+ d+-%(Bk_l,k_l)

(20)

Volume

27. Number

INFORMATION

2

PROCESSING

LE-ITERS

29 February

1988

In place of (4) we have E(&;)

= (d-

l)/(d’+’

2).

(21)

d - l)] .

(22)

+ d-

In place of (5) we have E( B,,,) = (d’+’ - 1)/[2( In place of (6) we have

E( B,,~)< E(

B,_,,,_,) + d/Cd’-‘+

It then follows from (20)-(23)

and Theorem

1) <

d’+’

(d-

_

1

l)(d'-'+

1) ’

(23)

that

C follows.

6. Final comments We do not believe that the upper bound in Theorem A is tight, although it is not clear how to improve it significantly. We have not mentioned lower bounds. We can obtain a rather weak bound of approximately $n on the expected number of exchanges as follows: the number of exchanges needed to insert u,, t E L,, is at least what would the required assuming that levels L,, L,, . . . , L,_, have their contents at time TV. Thus, (24)

Bj,k is at least the sum of the 2’+’ - 1 smallest values in ui, ZQ,. . . , z+~+I_,, and so E(B, k) 2 2i+1(2/+1_ 1)/2k+2. Substitution of this into (24) yields E(ZE, E LIX,) 2 ($ - 0(1))2~.

Now,

Acknowledgment I am particularly Dyer for his helpful

grateful to Trevor comments.

Fenner

for his careful

reading

of the manuscript,

and to Martin

References [I] B. Bollobb and I. Simon, Repeated random insertion into a priority queue, J. Algorithms 6 (1985) 466-477. [2] W. Hoeffding, Probability inequalities for sums of bounded random variables, J. Amer. Statist. Assoc. 58 (1963) 13-30.

[3] D.E. Knuth, The Art of Computer Programming, Vol. 3, Sorting and Searching (Addison-Wesley, Reading, MA, 1973).

109