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:
Define T(n) to be the time taken to mergesort n elements (neglecting constant factors). Then T(n) satisfies the following:
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.
Similarly, a recurrence relation for the time to do a binary search on an array of N elements is:
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).
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:
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 .