# Calculating the runtime for a recursive algorithm [duplicate]

If the runtime of a recursive algorithm could be expressed as

$T(n) = \begin{cases}O(1) & n \leq c \\ k * T\left(\frac{n}{k}\right) + \left(k + n * k \right)\end{cases}$

what would be the result of the asymptotic analysis (the Landau class)? $k \in \mathbb{N}, k \neq 0$ is a constant value.

I would say that $k$ even if it is larger than $n$ is still a constant and therefore $T(n) = O(1) * T\left(\frac{n}{k}\right) + O(n) = T\left(\frac{n}{k}\right) * n$ for $n > c$.

So I would say that the algorithm is $O(n * log_k(n))$. Is this right? How could I prove this (formally correct)?

I have two main problems with this algorithm:

1) What is the runtime of $f(n) = (k + n * k)$? Can the $k$ be ignored so the runtime is $O(n)$?

2) I would like to use the Master theorem. I could see $T(n)$ in the form $a \cdot T\left(\frac{n}{b}\right) + f(n)$ with $a = b = k$. But then I need to check if $f(n)$ is $\in O(log_b a) = O(log_1 1) = ?$ This logarithmus is undefined, so can't I use the Master theorem?

## marked as duplicate by FrankW, Yuval Filmus, David Richerby, D.W.♦, Luke MathiesonNov 5 '14 at 1:06

• @FrankW I added some infos why I asked the question and what problems occurred while trying to solve it – muffel Nov 1 '14 at 18:33
• I calculate $\log_kk = 1$. Also, $f(n)$ doesn't have a "runtime", since it's not an algorithm. It's just a function. If $k > 0$ is fixed then $f(n) = \Theta(n)$. – Yuval Filmus Nov 1 '14 at 19:04

Applying the master theorem will give you $T(n) = O_k(n\log n)$, but the interesting part is the dependence on $k$. For simplicity, let's assume that $n$ is a power of $k$, and that instead of $k+nk = k(n+1)$ you have the cleaner $kn$ (it's not going to change the answer appreciably). Then you have $$T(n) = kn + kT(n/k) = kn + kn + k^2T(n/k^2) = \cdots = kn\log_k n + nT(1).$$ Assuming, for example, that $T(1) = 0$, then we get $$T(n) = \frac{k}{\log k} n\log n.$$ For general $n$ you probably get $T(n) = \frac{k}{\log k} n\log n (1 \pm o(1))$ if you sweat. This gives you the dependence on $k$.
Since $k/\log k$ is increasing for $k > e$, in this scenario the best integral choice for $k$ would be either $k = 2$ or $k = 3$ (as it happens, $k = 3$ is better), which is probably the point of the exercise.
• thank you for your great answer. Could you please add which bases the logarithms in $T(n) = \frac{k}{\log k} n\log n$ have and what you did for getting this formula? – muffel Nov 2 '14 at 7:55
• I was just wondering about the last part from $kn + kn + T(n/k^2) = \cdots = kn \; log_k n + T(1)$, what exactly did you do there? In addition, when analyzing the function $f(x) = \frac{x}{log x}$, the (rounded) minimum seems to be 3, not 2, or am I missing something? – muffel Nov 2 '14 at 16:02
• You're right about $x/\log x$, it's increasing only for $x > e$. Regarding the rest, when you open up the recurrence $\log_k n$ times then you will have $\log_k n$ terms $kn$ and an extra term $nT(n/k^{\log_k n}) = nT(1)$ (I forgot the $n$). – Yuval Filmus Nov 2 '14 at 16:41
1) $f(n) \in O(n)$, since $k$ is a constant.
2) If $a=b=k$, then $\log_b a = \log_k k$. Your specific case of $\log_1 1$ only appears for $k=1$. And if we insert $k=1$ into the original recurrence we get $T(n) = T(n) + n + 1$, which does not make much sense as a description of a runtime. So it is sensible to simply exclude this case.