An example of how to solve a basic recurrence

The time complexity of most recursive algorithms can be represented by a mathematical function called a recurrence. An example of such function is

T(n) = T(\frac{n}{2}) + 1

As you can see, solving for some n involves solving for \frac{n}{2} and so on and so forth. In order to stop from recursing forever, recurrences also usually have a base case, usually a constant. In this case I am going to omit the base case for simplicity.

Solving recurrences involves a sort of backwards induction technique. Step back for a moment and recall the definition of Big-oh:

f(n) = O(g(n)) \mbox{ if and only if } \exists c, n_0 \ni f(n) \leq c g(n), \forall n \geq n_0

That reads, f(n) = O(g(n)) if and only if there exists a c and n_0 such that for every n greater or equal to than n_0, f(n) is less than or equal to c g(n). Our job is to pick a c and n_0 that makes the condition true.

So, the first task is to pick a g(n), given the definition of T(n) above, we’ll guess that it is of order lg n. Now as I mentioned earlier, we will be doing induction, but backwards, so first let’s start with the inductive hypothesis:

T(k) \leq clgk, \forall k < n

There, that should do. Basically if we’re looking at solving this recurrence for n, we assume it works for every other number thus far. Now we go ahead and prove it for n.

Since \frac{n}{2} < n when n \geq 2, if set our n_0 \geq 2, then we can use this inequality in our proof. Notice that I did not set n_0 equal to 2! If we set our variables, we cannot unset them later on! Thus we simply provide a bound on n_0 instead.

From the inequality previously discussed, and our inductive hypothesis, we have T(\frac{n}{2}) \leq clg\left(\frac{n}{2}\right) , and by our definition of T(n), we now have

T(n) \leq clg\left(\frac{n}{2}\right) + 1

By getting rid of the recursive term, we have already made this recurrence one step easier to solve. Next, from the rules of logarithms, we transform the equation into

T(n) \leq c \left(lg n - lg 2\right) + 1 = c \left(lg n - 1\right) + 1 = clg n - c + 1

Notice here that if we pick out c to be anything greater than or equal to 1, the right hand side will become less than or equal to clgn, which is exactly what we want. Therefore set c = 1, and thus

T(n) \leq clgn

We have concluded the inductive step, and now all that remains is to show it works for the base case. Since our n_0 is 2, we must show that our choice of c and n_0 hold true for all inputs less than 2, so 1 in this case. However a problem may arise where some base cases may require a larger c value in order for the equation to hold true. Therefore we set c' to be the maximum of such c values, and our previously picked c, and by the transitive law of \leq, our final choice for c' will hold for all cases.

Therefore we can conclude that T(n) = O(lgn), where c = c' and n_0 = 2.

Notice that in the above example I was not as mathematically formal as I could have been. The above solution works, and is a pretty good outline of how to solve a generic recurrence. I hope this helps someone. :)

About Bryan St. Amour

Why yes! I do own this place!
This entry was posted in Algorithms, Math and tagged , , . Bookmark the permalink.

One Response to An example of how to solve a basic recurrence

  1. Anthony Aziz says:

    It’s fate. 60-100 to study for, and a post a recent topic?

    Too bad I’m not really studying :/

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>