Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Example-Computing Powers

In this section we consider the running time to raise a number to a given integer power. I.e., given a value x and non-negative integer n, we wish to compute the tex2html_wrap_inline58739. A naıve way to calculate tex2html_wrap_inline58739 would be to use a loop such as

int result = 1;
for (unsigned int i = 0; i <= n; ++i)
    result *= x;
While this may be fine for small values of n, for large values of n the running time may become prohibitive. As an alternative, consider the following recursive definition

  equation975

For example, using Equation gif, we would determine tex2html_wrap_inline58751 as follows

displaymath58727

which requires a total of five multiplication operations. Similarly, we would compute tex2html_wrap_inline58753 as follows

displaymath58728

which requires a total of eight multiplication operations.

A recursive algorithm to compute tex2html_wrap_inline58739 based on the direct implementation of Equation gif is given in Program gif. Table gif gives the running time, as predicted by the simplified model, for each of the executable statements in Program gif.

   program993
Program: Program to compute tex2html_wrap_inline58739

 

 

time

statement

n=0 n>0 n>0
n is even n is odd
3 3 3 3
4 2 -- --
5 -- 5 5
6 -- tex2html_wrap_inline58781 --
8 -- -- tex2html_wrap_inline58783
TOTAL 5 tex2html_wrap_inline58787 tex2html_wrap_inline58789
Table: Computing the running time of Program gif

By summing the columns in Table gif we get the following recurrence for the running time of Program gif

  equation1015

As the first attempt at solving this recurrence, let us suppose that tex2html_wrap_inline58795 for some k>0. Clearly, since n is a power of two, it is even. Therefore, tex2html_wrap_inline58801.

For tex2html_wrap_inline58795, Equation gif gives

displaymath58729

This can be solved by repeated substitution:

eqnarray1025

The substitution stops when k=j. Thus,

eqnarray1031

Note that if tex2html_wrap_inline58795, then tex2html_wrap_inline58809. In this case, running time of Program gif is tex2html_wrap_inline58811.

The preceding result is, in fact, the best case--in all but the last two recursive calls of the function, n was even. Interestingly enough, there is a corresponding worst-case scenario. Suppose tex2html_wrap_inline58815 for some value of k>0. Clearly n is odd, since it is one less than tex2html_wrap_inline58821 which is a power of two and even. Now consider tex2html_wrap_inline58823:

eqnarray1034

Hence, tex2html_wrap_inline58823 is also odd!

For example, suppose n is 31 ( tex2html_wrap_inline58829). To compute tex2html_wrap_inline58753, Program gif calls itself recursively to compute tex2html_wrap_inline58833, tex2html_wrap_inline58835, tex2html_wrap_inline58837, tex2html_wrap_inline58839, and finally, tex2html_wrap_inline58841--all but the last of which are odd powers of x.

For tex2html_wrap_inline58815, Equation gif gives

displaymath58730

Solving this recurrence by repeated substitution we get

eqnarray1042

The substitution stops when k=j. Thus,

eqnarray1048

Note that if tex2html_wrap_inline58815, then tex2html_wrap_inline58851. In this case, running time of Program gif is tex2html_wrap_inline58853.

Consider now what happens for an arbitrary value of n. Table gif shows the recursive calls made by Program gif in computing tex2html_wrap_inline58739 for various values of n.

 

 

n tex2html_wrap_inline58863 powers computed recursively
1 1 tex2html_wrap_inline58865
2 2 tex2html_wrap_inline58867
3 2 tex2html_wrap_inline58869
4 3 tex2html_wrap_inline58871
5 3 tex2html_wrap_inline58873
6 3 tex2html_wrap_inline58875
7 3 tex2html_wrap_inline58877
8 4 tex2html_wrap_inline58879
Table: Recursive calls made in Program gif

By inspection we determine that the number of recursive calls made in which the second argument is non-zero is tex2html_wrap_inline58863. Furthermore, depending on whether the argument is odd or even, each of these calls contributes either 18 or 20 cycles. The pattern emerging in Table gif suggests that, on average just as many of the recursive calls result in an even number as result in an odd one. The final call (zero argument) adds another 5 cycles. So, on average, we can expect the running time of Program gif to be

  equation1072


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.