2.12 problems - wmich.edu · end of the world, for if the monks manage to move one ring per second....

9
54 2 Algorithms and Complex 2.12 Problems Problem 2.1 Write an algorithm that, given a list of n numbers, returns the largest and small. numbers in the list. Estimate the running time of the algorithm. Can you desi an algorithm that performs only 3n/2 comparisons to find the smallest and larg: numbers in the list? Problem 2.2 Write two algorithms that iterate over every index from (0, 0, ... ,0) to (nl' n2, .... ,r. Make one algorithm recursive, and the other iterative. ~Problem 2.3 Is log n -: O(n)? Is log n = O(n)? Is log n = 8(n)? Problem 2.4 You are given an unsorted list of n- 1 distinct integers from the range 1 to n. WritE linear-time algorithm to find the missing integer. I Problem 2.5 Though FlBONACCI(n) is fast, it uses a fair amount of space to store the array. How much storage will it require to calculate the nth Fibonacci number? Modify tl algorithm to require a constant amount of storage, regardless of the value of n. ~Problem 2.6 Prove that r; = Jg(¢n - (pn) where F; is the nth Fibonacci number, ¢ = 1+2v's and ¢ = 1-2v's. Problem 2.7 Design an algorithm for computing the n-th Fibonacci number that requires less tha O(n) time. Hint: You probably want to use the result from problem 2.6. Howeve computing ¢n naively still requires O( n) time because each multiplication is a sing operation. ~roblem2.8 Propose a more realistic model of rabbit life (and death) that limits the life span of ral bits by k years. For example, if k = 2.,5, then the corresponding sequence 1, 1,2,3, grows more slowly than the Fibonacci seqence. Write a recurrence relation and psel docode to compute the number of rabbits under this model. Will the number of ral bits ever exceed the number of atoms in the universe (under these assumptions)? Problem 2.9 Write an iterative (i.e., nonrecursive) algorithm to solve the Hanoi Tower problem.

Upload: others

Post on 31-Oct-2019

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2.12 Problems - wmich.edu · end of the world, for if the monks manage to move one ring per second. working night and day without ever resting nor ever making a mistake. their work

54 2 Algorithms and Complex

2.12 Problems

Problem 2.1

Write an algorithm that, given a list of n numbers, returns the largest and small.numbers in the list. Estimate the running time of the algorithm. Can you desian algorithm that performs only 3n/2 comparisons to find the smallest and larg:numbers in the list?

Problem 2.2

Write two algorithms that iterate over every index from (0, 0, ... ,0) to (nl' n2, .... , r.Make one algorithm recursive, and the other iterative.

~Problem 2.3

Is log n -: O(n)? Is log n = O(n)? Is log n = 8(n)?

Problem 2.4

You are given an unsorted list of n - 1 distinct integers from the range 1 to n. WritElinear-time algorithm to find the missing integer. I

Problem 2.5

Though FlBONACCI(n) is fast, it uses a fair amount of space to store the array.How much storage will it require to calculate the nth Fibonacci number? Modify tlalgorithm to require a constant amount of storage, regardless of the value of n.

~Problem 2.6Prove that

r; = Jg(¢n - (pn)

where F; is the nth Fibonacci number, ¢ = 1+2v's and ¢ = 1-2v's.

Problem 2.7

Design an algorithm for computing the n-th Fibonacci number that requires less thaO(n) time. Hint: You probably want to use the result from problem 2.6. Howevecomputing ¢n naively still requires O(n) time because each multiplication is a singoperation.

~roblem2.8Propose a more realistic model of rabbit life (and death) that limits the life span of ralbits by k years. For example, if k = 2.,5, then the corresponding sequence 1, 1,2,3,grows more slowly than the Fibonacci seqence. Write a recurrence relation and pseldocode to compute the number of rabbits under this model. Will the number of ralbits ever exceed the number of atoms in the universe (under these assumptions)?

Problem 2.9

Write an iterative (i.e., nonrecursive) algorithm to solve the Hanoi Tower problem.

Page 2: 2.12 Problems - wmich.edu · end of the world, for if the monks manage to move one ring per second. working night and day without ever resting nor ever making a mistake. their work

2 Algorithms and Complexii

~roblem 2.17

There are n bacteria and 1 virus in a Petri dish. Within the first minute, the virus kilone bacterium and produces another copy of itself, and all of the remaining bacterireproduce, making 2 viruses and 2 . (n - 1) bacteria. In the second minute, each (the viruses kills a bacterium and produces a new copy of itself (resulting in 4 viruseand 2(2(n - 1) - 2) = 4n - 8 bacteria; again, the remaining bacteria reproduce. Th:process continues every minute. Will the viruses eventually kill all the bacteria?so, design an algorithm that computes how many steps it will take. How does thrunning time of your algorithm depend on n?

Problem 2.18

A very large bioinformatics department at a prominent university has a mix of 10professors: some are honest and hard-working, while others are deceitful arid dnot like students. The honest professors always tell the truth, but the deceitful onesometimes tell the truth and sometimes lie. You can ask any professors the followinquestion about any other professor: "Professor Y, is Professor X honest?" ProfesscY will answer with either "yes" or "no." Design an algorithm that, with no more tha198 questions, would allow you to figure out which of the 100 professors are hones(thus identifying possible research advisors). It is known that there are more honesthan dishonest professors.

Problem 2.19

You are given an 8 x 8 table of natural numbers. In anyone step, you can eithedouble each of the numbers in anyone row, or subtract 1 from each of the numberin anyone column. Devise an algorithm that transforms the original table into a tablof all zeros. What is the running time of your algorithm?

vP"roblem 2.20

There are n black, m green, and k brown chameleons on a deserted island. When tw.chameleons of different colors meet they both change their color to the third one (e.gblack and green chameleons become brown). For each choice of n,m, and k decidwhether it is possible that after some time all the chameleons on the island are thsame color (if you think that it is always possible, check the case n = I, m = 3, anik = 5).

Page 3: 2.12 Problems - wmich.edu · end of the world, for if the monks manage to move one ring per second. working night and day without ever resting nor ever making a mistake. their work

2.12 Problems 55

Problem 2.10Prove that L~=li = n(n~+l).

Problem 2.11Prove that "n 2i = 2n+1 - 2 and that ,,",n 2-i = 1 _ 2-n.Wt= 1 L.."t= 1

~blem2.12

We saw that BETTERCHANGE is an incorrect algorithm for the set of denominations(25,20,10,5,1). Add a new denomination to this set such that BETTER CHANGE willreturn the correct change combination for any value of M.

Problem 2.13

Design an algorithm that computes the average number of coins returned by the pro-gram USCHANGE(M) as M varies from 1 to 100. '

Problem 2.14

Given a set of arbitrary denominations c = (ci , C2, ... .ca). write an algorithm thatcan decide whether BETTERCHANGE is a correct algorithm when run on c. .'

~m2.15A king stands on the upper left square of the chessboard. Two players make turnsmoving the king either one square to the right or one square downward or one squarealong a diagonal in the southeast direction. The player who can place the king on thelower right square of the chessboard wins. Who will win? Describe the winningstrategy.

Problem 2.16

Boband Alice are bored one Saturday afternoon so they invent the following game.Initially, there are n rocks in a single pile. At each turn, one of the two players cansplit any pile of rocks that has more than 1 rock into two piles of arbitrary size suchthat the size of each of the two new piles must add up to the size of the original bigpile.No player can split a pile that has only a single rock, and the last person to movewins.Does one of the two players, first or second, have an advantage? Explain whichplayerwill win for each value of n.

Page 4: 2.12 Problems - wmich.edu · end of the world, for if the monks manage to move one ring per second. working night and day without ever resting nor ever making a mistake. their work

~

,

.•. ~."

L--

56 Analysis or-Algorithms

3~2

3.3~, .'

3.4

(lOg2n)O';0 (nb). .I

3.5.' '.•.:1 .' . "1. •• - • ",

Compare the following pairsbf functions in terms of order "of !11agnituq,e, In ,each case, saywhether fCn);:;O (g (n», nn) =0(8 (n», an~/or !(n)= 9(g (n».'.. ' ;."~'::

\: .. ~ :~ ..~ ~;L .' ~ .:, .:!:.•..,,; .~ '-'~ ( ) t I ". .". :f'''':. ~ g n ," '. 'i·

. I ••.., '.', .~ lr .• • • !".t.~

.. ~+(log~)2,,lOOn +logn.,

). ; i, ''', f.t 'I. ~ I" " I"

.{ t· log (n 2)

n2~ ~ ~~.t'~- L~" n (logni

logn

.' n(Jog n)log" -• ,';'1' ~.d.

logn I

,.(logn)Sn'h "e. . .

.' '.'-, 3" .;"f. n 2~ .,

Solve the following recurrence relation. Give an exact solution.!'

T(n) = T(n -1) + n12; T(I) = 1.

Solve the following recurrence relation. Give an exact solution.

T(n) = 8T(n -I) -15T(n -2); T(1)= 1; T(2)=4,

Prove that T (n), which is defined by the recurrence relation

T(n) = 2T(LnI2J) + 2n log2n, T(2)=4,

satisfies T(n) = 0 (n loin).

3.9 The following recurrence relation describes the running time of a recursive algorithm formatrix multiplication ([Pan 1978]). What is the asymptotic running time of this algorithm?

S.L~ ft.....r ~/L(~!) ~ @(~~~)

Page 5: 2.12 Problems - wmich.edu · end of the world, for if the monks manage to move one ring per second. working night and day without ever resting nor ever making a mistake. their work

64 Analysing the Efficiency of Algorithms Chap. 2

Example 2.2.11. The towers of Hanoi. It is said that after creating theworld, God set on Earth three rods made of diamond and 64 rings of gold. These ringsare all different in size. At the creation they were threaded on one of the rods in orderof size, the largest at the bottom and the smallest at the top. God also created amonastery close by the rods. The monks' task in life is to transfer all the rings ontoanother rod. The only operation permitted consists of moving a single ring from onerod to another, in such a way that no ring is ever placed on top of another smaller one.When the monks have finished their task, according to the legend, the world will cometo an end. This is probably the most reassuring prophecy ever made concerning theend of the world, for if the monks manage to move one ring per second. working nightand day without ever resting nor ever making a mistake. their work will still not befinished 500.000 million years after they began!

The problem can obviously be generalized to an arbitrary number of rings. Forexample, with n = 3, we obtain the solution given in Figure 2.2.1. To solve the gen-eral problem, we need only realize that to transfer the m smallest rings from rod i torod j (where I ~ i ~ 3, I ~ j ~ 3. i ~j , and m ~ I), we can first transfer the smallestm - I rings from rod i to rod 6 - i-j, next transfer the m th ring from rod i to rod j,and finally retransfer the m - I smallest rings from rod 6 - i-j to rod j. Here is aformal description of this algorithm; to solve the original instance, all you have to do(!) is to call it with the arguments (64. I, 2).

procedure Hanoi(m • i , i)(moves the m smallest rings from rod i to rod j )ifm > OthenHanoi(m-l.i.6-i-j)

write i •.~..jHanoi(m - I, 6 - i=j , i)

To analyse the execution time of this algorithm, let us see how often the instruc-tion write. which we use as a barometer, is executed. The answer is a function of m,which we denote e (m). We obtain the following recurrence :

II if m = Ie m =() 2e (m -I) + I if m > I,

from which we find that e(m) = 2m - I (see Example 2.3.4). The algorithm thereforetakes a time in the exact" order of 2" to solve the problem with n rings. 0

Problem 2.2.14. Prove that the algorithm of Example 2.2.11 is optimal in thesense that it is impossible with the given constraints to move n rings from one rod toanother in less than 2n

- I operations. 0

• Problem 2.2.15. Give a nonrecursive algorithm to solve this problem. (It ischeating simply to rewrite the above algorithm using an explicit stack to simulate therecursive calls.) 0

;

Page 6: 2.12 Problems - wmich.edu · end of the world, for if the monks manage to move one ring per second. working night and day without ever resting nor ever making a mistake. their work

Sec. 2.3 Solving Recurrences Using the Characteristic Equation 65

2.3 SOLVING RECURRENCES USINGTHE CHARACTERISTIC EQUATION

We have seen that the indispensable last step when analysing an algorithm is often tosolve a system of recurrences. With a little experience and intuition such recurrencescan often be solved by intelligent guesswork. This approach, which we do not illus-trate here, generally proceeds in four stages: calculate the first few values of therecurrence, look for regularity, guess a suitable general form, and finally, prove bymathematical induction that this form is correct. Fortunately there exists a techniquethat can be used to solve certain classes of recurrence almost automatically.

2.3.1 Homogeneous Recurrences

Our starting point is the resolution of homogeneous linear recurrences with constantcoefficients, that is, recurrences of the form

aO(n +al(n-I+ ... +ak(n-k =0 (*)I

where

i. the t, are the values we are looking for. The recurrence is linear because it doesnot contain terms of the form t, t,+j , (,1, and so on;

ii. the coefficients a, are constants; andiii. the recurrence is homogeneous because the linear combination of the t, is equal

to zero.

After a while intuition may suggest we look for a solution of the form

t; = x"

where x is a constant as yet unknown. If we try this solution in (*), we obtain

aoxn +alxn-I + ... +akXn-k =0.

This equation is satisfied if x = 0, a trivial solution of no interest, or else if

aoxk +alxk-I+ ... +ak =0.

This equation of degree k in x is called the characteristic equation of the recurrence(*).

Suppose for the time being that the k roots r I, r i •...• rk of this characteristicequation are all distinct (they could be complex numbers). It is then easy to verify thatany linear combination

k

(n = 1: c, rt,=1

Page 7: 2.12 Problems - wmich.edu · end of the world, for if the monks manage to move one ring per second. working night and day without ever resting nor ever making a mistake. their work

In - 3/,,_ I - 4/,,_2 = 0 n ~ 2

1

66 Analysing the Efficiency of Algorithms Chap. 2 J!~of terms r/' is a solution of the recurrence (*). where the k constants C I • C2 • . . .• Ct

are determined by the initial conditions. (We need exactly k initial conditions to deter-mine the values of these k constants.) The remarkable fact. which we do not provehere, is that' (*) has only solutions of this form.

Example 2.3.1. Consider the recurrence

subject to 10 = O. II = I.The characteristic equation of the recurrence is

x2 - 3x - 4 = 0

In = tl4" - (_I)n J. o

whose roots are -I and 4. The general solution therefore has the form

I" =CI(_l)n +C24n.

The initial conditions give

C I + C2 = 0

-(' I + 4('2 = I

n =0

n = I

that is. C I = - t. C2 = t·We finally obtain

Page 8: 2.12 Problems - wmich.edu · end of the world, for if the monks manage to move one ring per second. working night and day without ever resting nor ever making a mistake. their work

, n + 1 [' , n [ ,~ - I' [ ,: -4]]]'.'T(n):::;2+-- 2+-- 2+-- "'-• n n -I n -2 3,

=2[1+ n+1 + n+1 ~+ n+1 .s: n~1 + '" + ~+1 .s: n-l'''' 4J"n n n-l n n-l n-'-2 n n-l n-2 3

I . ....,

'" 2[1 n + 1 n + 1 n + 1 n ~ IJ=' +--+--+--+'" +-,-n , n-l n-2, ' 3"

, , [I 1 1 IJ" "=2(n+I)-,,-+-+-,-+ ... +~ ,t",.. n + 1 n n..:.l 3 ',,'"

~ .t

=2(n+I)(H(n+l)-1.5), ,I ": . ,.! ~.~.~••; t :," .

where H (n ) ~ 1+ 1/2 + 1/3 + . . . + 1/n is the Harmonic series. The Harmonic series hasa simple approximation, which we will not pro~e,' H (n) = In n +"f + 0 (l /n), where1=0.577 .. is Euler's constant. Hence, the solution for T(n) is

. I,. J, : ~ ~ ~, .

, T(n):::; 2(n + 1)(lnn +1-1.5) + 0 (1) = 0 (n logn).

3.6 Useful FactsI • <

, I

In this section, we present, without proof, several equalities and inequalities that areuseful in analyzing algQrithms.'

. Arithmetic series" '", ~•.-~<,.<. ,

i'

1 + 2 + 3 + ... + n = n (n +,1).. ,2

,(3.22)

More generally, if an =an-I +e, where e is a constant, then'. -, '.\

al +a2+a3 + '... + _ n(an+al)an--~-~2

,Geometric series

1 + 2 + 4 + ... + 2n = 2n+1 -I, (3.24)

More generally, if an =ean_l, where e;tl is a constant, thenen -I

al+a2+a3+ .. , +an=:=al--I-', e-

If 0 < e < 1, then the sum of the infinite geometric series is

(3.25)

Page 9: 2.12 Problems - wmich.edu · end of the world, for if the monks manage to move one ring per second. working night and day without ever resting nor ever making a mistake. their work

/

54 Analysis of Algorithms

al1:. a; = l-=C';=1

Sum of squares

i i2 = n (n + 1)(2n + 1)

1=1 ... . 6

(3.27):,. ~

Harmonic seriesn 1~,

H; = L - = Inn +r+ O(1/n),k=1 k

(3.28)

where r=0.577 .. is Euler's constant.

Basic rules involving logarithms .i·.•.

1···.•logba = IO&1b·. ~

I . '~. t··

(3.29)

; logsx .~.log,» = log,«

. (3.30)~!,

(3.31)b10gbK = x. '.

blO~ = x1ogab.

(3.32)

Sum of logarithms,.

iLIog2iJ =(n+1)Llog2nJ _2Llog,nJ+l +2=8(nlogn).

;=1 •

(3.33)

Bounding a summation by an integralIf f (x) is a monotonically increasing continuous function, then

n x=n+l

Lf (i) s J f(x)dx.;=1 x=l

(3.34)

./

Stirling's approximation

n!=·,.f2nn l:jn (1+0(1/n)).(3.35)

.•.