Correctness of Horner’s Rule

The following code fragment implements Horner’s rule for evaluating a polynomial

given the coefficients $a_0, a_1, . . . , a_n$ and a value for $x$:

  1   y = 0
2   for i = n downto 0
3       y = a_i + x * y

1. In terms of $\Theta$-notation, what is the asymptotic running time of this code fragment for Horner’s rule?
2. Write pseudocode to implement the naive polynomial-evaluation algorithm that computes each term of the polynomial from scratch. What is the running time of this algorithm? How does it compare to Horner’s rule?
3. Consider the following loop invariant:. At the start of each iteration of the for loop of lines 2-3, $y = \sum_{k = 0}^{n − (i + 1)} a_{k + i + 1}x^k\\$ Interpret a summation with no terms as equaling 0. Your proof should follow the structure of the loop invariant proof presented in this chapter and should show that, at termination, $y = \sum_{k = 0}^n a_k x^k$.
4. Conclude by arguing that the given code fragment correctly evaluates a polynomial characterized by the coefficients $a_0, a_1, . . . , a_n$.

1. Asymptotic Running Time

From the pseudocode of Horner’s Rule, the algorithm runs in a loop for all the elements, i.e. it runs at $\Theta(n)$ time.

2. Comparison with Naive Algorithm

Pseudocode for NAIVE-POLY-EVAL(A, x), where $A$ is the array of length $n + 1$ consisting of the coefficients $a_0, a_1, . . . , a_n$.

y = 0
for i = 1 to A.length
m = 1
for j = 1 to i - 1
m = m * x
y = y + A[i] * m


The above algorithm runs with for inside another for loop $j$ multiplications to evaluate $a_j x^j$ and $(n - 1)$ additions in total to evaluate a polynomial. Hence, it does $\sum_{j = 0}^n j = n(n + 1)/2$ multiplications and $(n - 1)$ additions. Therefore, the algorithm runs at $\Theta(n^2)$ time.

This algorithm is obviously worse than Horner’s rule which runs at linear time.

3. Loop Invariant for the While Loop

Initialization: At the start of the first iteration, we have $i = n$. So,

As the sum is zero, the loop invariant holds after the first loop.

Maintenance: From the loop invariant, for any arbitrary $% $, at the start of the $i$-th iteration of the while loop of lines 3–5, $y = \sum_{k = 0}^{n − (i + 1)} a_{k + i + 1}x^k$

Now, after the $i$-th iteration,

So, the loop invariant also holds after the loop.

We make two assumption:

• $k' = k + 1$ : This is valid as $k$ is nothing but the summation parameter.
• $i' = i - 1$ : This holds as this is precisely the operation done in line 5.

Termination: When the loop terminates, we have $i = -1$. So,

This is precisely what we wanted to calculate.

4. Correctness Argument

When Horner’s rule terminates it successfully evaluates the polynomial as it intended to. This means the algorithm is correct.