Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
25
Добавлен:
20.03.2016
Размер:
445.06 Кб
Скачать

9.3 Exercises

1. Solve T (n) = T (n 1) + n for n 1 with T (0) = 2:

1. 2 +

n

k = 2 + n(n + 1)=2 = (n2 + n + 4)=2

 

 

 

Let n P

k=1

 

T

f

 

2

 

 

+ n + 4)=2g

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

= 0: Let

 

=

n

N : T (n) = (n2

 

 

 

 

 

 

(Base step) For n = 0 both T (0) = 2 and (02 + 0 + 4)=2 = 2.

 

(Inductive step) Choose n such that

n

 

n

0 and assume n

2 T : Now

 

2

 

prove that n + 1 2 T :

That is, T (n) = (n

 

+ n + 4)=2 is true and we must

 

2

+ (n + 1) + 4)=2 is true.

 

show T (n + 1) = ((n + 1)

 

 

T (n + 1) = T (n) + n + 1

=(n2 + n + 4)=2 + n + 1 (by ind. hyp.)

=(n2 + 3n + 6)=2

=((n + 1)2 + (n + 1) + 4)=2

Therefore, n + 1 2 T .

By the Principle of Mathematical Induction. T = N.

3. Solve T (n) = T (n 1) + 2n + 1 for n 1 with T (0) = 1:

3.

n

(2

k + 1) = 1 + n2

 

 

k=0

: T (n) = 1 + n

g

P

0 = 0: Let T = fn 2

 

Let n

 

 

N

2

 

 

 

 

 

(Base step) For n = 0 both T (0) = 1 and 02 + 1 = 1.

(Inductive step) Choose n such that n n0 and assume n 2 T : Now prove that n + 1 2 T : That is, T (n) = n2 + 1 is true and we must show T (n + 1) = (n + 1)2 + 1 is true.

T (n + 1) = T (n) + 2n + 1

=n2 + 1 + 2n 1 (by ind. hyp.)

=n2 + 2n + 1 1

=(n + 1)2 + 1

Therefore, n + 1 2 T .

By the Principle of Mathematical Induction, T = N.

5. Solve T (n) = T (n 1) + 2 for n 1 with T (0) = 2:

n

 

5. 2 + Pk=1

2 = 2n + 2

Let n0 = 0: Let T = fn 2 N : T (n) = 2n + 2g

(Base step) For n = 0 both T (0) = 2 and 2 0 + 2 = 2.

(Inductive step) Choose n such that n n0 and assume n 2 T : Now prove that n + 1 2 T : That is, T (n) = 2n + 2 is true and we must show T (n + 1) = 2(n + 1) + 2 is true.

T (n + 1) = T (n) + 2

=2n + 2 + 2 (by ind. hyp.)

=2(n + 1) + 2

Therefore, n + 1 2 T .

By the Principle of Mathematical Induction, and T = N.

7. Solve T (n + 1) = T (n) + 1 for n 1 with T (0) = 1:

n

 

 

 

 

 

 

 

 

 

 

 

7. 1 + Pk=0( 1)n = (1 ( 1)n+1)=2:

 

 

 

 

 

 

 

 

 

Let n0 = 0: Let T = fn 2 N : T (n) = (1 ( 1)n+1)=2g

 

 

(Base step) For n = 0 both T (0) = 1 and (1 ( 1))=2 = 1.

 

 

(Inductive step) Choose n such that n n0

and assume n

2 T

: Now

 

1)

n+1

 

 

prove that n + 1 2 T : That is,

T (n) = (1

 

(

 

 

)=2 is true and we

n+2

 

 

 

 

must show T (n + 1) = (1 ( 1)

 

)=2 is true.

 

 

 

 

 

 

T (n + 1) = T (n) + 1

=(1 ( 1)n+1=2 + 1 (by ind. hyp.)

=( 1 + 2 + ( 1)n+1)=2

=(1 + ( 1)( 1)n+1)=2

=(1 + ( 1)n+2)=2

Therefore, n + 1 2 T .

By the Principle of Mathematical Induction, T = N.

9. Solve T (n) = T (n 1) + n for n 2 with T (1) = 1:

Pn

9. 1 + k=1 k = 1 + n(n + 1)=2

Let n0 = 1: Let T = fn 2 N : T (n) = n(n + 1)=2g

(Base step) For n = 1 both T (1) = 1 and 1(1 + 1)=2 = 1.

(Inductive step) Choose n such that n n0 and assume n 2 T : Now prove that n + 1 2 T : That is, T (n) = n(n + 1)=2 is true and we must show T (n + 1) = (n + 1)(n + 2)=2 is true.

T (n + 1) = T (n) + n + 1

=n(n + 1)=2 + n + 1 (by ind. hyp.)

=(n2 + n + 2n + 2)=2

=(n2 + 3n + 2)

=(n + 1)(n + 2)=2

Therefore, n + 1 2 T .

By the Principle of Mathematical Induction, T = n 2 N : n 1.

11. Solve T (n) = T (n 1) + n3 for n 1 with T (0) = 0:

11.

n

k3 = (n(n + 1)=2)2

 

 

g

P

k=1

 

T

f

2

 

 

 

 

 

 

 

 

Let n0 = 0: Let

 

=

n

N : T (n) = (n(n + 1)=2)2

 

(Base step) For n = 0 both T (0) = 0 and (0(0 + 1)=2)2 = 0.

(Inductive step) Choose n such that n n0 and assume n 2 T : Now prove that n + 1 2 T : That is, T (n) = (n(n + 1)=2)2 is true and we must show T (n + 1) = ((n + 1)(n + 2)=2)2 is true.

T (n + 1) = T (n) + n3

=(n(n + 1)=2)2 + (n + 1)3 (by ind. hyp.)

=(n2(n + 1)2 + 4(n + 1)3)=4

=(n + 1)2(n2 + 4n + 4))=4

=(n + 1)2(n + 2)2=4

Therefore, n + 1 2 T .

By the Principle of Mathematical Induction, T = N.

13. Solve T (n) = T (n 1) + 2 for n 1 with T(0) = 1.

0

P

n

T

f 2

 

g

 

 

13. 1 + k=1 2 = 2n + 1

 

 

Let n

= 0: Let

=

n

N : T (n) = 2n + 1

 

(Base step) For n = 0 both T (0) = 1 and 2 0 + 1 = 1.

(Inductive step) Choose n such that n n0 and assume n 2 T : Now prove that n + 1 2 T : That is, T (n) = 2n + 1 is true and we must show T (n + 1) = 2(n + 1) + 1 is true.

T (n + 1) = T (n) + 2

=2n + 1 + 2 (by ind. hyp.)

=2(n + 1) + 1

Therefore, n + 1 2 T .

By the Principle of Mathematical Induction, T = N.

15.Find and solve the recurrence relation for the number of ways to arrange n distinct objects in a row.

15.

 

1

 

n = 0

T (n) =

nT (n

 

1) n 1

17. How many strings of length n formed using the alphabet f0,1,2,3g have an even number of zeros?

17. T (n) = 3T (n 1) + 4n 1 T (n 1). Any string that satis es the criterion and has length n 1 can be extended by any of the three nonzero symbols to become a valid string of length n. Any string of length n 1 that does no satisfy the criterion can be extended by appending a zero and become a valid string of length n.

19. The population of a fruit y colony doubles every day. If a colony of fruities has 100 members at the start of an experiment, how many fruit ies will be present after n days? If the population triples every day, how many fruit ies will be present after n days?

19.

2T (n 1) n 1

T2(n) =

 

100

n = 0

T3(n) =

3T (n 1) n 1

 

100

n = 0

The answers are T2(n) = 100 2n and T3(n) = 100 3n.

21.Find the general solution for a rst order homogeneous recurrence relation with constant coe cients. Is the restriction that the coe cients be constant necessary?

21. T (n) = aT (n 1) + b with T (0) = c has as a solution anT (0) +

bn 1 ai: i=0

23.Let A be an array with N elements for some positive integer N . SelectionSort sorts the elements of A in decreasing order by swapping the largest element in A[1]; A[2]; A[3]; : : : ; A[S 1] with A[S] where S ranges from N

to 1. Find and solve a recurrence relation that describes the complexity of SelectionSort: Let comparison of two elements be the key operation. Assume that the minimum operation on a set of size M requires M 1 comparisons for any M 2 N.

ALGORITHM: Selection Sort

INPUT: A list of distinct values List [1]; : : : ; List[N ] OUTPUT: List [1]; : : : ; List [N ] with values in increasing order

SelectionSort( List ; N ) /* Initial call */

SelectionSort ( List, M) /* Recursive procedure */ if (M = 1) then

return

else

S = index of maximum element of List[1..M]

Temp = List [S]

List[S] = List[M ]

List [M] = Temp SelectionSort (A, M - 1)

23. Solve T (n) = n 1 + T (n 1) where T (1) = 0: We have T (n) = n (n 1)=2:

25.Write a procedure to solve the general n-disk Tower of Hanoi problem using the following idea. Number the disks from smallest to largest starting at 1. Consider the towers as being in a triangular formation with the pegs numbered counterclockwise. Describe the disk moved and the direction it should be moved where XC means move the top disk on peg X in a clockwise direction to the next peg and XA means to move the top disk on peg X in a counterclockwise direction. For example, the following moves solve the problem for n = 3:

1C 2A 1C 3C 1C 2A 1C

Draw pictures of the three towers and how the disks are positioned for each of the moves indicated.

25. Something to think about.

9.5 Exercises

Solve Exercises 1 through 5 using the characteristic equation method.

1.an = 2an 1 + 3an 2 for n 2 where a0 = 2 and a1 = 2. 1.

x2 2x 3

=

0

(x 3)(x + 1)

=

0

x

= 3; 1

an

= A3n + B( 1)n

2= A + B

2= 3A B A = 1

B = 1

an = 3n + ( 1)n

3. an + 8an 1 + 12an 2 = 0 for n 2 where a0 = 2 and a1 = 6.

3.

 

 

x2 + 8x + 12

=

0

(x + 6)(x + 2)

=

0

x

= 6; 2

an

= A( 6)n + B( 2)n

2

=

A + B

6

= 3A B

A = 1=2

B

= 3=2

an

=

(1=2)( 6)n (3=2)( 2)n

5. an = 2an 1 + 15an 2 for n 2 where a0 = 3 and a1 = 1.

5.

x2 2x 15

=

0

(x 5)(x + 3)

=

0

 

x

= 5; 3

 

an

= A5n + B( 3)n

3

=

A + B

1

=

5A 3B

A

=

1

 

B

=

2

 

an

=

5n + 2( 3)n

7.Let m; n 2 N: Using m colors, nd and solve a recurrence relation that gives the number of ways to color a disk with n sectors such that no pair of neighboring sectors receive the same color.

7. The recurrence relation will involve both the number of paints and the number of regions. Consider the number of paints as a constant. If therst and the third regions have the same color, then we could remove the second region and put the rst and the third regions together to get an n 2 region disk to color. After coloring the n 2 region disk, the second region can be colored in any one of the p 1 colors remaining. This gives

the second term of the recurrence we are looking for. The rst term of the recurrence comes from choosing any of the n 2 colors not used for the rst and the third region after coloring the n 1 region disk as if the second region were not there. Thus the recurrence relation to solve is

T (n) = (p 2)T (n 1) + (p 1)T (n 2)

p is the number of colors. The answer is

rn = (p 1)((p 1)n 1 + ( 1)n)

9.For students who have been introduced to the Backus-Naur forms, how many valid expressions can be formed using exactly k symbols from the set f0,1,2,3,4,5,6,7,8,9,+,-,/,*g, each with whatever repetition is desired. The normal form for an Algol expression is

<expression >::=< expression >< digit > j

<expression >< sign >< digit >

9. In order to illustrate a method, we consider how many valid Algol expressions can be formed using exactly k symbols drawn from the set f0,1,2,3,4,5,6,7,8,9,+,-,/,*g, each with whatever repetition is desired.

The number of expressions that we have to nd clearly depends upon the syntax of Algol and the set of symbols we are allowed to use, but more signi cantly for our start upon the problem, it depends upon the number, k of symbols in the expression. It is reasonable, therefore, for us to let the number we require be uk and to attempt to derive a relationship between uk and uk 1; uk 2; : : : In order to do so we must now use a knowledge of the syntax and the symbols available. An expression cannot end with an arithmetic sign and so all the valid arrangements of the symbols must have a digit at the end. The last symbol but one must therefore either be a digit, in which case the rst k 1 symbols form a valid Algol expression, or that symbol must be one of the four arithmetic signs in which case therst k 2 symbols must form a valid Algol expression. In BNF notation this part of the syntax can be written

< expression > ::= < expressiion >< digit > j

< expression >< sign >< digit >

Thus the expressions using just k symbols taken from the set of those permitted can be divided into two mutually exclusive and exhaustive classes, namely those with a digit in the penultimate position and those with a sign there. The number in the rst class must be 10uk 1 since there are ten di erent digits which may be append3ed to a valid expression of k 1 symbols. The second class has 40uk 2 since each one of the four signs may be followed by any one of the ten digits and both appended to a valid expression of k 2 symbols. Hence we have the recurrence relation

uk = 10uk 1 + 40uk 2

This second order linear di erence equation, valid for k 2, needs two boundary condition for its solution. The number of single symbol expressions is just the number of di erent digits, hence u1 = 10: Similarly the number of two symbol expressions is 120, being formed of the 100 pairs of digits (00,01,. . . , 99) and 20 others formed by a digit following one of the signs + or -.

The equation can be solved in the usual manner by obtaining the roots of

the initial equation

m2 10m 430 = 0 p

If the roots ; so that ; = 5 65; we have

uk = A + B

where

10 = A + B

120 = A 2 + B 2

from the initial conditions. The solution of these simultaneous linear equation gives

A =

120 10

; B =

120 10

( )

( )

 

 

These expressions and that for uk can of course be expressed in terms of the appropriate surds, but in this and in similar problems it is wise to defer the manipulation of the surds until as late as possible in order to reduce algebra { in some cases that labour and the unattractive complications of the algebra may be avoided.

11.Let f (n) be the number of strings of n symbols formed using the symbols 0, 1, and 2 such that no two consecutive 0's occur. Show that f (n) = 2f (n 1) + 2f (n 2): Solve the recurrence, and enumerate all such strings of length four.

11. Each string of n 1 can be made into a string of n by adding 1 or 2 to the front. The question is how many n 1 strings can have a zero added to make the string an n string. We need all the n 2 strings that were not extended by a zero to become an n 1 string{there are 2 f (n 2) such strings.

p

 

 

p

 

 

n

 

 

p

 

 

p

 

 

n

 

 

+ (1=2

1=

3)(1

 

f (n) = (2 + 1= 3)(1 +

3)

 

 

3)

 

9.11 Exercises

1. Show that the recurrence relation an = an 1=(1 + an 1), with a0 = C has the function S(n) = C=(1 + Cn) as a solution.

1. S(0) = C=(1 + C 0) = C = a0: Suppose true for S(n) and show it is true for S(n + 1):

S(n+1) = C=(1+C(n+1)) = C=(1+Cn)=(1+C=(1+Cn)) = an=(1+an) = an+1

3.Solve these recurrences using back substitution. Verify the solutions are correct by induction.

(a)

an =

3a

n=2 + 4

 

for k > 0;

n

= 2k

7

 

for

n = 1

(b)

 

 

 

 

 

 

 

 

 

 

 

 

 

an =

5a

 

 

 

 

 

 

 

for k > 0;

n = 5k

12n=5 + 7

 

for

n = 1

(c)

 

 

 

 

 

 

 

 

 

 

 

 

 

an =

2a

n=3

+ 5

 

for k > 0;

n = 3k

7

 

 

 

for

n = 1

(d)

 

 

 

 

 

 

 

 

 

 

 

 

 

an =

7a

n=4

+ 3

 

for k > 0;

n = 4k

1

 

 

 

for

n = 1

3. (a)

 

 

 

 

 

 

 

 

 

 

 

 

 

an

=

3ka1 + 4(3k 1 + + 3 + 1)

 

=

7

 

3k + 4

(3k 1)

 

 

 

 

(3 1)

 

 

 

 

 

 

 

 

 

k+1

 

 

 

 

 

 

 

 

3

4

 

 

 

 

=

6

 

 

 

 

 

 

2

 

 

 

 

 

 

 

3. (b)

 

 

 

 

 

 

 

 

 

 

 

 

 

an =

5an=5 + 7

 

 

 

 

 

=

5k 12 + 7(5k 1 + 5k 2 + + 5 + 1)

= 11 5k+1 7 4

3. (c)

an = 2an=3 + 5

=2ka1 + 5(1 + 2 + 22 + + 2k 1)

=12 2k 5

3. (d)

an = 7an=4 + 3

= 7k a1 + 3(7k 1 + + 72 + 1)

= 9 7k 3 6

Соседние файлы в папке Student Solutions Manual