4
x=0
for i=1 to ceiling(log(n))
    for j=1 to i
        for k=1 to 10
            x=x+1

I've included the answer I've come up with here:

enter image description here

I think the time complexity is θ(n^2 log(n)), but I am not sure my logic is correct. I would really appreciate any help understanding how to do this type of analysis!

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
guest
  • 43
  • 5

2 Answers2

3

Outermost loop will run for ceil(log n) times. The middle loop is dependent on the value of i.

So, it's behaviour will be :

1st iteration of outermost-loop    - 1
2nd iteration of outermost-loop    - 2
.....................................
ceil(log n) iteration of outermost-loop     - ceil(log n)

Innermost loop is independent of other variables an will always run 10 times for each iteration of middle-loop.

Therefore, net-iterations

= [1*10 + 2*10 + 3*10 + ... + ceil(log n)*10] 
= 10 * {1+2+...+ceil(log n)}
= 10 * { (ceil(log n) * ceil(log n)+1)/2} times
= 5 * [ceil(log n)]^2 + 5 * ceil(log n)
= Big-Theta {(log n)^2}
= Θ{(log n)^2}.

I hope this is clear to you. Hence, your answer is incorrect.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • Is `ceil` actually significant in the result? It can possibly add something in `[0; 1)` only, which results in having at most `ceil(log n) * 10` extra iterations, which does not beat `(log n)^2 * 10`. – D-side May 20 '15 at 05:50
  • @D-side- yes, it does carry significance, as iteration will increase by one in the latter case and it's square will be worth it. **`(log n)^2 isn't the same as {(log n)+1}^2`**. I hope you got my point. – Am_I_Helpful May 20 '15 at 05:52
  • You may have misunderstood that section. It's about the website that I reference pretty much everywhere but I still haven't published an english version of, well, anything =\ – D-side May 20 '15 at 05:57
  • @D-side- OOPS, Anyways, just be proud of it. If I can be helpful to you any how? Leave it by the way, people are always constrained by several factors- Time being the major one. Good luck to you for your future work. :) – Am_I_Helpful May 20 '15 at 05:59
  • 1
    I still don't get that point btw :) `{(log n) + 1}^2` is the same as `(log n)^2 + 2 * (log n) + 1`. Since asympthotically only the highest of the powers matters, it's asymptotically the same as `(log n)^2`. Is there any flaw in that chain? – D-side May 20 '15 at 06:18
  • @D-side- In terms of theta-notation it will be log(n)^2, but, in raw form ,it can't be omitted. It seems I've repeated the same in theta-notation, I am improvising it. Thanks – Am_I_Helpful May 20 '15 at 06:23
  • Don't know why Mr.Anonymous downvoted this answer. Please let me know the reason. – Am_I_Helpful May 20 '15 at 07:32
-1

You have three loops. Lets consider one by one.

Innermost loop: It is independent of a n or i, and will run always 10 times. So time complexity of this loop is Theta(10).

Outermost loop: Very simply time complexity of this loop is Theta(logn).

Middle loop: As value of i can be upto logn time complexity of this loop is also O(logn)

Overall complexity: Theta(logn)*O(logn)*Theta(10) or O(logn*logn*10) or 10*O((logn)^2) or O((logn)^2)

taufique
  • 2,701
  • 1
  • 26
  • 40
  • In order to get a Theta bound, how can I change the analysis of the middle loop to get rid of the big O? – guest May 20 '15 at 02:25
  • Can you mention your original problem? For which you wrote the pseudocode? – taufique May 20 '15 at 02:31
  • I guess then there is no way. At least I am not getting any. – taufique May 20 '15 at 03:20
  • I don't know if you're misunderstanding. I don't mean to reduce the complexity of the the problem, but rather get a Theta bound instead of big O bound. – guest May 20 '15 at 03:32