Divide and Conquer Algorithm - Implementation Issues - Choosing The Base Cases

Choosing The Base Cases

In any recursive algorithm, there is considerable freedom in the choice of the base cases, the small subproblems that are solved directly in order to terminate the recursion.

Choosing the smallest or simplest possible base cases is more elegant and usually leads to simpler programs, because there are fewer cases to consider and they are easier to solve. For example, an FFT algorithm could stop the recursion when the input is a single sample, and the quicksort list-sorting algorithm could stop when the input is the empty list; in both examples there is only one base case to consider, and it requires no processing.

On the other hand, efficiency often improves if the recursion is stopped at relatively large base cases, and these are solved non-recursively. This strategy avoids the overhead of recursive calls that do little or no work, and may also allow the use of specialized non-recursive algorithms that, for those base cases, are more efficient than explicit recursion. Since a D&C algorithm eventually reduces each problem or sub-problem instance to a large number of base instances, these often dominate the overall cost of the algorithm, especially when the splitting/joining overhead is low. Note that these considerations do not depend on whether recursion is implemented by the compiler or by an explicit stack.

Thus, for example, many library implementations of quicksort will switch to a simple loop-based insertion sort (or similar) algorithm once the number of items to be sorted is sufficiently small. Note that, if the empty list were the only base case, sorting a list with n entries would entail n+1 quicksort calls that would do nothing but return immediately. Increasing the base cases to lists of size 2 or less will eliminate most of those do-nothing calls, and more generally a base case larger than 2 is typically used to reduce the fraction of time spent in function-call overhead or stack manipulation.

Alternatively, one can employ large base cases that still use a divide-and-conquer algorithm, but implement the algorithm for predetermined set of fixed sizes where the algorithm can be completely unrolled into code that has no recursion, loops, or conditionals (related to the technique of partial evaluation). For example, this approach is used in some efficient FFT implementations, where the base cases are unrolled implementations of divide-and-conquer FFT algorithms for a set of fixed sizes. Source code generation methods may be used to produce the large number of separate base cases desirable to implement this strategy efficiently.

The generalized version of this idea is known as recursion "unrolling" or "coarsening" and various techniques have been proposed for automating the procedure of enlarging the base case.

Read more about this topic:  Divide And Conquer Algorithm, Implementation Issues

Famous quotes containing the words choosing the, choosing, base and/or cases:

    Constantly choosing the lesser of two evils is still choosing evil.
    Jerry Garcia (1942–1995)

    Although I’ve risen! and my back is bold.
    My tongue is brainy, choosing from among
    Care, rage, surprise, despair, and choosing care.
    I’m semi-splendid within what I’ve defended.
    Gwendolyn Brooks (b. 1917)

    I think it is worse to be poor in mind than in purse, to be stunted and belittled in soul, made a coward, made a liar, made mean and slavish, accustomed to fawn and prevaricate, and “manage” by base arts a husband or a father,—I think this is worse than to be kicked with hobnailed shoes.
    Frances Power Cobbe (1822–1904)

    Medication alone is not to be relied on. In one half the cases medicine is not needed, or is worse than useless. Obedience to spiritual and physical laws—hygeine [sic] of the body, and hygeine of the spirit—is the surest warrant for health and happiness.
    Harriot K. Hunt (1805–1875)