Write recurrence relation of function

Richard picture Richard · Mar 5, 2012 · Viewed 12.6k times · Source

I know the formula for the recurrence relation is T(n)=aT(n/b)+f(n). And given that equation I know how to solve for the BigO. My homework question asked me to write a recursive function to count the number of nodes in a list, which I did but then asked me to write a recurrence relation. Here's my code:

int count(ListNode *l)
{
    if(!l) return 0;
    if(!l->next) return 1;

    return 1 + count(l->getNext());
}

But I am totally lost on how to create/formulate my own recurrence relation formula...how do I find a or b....I don't know where to begin. How do I write the recurrence relation for this function? Thank you.

Answer

ffriend picture ffriend · Mar 5, 2012

Your first recurrence relation is normally used to describe running time of divide-and-conquer algorithms. a here shows how many parts you are dividing your data to, 1/b shows what piece of original data is used in each part, and f(n) shows how much time you need on each "level". For example, in QuickSort algorithm you divide your data (array or list) into 2 parts, each of which is exactly half (1/2) of original data, and on each "level" of dividing you need to go through all n elements 1 time. So recurrence relation is

T(n) = 2T(n/2) + n

(which evaluates to O(n * lg(n))) And in binary search you divide data into 2 parts, but take only 1 of them, and time on each "level" is constant, so relation is:

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

However, your function in C doesn't follow divide-and-conquer strategy. Instead it demonstrates mathematical induction. You define start conditions: if l equals NULL, then length is 0, and if it l->next equals NULL (you also define condition for 1 element in list). Then you define a kind of inductive step - you define how to compute function for nth element, if you know function value for (n - 1)th element. So, knowing value of a function for 1st element you can apply this rule and get value for 2nd element, then for 3rd and so on.

So you can see that induction method is recurrence algorithm by the nature. Here we have 2 times to consider. First is a time to compute value at the start point (or end point - it depends on the order you are looking at the list). In your function you just return this value, so it is constant (C1). Second is a time to make 1 step. In your function it is also constant (C2). Intuitively you should see that execution time of this algorithm will be:

T(n) = C1, when n = 1 
T(n) = T(n - 1) + C2, when n > 1

Thus, unless n = 1, you define time to execute algorithm on n element through time to execute it on n - 1 elements. In BigO notation any constant is defined as 1 and case of 1 element is not considered, so your final recurrence relation is:

T(n) = T(n - 1) + 1

(which evaluates to T(n) = T(n - 1) + 1 = T(n - 2) + 2 = ... = 1 + n, or O(n))

Further reading: