Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous index

Running Time of Divide-and-Conquer Algorithms

A number of divide-and-conquer algorithms are presented in the preceding sections. Because these algorithms have a similar form, the recurrences which give the running times of the algorithms are also similar in form. Table gif summarizes the running times of Programs gif, gif and gif.

 

 

program recurrence solution
Program gif T(n)=T(n/2)+O(1) tex2html_wrap_inline59347
Program gif T(n)=2T(n/2)+O(1) O(n)
Program gif T(n)=2T(n/2)+O(n) tex2html_wrap_inline59353
Table: Running times of divide-and-conquer algorithms.

In this section we develop a general recurrence that characterizes the running times of many divide-and-conquer algorithms. Consider the form of a divide-and-conquer algorithm to solve a given problem. Let n be a measure of the size of the problem. Since the divide-and-conquer paradigm is essentially recursive, there must be a base case. That is, there must be some value of n, say tex2html_wrap_inline58491, for which the solution to the problem is computed directly. We assume that the worst-case running time for the base case is bounded by a constant.

To solve an arbitrarily large problem using divide-and-conquer, the problem is divided into a number smaller problems, each of which is solved independently. Let a be the number of smaller problems to be solved ( tex2html_wrap_inline67995, tex2html_wrap_inline67997). The size of each of these problems is some fraction of the original problem, typically either tex2html_wrap_inline67999 or tex2html_wrap_inline68001 ( tex2html_wrap_inline68003, tex2html_wrap_inline68005).

The solution to the original problem is constructed from the solutions to the smaller problems. The running time required to do this depends on the problem to be solved. In this section we consider polynomial running times. That is, tex2html_wrap_inline60361 for some integer tex2html_wrap_inline60883.

For the assumptions stated above, the running time of a divide-and-conquer algorithm is given by

  equation32820

In order to make it easier to find the solution to Equation gif, we drop the tex2html_wrap_inline57621s as well as the tex2html_wrap_inline57945 from the recurrence. We can also assume (without loss of generality) that tex2html_wrap_inline58989. As a result, the recurrence becomes

displaymath67973

Finally, we assume that n is a power of b. That is, tex2html_wrap_inline68021 for some integer tex2html_wrap_inline68023. Consequently, the recurrence formula becomes

  equation32828

We solve Equation gif as follows. Divide both sizes of the recurrence by tex2html_wrap_inline68025 and then telescope :

   eqnarray32838

Adding Equation gif through Equation gif, substituting T(1)=1 and multiplying both sides by tex2html_wrap_inline68025 gives

  equation32869

In order to evaluate the summation in Equation gif we must consider three cases:




next up previous index

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