1
$\begingroup$

This question already has an answer here:

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?

$\endgroup$

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ @FrankW I added some infos why I asked the question and what problems occurred while trying to solve it $\endgroup$ – muffel Nov 1 '14 at 18:33
  • $\begingroup$ 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)$. $\endgroup$ – Yuval Filmus Nov 1 '14 at 19:04
1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – muffel Nov 2 '14 at 7:55
  • $\begingroup$ The bases of the logarithm don't matter as long as you use the same base for both. As to how I got the formula, I tried to explain this in the answer. I just unrolled the recurrence relation and there the formula was. $\endgroup$ – Yuval Filmus Nov 2 '14 at 9:25
  • $\begingroup$ 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? $\endgroup$ – muffel Nov 2 '14 at 16:02
  • $\begingroup$ 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$). $\endgroup$ – Yuval Filmus Nov 2 '14 at 16:41
1
$\begingroup$

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.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.