1
$\begingroup$

Any help will be appreciated, as I wasn't able to find much about it online in the last few days and I can't seem to write a suitable recurrence relation for this kind of functions..

Are there any tips or general guidelines for calculating the complexity (time/space) for functions of the following form?

int f1(int n) { if(n < k) // for an arbitrary k return 1; return f1(f1(n/2) + f1(n/3) - f1(n/5)); } 

Or:

int f2(int n) { if(n < k) return k; return f2(g(n/k)); } int g(int n) { // Some code of known complexity here to calculate an arbitrary c return f2(n/c); } 

Specifically, recursive functions that call themselves with their own return value as a parameter (e.g func(func(value)); )

Thank you in advance for any assistance provided.

$\endgroup$
0

3 Answers 3

0
$\begingroup$

I cant think of one simply way to solve such questions, but I can show you a way to start tackling the questions and making them simpler.

In such cases, that $f$ recursively calls itself with their own return value, the running time of $f$ might depend heavily on its output. So contrary to usual complexity computation tricks, here we want to know precisely what the output of $f$ is. For example, we can prove by induction that f1 will always output 1.

Now, replace each inner recursive call to $f$ with the value it returns. In the f1 case, we replace f1(f1(n/2) + f1(n/3) - f1(n/5)) by f1(1+1-1) which is simplified to f1(1). This means that we did a total of 4 calls to $f$, and for each call to $f$ we know exactly what the input was. Here, in this example, the inputs are $n/2,n/3,n/5,1$.

Finally, we can start calculating the time complexity: since we know how many recursive calls happened and what input was there for each recursive call, we can build a recursive formula for the time complexity. In the case of f1, it will be: $T(n)=T(n/2)+T(n/3)+T(n/5)+T(1)+c$, since we add a recursive call with the input of f1 for each time f1 was invoked.

This can be solved using traditional techniques, such as the master theorem.

$\endgroup$
4
  • $\begingroup$ That was a nice explanation, תודה $\endgroup$ Commented Feb 21, 2021 at 15:13
  • $\begingroup$ @DolevBaron: maybe nice, but wrong. The substitution by $f_1(1+1-1)$ does not work when $n\ge k$. $\endgroup$ Commented Nov 14, 2022 at 11:37
  • $\begingroup$ But it does (assuming k ≥ 2). If n < k then the result is 1. If n < 2k then we make three calls with n < k, each returns 1. If n < 4k then we make three calls with n < 2k, each returns 1, and so on. $\endgroup$ Commented Nov 14, 2022 at 12:29
  • $\begingroup$ @gnasher729: do you mean that $f_1(n)$ is identically $1$ ? $\endgroup$ Commented Nov 14, 2022 at 15:14
0
$\begingroup$

If k ≤ 1 then we are stuck; there will be infinite recursion unless the input is n = 0 for k = 0.

Otherwise: The result is 1 if $n < k \cdot 2^m$, for every m ≥ 0. Proof by induction: It is obviously true if m = 0. Assume it is true for all m' ≤ m: Let $n < k \cdot 2^{m+1}$. If $n < k \cdot 2^m$ then the result is 1 by induction. Otherwise we calculate f(n/2), f(n/3), f(n/5), each argument less than $k \cdot 2^m$, so the three results are 1, and the final result is 1.

With the result always being 1, we can try to find the complexity using the master theorem. It will be proportional to $n^{1/2 + 1/3 + 1/5}$ = $n \cdot n^{1/30}$

$\endgroup$
0
$\begingroup$

For your $f_1$, you need to solve the recurrence

$$T(n)=\begin{cases}n<k\to T(1)\\n\ge k\to T(f_1(\frac n2) + f_1(\frac n3) - f_1(\frac n5))+T(\frac n2)+T(\frac n3)+T(\frac n5).\end{cases}$$

This mandates the resolution of the recurrence on $f_1$ itself, probably not the easiest thing.


Your second example is contrived. "$\frac nc$ for some code" could just be written $g(n)$, and $$n\ge k\to T(n)=T(g(n))+U(n)+c$$ where $U$ is the "known complexity".


Update:

As shown by others, one can check that $f_1(n)=1$ for all $n$. So the recursion simplifies to

$$T(n)=\begin{cases}n<k\to T(1)\\n\ge k\to T(1)+T(\frac n2)+T(\frac n3)+T(\frac n5).\end{cases}$$

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.