1

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?

muffel
  • 125
  • 4

2 Answers2

1

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.

FrankW
  • 6,609
  • 4
  • 27
  • 42
1

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.

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514