Logo Data Structures and Algorithms with Object-Oriented Design Patterns in Java
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. That is, given a value x and non-negative integer n, we wish to compute the tex2html_wrap_inline57762. A naıve way to calculate tex2html_wrap_inline57762 would be to use a loop such as

int result = 1;
for (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

  equation1044

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

displaymath57750

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

displaymath57751

which requires a total of eight multiplication operations.

A recursive algorithm to compute tex2html_wrap_inline57762 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.

   program1062
Program: Program to compute tex2html_wrap_inline57762.

 

 

time

statement

n=0 n>0 n>0
n is even n is odd
5 3 3 3
6 2 -- --
7 -- 5 5
8 -- tex2html_wrap_inline57804 --
10 -- -- tex2html_wrap_inline57806
TOTAL 5 tex2html_wrap_inline57810 tex2html_wrap_inline57812
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

  equation1085

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

For tex2html_wrap_inline57818, Equation gif gives

displaymath57752

This can be solved by repeated substitution:

eqnarray1095

The substitution stops when k=j. Thus,

eqnarray1101

Note that if tex2html_wrap_inline57818, then tex2html_wrap_inline57832. In this case, running time of Program gif is tex2html_wrap_inline57834.

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

eqnarray1104

Hence, tex2html_wrap_inline57846 is also odd!

For example, suppose n is 31 ( tex2html_wrap_inline57852). To compute tex2html_wrap_inline57776, Program gif calls itself recursively to compute tex2html_wrap_inline57856, tex2html_wrap_inline57858, tex2html_wrap_inline57860, tex2html_wrap_inline57862, and finally, tex2html_wrap_inline57864--all but the last of which are odd powers of x.

For tex2html_wrap_inline57838, Equation gif gives

displaymath57753

Solving this recurrence by repeated substitution we get

eqnarray1112

The substitution stops when k=j. Thus,

eqnarray1118

Note that if tex2html_wrap_inline57838, then tex2html_wrap_inline57874. In this case, running time of Program gif is tex2html_wrap_inline57876.

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

 

 

n tex2html_wrap_inline57886 powers computed recursively
1 1 tex2html_wrap_inline57888
2 2 tex2html_wrap_inline57890
3 2 tex2html_wrap_inline57892
4 3 tex2html_wrap_inline57894
5 3 tex2html_wrap_inline57896
6 3 tex2html_wrap_inline57898
7 3 tex2html_wrap_inline57900
8 4 tex2html_wrap_inline57902
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_inline57886. 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

  equation1142


next up previous contents index

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