CSI 101 Discrete Structures Witer 01 Prof. Lucia Moura Uiversity of Ottawa Homework Assigmet #4 (100 poits, weight %) Due: Thursday, April, at 1:00pm (i lecture) Program verificatio, Recurrece Relatios 1. Cosider the followig program that computes quotiets ad remaiders: r a; q 0; while r d do begi r r d; q q + 1; ed Use the followig steps i order to verify that the program is correct with respect to the iitial assertio a ad d are positive itegers ad fial assertio q ad r are itegers such that a = dq + r ad 0 r < d. (a) Fid a appropriate loop ivariat that is strog eough to give the fial assertio, ad prove that it is a loop ivariat. (b) Usig part (a) ad other iferece rules for program verificatio, prove the program is partially correct with respect to the iitial ad fial assertios. (c) Complete a proof of correctess by formally provig the termiatio of the loop. (a) We claim that the loop ivariat we eed is the followig propositio p: p = a = qd + r ad r 0. To show that p is a loop ivariat, we must show that: i. p is true before the loop executes. Sice a is a positive iteger ad r a before the loop executes, we have that r 0. Sice q 0 before the loop executes, the qd + r = 0d + a = a. Thus, p is true before the loop executes. ii. If p is true before the loop is executed, the p is true after the loop executes. Assume that p is true before the loop is executed. The, after the loop executes, we have the ew values r = r d ad q = q + 1. We must show that p still holds with regards to these ew values. Sice, by 1
the coditio of the loop, r d, we have that r = r d d d = 0. Furthermore: a = qd+r = qd+r d+d = (qd+d)+(r d) = (q +1)d+(r d) = q d+r. Thus, p is still true after the loop executes. Therefore, p is a loop ivariat. (b) Let S deote the etire program, S 1 deote the two statemets before the while loop, ad S deote the statemets i the while block. If q is the predicate a ad d are positive itegers, ad t is the predicate q ad r are positive itegers such that a = dq + r ad 0 r < d, we show that q{s}t holds. This is equivalet to showig q{s 1 while r d{s }}t holds. We must the show that q{s 1 }p ad (p r d){s }p holds: this is true from the first part, where we showed that p is a loop ivariat. Thus, by the rules of iferece for while loops, we have that p{while r d{s }}(p (r d)). This implies that if the loop termiates, it does so with p true ad r d false, i.e. r < d, ad thus a = qd + r ad 0 r < d, which is precisely t. Thus, this is equivalet to p{while r d{s }}t holds. Sice q{s 1 }p holds, we ca combie these ad have that q{s 1 while r d{s }}t, or q{s}t, as required. (c) We show that the loop termiates evetually. Associate with each iteratio of the loop the value of r. Sice r is, by assumptio, a positive iteger, ad i every iteratio we decrease the value of r by d, the value of r forms a strictly decreasig sequece. Furthermore, sice the loop termiates whe r < d, we have that the value of r is bouded below by 0. Thus, by the well-orderig priciple, the loop must termiate i a fiite umber of iteratios.. (a) Fid the characteristic roots of the liear homogeeous recurrece relatio a = a 1 a. (Note these are complex umbers) (b) Fid the solutio of the recurrece relatio i part (a) with a 0 = 1 ad a 1 =. The relatio has characteristic equatio: r r + = 0. By usig the quadratic equatio, we have that: r = b ± b 4ac a = ± 4 8 Thus, the characteristic roots are 1 + i ad 1 i. This gives that the solutio to the relatio has form: = 1 ± i. a = α(1 + i) + β(1 i)
for some umbers α, β. We use the iitial values to determie α ad β: a 0 = 1 = α + β a 1 = = α(1 + i) + β(1 i) By substitutig β = 1 α ito the secod equatio, we derive: α(1 + i) + (1 α)(1 i) = iα = 1 + i α = 1 + i i = 1 + i i Thus: β = 1 α = 1 1 i Hece, the solutio to the recurrece relatio is: ( ) ( 1 i 1 + i a = (1 + i) + = 1 + i. i i = 1 i. ) (1 i). 3. Fid all solutios of the recurrece relatio a = 7a 1 16a + 1a 3 + 4 with a 0 =, a 1 = 0 ad a =. This is a ohomogeeous recurrece relatio, so we eed to fid the solutio to the associated homogeeous recurrece relatio ad a particular solutio to the origial relatio. The associated homogeeous recurrece relatio is: a (h) This has characteristic equatio: = 7a (h) 1 16a (h) + 1a (h) 3. r 3 7r + 16r 1 = 0 (r ) (r 3) = 0 Thus, the solutio to the homogeeous relatio is: a (h) = α + β + γ3 for some real umbers α, β, γ, which we will fid later via the iitial values after we have the geeral solutio to the full recurrece. We ow eed the particular solutio. We have that: F () = 4 3
This has polyomial part, so the degree of the polyomial part is t = 1. It has expoetial part 4, so s = 4. By S7. Theorem 6, the particular solutio thus has form: = (q + p)4 for some real umbers p, q. We fid the values of p ad q by substitutig the particular solutio ito the origial recurrece relatio: = 7 1 16 + 1 3 + 4 (q + p)4 = 7(q( 1) + p)4 1 16(q( ) + p)4 + 1(q( 3) + p)4 3 + 4 We ow divide the equatio by 4 3 to get: (q + p)4 3 = 7(q( 1) + p)4 16(q( ) + p)4 1 + 1(q( 3) + p) + 4 3. Multiplyig out ad simplifyig gives: (4q 64) + (4p 0q) = 0 = 0 + 0. This ca be separated ito two equatios by settig the coefficiets of the polyomials to be equal: 4q 64 = 0 4p 0q = 0 This has solutio p = 80, q = 16, so the particular solutio is: = (16 80)4. Thus, the format of the geeral solutio to the recurrece relatio is: a = a (h) Usig the iitial values, we have: + = α + β + γ3 + (16 80)4. a 0 = = α + γ 80 a 1 = 0 = α + β + 3γ + ( 64) 4 a = = 4α + 8β + 9γ + ( 48) 16 This gives a system of three liear equatios i three ukows, which has solutio α = 17, β = 39, γ = 61. Hece, the recurrece relatio has solutio: a = 17 + 39 + 61 3 + (16 80)4 = 17 + 39 1 + 61 3 + (16 80)4. 4
4. Cosider the followig recursive procedure to compute the fiboacci umbers: procedure FIB(: o-egative iteger) if = 0 the retur 0 else if = 1 the retur 1 else retur FIB( 1)+FIB( ) (a) Set up a recurrece relatio that couts the umber of times the sum (+) is executed cosiderig all the recursive calls used for iput. (Do t forget to provide iitial coditios as well) (b) Solve the recurrece relatio of part (a). Let a be the umber of sum operatios that are performed i calculatig the th fiboacci umber usig the recursive procedure. If = 0 or = 1, o sum operatios are performed, which gives the iitial coditios a 0 = a 1 = 0. For > 1, we have that the recursive procedure calculates the ( 1)th ad ( )th umber ad adds them together. Calculatig the ( 1)th umber requires a 1 sum operatios, ad calculatig the ( )th umber requires a of them. We the have oe more sum operatio to add the two umbers together, givig that: a = a 1 + a + 1. This is a ohomogeeous recurrece relatio. The associated homogeeous recurrece relatio is: = a (h) 1 + a (h) which has characteristic equatio: This equatio has roots: a (h) r r 1 = 0. r = b ± b 4ac a = 1 ±. Thus, the homogeeous relatio has solutio: ( a (h) 1 + ) ( 1 ) = α + β for values α, β that we will later derive from the iitial values. We ow eed to fid a particular solutio to the origial recurrece. Sice F () = 1, we have that the polyomial part is 1, so t = 0, ad the expoetial part is 1 = 1, so s = 1. Thus, the particular solutio has form: = (p)1 = p
for some value p. relatio: To fid p, we substitute the particular solutio ito the origial Thus, the geeral solutio is: a = a (h) + = α a = a 1 + a + 1 p = p + p + 1 p = 1 Usig the iitial values, we have that: ( 1 + ) ( 1 ) + β 1 a 0 = 0 = α + β 1 ( 1 + ) ( 1 ) a 1 = 0 = α + β 1 The solutio to this system of equatios is: α = + 10, β =. 10 Thus, the solutio to the recurrece relatio is: a = + ( 1 + ) + ( 1 ) 1. 10 10. Cosider the method by Karatsuba for multiplicatio of large itegers give below: procedure KMULT(A, B, : A ad B are itegers with bits) 1. If = 1 the retur A B;. else Write A = A h / + A l ad B = B h / + B l 3. Compute A = A h + A l ad B = B h + B l 4. C = KMULT(A, B, /). D h = KMULT(A h, B h, /) 6. D l = KMULT(A l, B l, /) 7. retur X = D h + [C D h D l ] / + D l (a) Based o the program we ca see that the umber of basic operatios for lie 1 is 1 ad the total umber of basic operatios for lies, 3 ad 7 is at most C for some costat C (sice the operatios are o umbers of at most bits). Write a recurrece relatio for T (), the umber of basic operatios used i all recursive calls for the cases i which is a power of (i.e. = k for some k). 6
(b) Use the master theorem (page 479) to fid a big-oh estimate for T (). We have that there are three recursive calls to KMULT with sequeces of about half the umber of the origial umber of bits, thus givig that the recurrece relatio is: ( ) T () = 3T + C. Additioally, T (1) = 1 sice whe = 1, we perform oe operatio (lie 1). This, however, is ot ecessary to apply the master theorem. Usig the master theorem, we have that a = 3, b =, ad d = 1. Thus, b d = 1 =, ad we have that a > b d. Hece, we are i the third case of the master theorem, which says that T () is O( log b a ) = O( log 3 ) = O( 1.8 ). 7