1

I'm taking introduction to Algorithms and trying to understand recursion trees a bit more (I'm using the CLRS textbook). Here is an example algorithm:

 T(n) = 3T(n/4) + cn^2

My understanding of this is that 3T(n/4) represents the number of recursive sub-problems and what they do. For this example algorithm each recursion divides the array (n/4). With 4 sub-problems as shown by the 4 in-front of the T. The cn^2 is the cost to perform that level of recursion in the tree.

So with all that in place the issue I'm having is when trying to build the recursion tree: enter image description here

Why in step c of this image do the children seem to inherit the cn^2 form?? I thought the cn^2 was just the cost to run that first step? The addition sign in the original T(n) equation is throwing me off, why is this represented as addition and then in the tree each step gets a cn^2 form applied? Wouldn't this be more like a multiply?

Looking for some intuitive answers.

sdub0800
  • 139
  • 1
  • 9

1 Answers1

0

Let's make a quick notational change. Instead of using T(n), let's call it T(k). That gives us the relation

T(k) = 3T(k / 4) + ck2.

The idea here is that T(k) means "the total amount of work done when evaluating the recursive call on an input of size k." If you initially kick off the recursion by calling the recursive function on an input of size n, then you'd be interested in evaluating T(n).

The reason I think it's helpful to change the variable name to k is to make clearer what's going on. T(k) is a general formula for the amount of work required to evaluate the recurrence on an input of size k. Here, k is a placeholder variable. You could ask what T(137) is to see how much work is done on an input of size 137, or you could ask what T(2m) is if you're curious how the work scales when the input is a perfect power of two.

Now, let's dissect what the formula says. The formula T(k) = 3T(k / 4) + ck2 says that the work required to compute the function on an input of size k is given by the following:

  • Some baseline amount of work that's done by the top-level recursive call. That amount is ck2.
  • Some amount of work required by the recursive calls. In particular, there's three recursive calls, each of which is made to an input one quarter the size of the original.

Let's try this on an example. What is T(16)? Well, that would be c(162) = 256c, plus 3T(16 / 4) = 3T(4). That is:

T(16) = 256c + 3T(4).

Now, what's T(4)? Well, that's c(42) = 16c, plus 3T(4 / 4) = 3T(1). Therefore, we have

T(4) = 16c + 3T(1),

and combining that with what we have above gives

T(16) = 256c + 3(16c + 3T(1)) = 256c + 48c + 9T(1) = 304c + 9T(1).

Do you see how, as we're working out each step, we're passing down a smaller value of k? That's what's going on as we evaluate the recurrence. Each individual value of T(k) always adds in some function of k, and the value of k it inherits is determined by the recurrence.

Hope this helps - let me know in the comments if there's anything else I can clarify!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065