• Keine Ergebnisse gefunden

A Note on the Average Behavior of Heapsort

N/A
N/A
Protected

Academic year: 2022

Aktie "A Note on the Average Behavior of Heapsort"

Copied!
29
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

deposit_hagen

Publikationsserver der Universitätsbibliothek

Mathematik und

Informatik

Informatik-Berichte 03 – 1980

Ernst-Erich Doberkat

A Note on the Average Behavior of

Heapsort

(2)

A Note on the Average Behavior of Heapsort Ernst-Erich Doberkat

Abstract:

Using some ideas from Calculus and Stochastic Processes, the sift-up phase and the selection phase of heapsort are analysed with respect to the average number of comparisons and interchanges. In particular a Conservation of Ignorance Theorem is proved: both phases of heapsort preserve the given distribution of

the keys tobe sorted up to a multiplicative constant for scaling.

(3)

1. Introducti on

In this paper the problem is posed to analyse the expected performance of heapsort in case the items tobe sorted are real numbers which are taken from the

intervall [0,1], and which are assumed tobe continuously distributed. This means that a set of Lebesgue measure zero has vanishing probability; moreover it is assumed that the corresponding density is invariant under permutations of the respective components.

Under these assumptions bounds are given for the expected number of interchanges, and comparisons, respectively, during the heap creation phase (what Knuth [3]

calls the "sift up"). These bounds are attained in case the number of items tobe sorted is a power of 2, or if it has the form 2k-1 for some k. The expected number of interchanges, and of comparisons in the selection phase can be given more

accurately, and independently of the binary representation of the number of items.

This paper is organized as follows: in Section 2 , a Pascal program for heapsort is presented, and the basic assumptions concerning the distribution considered are stated. It is shown that the results obtained here do not only apply in case continuously distributed real numbers are tobe sorted, but also in case a random permutation is given. Hence it might be interesting to observe whether the basic techniques of noncountable Probability Theory with its measuretheoretic ideas are applicable to analyse t~e expected performance of algorithms which work over a finite domain of possible inputs as well as over an uncountable domain in some Euclidean space. In Section 3, the sift-up is analysed with respect of interchanges and comparisons, analogously in Section 4 the selection is considered, in both cases the asymptotic number of the respective quantities is calculated. In both phases_t~e rather interesting fact (observed by Knuth [3] in case of random

(4)

permutations) is established that the originally given distribution is preserved by the heap creation as well as by the selection up to a constant for scaling.

2. Heapsort - Some Introductory Remarks

Fix the number N of items tobe sorted, then a heap is an N-dimensional array b of real numbers such that b[L{JJ<b[j] holds for 2~j~N. If bis represented as a tree such that b[ L{J] ts the father of b[j], b i s a heap iff any son has a greater value, than its father. The well known sorting method heapsort consists of two phases: in the first phase the array tobe sorted is made a heap, in the second the respective items are selected and arranged in ascending order. The job is done by the following Pascal program (cp.[3] , Algorithm H,p.146,[1], Algorithm 3.3):

phogJtam hea.p~oltt. (Inpu.t,ou.tpu.t);

c..o~,t m = N; (* ~a.y *)

,type index = 1 ... m;

va.h b : aJUtay [Index] o 6 hea.l;

k.: Index;

phoc..edwte hea.pISy (l,h: Index);

va.h I: Index;

j : In;t.eg eh;

b egin I : = l; j : = 2

*

l;

I6 j < h -then I6 b [ j] > b [ j

+

1 ] then j : = j + 1 ;

I6 b[I] > b[jJ .then begin In;t.ehc..ha.nge (b[I], b[j]);

·· .· :•,. ·•. hea.p;taY: (j,h) .·

end end(* hea.pISy *};

2

(5)

6ofL h. := 1 to m do ite.,ad (6[12.]);

6afL h. : ::: m cüv 2 dawn:ta ( * .o el.eilia n. pha..,~ e., *)

da he..apiiy (h.,m);

S afL k. : ::: m dawn;ta 2 da b e.,g,ln. in.teJtc.han.g e.. ( 6 [ 1 ].. b [ 12. ] ) ; he..apiSy ( 1, 12.-1 l

e.,n.d;

6M. h. :"' 1 to m da Wlid:e.. (b [ld)

The correctness of heapsort is established by the following well-known Theorem 1:

a) If the heap condition

(*) b[L{JJ<b[j]

is fulfilled for k+ ls L{J <j sN, then after the call of heapify (k,N) this condition is fulfilled for ks

L{J

<j SN,

b) After the heap creation phase bis a heap,

c) After the selection phase bis ordered in ascending order, d) Heapsort sorts N elements in O(NlgN) time in the warst case!

Proof: See [1], p.90.::_:j

Call b an k-heap if the heap condition (*) is met for every N-dimensional array of real numbers is a k-heap

k s L{J < j < N, for k> L2M J.

then ooviously

Now let b be represented as a labeled binary tree as shown in Fig.l (which is

borrowed from [3], p.154, where each node is 1abeled in binary notation corresponding to its subscript in the heap)

t

lg = log

2, ln = loge

(6)

1 III

·If the binary notation of N is (bn ... b

0)

2 with n := Llg NJ,then the special node (as Knuth calls it) on level j is ,. := (b .... b )

2; a node k on level j is said to

J J 0

be a left node if k<,j and a right node if k>,j (e.g. 9 is a left node, 13 the special node, and 14 is a right node on level 1 in Fig.l, when N=26). It is easily seen that there exists

T · - 2n-j J

l eft nodes ( j 2:'.. 0) and

zn+l-j_1-,.

J

right nodes (j 2:'.. l) on level j. Moreover a left [right] node on a level j has 2j+l_ 1[2j-1] offsprings, since in this case the binary tree rooted at this node is completely balanced. The tree rooted at the special node 'j contains

(lbj_

1 ... b

0)

2 nodes, see [3], p.154,(14).

The algorithm given above is tobe analysed with respect to its average performance.

For this, let us do some preparations and fix some notations. It is assumed that the array tobe sorted contains N mutually different real numbers between O and 1.

Let

be endowed with its Borel sets BG' i.e. the smallest a-field that contains the open sets of G. The array tobe sorted is assumed tobe a random element of G.

Hence we fix a probability space (rl,A,P) and a random variable

(7)

such that under the observation wE~ the vector cr(w) is tobe sorted. It is assumed that 0 is distributed continuously with a symmetric density, i.e. there exists a measurable map

f: G-R with the property that

holds for any permutation n of {l, ... ,N}, such that lP (aEA) = ff(x

1, ... ,xN)ctx

1 ... dxN A

holds for any ßore1 set AEBG.

This is the case e.g. if 0(w)=(X

1(w), ... ,X~/w)), where Xi:~-[0,1] are identica1 distributed and independent random variables such that

JP(X.Eß)=J g(x)dx

l ß

holds for any ßorel set Bin [0,1]. Then JP(aEA)=f g(x

1)•··g(xN)ctx1 ... dxN A

is demonstrated by Fubini 's Theorem ([4], 7.8).

We need two Theorems from Calculus. The first is the Change of Variable Formula for multiple integrals: let X,Y be open subsets of lRN such that X=g[Y] for some

homeomorphism g: IRN .... ,RN, which is continuously differentiable such that ldet g' (y) 1 = 1 holds for any y E Y. Then

f

h(xl' ... ,xN)ctx1 ... ctxN=

f

h(g(x1, ... ,xN))ctx1 ... dxN

X y

holds for every bounded and measurable function h:X-lR. The second is the Uniqueness Theorem for probability measures on BG: let µ

1

2 be probabilities on BG such that

f

gdµl =

f

gdµ2

G G

holds for any continuous and bounded map g:G-R; then we have µ

1(A)=µ

2(A) for any A E BG. Both Theorems are proved in [ 4], 8. 27 and 2 .14, respecti vely. In hi s

(8)

analysis of heapsort, Knuth considers the case that all perrn~tations of {l, ... ,N}

are equally likely ([31, Theorem H, p.155). The following construction shows that this case is considered here implicitely.

Given xEG, denote by K(x):=(rr(l), ... ,TI(N)) the unique permutation such that xi is the TI(i)-th greatest component in (x1, ... ,xN). Denote by

l(rr) := {XE G; K(x) = TI}

the set of all xE G which are mapped to the permutation rr. If

g:(x1, ... ,xi, ... ,xj, ... ,xN) (x1, ... ,xj, ... ,x;,···•xN) interchanges the i-th and the j-th component of x, and p denotes the permutation of {1, ... ,N} which inter- changes i and j and leaves the other elements fixed, then we have by rneans of the

Change of Variable Formula

J

f(xl' ... ,xN)dx1 ... dxN=

f

f(x1 , ... ,xN)dx1 ... dxN,

I(rrop) 1(-rr)

(since x E l(Tiop) iff g(x) E I(1r}) for an arbitrary permutation 1T, it is concluded that

r{1r/(xl' ... ,xN) dx1 ... ctxN =

f

f(x1,. .. ,xN) ctx

1 ... ctxN I(id)

(where id is the identity permutation) holds. Consequently, we have

1 = lP (K o a is a permutation of {1, ... ,N})

=c

JP ( a E

r

(TI))

TT

= N! lP(aE I(id)), hence

IP (crE I(1r)) =NT 1

holds for any TT. This means that every permutat,·on has th e same pro a , b b'l · 1 ty to occur in this derived model, and will be of use later.

Denote for the positive integer k by v(k) the number of 1s in the dual representation (ck ... c0)2of k, then we have

1 s v( k) s Ll g kJ + 1,

and every integer value between these bounds is taken as a value of v. o(f(n)) denotes a term which converges to zero when divided by f(n), as n

- 00

(9)

3. The Heap Creation Phase

The goal of this section is to calculate the expected values for the number of interchanoes, and comparisons necessary to create a heap from the given random vector cr. For this, let the sequence of sets BkcG be inductively defined by

B : = G, L;J+l

Bk:={xEBk+l' x is a k-heap}.

lf x E Bk+l i s represented by the array b in the Pasca 1 program , l et b represent Tk(x) after a call to heapify (k,N). Thus

Tk:8k+l - 8k

is a map by means of which the heap creation can be described as the composition T1oT2o ... oTN oTN.

L-J-1 2

L-J

2

lt will be helpful to have a close. look at Tk; for this let us describe the way xk takes when it is brought into the correct position, when the (k+l)-heap x is made

*

to the k -heap Tk(x). This is best done by means of a ward v E{O,l} such that a 0 means that xk goes to the left, and a i means that xk goes to the right,

respectively. Let for such a v denote s( v) the number which corresponds to v if v is considered as the dual representation of a number, e.g. s(OOllO) = (110)

2 = 6.

VE{0,1}* is said tobe an admissible path for k iff zlVI • k+s(v)sN holds, Le.

l.f zlvl • k+s(vJ· 1·s an offspr1·ng of k ,·n the representat1· n of {l o , ... , N} a s a tree . lt will be necessary to count the number of admissible paths:

Lermia· 1:

Denote by gk the number of admissable paths for k , then zn-Llg kJ +l _ 1, if k is a left node 2n-Llg kJ_l , 1 "f k. 1s a r1g . ht no e d

(lbj_ 1 ... b0)2 , if k= 'j is a special node.

(10)

Proof: Since the admissible paths for k in a one-to-one correspondence with the number of offsprings of k, the assertion follows. 1

Denote by \ ( v) the set of all x E Bk+l' which have the foll owing property:

xk > x . A x i = x i for 1 :s: i :s: 1 v 1 , and i f

27•k+S(v;_

1o) 2 •k+s(v;_

11) 2 -k+s(v;)

zlvl•k+s(v)<~, x <x AX , i f however

k zlvl+l.k+s(vü) zlvl+l•k+s(vl)

1 VI N-1

2 •k+ s(v) =-2- , xk<x I l+l . Here v is an admissible path with vJ- as 2 v •k+s(vO)

length j prefix, and rAS denotes the minimum of the reals r and s. Then xESk(v) iff xk is interchanged with x . , which is

27-k+s(v.)

7

the smaller of x .

2 •k 7 + s(v;_1o) and xi (h_i;;1v1), but xk is not interchanged with the label of any

2 •k+s(vi_11)

offspring of 21v1.k+s(v). Thus v describes the way of xk in the desired manner.

Now denote by "k(v)(x) the effect of doing these interchanges, i.e. if "k(v;(x)=z, then we have z. = x. if i is not on the path v, and

7 7

z i = x i+l for 0:S: i :s: lvl - 1, and finally z lvl = xk

2 ·k+s(v;) 2 •k+s(vi+l) 2 •k+s(v)

holds.

Lemma 2:

If v is an admissible path for k, then

holds.

Proof: 1. It is not difficult to see that "k(v)(x) is a k-heap, i f xE\(v). Thus we have to show the reversed inclusion. Take zEBk and define p(z) :=x in the fo 11 owi ng way:

(11)

x. :=z., if i is not on v,

l l

xi+l :=Z; ,ifO:::;i:::;1v1-1,

2 ·k+s(vi+l) 2 •k+s(v;)

X ·= Z

k" 2lvl.k+s(v)·

Then it has to be shown that p maps Bk to Bk+l' and that p(z) ESk(v) holds.

2. Let us assume that there exists a node i with k + 1 :::;L{J < i:::; N such that

x > x .. This is only possible if L~J lies on v, but i does not (since if both

L~J l t:..

lie on the path, or both don't, p does not chance the respective components, and the case that L{J is not on v, but i is, is impossible). Consider these cases:

a) i ) V 1

L-zJ =2 ·k+s(v); then x i =z lvl-l <z; =X; holds, contra- L7J 2 •k+s(vlvl-1!

dicting the assumption.

b) L { J = 2j • k + s ( v j) for some j, 0 < j < 1 v 1 , then we ha ve

< z . /\ z .

2J • k + s ( V •

1o) 2J • k + s ( v . 11)

J- J-

= Z . /\Z.,

2J • k + s ( v . ) 1 J

whi eh contradi cts the assumption that z E Bk. Note that xk does not come into consideration since

LiJ

2':k + 1 > k was assumed to hold.

From these contradictions we conclude that p maps Bk into Bk+l·

3. To begin with, it has tobe shown that

Xk > X . 1 /\ X . 1 = X . 1

21+ • k + s ( v. 0) 2 1+ • k + s ( v .1) 2 1+ • k + s ( v. +1)

l l l

holds for O:::; i:::; lvl -1. This is done easily by means of the definition of x, since z is a k-heap. Moreover it is concluded that

X <X AX ,

k 21 v 1 + 1 • k + s ( vO) 21 v 1 + 1 • k + s ( v 1) 1,, 1 +1

in case 2 •k+s(vl):::;N, or

(12)

Xk<xN-1 '

7. f 2 I v I k . +sv =-2-· ( ) N-1

From these considerations the Lemma follows.j Now define the fo]lowing random variables on n:

l~J+l 0 2 :=0,

k k+l

0 :=Tko0 (k~l), and l et

i.ik(A) := lP (0 EA) k

be the probability that 0k is an element of the Borel set A. The following Theorem states that the distribution i.ik of 0k equals the distribution of 0 up to a constant factor which is for scaHng only. Since this holds in particular in case f

=

1, i .e.

in case 0 is equidistributed, Theorem 2 might be considered as the continuous generalization of Knuth's Theorem 5.2.3. H ([3],p.155), which states that under the assumption that all permutations of {l, ... ,N} are equally likely each of the possible heaps is equally likely.

Theorem 2:

Let yk :=g N • .. •gk+l•gk be the product of the number of all admissible paths L--zJ+l

down to and including k. Then

holds for any Borel set AcG.

The Proof is done by a backward induktion on k, starting at k =LN

2J+ 1. Since 3 =1 (only the empty path is admissible), we have

L;J+ 1

(13)

Now l et ( *) be true for some k + 1 :s; 1 ; ! + 1, and l et h be a conti nuous and bounded map from Bk to lR, then we have

i

hdµk =

f

h o \dµk+l

k Bk+l

(since f is invariant under permutations)

=yk+lL {

J

h(Tk(x 1, ... ,xN)) • f(Tk(x1, ... ,xN))dx1 ... dxN;

Sk(v)

v is an admissible path for k}

=yk+lL {

J

h(1Tk(v)(x 1, ... ,xN))f(1Tk(v)(x1, ... ,xN)) dx 1 ... dx:1:

Sk(v)

v is an admissible path for k}

(since Tk(x) =1Tk(v)(x), whenever xESk(v))

=yk+l

L { J

h(x 1, ... ,xN) f (x1, ... ,xN)dx1 ... dxN; v is an

B

k admissible path for k}

(by Lemma 2 and the Change of Variable Formula)

This implies the assertion by the Uniqueness Theorem.!

Now let Vk(w) (Ck(w)) be the number of interchanges (comparisons, respectively) which are necessary to transform crk+l(w) into crk(w), then it is obvious that Vk(w) = lvl, whenever cr k+l (w) E Sk(v). Denote by IE(Vk) the expectation of Vk, then the following holds:

(14)

Lemma 3:

J_·(2+(n-Llg kJ -1)·2n-Llg kJ+l, if k is a left node, gk

[(Vk)=

9\•(2+(n-LlgkJ··2)·n-LlgkJ, if k is a right node, ( 1 b j - l ... b

O)

2 + ( 2j -3) • 2j + 3 (lb.

1 ... b ) J- 0 2

, if k=c·•

J

Proof: 1. Let A r, k be the number of admissible paths for k which have exactly the length r, then

A r,k =

zr, if k is no special node, and r< n- Llg kJ, zr, if k is a special node with r<n- Llg kJ

gk-zn-LlgkJ+l, i f k is a special node, r=n-LlgkJ 0, otherwise

holds. This is derived from the fact that under those assumptions the trees rooted at k and consisting of those nodes which have a level in that tree not exceeding rare completely balanced.

2. Since µk+l (Sk(v)) = Yk+l

f

f(x 1, ... ,xN)dx1 ... dxN Sk ( v)

=yk+l

f

f(x 1, ... xN)ctx1 ... dxN

Bk

(by the change of variable formula: if ;k(v) is the inverse of ,rk(v), by Lemma 2 Sk(v) =:;i'.k(v)[Bk] holds, and idet(;(v)) 1i=l)

Yk+l

= ~ · yk · / f(x 1, ... ,xN)dx1 ... dxN k

(15)

because

holds by Theorem 2.

Consequentl y,

Jl (Vk=r)=L {Jl (/+lESk(v)); v is an admissible path for k, lvl=r}

=r=

{µk+l (Sk(v)); v is an admissible path for k, lvl = r}

A

= ~

gk Hence we have

=-L_ 1 A ' gk r r,k

and from this the Lemma follows.

l

Note that the last step in this proof uses the formula

for y = 2.

Let

k . k k+2 (k·l~ k+l

) ;y7

= y - T y + y

w

(y- 1) (y :j:: 1)

L. J : = ~ /__. {[(Vk); k is a 1eft node on level j}

= (, . - 2 n- j ) 2 + ( j -1 ) 2j + 1 ( 1 < J. < n)

J 2j+l - 1 - - '

Rj

:=r=

{[(Vk); k is a right node on level j}

= ( 2n+ 1- j _

1 _ .,. . ) 2 + ( j-2) • 2j

, (2::::j~n),

J 2J - 1

(16)

and

hence

Since

S : = n 3 + ( 2j - 3 ) • 2j , j=l (lb.

1 ... b ) 2

J- 0

n

J [ ( V )=n+S

J=f

'j

2(,.-1-,. 1)=b.+,.-2,

J J+ J J

we get

(b.+,.-2)(1+(j-1)2j)

R.l+L.= J J ·1 ,

J+ J 2J+ - 1

and defining

=n+S+T with

n (b.+,.-2)(1+(j-1)2j)

T: = ) J ~j+l 1

T=T -

has tobe calculated.

Using the well known identity

and the abbreviations

1

n . N

N1 :=Lb--21 =2L- 2J ,

. 1 l l =

the following calculations are done

(17)

n ,.

( 1)

) . i

= ( 2a l - 3) N l - X l ,

0

2J+ - 1

1 2 1

where SN - 2 $ xl 5. 7 - 8N - 2 (2)

n (j -1 ) • T . • 2j

j=I

2j+lJ_ 1

n b.2i n (i-l)b.2;

=(a2-al+l)•N1-)/ k•'1 k 2 - / / k 'k1 k

' S n

2 1(2 -1)

füTT

2 ·12 (2 -1) where the first double sum equals

v ( N l ) + "J( 1 0 . b l . . . b n ) 2. + x 2 with - - < x <a - - . - -1 1 1

4N - 1 - 2 - 3 64N SN - 1

the last double sum equals

n ib.•2;

~2:l ~=l 2k(i+I)(i

n

-~~

n

= ( ; ( 1 + 1 + 1 }ib. +x3) - (-2lv(N1) +~(O.b1···bn)2+x4)

TI

12-21 56•41 l J.l.

where Llg NJ <x < 1 _ 7 LlgNJ+8 240•N3 - 3 - 735 5880•N3

1 1 1 1 1

- - • - - 5a 16 N 4N-l x4 $ 4 ( a2 - a l + 1) - -32N • - - . 8N-1

(3)

(4)

L l g N J < x < J_ _ 7 Ll g NJ + 8 l6°N3 - 6 -49 49-N3 '

(18)

and

( 5) _2 ~n l+(j-1)2j

· 2j+l 1

J= -

= LlgNJ 2

+LlgNJ +LlgNJ+3 +3LlgNJ+7 + 1 _59543_x 2 2 2.2Llg NJ 36-4Llg NJ 56-sLlg NJ 35280 8 1 _ 7 L1 g NJ +8 < X < 289 _ 7L 1 g N J +8 _ 1

24°N3 392-N2 - 8 -11760 196°N3 120•N4 . On collecting these calculations, it is seen that

L

{[(Vk); k is no special node}

= ( a 1 + a·2 - 2 ) N l - L 1 g 2N J 2 + L 1 92 N J - v ( N 1 ) +

i(

0 . b 1 ... b n ) 4 n

+

C

i b . (

-l

+ 5 + 13 ) + i=l 1 2 12-21 56-41

Llg NJ+3 + 3Llg NJ+7 + 1 22Llg NJ

36_4Llg NJ 56_8Llg NJ _59543+F,

35280

where the error Flies within the bounds

and

7 3

2 8N-2

+ o(l.) N

1( +l) 293 1 +o(l)

4

a2 - al - a3 - 18816 - SN-2 N Using the formula

(b. 1· .. b )2 J- 0 3

it is seen that

3

equals

n 3

N-2 3·(b ... b )4 3·N 10 b 211 1

- - + n o ----~(O.b1···b )2-....Q+-+o(-)

4n 28·8n 7.3n 28 n 4 49 N

(19)

Similarly,

is equal to

where

(2j-3) ·2j (lb.

1 ... b ) J- 0 2

n-1 n-1 n

n+2Lib.+3)b.-Z(n+l) (N-2n-b )-Z(n-l)_N-Z +b -2+Q(N),

i =l 1

H

1 2n o 2n 2n o

n Q(N) : = ~

~

( 2j -3) ( b . l ... b ) 2

J- 0

is for adjustment within the interval [2n, 2n+l _ l] such that Q increases monotoni- cally within each interval [2k, zk+l _ l] 111ith the ~roperty that

2a - 3a - 1

Q( 2k+l_l)=k+54k-79+ 2 1 +o(J_)

2 36°2k 2k

hold at the respective boundaries.

On collecting these results, it has been proved

Theorem 3:

The expected number of interchanges during the heap creation phase is asymptotically equa 1 to

+ (

Q _

a _ a ) b _ 2 ( L 1 g N J + 1 ( N _ 2 L 1 g NJ _ b )

4 1 2 o 2L 1 g NJ o

N- 2Llg NJ 24253

- 2UgNJ -35280+Q(N)+o(l)+BN, where

- 1. 3697937:::; BN:::; 0. 2670145. 1

(20)

Let us have a lock at the number of comparisons during the heap creation phase.

Assume that xESk(v) such that 21v1.k+s(v) has i sons, 0:Si:S2. Then there are exactly 2ivl + i comparisons necessary in order to bring xk into the correct position, namely x . is compared with xi , then is xk

21•k+s(vi_ 10) 2 •k+s(vi_

11)

compared with xi , and, finally, xk is compared with the label of every 2 •k+s(vi)

son of 2lvl.k+s(v).

This consideration leadsto Lemma 4:

Denote by C(w) the number of comparisons which are necessary to make o(w) to a heap.

Then we have the following

where

Proof: 1. Let heapify (k,N)

C =

12 •

V + ( N-1)

~

i f N i s odd,

2•V+N-;1 T-+l ,

{ 1, 0,

B

{a J ES (n.)}

Tj J

if w E A

if w~A,

if N i s even,

Ck(w) the number of comparisons which are necessary in k+l and denote by Hk(i) the set of admissible for a . (w),

a call of paths for such that 21v1.k+s(v) has i sons. Then we have

v is an admissible path for k}

; C • l

VE~i) k {crk+lESk(v)}

2

=2Vk+Ci• ; _ 1 k+l

l =Ü VElfjJ l ) fo S k ( V ) }

k

(21)

=2Vk+l+

J

1 k+l (since Hk(O)=0, if ks;L;J).

VE~2) {cr ESk(v)}

Thus we have

Ck = 2V k + 2,

if k is no special node or if

N

is odd (since in this case

L;J

has two sons), and

Ck=2Vk+2-1 k+l ,

{cr ES k ( n k) }

if k is a special node and N is even, where nk is the unique path leading from

k to 2 N . This holds in particular, if k =Tl.

From these observations the assertion follows.j Note that

Corol lary:

[(1 T-+1 )

{cr J ES ( n . ) }

T. J

J

T.+l

= ]P ( cr J ES ( n . ) )

T. J

J

1

= - - - - -1 (lb.

1 ... b ) 2

J- 0

The expected number of comparisons during the heap creation phase is asymptotically equal to

2 1 19

(2a1+2a

2-3)N-LlgNJ +LlgNJ +4v(N)+4(o.b1 ... bn)4 - 49(O.b1 ... bn) 2 +

~

i b . ( 3 + -2...,_ + 13 ) + ( 23 - 2a - 2a ) b - 4 ( L l g N J + 1 ) ( N - 2 L 1 g N J - b )

w

l 6- 2 l 28• 4 l 2 1 2 0 2 L l g M J 0

N - 2llg NJ 24253 211 19

-2 2LlgNJ - 17640 +2Q(N)+2BN+o(l)+bo+(l-bo)(l47 - 84 (O.b1···bn~

(22)

In order to get better estimates of the Llg NJ 2 and the Llg NJ terms, the sum

n 3 + ( 2j - 3) • 2j

~=l (lb. J-1 ... b )2 0

has tobe calculated in a more exact manner, than it could be done here. But it is not quite clear, how this could be accomplished.

4. The Selection Phase

In contrast to the heap creation phase, the root of the tree tobe considered does not shift but is the root of the heap. This makes the analysis somewhat easier.

Call xEG an i heap/chain iff the following holds: (xl' ... ,xi) is a heap, and x1>xi+l> ... >xN is a chain. Let XEB1 be represented by the array b, then after executing the piece of program

0011. j : = M down.:to ;_ da

b e.g,i__n .fo.:te.11.c.hang e. a [ 1 ] and a [ j ] ; he.ap;,6!:r tJ , j -1 )

e.nd

the vector represented by bis an i-heap/chain. Denote by HC(i) the set of all xEG which are i-heap/chains. Let R; be the map which is induced by interchanging the first and the i-th component and then calling heapify (l,i-1), then R. maps

1

obviously HC(i) onto HC(i-1), and the selection phase is described by the composition R2 o . . . o RN-1 o RN

of these maps.

The path

X;

goes when R;(x) is constructed is best described by a word over {0,1}.

Call v E {0,1}* viable for i iff 2lvl + s(v)::; i -1 holds (note that in constructing R;(x) only the nodes 1, ... ,i-1 have tobe relabeled, since the i-th node has x

1 as its new label). Unl ess i::; 3, the empty word constitutes never a viable path for i, since if xEHC(i), then (x1, ... x;) is a heap in particular xi>x

2"x

3 holds.

(23)

If i = 2 or i = 3, the empty word a viable path, since (x 3,x

2) is a heap if x 3 < x

2 Consequently, we have for the number di of viable paths for i

{

i-2,ifi>3 di= 2 , i f i = 3 1 , i f i = 2 .

As in the heap creation phase we use the possible paths to split the set of vectors under consideration. Let x E HC(i) and v be a viable path for i, then

11 x . = x . holds for 1::; j::; 1 VI, and 2J +s ( v .

11 ) 2J +s ( v . )

J- J

i f x. <x 1 1 11x 1 , provided 2lvl +s(v) = i- 2

1 , respectively, holds<

1 21v + +s(vO) 2lvl+ +s(vl) It is readily verified that

HC(i) = U{Ai (v); v is viable for i}

hol ds fo r every i . Now def i ne as above for x E Ai ( v)

J.. i (V)( X) : = ( Z l, ... , ZN) , where

and

(zi, ... ,zN) := (xl,xi+I•···•xN), zj := xj, if j is not on the path v, z . J-1

2 +S (V. J-l)

: = X . '

2J +S ( V . ) J

i f 1::;j::;lvl,

Then we have the following statement, which is proved exactly as Lemma 2:

Lemma 5:

J..i(v) is a bijection from Ai(v) to HC(i-1), and Ri(x)=J..i(v)(x), whenever x E A i ~

(24)

Now define the random variables

by

and

s. l : = R. o s.

1 - 1 1

Let for the Bore l set Ac G xi(A) := lP (si EA),

the probability that 1:;. is an element of the Borel set AcG, and define

1

C. : = C.

1• d.

1 1 - l '

where

CN : = yl ,

then it can be stated concerning

1

Proposition 1:

a) xi(A) =Ci• { f(x1, ... ,xN)dx1 ... dxN holds for every Borel set AcG, b) x-(A.(v)) = - : r . 1

1 1 ~

This Proposition is proved exactly as the corresponding statements for the sift-up phase, and part a) is a continuation of Theorem 2. In particular the originally given distribution of cr remains stable under heapify up to a factor for adjustment, which takes care of xi(HC(i)) = 1 .

Let 1 + Ui(w) be the number of interchanges which are necessary to transform s i ( w) to s· 1(w), i.e. to apply R. to 1:;.(w), then

1 - 1 1

2

U:=;(l+U.)

~ 1

is the number of interchanges which are necessary to sort cr 1

(25)

Since

Ui =

C

{U;•l{r;:.EA.(v)}; v ·;s a viable path for i},

l l

= C {lvl•l{c;.EA.(v)}; v is a viable path for i},

l l

the expectation of U. equals

l

cr:C

1 {lvl; v is a viable path for i} .

l

The cases i = 3 and i = 2 are easi ly treated:

1 1

[ ( U 3 ) = 2(1 + O ) = 2 ' [(U2) = 0.

Now let us assume that i2:4 holds. Then

L_ {

1 v 1 ; v i s a vi ab l e pa th for i}

L1 g iJ .

= ~=l j2J - Llg; J (2llg iJ +l _ 1)

=iLlgiJ +2(1-2LlgiJ) holds, hence

N N iLlgiJ+2(1-2LlgiJ)

~ [(U;) = ~

i - 2

n

= ~

Using Euler1s summation formula H(n) n 1

:=~,

1 1

=ln 2+y+2n-

2+s(n), 12•n

where y is the Euler-M<S:heroni constant, and 1

O < s(n) < 120-n4 ([2],p.111), it is seen that

(26)

with

hence

= 2n+l(n - 1 - ln4) - n2

1n2 + (~ + ln2)n

_ 4n + 35 + 39n + 135+ 161n - 64_9 + Zy + 12 1 n2 _ 8·2n 72.4n 1·96.3n

-2·(p(l)

lf3003 3528

n · ·1 · 1 7 1

cp(l)

: = /

(j+2J)[E:(2J+ )-s(2J))+-y(-y--- j+l ) J

n s z

-1 s(2 -1)

n+l 1

- d

2 ) + - n+ 1 n+ 1

1 r~( 1) ! < 31 864000

< 886267 6048000

8 (2 - 1)

15n + 16 + _7_

54000·16n 1920

= 0.1465389.

A simular calculation demonstrates

2n+l_1

J

i·n+2(1-2n)

with hence

This proves

- i ~ i - 2

n+l zn+l

= n•N+n-n•2 +ln-N-(2(2n-l)-2n)

60n + 23 + 74n - 46 + lO(n+l) + 3(2n -1 -n) + 11(2n - 1 - n)

12·2n+l 12·4n+l sn+l N 6-N2

n

+2(2 31-n) -~+ 2(2n-l-n)cp(2), N

( 2) n+l 1 g

<+> := 2(2 ) - s(N) +

3 - --.---.--

N (N-1) gn+l(2n+l_l)

(27)

Theorem 4:

The expected number of interchanges during the selection phase amounts to N· Llg NJ -(ln 4 + 1)·2Llg NJ+l _ Llg NJ2

•ln 2 - LlgNJ•(i + ln 2)

2Llg NJ+l l N llg Nj

+ l n - - - [ 2 ( 2 L g J-1)-2LlgNJ1 +3, 2 +243619964 ()

N :J N + o 1 + EN,

where

0 < EN < 0.2930777.,

Now let v be a viable for i:?:5 such that 21V1+s(v) has two sons in the tree with the nodes l, ... ,i-1, and xEAi(v). Then there are 2ivl +2 comparisons necessary for xi to find its correct position in the heap reconstruction phase. This applies to the cases 2lvl +s(v)<Li

2

1~, and 2lvl +s(v)= li

2

1J, if i is even, respectively.

In case i is odd, and 2lvl +s(v) = li

2 \1

holds, there are 2Jvl +1 comparisons necessary. If 21v1+s(v) has no son in the tree under consideration, the heap reconstruction needs 2ivl comparisons. Obviously there are L-½!J -2 paths v such

IVI i-1 IVI i-1 i

that 2 + s(v) < L--yJ, exactly one path with 2 + s(v) = L2 J, and L-zJ paths such that 2lvl +s(v) has no son in the tree (or, equivalently, for which

i-1 1 V 1

L-y-J < 2 + s(v)::; i-1 holds)

Denote by Vi(w) t~e number of comparisons which are necessary in a call to heapify (i-1,N), when the array is given according to s;(w), and let i:?:5 .

. We then ha ve

V.= 1 L._ . {V. 1{ 1 i'.;. EA ( )}; v is V a viable path for i}

1 1

~ IVI i-1

= t _ _ {(21vl +2)1{;:;.EA-(v)}; 2 +s(v) < L-yJ}

l 1

+

L

{V; 1{;:;.EA.(v)}; v ends in the father of i-1}

1 1

(28)

Hence

[ (V. ) = 2[ ( U. ) + 2 ~ { lP ( s -EA. ( v)) ; 2 I v I + s ( v) < l i

2

1J }

l l f _ l l

+ lP (s -EA. (v*))' if i is odd, l l

[ ( V i) = 2[ (

u

i) + 2

L {

lP (

s

i EA; (V)); 21 V 1 + s (V) < l i

2

lJ}

+2lP (s-EA.(v*)), if i is even,

l l

where v* is the uniquely determined path which leads to the father of i-1. If m.

l

abbreviates [(V. l -2U-), l it is seen from this discussion that

holds, hence

d. • m. =

l l

2 ( L

4

J - 2) + 1, i f i i s odd 2 (L-i-1 2-J - 2) + 2, i f i i s even

holds in any case.

If i = 4, x2 and x3 are compared, and x

4 is compared with the winner, hence

v

3 = 2.

Analogously

v

2 is seen to equal 1. Thus

hence

2 V:=> V.

,-;-r;f l N

=3+C

v. ,

. 5 l 1=

N N

[ (V) = 3 + 2 ~ =S [ ( U i ) + ~ =S m;

N 3

= (N"-1) + 2 ~=S. [(U;) - 2(H(N-2) -2)

= (N-1) + 2[(U) -2 ln N +J- 3 - 2y + o(!) Thus we get from Theorem 4.

(29)

Corollary:

The expected number of comparisons during the selection phase is asymptotically N ( 2 L l g NJ + 1) - ( l n 4 + 1) · 2 L l g N J +2 - Ll g N J 2 · l n 4 + Ll g N J ( 3 + l n 4) - 2 l n N

+ 2 l n 2 L 1

g

~

J + 1 [ 2 ( 2 L 1 g N J - 1 ) - 2 Ll g N J ] + 6 • 2

L

1 g NJ

N

- 2.7182316 + o(l) + FN, where G < FN < 0.5861554. J

Let us have a final look at the situation in the finite case, in which all

permutations have the same probability to occur. From Theorem 2 and Proposition 1 it is seen that the equidistribution is preserved during the heap creation phase as well as during the selection phase, when the transformation K (which has been introduced at the end of Section 2) is considered. Consequently, the asy~ptotic values given here apply to this finite model, too.

It would be interesting for the average case analysis of other algorithms if

similar methods can be applied, namely to study continuous distributions, and then to transform the results obtained there to the discrete, finite case.

References:

[1] Aho, A.V., Hopcroft, J.E., Ullman, J.D.: The Design and Analysis of Computer Algorithms. Addison-Wesley, Reading, Mass., 1974

~2] Knutn, D.E.: The Art of Computer Programming, vol. I: Fundamental Al0orithms. Addison-Wesley, Reading, Mass., 2nd Edition, 1973 [3] Knuth, D.E.: The Art of Computer Programming, vol. III: Sorting

and Searching. Addison-Wesley, Reading, Mass., 1973 [4] Rudin, W.: Real and Complex Analysis, 2nd Edition.

Tata McGraw-Hill, New Delhi, 1974

Referenzen

ÄHNLICHE DOKUMENTE

Concerning engine types, we assume that the energy required for the propulsion of the mining and transport vessels is generated by the ship’s main engine

Linking model design and application for transdisciplinary approaches in social-ecological

While both models simulated reduced soil water content and above-ground biomass in response to drought, the strength and duration of these responses differed4. Despite

Heat stress, temperature, ATES, shallow aquifer, groundwater, microbial community, amplicon 466. sequencing

Electro-assisted removal of polar and ionic organic compounds from water using activated carbon

The bioavailable BEQs (passive equilibrium sampling) typically were 10-100 times and up to 420 times lower than the total BEQ (solvent extraction) for the AhR and AREc32

Effects of electrokinetic phenomena on bacterial deposition monitored by quartz crystal microbalance with dissipation

The world needs effective thermal insulation of buildings for pollution control and energy savings. Optimum thermal, fire and acoustic insulations are achieved by using