You are on page 1of 2



1y =0 2i =n 3 while i >= 0 do 4 y= ai + x*y 5 i =i - 1 6 return(y)

a. What is the asymptotic running time of horners rule. b. The naive polynomial-evaluation algorithm computes each term of the
polynomial from scratch. What is the asymptotic running time of this algorithm? c. Prove that the following is a loop invariant for the while loop in lines 3-5: y=sum(ak+i+1(x*)^k:) for k=0 to n-(i+1)

d. Conclude by arguing that the given code correctly evaluates

a polynomial characterized by the coefficients a0,a1 an at x*. SOLUTION: a. Line 3 is executed n + 1 times. Inside the while loop there are 2 arithmetic operations over R. Therefore the asymptotic running time is omega(n) b. The evaluation of (x)^i takes at most lg(i) arithmetic operations (Using repeated squaring), thus we have sum(lgi)=lg(!n) which belongs to O(nlgn) multiplications and n additions.,which is O(nlgn) . c. Initially i=n y=0 and the sum in the claim is empty as n-(i+1)=-1. Inductive hypothesis: the loop invariant holds when we enter j-th times the loop at line 3, i.e. i=n-j+1 and y=sum(ak+i+1(x*)^k.) for k=0 to n-(i+1) At the j+1th iteration we have i<-i-1=n-j, and y<-ai+ x*y where y on the right hand side is equal to sum(ak+i+1(x*)^k) for k=0 to n-(i+1) by induction. Therefore we have

Y=ai + x*(sum(ak+i+1(x*)^k)) = ai + sum(ak+i(x*)^k) for k=1 to n-1 = sum(ak+i(x*)^k) for k=0 to n Which proves the invariance. d. At termination we have i=-1 and by loop invariant(proved in part c) we have that we return y=sum(ak+i(x*)^k) for k=0 to n=P(x*)