ClassS04CS141/Notes 04 07

ClassS04CS141 | ClassS04CS141 | recent changes | Preferences

Showing revision 5

Introduction to Recurrence relations

Read section 5.2 in the text.

Recurrence relations are a tool for analyzing recursive algorithms.

As an example, recall our analysis of mergesort from lecture 1. We diagrammed a recursion tree for mergesort 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 holds 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 tree 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

As a class exercise, we derived the following recurrence for the number of single-disc moves to transfer N discs in the Towers of Hanoi problem:

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

The corresponding recursion tree 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.

See /ChallengeProblem2 .



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