0

I can't figure out the solution of this excercise:

Calculate the complexity of f(g(n))+g(f(n)) with g and f defined as follows:

int f(int x) {
 if (x<=1) return 2;
 int a = g(x) + 2*f(x/2);
 return 1+ x + 2*a;
}

int g(int x) {
int b=0;
 if (x<=1) return 5;
     for (int i=1, i<=x*x;i++)
         b+=i;
 return b + g(x-1);
}

could anyone explain me how to get to the solution?

9
  • Does your textbook or class material explain how to get the solution? Where are you stuck? Commented Nov 6, 2017 at 15:47
  • Complexity of g can simply be computed... Commented Nov 6, 2017 at 15:51
  • Better to write 2 * f(x/2) instead of f(x/2)+f(x/2). Commented Nov 6, 2017 at 15:52
  • @TypeIA I have to calculate the recurrence of time and result for both functions, but i don't know how. The time recurrence for f(x) is :kn^2 + 2Tf(n/2) (why?) so it's O(n^3) (why?). And result recurrence is kn^4 + 4Rf(n/4) (why?) so it's O(n^5) (why?). Does the same thing for the g function and then calculates f(g(n))+g(f(n)). The solution is O(n^15),but i can't figure out why. Commented Nov 6, 2017 at 16:05
  • by "complexity" do you mean that of the output or execution time? Commented Nov 6, 2017 at 18:44

1 Answer 1

3

There are two separate steps to solving this problem. Firstly we must look at the time complexity of each function, and then the output complexity.


Time complexity

Since g is self-contained, let's look at it first.

The work done in g consists of:

  • x^2 executions of a loop
  • A recursive call with parameter x - 1

Hence one might write the time complexity recurrence relation as (using upper-case to distinguish it from the original function)

enter image description here

To solve it, repeatedly self-substitute to give a summation. This is the sum of the squares of natural numbers from 6 to x:

enter image description here

Where in the last step we used a standard result. And thus, since a is a constant:

enter image description here

Next, f:

  • One call to g(x)
  • One recursive call with parameter x / 2
  • Some constant amount of work

enter image description here

Using a similar method:

enter image description here

Applying the stopping condition:

enter image description here

Thus:

enter image description here

Since the exponential term 2^(-x^3) vanishes:

enter image description here


Output complexity

This is basically the same process as above, with slightly different recursion relations. I'll skip the details and just state the results (using lower case for output functions):

enter image description here


Thus the final time complexity of f(g(n)) + g(f(n)) is:

enter image description here

Which matches the result given by your source.

Sign up to request clarification or add additional context in comments.

5 Comments

What does "output complexity" mean?
@ruakh yeah I should have clarified - the growth complexity of the functions' return value with respect to the input variable x.
Oh, I see. I think you've misunderstood the term "complexity". "Complexity" refers to the amount of resources needed for something. For example, we might say that an algorithm has a time complexity of 2‌𝑛 operations, where 𝑛 is the input. As you know, we typically talk about complexity in terms of asymptotic growth rates (by using Landau notation); but when we talk about other things in terms of asymptotic growth rates, we don't use the term "complexity" for that.
@ruakh thanks for the correction but I'm not sure I understand your explanation - what differentiates "complexity in terms of asymptotic growth rates" from "other things"? I've seen countless examples of "growth complexity" and "asymptotic growth rate" being interchangeably used, and also for all kinds of quantities, not just time.
There are many kinds of resource -- time, storage, logic gates, etc. -- and therefore many kinds of "complexity". But a function's return-value isn't a resource it consumes, so it doesn't make sense to say that (for example) the function 𝑓 : 𝑛 ↦ 2𝑛 has an "output complexity" of 2𝑛 or 𝑂(𝑛). Rather, we say that 𝑓 ∈ 𝑂(𝑛). If you've really seen "growth complexity" in reference to things other than complexity, then -- that's lame, but now you know better. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.