School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,148 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,735 people online right now. Registration is fast and FREE... Join Now!




Time Complexity

 

Time Complexity

needhelp83

11 Sep, 2009 - 03:14 PM
Post #1

New D.I.C Head
*

Joined: 11 Sep, 2009
Posts: 5

Alright, I didn't get this one quite right on the homework, but I am trying to figure out exactly how to solve this thing. The answer was n log n , but how do you total this up when going through the loop


CODE


i =  n                                          1
   while i >= 1                              n
      j =  i                                     1
         while j <= n                        log n?
              <body of the j loop>,      \\Theta(1)
              j =j*2                            1
         end j while
     i =i-1                                      1
end i while



Now when I total up I disregard so I left with n. Now I assume that the inner loop is log n, but I don't fully understand. Any help??

User is offlineProfile CardPM
+Quote Post


Neumann

RE: Time Complexity

11 Sep, 2009 - 03:57 PM
Post #2

I can judge a book by its cover
Group Icon

Joined: 8 Jul, 2009
Posts: 686



Thanked: 93 times
Dream Kudos: 225
My Contributions
I don't think that completely writing out the solution, including all the mathematical lingo, will be of much help here, so I'll be brief. Yes, the first while loop is O(n) because it iterates n times. In the inner loop, j gets multiplied by 2 until it becomes greater than n. The initial value of j iterates from 1 to n. So it will have to be multiplied at least once, and at most log n times. When it equals to n, it will be multiplied once. when it equals to 1 it will be multiplied log n times. Therefore the inner loop is O(log n).
User is offlineProfile CardPM
+Quote Post

needhelp83

RE: Time Complexity

11 Sep, 2009 - 05:38 PM
Post #3

New D.I.C Head
*

Joined: 11 Sep, 2009
Posts: 5

QUOTE(Neumann @ 11 Sep, 2009 - 03:57 PM) *

I don't think that completely writing out the solution, including all the mathematical lingo, will be of much help here, so I'll be brief. Yes, the first while loop is O(n) because it iterates n times. In the inner loop, j gets multiplied by 2 until it becomes greater than n. The initial value of j iterates from 1 to n. So it will have to be multiplied at least once, and at most log n times. When it equals to n, it will be multiplied once. when it equals to 1 it will be multiplied log n times. Therefore the inner loop is O(log n).


Awesome, now with that said I am assuming you multiply the outer loop by the inner loop to give me n log n and I just drop the 1's?

User is offlineProfile CardPM
+Quote Post

KYA

RE: Time Complexity

12 Sep, 2009 - 03:47 PM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,500



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
Yup, read me for the simple rules of Big O

You drop constants.
User is online!Profile CardPM
+Quote Post

needhelp83

RE: Time Complexity

13 Sep, 2009 - 01:19 PM
Post #5

New D.I.C Head
*

Joined: 11 Sep, 2009
Posts: 5

QUOTE(KYA @ 12 Sep, 2009 - 03:47 PM) *


Thanks KYA, this link was amazing help!
User is offlineProfile CardPM
+Quote Post

Neumann

RE: Time Complexity

14 Sep, 2009 - 11:36 AM
Post #6

I can judge a book by its cover
Group Icon

Joined: 8 Jul, 2009
Posts: 686



Thanked: 93 times
Dream Kudos: 225
My Contributions
Sorry, was away for couple days...

QUOTE(needhelp83 @ 11 Sep, 2009 - 05:38 PM) *

Awesome, now with that said I am assuming you multiply the outer loop by the inner loop to give me n log n

No, I'd have to use some math to explain exactly what happened - it involves a ceiling function (http://en.wikipedia.org/wiki/Floor_and_ceiling_functions) and a simple inequality. You can think that that's what I did for now, but be aware that it's not how it was calculated.

QUOTE

and I just drop the 1's?

No, some of those 1's stay - they are your basic operations, and you have to calculate how fast those 1's will grow as n grows. That's the first part of analyzing algorithms - determining the basic operation. The other 1's, like those outside your loops, may be ignored - they don't contribute much of anything.
User is offlineProfile CardPM
+Quote Post

KYA

RE: Time Complexity

14 Sep, 2009 - 11:41 AM
Post #7

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,500



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
Yeah, it depends on context, but generally, the rule of thumb is to drop constants.

i.e 2n->n
n+1->n etc...

This post has been edited by KYA: 14 Sep, 2009 - 11:43 AM
User is online!Profile CardPM
+Quote Post

Neumann

RE: Time Complexity

14 Sep, 2009 - 12:02 PM
Post #8

I can judge a book by its cover
Group Icon

Joined: 8 Jul, 2009
Posts: 686



Thanked: 93 times
Dream Kudos: 225
My Contributions
Well, those constants could be dropped after you're done all the calculations, in order to make the Big-O look "simpler". But while calculating the Big-O, what is usually being done is increasing the amount operations being performed, because that's the nature of Big-O. So,

n+1 < n + n = 2n (for n greater than 1)
But such constants drop off at the end of the calculation, anyway. Most of the time, it's just a matter of formality. Let me add that parts of the answer with the lesser big-O can be dropped off too, so,

O(n logn + n) = O(n log n) + O(n) = O(n logn). In fact, that's what happened in this case. My final answer was,

1/2 * n * logn + n which can be considered as O(n logn).
User is offlineProfile CardPM
+Quote Post

KYA

RE: Time Complexity

14 Sep, 2009 - 12:21 PM
Post #9

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,500



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
Right, which isn't the case of a constant, but rather keeping the term with the largest growth rate.
User is online!Profile CardPM
+Quote Post

Neumann

RE: Time Complexity

14 Sep, 2009 - 12:29 PM
Post #10

I can judge a book by its cover
Group Icon

Joined: 8 Jul, 2009
Posts: 686



Thanked: 93 times
Dream Kudos: 225
My Contributions
No, it wasn't the case of a constant, that's why I said "too". I was simply making a general notion - no operations should be dropped off while calculating the Big-O.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 04:08PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month