What is the time complexity of this algorithm?

491 Views Asked by At

I asked a question on cs.stackexchange.com and I eventually (some days after) answered myself(After some research). I want to know the time complexity of my answer. But my math is too weak for it. I used Wolfram Alpha to arrive at a function for the time complexity of my answer. I need to convert that function to Big-Oh notation.

Here's the relevant part of my answer:

The usual worst case is when a = 1. The first instance will run $\lfloor{\lg b}\rfloor$ times. Then it's $\lfloor{\lg(\frac{b}{2})}\rfloor$, then $\lfloor{\lg(\frac{b}{4})}\rfloor$ until it gets to $\lfloor{\lg(\frac{b}{b})}\rfloor$ which will give 0.

This will give $$\sum_{i=0}^n \log_2\left(\frac{b}{2^i}\right)$$

This is equal to $$\sum_{i=0}^{n} \log_2\left(\frac{2^n}{2^i}\right)\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\, (1.0)$$ Because $b = 2^n$ (explained below).

However if we denote $n$ by the number of bits needed to represent $b$, then $n = \lceil{\log_2(b)}\rceil$. Thus, $b = 2^n - k$ where $k: 0 \le k \le 2^{n-1}$. To Big-Oh notation, $k$ is irrelevant, so we can just treat $b$ as $2^n$.

Plugging this into Wolfram Alpha yielded an unhelpful result.

Now to convert this to Big-Oh and Big-Theta Notation,.

I want to express $(1.0)$ as a Big-Theta function of $n$. Help please.

EDIT There was a major error in my formula, I corrected it and it drastically changed the question, invalidating the original answer.

2

There are 2 best solutions below

0
On BEST ANSWER

Now to convert this to Big-Theta Notation,.

Note, that $(1.0)$ is $\log_2(\frac{2^n}{2^0}) + \log_2(\frac{2^n}{2^1}) + \log_2(\frac{2^n}{2^2}) + \dots + \log_2(\frac{2^n}{2^n})$

Also note that $\frac{2^n}{2^i} = 2^{n-i}$
Also, $\log_2(2^{n-i}) = (n-i)(\log_2{2^2})$ this gives $n-i$

Thus, $(1.0)$ can be restated as: $$\sum_{i=0}^n {n-i}\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\, (2.0)$$ This ks the sum of the natural numbers to $n$ this gives $$\frac{n\cdot(n + 1)}{2} \,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\, (3.0)$$

We can see that $(3.0)$ is trivially $\Theta(n^2)$

0
On

No need to complicate the things $$\log_2 b+\log_2 (b-1)+\log_2 (b-2)+........+\log_2 1=log_2(1\times 2\times......\times b)$$ $$=\log_2 b!=O(\log b^b)=O(b\log b)$$. The r.h.s of your equation says the same thing.