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

Welcome to Dream.In.Code
Become an Expert!

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




Big O notation/Time complexity/Algorithm analysis

 

Big O notation/Time complexity/Algorithm analysis

nebulinda

11 Sep, 2009 - 09:14 AM
Post #1

New D.I.C Head
*

Joined: 21 Apr, 2009
Posts: 10


My Contributions
I'm in a 200 level CS class called Data Structures and Algorithms, and I'm having a hard time understanding how to find the time complexity of an algorithm. I don't get how you look at an algorithm and decide if it's n, or log(n), or what.

I can usually tell that if something is just assignments, that's constant, and a loop is n, and a nested loop is n^2, n^3, etc.

But then there's something like this:
CODE

sum=0;
for(int j=1; j<n; j*=2){
     sum++;
}


Apparently that's O(log(n)), but I have no idea where that comes from. My professor seems to expect that we can just look at an algorithm and just know what it's time complexity is.

And we had a quiz question recently that said, "For the typical algorithm that you use to perform calculations by hand, determine the running time in terms of n to a) add two n-digit numbers, and b ) multiply two n-digit numbers."

I said that adding would be constant, and multiplying would be n. But the right answer is that adding is n, and multiplying is n^2, and I don't understand why.

Is there some method by which I can figure this out?

Help appreciated, thanks!

This post has been edited by nebulinda: 11 Sep, 2009 - 01:33 PM

User is offlineProfile CardPM
+Quote Post


William_Wilson

RE: Big O Notation/Time Complexity/Algorithm Analysis

11 Sep, 2009 - 10:28 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,667



Thanked: 97 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
It takes practice and you need to really understand how code works and more importantly how logarithmic functions/loops work. Anything that is big O of a log, usually is effectively the result of an efficient binary tree approach. (At least this is the best way I found to visualize it).

In the example you gave, the values looped are between 1 and n. That much I assume you could already determine, which means a worst case of n, if we were to search through each element. i.e. a linear search. However the loop skips through at j *= 2, which doubles the value of j each iteration of the loop.
*This is where the binary tree part comes in. This is easily mapped to the path traversing a binary tree. (Assuming it is a balanced tree)

A short example:
CODE

     o
    /  \
   o    o
  / \   / \
o   o  o   o

In this diagram, n would be 7. To traverse linearly it would take 7 iterations, but at j*=2, we are making a decision of left or right at each branch, cutting the tree to be searched in half with each iteration, which as far as the example goes would be 3 in a worst case. Thus it runs in log(n) which is MUCH faster than linear.


I hope that clears it up a little, if not, feel free to ask more questions smile.gif
User is offlineProfile CardPM
+Quote Post

mojo666

RE: Big O Notation/Time Complexity/Algorithm Analysis

11 Sep, 2009 - 12:02 PM
Post #3

New D.I.C Head
*

Joined: 27 Jun, 2009
Posts: 18



Thanked: 2 times
My Contributions
log(n) means you need to double (or triple, quadruple, ect...) the input size in order to increase run time by 1. If log(n)=t then log(2*n) = t+1. In the example you gave, doubling n will result in only one additional loop. Doubling that will result in another loop.

For the quiz problem, try and code it to at least the looping parts of the algorithm.

CODE

int n1[n]
int n2[n]
for int i = 1 to n
result[i] = n1[i] + n2[i]
//you add each of n digits of the two numbers.  This is order n.



CODE

int n1[n]
int n2[n]
for int i = 1 to n
for int j = 1 to n
result[i] = result[i] + n1[i] * n2[j]
//One nested loop since you need to multiply each digit of n1 with each digit of n2.  This is O(n^2).


This post has been edited by mojo666: 11 Sep, 2009 - 12:03 PM
User is offlineProfile CardPM
+Quote Post

Neumann

RE: Big O Notation/Time Complexity/Algorithm Analysis

15 Sep, 2009 - 04:50 PM
Post #4

I can judge a book by its cover
Group Icon

Joined: 8 Jul, 2009
Posts: 686



Thanked: 93 times
Dream Kudos: 225
My Contributions
How many times would you have to multiply 1 by 2 to get a number that is equal to or greater than n? Assume that n is some power of 2. In that case, you would have to perform the multiplication logn times. Otherwise you'd multiply it logn + 1 times. Both are O(logn) operations.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 07:18AM

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