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)

    Some of the smartest women in the country said that they’re too embarrassed to attend their reunions at Harvard Business School if they have dropped out of the work force, left the fast track by choosing part-time work, or decided to follow anything other than the standard male career path.
    Deborah J. Swiss (20th century)

    Time, force, and death
    Do to this body what extremes you can,
    But the strong base and building of my love
    Is as the very centre of the earth,
    Drawing all things to it.
    William Shakespeare (1564–1616)

    The world men inhabit ... is rather bleak. It is a world full of doubt and confusion, where vulnerability must be hidden, not shared; where competition, not co-operation, is the order of the day; where men sacrifice the possibility of knowing their own children and sharing in their upbringing, for the sake of a job they may have chosen by chance, which may not suit them and which in many cases dominates their lives to the exclusion of much else.
    Anna Ford (b. 1943)