RecurrenceRelations

ClassS04CS141 | recent changes | Preferences

Showing revision 2

Introduction

Recurrence relations are a tool for analyzing recursive algorithms.

MergeSort?

An example you may be familiar with is MergeSort? . A recursion diagram for mergesort looks like this:


upload:S014mergesorttree.tiff


Define T(n) to be the time taken to mergesort n elements (neglecting constant factors). Then T(n) satisfies the following:

T(1) = 1
T(n) = 2 T(n/2) + n (for n > 1)

The above relation between T(n) and T(n/2) is called a recurrence relation.

The relation is true because the time to mergesort an array with n elements equals the time to mergesort an array with n/2 elements, plus the time to mergesort another array with n/2 elements, plus n.

Binary Search

Similarly, a recurrence relation for the time to do a binary search on an array of N elements is:

T(1) = 1
T(N) = 1 + T(N/2).

That is, a binary search in an array of size N does constant work, then recurses on one of the two halves of the array (a problem of size N/2).

The recursion diagram in this case just looks like this:

      
     N
     |
    N/2
     |
    N/4
     |
    ...
     |
     4
     |
     2
     |
     1

In this case, the work associated with each node of the tree is 1, and the depth of the tree is log(N), so the total work done is O(log N).

Towers of Hanoi

The number of single-disc moves to transfer N discs in the TowersOfHanoi? problem satisfies:

T(N) = 2 T(N-1) + 1
T(1) = 1

The corresponding recursion diagram is a complete binary tree of depth N, and for each node, 1 disc is moved. Thus, the total number of moves made to transfer N discs is 2N-1.


References


ClassS04CS141 | recent changes | Preferences
This page is read-only | View other revisions | View current revision
Edited May 4, 2004 7:41 pm by Neal (diff)
Search: