Solutios to Exercises Chapter 4: Recurrece relatios ad geeratig fuctios 1 (a) There are seatig positios arraged i a lie. Prove that the umber of ways of choosig a subset of these positios, with o two chose positios cosecutive, is F +1. (b) If the positios are arraged aroud a circle, show that the umber of choices is F + F 2 for 2. (a) Proof by iductio. If g() deotes this umber, the we have g(1) = 2 = F 2, g(2) = 3 = F 3. (For = 2, we caot occupy both positios; but all other choices are possible.) For > 2, we separate the seatig selectios ito those i which the last positio is uoccupied ad those i which it is occupied. There are g( 1) of the first kid. If the last positio is take, the the oe before it must be free, ad we have a arbitrary seatig pla o the first 2 positios; so there are g( 2) of these. Hece ad the iductive step is proved. g() = g( 1) + g( 2) = F + F 1 = F +1, (b) Cosider a particular positio o the circle. If it is uoccupied, we ca break the circle at that poit, ad obtai a lie with 1 positios, which ca be filled i g( 1) = F ways. If the positio is occupied, the its eighbours o either side are uoccupied, ad (if 3) we ca remove the positio ad its two eighbours, obtaiig a lie of 3 positios which ca be filled i g( 3) = F 2 ways. The same holds if = 2, sice there is just oe seatig pla with the give positio occupied, ad F 0 = 1. So we have F + F 2 seatig plas i all. 1
2 Prove the followig idetities: (a) F 2 F +1 F 1 = ( 1) for 1. (b) F i = F +2 1. (c) F 2 1 + F 2 = F 2, F 1 F + F F +1 = F 2+1. (d) F = /2 ( i i ). (a) Iductio: the result holds for = 1. For 2, we have F 2 F +1 F 1 = F 2 (F +F 1 )F 1 = (F F 1 )F F 2 1 = (F 2 1 F F 2 ), so, if F 2 1 F F 2 = ( 1) 1 the F 2 F +1 F 1 = ( 1). (b) Iductio. The result is true for = 0 (the empty sum is zero). Assumig it for 1, we have F i = (F +1 1) + F = F +2 1. (c) Agai, iductio. The result holds for = 1 by ispectio. Assume it for ; that is, F 2 2 + F 2 1 = F 2 2, F 2 F 1 + F 1 F = F 2 1. Addig these equatios ad usig the Fiboacci recurrece, we get F 2 F + F 1 F +1 = F 2. Usig (a) twice, this implies that F 2 1 + F2 = F 2. Now add this equatio to the secod displayed equatio usig the Fiboacci recurrece to get F 1 F + F F +1 = F 2+1. (d) Most easily, this follows from our origial iterpretatio of F as the umber of expressios for as a ordered sum of 1s ad 2s. Such a expressio with i 2s will have 2i 1s, hece i summads altogether; there are ( i) i ways to choose the positios of the 2s i the sum. Now summig over i gives the result. 2
3 Show that F is composite for all odd > 3. By 2(c), F 2+1 = F (F 1 + F +1 ); ad if > 1, the both factors are greater tha 1. 4 Show that for 1. ( 1)/2 F 2i = F +1 1 The proof is by a iductio which goes from 2 to, so the iitial cases = 1 ad = 2 must both be checked. Assumig the result for 2, we have ( 1)/2 F 2i = (F 1 1) + F = F +1 1. i=1 5 Prove that every o-egative iteger x less tha F +1 ca be expressed i a uique way i the form F i1 + F i2 +... + F ir, where i 1,i 2,...,i r {1,...,}, i 1 > i 2 + 1, i 2 > i 3 + 1,... (i other words, i 1,...,i r are all distict ad o two are cosecutive). Deduce Exercise 1(a). Follow the hit. If we had ay expressio of this form usig Fiboacci umbers below F, the we could if ecessary replace the summads by larger oes ad add ew summads to obtai F 1 + F 3 +... = F 1 (by Questio 4). So the sum of the origial expressio was at most F 1. Hece ay expressio summig to x, with F x < F +1, must iclude F. Now x F < F +1 F = F 1, so by iductio there is a uique expressio for x F, ad hece for x (sice the expressio for x F caot ivolve F 1, so does ot cotai cosecutive Fiboacci umbers). Hece, the umber of expressios of this form (that is, the umber of ways of choosig a subset of the idices 1,2,..., with o two cosecutive) is equal to the umber of possible sums 0,1,...,F +1 1, that is, F +1, as asserted i 1(a). 6 Fiboacci umbers are traditioally associated with the breedig of rabbits. Assume that a pair of rabbits does ot breed i its first moth, ad that it produces a pair of offsprig i each subsequet moth. Assume also that rabbits live forever. Show that, startig with oe ewbor pair of rabbits, the umber of pairs alive i the th moth is F. 3
I the 0th ad 1st moths, oe pair is alive, so the result is true for = 0,1. Assume it holds for 1. The, i the ( 1)st moth, F 1 pairs of rabbits are alive, of whom F 2 were alive i the precedig moth (ad hece old eough to breed), providig F 2 ewbor pairs i the th moth. Thus, the total umber of pairs i the th moth is F 1 + F 2 = F. 7 Prove that the umber of additios required to compute the Fiboacci umber F accordig to the iefficiet algorithm described i the text is F 1. Iductio: check the result for small. Now F 1 takes F 1 additios, ad F 2 takes F 1 1 additios; oe further additio is required to combie them, givig i all (F 1) + (F 1 1) + 1 = F +1 1 additios. 8 (a) Prove that F m+ = F m F +F m 1 F 1 for m, 0 (with the covetio that F 1 = 0). (b) Use this to derive a algorithm for calculatig F usig oly clog arithmetic operatios. (c) Give that multiplicatio is slower tha additio, is this algorithm really better tha oe ivolvig 1 additios? (a) Iductio o m. For m = 0, the result is a tautology, ad for m = 1 it is the Fiboacci recurrece. For m > 1 we have F (m+1)+ = F m+ + F (m 1)+ = F m F + F m 1 F 1 + F m 1 F + F m 2 F 1 = F m+1 F + F m F 1, usig the covetio that F 1 = 0 ad the fact that, the, the relatio F m = F m 1 + F m 2 holds also for m = 1. (b) The trick is to calculate pairs (F 1,F ) of Fiboacci umbers, observig that we ca go efficietly from the pairs (F m 1,F m ) ad (F 1,F ) to the pair (F m+ 1,F m+ ) usig (a). A cleaer way to express this is i terms of matrices. We have (F F +1 )A = (F +1 F +2 ), where A is the 2 2 matrix ( 0 1 1 1 ). Hece (F F +1 ) = (1,1)A. By the aalogue for powers of Russia peasat multiplicatio (Chapter 2, Exercise 12(iii)), we ca fid A i about 2log 2 matrix multiplicatios, each requirig eight iteger multiplicatios ad four additios (though this ca be improved). (c) Sice F is expoetially large, the umber of its digits is proportioal to. Hece a log multiplicatio ivolves a umber of additios proportioal to, ad we have ot really made much savig over a method ivolvig additios. 4
9 (a) Solve the followig recurrece relatios. (i) f ( + 1) = f () 2, f (0) = 2. (ii) f ( + 1) = f () + f ( 1) + f ( 2), f (0) = f (1) = f (2) = 1. 1 (iii) f ( + 1) = 1 + f (i), f (0) = 1. (b) Show that the umber of ways of writig as a sum of positive itegers, where the order of the summads is sigificat, is 2 1 for 1. (a) (i) Let g() = log 2 f (). The g( + 1) = 2g(), g(0) = 1; so g() = 2 (Sectio 3.1), ad f () = 2 2. (ii) The characteristic equatio (see Sectio 4.3) is x 3 = x 2 + x + 1. This cubic has three distict real roots α, β, γ, ad so the geeral solutio is f () = aα + bβ + cγ, where a,b,c are determied by the three equatios a + b + c = 1, aα + bβ + cγ = 1, aα 2 + bβ 2 + cγ 2 = 1. (iii) We have f (1) = 1, sice the empty sum is zero. For 1, we have ( ) 1 2 f ( + 1) = 1 + f (i) = 1 + f (i) + f ( 1) = f () + f ( 1), ad so by iductio f () is the Fiboacci umber F. (b) There is oe expressio cosistig of the sigle umber. Ay other expressio eds with some positive umber j <, preceded by a expressio with sum j; so the umber f () of expressios satisfies 1 1 f () = 1 + f ( j) = 1 + f (i). j=1 i=1 Just as i (a)(iii), we fid that f ( + 1) = f () + f () = 2 f (). Sice f (1) = 1, we have f () = 2 1, as claimed. 5
10 The umber f () of steps required to solve the Chiese rigs puzzle with rigs satisfies f (1) = 1 ad { 2 f (), odd, f ( + 1) = 2 f () + 1, eve. Prove that f ( + 2) = f ( + 1) + 2 f () + 1. Hece or otherwise fid a formula for f (). If is odd, the f ( + 2) = 2 f ( + 1) + 1 ad f ( + 1) = 2 f (); if is eve, the f ( + 2) = 2 f ( + 1) ad f ( + 1) = 2 f () + 1. So the result holds i either case. This is ot a pure recurrece relatio because of the added 1. But if we set g() = f () + 2 1, the g( + 1) + 2g() = f ( + 1) + 2 f () + 3 2 = g( + 2). The characteristic equatio is x 2 = x + 2, with solutios 2 ad 1, so g() = a2 + b( 1). The iitial values g(1) = 3 2 ad g(2) = 5 2 yield a = 2 3 ad b = 1 6. So f () = 2 3 2 1 6 ( 1) 1 2. 11 (a) Let s() be the umber of sequeces (x 1,...,x k ) of itegers satisfyig 1 x i for all i ad x i+1 2x i for i = 1,...,k 1. (The legth of the sequece is ot specified; i particular, the empty sequece is icluded.) Prove the recurrece s() = s( 1) + s( /2 ) for 1, with s(0) = 1. Calculate a few values of s. Show that the geeratig fuctio S(t) satisfies (1 t)s(t) = (1 +t)s(t 2 ). (b) Let u() be the umber of sequeces (x 1,...,x k ) of itegers satisfyig 1 x i for all i ad x i+1 > i j=1 x j for i = 1,...,k 1. Calculate a few values of u. Ca you discover a relatioship betwee s ad u? Ca you prove it? (a) We have s(0) = 1, sice oly the empty sequece occurs. For > 0, divide the sequeces couted by s() ito two classes: those ot cotaiig, ad those cotaiig. There are s( 1) of the first class. For the secod, all terms other tha are at most /2, so such a sequece is obtaied from a sequece of umbers with 1 x i /2 ad x i+1 2x i by adjoiig ; so there are s( /2 ) of these. The recurrece relatio follows. If S(t) = s()t, the we have S(t) = ts(t) + (1 +t)s(t 2 ); 6
for the coefficiet of t o the right is s( 1) + s( /2 ), ad the costat term o both sides is equal to 1. (b) The relatioship is u() u( 1) = s()/2 for 2. The proof is quite itricate, ad depeds o defiig aother sequece v() as follows: v() is the umber of sequeces (x 1,...,x k ) of positive itegers which satisfy x i > i 1 j=1 x j for all i k ad k i=1 x i =. Now such a sequece must have last term x k > /2, ad is obtaied by takig a sequece with sum x k ( 1)/2 ad appedig x k to it. So we have the recurrece v() = ( 1)/2 v(i). i=1 It follows from this that v(2) = v(2 1) = v(2 2) + v( 1) for all > 0. Now we claim that v(2) = s()/2 for all 1. This is proved by iductio, beig true for = 1. Assumig the result for 1, we have v(2) = v(2 2) + v( 1) = s( 1)/2 + s( /2 )/2 = s()/2, ad the iductio goes through. Now u() u( 1) is the umber of sequeces satisfyig the specificatio for u which have last term. These are obtaied by appedig to a sequece with sum strictly smaller tha ; so we have u() u( 1) = 1 v(i) = v(2) = s()/2. Remark: Sequeces satisfyig this coditio are called supericreasig; they arose i the first example of a public-key cryptosystem, the Merkle Hellma kapsack system. The two sequeces s ad u occur as umbers M1011 ad M1053 i the Ecyclopedia of Iteger Sequeces, where further refereces ca be foud, or you ca fid them i the olie versio here (for s) ad here (for u). 12 Let F(t) be a formal power series with costat term 1. By fidig a recurrece relatio for its coefficiets, show that there is a multiplicative iverse G(t) of F(t). Moreover, if the coefficiets of F are itegers, so are those of G. 7
Let F(t) = f t ad G(t) = g t, with f 0 = 1. The relatio F(t)G(t) = 1 is equivalet to g 0 = 1 ad the recurrece relatio g = i=1 f i g i for g i terms of earlier values. So the values g are determied, ad are itegers if the values f are. 13 A permutatio π of the set {1,...,} is called coected if there does ot exist a umber k with 1 k < such that π maps the subset {1,2,...,k} ito itself. Let c be the umber of coected permutatios. Prove that i=1 c i ( i)! =! Deduce that, if F(t) = 1!t ad G(t) = 1 c t are the geeratig fuctios of the sequeces (!) ad (c ) respectively, the 1 G(t) = (1 + F(t)) 1. For a permutatio π, let i(π) be the least positive iteger k such that π maps the set {1,...,k} ito itself. (Such a k exists, sice certaily π maps {1,...,} to itself.) The π is the composite of a coected permutatio o {1,...,i(π)} ad a arbitrary permutatio o {i(π)+1,...,} (this last set beig empty if i(π) = ). Summig over i, we obtai the stated recurrece. As i the precedig questio, this recurrece is equivalet to (1 + F(t))(1 G(t)) = 1. 14 Let (1 +t ) = a t. 1 0 Prove that a is the umber of ways of writig as the sum of distict positive itegers. (For example, a 6 = 4, sice 6 = 5 + 1 = 4 + 2 = 3 + 2 + 1.) A term i the ifiite product (1 + t ) is obtaied by selectig t m from the mth factor for a fiite umber of values of, ad 1 from all the other factors. So there is a cotributio of t for every expressio of as a sum of distict positive itegers. 8
15 (a) I a electio, there are two cadidates, A ad B; the umber of votes cast is 2. Each cadidate receives exactly votes; but, at every itermediate poit durig the cout, A has received more votes tha B. Show that the umber of ways this ca happe is the Catala umber C. Ca you costruct a bijectio betwee the bracketed expressios ad the votig patters i (a)? (b) I the above electio, assume oly that, at ay itermediate stage, A has received at least as may votes as B. Prove that the umber of possibilities is ow C +1. (a) As i the Hit, let f () be the umber of ways of coutig. Now A leads by just oe vote after the first vote is couted. Suppose that this ext occurs after 2i + 1 votes have bee couted. (It must happe agai, sice B evetually catches A.) The there are f (i) choices for the cout betwee these poits, sice each cadidate receives i votes ad A is always strictly ahead. Also, there are f ( i) choices for the rest of the cout; for if we preted at this stage that A has just oe vote ad B oe, we are ruig the cout satisfyig the same coditios with just 2( i) votes altogether. So we have f () = 1 i=1 f (i) f ( i), which is the Catala recurrece. (b) Agai follow the hit: I the modified electio where A gets a extra vote at the start ad B a extra vote at the ed, there are + 1 votes for each cadidate, ad A is alway strictly ahead. So there are C +1 ways of doig the cout uder this coditio. 16 A clow stads o the edge of a swimmig pool, holdig a bag cotaiig red ad blue balls. He draws the balls out oe at a time ad discards them. If he draws a blue ball, he takes oe step back; if a red ball, oe step forward. (All steps have the same size.) Show that the probability that the clow remais dry is 1/( + 1). Imagie that the clow wears a divig suit, ad cotiues to draw balls eve after he gets wet. There are ( 2) ways i which the balls could be draw. The clow stays dry if ad oly if the umber of red balls ever exceeds the umber of blue oes; accordig to the votig iterpretatio, this is C +1. The ratio is 1/( + 1). For a harder exercise, fid a direct proof of this exercise, ad reverse the above argumet to deduce the formula for the Catala umbers. 17 Prove that lim ( ) 1/ B = 0.! 9
of The radius of covergece of the power series 0 B t /! is the reciprocal lim sup ( ) 1/ B.! But the radius of covergece is ifiite, sice the series coverges everywhere. (I geeral, the radius of covergece is the distace from the origi to the earest sigularity i the complex plae; if the sum fuctio has o sigularities, the radius of covergece is ifiite.) So the limsup is zero, which meas (sice all the values are positive) that the limit is zero. 18 (a) Prove that the expoetial geeratig fuctio for the umber s() of ivolutios o {1,...,} (Sectio 4.4) is exp(t + 1 2 t2 ). (b) Prove that the expoetial geeratig fuctio for the umber d() of deragemets of {1,...,} is 1/((1 t)exp(t)). (a) The fuctio S(t) = exp(t + 1 2 t2 ) is the uique solutio of the differetial equatio S (t) = (1 +t)s(t) with the iitial coditio S(0) = 1. So it suffices to check that the e.g.f. of the umbers s() satisfies this differetial equatio. (Clearly it satisfies the iitial coditio.) Now, if we put S(t) = 0 s()t /!, the the coefficiet of t 1 /( 1)! i (1 + t)s(t) is s( 1) + ( 1)s( 2), whereas the coefficiet i S (t) is s() (sice differetiatio of the e.g.f. correspods to a left shift of the sequece). By the recurrece relatio i Sectio 4.4, these two expressios are equal. (b) We have by (4.4.1). Hece d()t 0! d() =! = = ( ) ( 1) i. i! ( ) ( ) ( t) i i 0 i! t k k 0 1 e t (1 t). (I the first lie, we put k = i; the i ad k ru idepedetly over the oegative itegers.) 10
19 The Beroulli umbers B() (ot to be cofused with the Bell umbers B ) are defied by the recurrece B(0) = 1 ad k=0 ( + 1 k ) B(k) = 0 for 1. Prove that the expoetial geeratig fuctio B()t f (t) = 0! is give by f (t) = t/(exp(t) 1). Show that f (t) + 2 1 t is a eve fuctio of t, ad deduce that B() = 0 for all odd 3. What is the solutio of the similar-lookig recurrece b(0) = 1 ad for 1? k=0 The recurrece ca be writte as ( + 1 +1 k=1 k ( ) b(k) = 0 k ) B(k) = B( + 1) for 2. Multiplyig by t +1 /(+1)! ad summig over 2, the two sides are f (t)exp(t) ad f (t) with the costat ad liear terms omitted. Thus f (t)exp(t) 1 t + 1 2 t = f (t) 1 + 1 2t, whece f (t) = t/(exp(t) 1), as required. Now f (t)+ 2 1t = 1 2 t(exp( 1 2 t)+exp( 2 1t))/(exp( 1 2 t) exp( 1 2 t)) = 1 2 t coth 1 2 t, a eve fuctio. The last recurrece obviously has the solutio b(k) = ( 1) k. 11
20 For eve, let e be the umber of permutatios of {1,...,} with all cycles eve; o, the umber of permutatios with all cycles odd; ad p =! the total umber of permutatios. Let E(t), O(t) ad P(t) be the expoetial geeratig fuctios of these sequeces. Show that (a) P(t) = (1 t 2 ) 1 ; (b) E(t) = (1 t 2 ) 1/2 ; [HINT: Exercise 15 of Chapter 3] (c) E(t).O(t) = P(t); (d) e = o for all eve. Fid a bijective proof of the last equality. (a) Sice we are oly cosiderig eve vales of, we have ( ) (2m)! P(t) = t 2m = 1 m 0 (2m)! 1 t 2. (b) By Chapter 3, Exercise 16, the umber e 2m of permutatios of a (2m)-set with all cycles eve is ((2m)!!) 2. So we have ( ((2m)!!) 2 ) E(t) = t 2m = (1 t 2 ) 1/2, m 0 (2m)! the secod equality beig verified by expadig the last expressio usig the Biomial Theorem. (c) This is a istace of a very geeral coutig priciple. If F(t) ad G(t) are the e.g.f.s for labelled structures i two classes F ad G, with F(t) = f t /! ad G(t) = g t /!, the F(t)G(t) is the e.g.f. of structures cosistig of a partitio of the poit set with a F -structure o oe part ad a G-structure o the complemet. For, if H is the class of such composite structures, the the umber of H -structures o a -set is give by h = k=0 ( k ) f k g k, from which a simple calculatio shows that H(t) = h t /! = F(t)G(t). Now (c) follows o applyig this result, where F ad G deote the classes of permutatios with all cycles eve, resp., all cycles odd: give a arbitrary 12
permutatio π o {1,...,2m}, there is a uique partitio of the set ito two parts o which the iduced permutatios satisfy these coditios; ad both parts have eve cardiality. (d) From (a), (b) ad (c), we deduce that O(t) = (1 t 2 ) 1/2 = E(t); so o = e for all. Here is a outlie of the costructio of a bijectio: you should fill i the details. Give the cycle decompositio of a permutatio of {1,...,}, we ca rotate each cycle so that the least elemet comes first, ad the order the cycles accordig to their least elemets. Call this the caoical form of the cycle decompositio. We ow defie the bijectios: (a) Give a caoical form C 1,...,C 2r, where each cycle C i has odd legth, for i = 1,...,r remove the last elemet of the cycle C 2i ad add it at the ed of the cycle C 2i 1. (Some cycles may disappear i this process.) The resultig cycles all have eve legth, ad the expressio is i the caoical form. (b) Give a caoical form C 1,...,C m, where each cycle has eve legth, proceed recursively as follows. Let x be the last elemet of C 1, ad y the first elemet of C 2. (Take y = if C 2 does t exist, that is, if there is oly oe cycle.) If x > y, remove x from C 1 ad add it at the ed of C 2 ; the process C 3,...,C m. If x < y, remove x from C 1 ad add it as a ew sigleto cycle after C 1 ; the process C 2,...,C m. The resultig cycles all have odd legth ad the expressio is i caoical form. It remais to show that these two maps are mutually iverse. See R. P. Lewis ad S. P. Norto, Discrete Mathematics 138 (1995), 315 318, for further details. 21 Show that QUICKSORT sometimes requires all ( 2) comparisos to sort a list. For how may orderigs does this occur? Oe such orderig is the case whe the list is already sorted is this a serious defect of QUICKSORT? Suppose that we start with a sorted list. The QUICKSORT selects the first elemet (which is the smallest), ad partitios the remaider ito the empty list ad all the other elemets (with 1 comparisos). Oe brach of the recursio is ( trivial, but i the other we agai have a sorted list, requirig (by iductio) 1 ) ( 2 comparisos. So ) 2 comparisos are eeded altogether. The sorted list is ot the oly permutatio requirig so may comparisos. As log as each elemet is either smaller tha or greater tha all its successors, the same ueve split will occur at eac stage. So there are 2 1 bad permutatios, sice there are two choices for each elemet except the last. 13
This is a problem, but i practice ot too serious. If the lists to be sorted are truly radom, the the proportio of bad permutatios decreases faster tha expoetially. If we are likely to meet sorted or partially sorted lists quite ofte, we ca modify the algorithm by choosig a to be the middle item of the list, rather tha the first. 22 Let m be the miimum umber of comparisos required by QUICKSORT to sort a list of legth. Prove that, for each iteger k > 1, m is a liear fuctio of o the iterval from 2 k 1 1 to 2 k 1, with m 2 k 1 = (k 2)2k + 2. If = 2 k 1, what ca you say about the umber of orderigs requirig m comparisos? The miimum umber of comparisos will occur whe the sublists are as early equal as possible: both of legth ( 1)/2 if is odd, or of legths ( 2)/2 ad /2 if is eve. So we have the recurrece { 1 + 2m( 1)/2, if is odd, m = 1 + m ( 2)/2 + m /2, if is eve. Suppose that m = a + b for c d. The, for 2c + 1 2d + 1, we have m = 1 + 2(a( 1)/2 + b) = (a + 1) + (2b a 1). But m is liear (i fact idetically zero) for 0 1; by iductio o k, it is liear o each iterval 2 k 1 1 2 k 1. If the value o this iterval is give by m = a k +b k, the we have a 0 = b 0 = 0, a k+1 = a k + 1, ad b k+1 = 2b k a k 1. By iductio, we fid that a k = k ad b k = 2 k+1 + k + 2. Settig = 2 k 1, we have as required. m 2 k 1 = k (2k 1) 2 k+1 + k + 2 = (k 2)2 k + 2, To cout the umber of orders which require the miimum umber of comparisos, ote that the first step i the algorithm splits the list ito sublists of legth i ad i 1, say; ow we require that each of these sublists ca be sorted with the miimum umber of comparisos, ad also that both i ad 1 i lie i a iterval i which m j is a liear fuctio of j, that is, oe of the form [2 d 1 1,2 d 1]. However, the two sublists ca be merged arbitrarily. So the umber f () of orders requirig the miimum umber of comparisos satisfies the recurrece ( ) 1 f () = f (i) f ( i 1), i 14
where the sum is over all i such that i ad i 1 lie i the same iterval of the form [2 d 1 1,2 d 1]. For example, if = 5 ad the list cotais 1,...,5 i some order, the it requires the miimum umber 6 of comparisos to sort if ad oly if oe of the followig holds: the first elemet is 3; the first elemet is 2, ad 4 precedes 3 ad 5; the first elemet is 4, ad 2 precedes 1 ad 3. There are 40 such lists, out of 120. As i the previous questio, the maximum umber of comparisos is 10, ad 16 lists require this umber. Check that the umbers requirig 7, 8, 9 comparisos are 32, 24, 8 respectively. Now check that the average agrees with the value calculated i the recurrece of Sectio 4.7. 15
23 This exercise justifies the Twety Questios priciple. We are give N objects ad required to distiguish them by askig questios, each of which has two possible aswers. The aim of this exercise is to show that, o matter what scheme of questioig is adopted, o average the umber of questios required is at least log 2 N. (For some schemes, the average may be much larger. If we ask Is it a 1?, Is it a 2?, etc., the o average (N + 1)/2 questios are eeded!) A biary tree is a graph (see Chapter 2) with the followig properties: there is a vertex (the root) lyig o just two edges; every other vertex lies o oe or three edges (ad is called a leaf or a iteral vertex accordigly); there are o circuits (closed paths of distict vertices), ad every vertex ca be reached by a path from the root. It is coveiet to arrage the vertices of the tree o successive levels, with the root o level 0. The ay o-leaf is joied to two successors o the ext level, ad every vertex except the root has oe predecessor. The height of a vertex is the umber of the level o which it lies. I our situatio, a vertex is ay set of objects which ca be distiguished by some sequece of questios. The root correspods to the whole set (before ay questios are asked), ad leaves are sigleto sets. The two successors of a vertex are the sets distiguished by the two possible aswers to the ext questio. The height of a leaf is the umber of questios required to idetify that object uiquely. STEP 1. Show that there are two leaves of maximal height (h, say) with the same predecessor. Deduce that, if there is a leaf of height less tha h 1, we ca fid aother biary tree with N leaves havig smaller average height. Hece coclude that, i a tree with miimum average height, every leaf has height m or m + 1, for some m. STEP 2. Sice there are o leaves at height less tha m, there are altogether 2 m vertices o level m. STEP 3. If there are p iteral vertices o level m, show that there are 2p leaves of height m + 1, ad N 2p = 2 m p of height m; so N = 2 m + p, where 0 p < 2 m. STEP 4. Prove that log 2 (2 m + p) m+2p/(2 m + p), ad deduce that the average height of leaves is at least log 2 N. 16
Follow the suggested proof. Step 1: If v is a vertex of maximum height ad w its predecessor, the the other successor v of w would also be a leaf (else its ow successors would be higher tha v). If there is a leaf x with height less tha h 1, the remove the two leaves v,v of height h ad add two ew successors y,y of x with height less tha h, reducig the average height without chagig the umber of leaves. So, i a tree of miimum height, o leaf has height less tha h 1 (where h is the maximum height). Put m = h 1. Step 2: If there are o leaves o level k, the each vertex o this level has two successors, ad so the umber of vertices o level k + 1 is twice the umber o level k. I our case, this holds for k = 0,1,...,m 1, so there are 2 m vertices o level m. Step 3: Suppose that there are p iteral vertices o level m. The there are 2p vertices o level m + 1, all of them leaves; ad there are 2 m p leaves o level m, givig 2 m + p altogether. Thus, if N is the umber of leaves, the m ad p are determied by N: 2 m is the largest power of 2 ot exceedig N, ad p = N = 2 m. (If N is a power of 2, we ca take it to be 2 m rather tha 2 m+1, to simplify thigs.) Step 4: The average height of the leaves is (2 m p)m + 2p(m + 1) 2 m + p = m + 2p 2 m + p. By calculus, show that log 2 (1 x) 2x for 0 x 2 1. (The two expressios are equal whe x = 0 ad whe x = 1 2 ; ad their differece has a uique statioary value i the iterval, at x = 1 1/(2log2), which is a miimum.) Apply this iequality with x = p/(2 m + p), so that m log 2 (1 x) = m + log 2 (1 + p/2 m ) = log 2 (2 m + p). Thus, log 2 N m + 2p/(2 m + p), ad we are doe. 24 Suppose that the two successors of each o-leaf ode i a biary tree are distiguished as left ad right. Show that, with this covetio, the umber of biary trees with leaves is the Catala umber C. Let T be the umber of biary trees with leaves, with the left-right distictio. The T 1 = 1 (the root is the uique leaf); ad, for > 1, a tree with leaves is specified by a left subtree with k leaves ad a right subtree with k leaves, where 1 k 1. Hece T = 1 k=1 T kt k for > 1. By (4.5.1) ad iductio, T = C for all. 17