-
October 20th, 2012, 10:57 AM
#1
[RESOLVED] Big-Oh Notation Question
Ive been trying to figure out what the big-O notation is for this code segment:
for (int count =1; count < n; count++)
{
int count2 = 1;
while (count2 < count)
{
count2 = count2 * 2;
}
}
My friend has been telling me its O(N^2) but I dont know if that is correct and if it is correct, I have no clue how they found that answer.
Anyone have any ideas???
-
October 20th, 2012, 12:49 PM
#2
Re: Big-Oh Notation Question
your friend is wrong, it's not O(n^2). Try counting the number of cycles in the inner loop given an arbitrary "count" value; then, sum the result from 1 to n; you'll get ...
-
October 20th, 2012, 12:55 PM
#3
Re: Big-Oh Notation Question
Looks to me like the inner loop wont run for the first time through the segment because count2 is equal to count. After count is incremented once that inner loop will have to run one more time every time through. So it cant just be O(n) right?
Last edited by quixomatic; October 20th, 2012 at 12:58 PM.
-
October 20th, 2012, 03:09 PM
#4
Re: Big-Oh Notation Question
Originally Posted by quixomatic
After count is incremented once that inner loop will have to run one more time every time through.
Not quite: notice that count2 is doubled, not incremented, on each iteration of the inner loop. As such, what is the complexity of the inner loop?
-
October 20th, 2012, 04:10 PM
#5
Re: Big-Oh Notation Question
The complexity of the inner loop is O(2^n) then? Each time it multiplies count2 by 2 ...this stuff is driving me crazy I just cant wrap my head around it.
Then the complexity of the outer loop would just be O(n)?
Do you add the two big-o notations of the loops?
-
October 20th, 2012, 04:36 PM
#6
Re: Big-Oh Notation Question
for now, forget the big-oh and just answer this question: given an integer n, how many times the line "count2 = count2 * 2" is invoked ?
-
October 20th, 2012, 04:40 PM
#7
Re: Big-Oh Notation Question
the while loop would get invoked n-1 times
-
October 20th, 2012, 04:42 PM
#8
Re: Big-Oh Notation Question
not the while loop, I said: how many times the line "count2 = count2 * 2" is invoked ?
-
October 20th, 2012, 04:45 PM
#9
Re: Big-Oh Notation Question
-
October 20th, 2012, 11:57 PM
#10
Re: Big-Oh Notation Question
Originally Posted by quixomatic
Just once per while loop
If that were true, then this C++ program:
Code:
#include <iostream>
int main()
{
int n = 5;
for (int count = 1; count < n; count++)
{
int count2 = 1;
while (count2 < count)
{
count2 = count2 * 2;
std::cout << count2 << ' ';
}
}
std::cout << std::endl;
}
would have the exact same output as this C++ program:
Code:
#include <iostream>
int main()
{
int n = 5;
for (int count = 1; count < n; count++)
{
int count2 = 1;
if (count2 < count)
{
count2 = count2 * 2;
std::cout << count2 << ' ';
}
}
std::cout << std::endl;
}
However, the former gives me the output:
and the latter gives me the output:
Clearly, your statement must be wrong.
-
October 21st, 2012, 11:30 AM
#11
Re: Big-Oh Notation Question
Originally Posted by laserlight
If that were true
...
Clearly, your statement must be wrong.
Appreciate the help but I figured it out just looking at the patterns of the code and determined the outer loop is O(N) and that the inner loop just performs 2^n like I said earlier, which in log base 2 is just logN.
Since the while loop is inside of outer loop I multiplied O(N) by O(logN) giving me O(NlogN)
-
October 21st, 2012, 11:41 AM
#12
Re: Big-Oh Notation Question
sorry, it's still wrong. Again, count the number of times the inner loop cycles ( no, it's not 1 nor 2^n. try following the code as it runs, eventually with a debugger or just with pen and paper ).
-
October 21st, 2012, 11:49 AM
#13
Re: Big-Oh Notation Question
Originally Posted by superbonzo
sorry, it's still wrong. Again, count the number of times the inner loop cycles ( no, it's not 1 nor 2^n. try following the code as it runs, eventually with a debugger or just with pen and paper ).
You know what, I really dont care...I worked on this for a huge chunk of yesterday and it was only a tiny fraction of my assignment. Not only that I was trying to be nice saying I appreciate the help, but what help have you given? Day and a half later and still not even a single post on what direction I should take other than "how many times was count = count * 2 invoked. I've asked multiple people who know how to find big-oh and they all agree its nlogn.
-
October 21st, 2012, 11:58 AM
#14
Re: [RESOLVED] Big-Oh Notation Question
Originally Posted by quixomatic
the inner loop just performs 2^n like I said earlier, which in log base 2 is just logN.
You did not say that. Rather, you asked if "the complexity of the inner loop is O(2^n)". It isn't, as you yourself have now concluded. What you probably have in mind is the idea that the value of count2 increases at a rate proportional to 2^n. Thus, the number of iterations of the inner loop is proportional to log n.
Originally Posted by quixomatic
Since the while loop is inside of outer loop I multiplied O(N) by O(logN) giving me O(NlogN)
Yes, that is correct
EDIT:
Originally Posted by quixomatic
Day and a half later and still not even a single post on what direction I should take other than "how many times was count = count * 2 invoked.
The thing is, figuring that out is the key to figuring out the complexity of the inner loop, and is likely to be the core lesson of this exercise. Once you know the complexity of the inner loop, the overall complexity of the code snippet is obvious since the outer loop's complexity is obvious.
Last edited by laserlight; October 21st, 2012 at 12:03 PM.
-
October 23rd, 2012, 01:09 AM
#15
Re: [RESOLVED] Big-Oh Notation Question
Appreciate the response Laserlight, I admit I did not have an strong understanding of what complexity is or how to find it, but when I said it performs 2^n I did mean that the inner function itself was computing exactly that. I may have reached my answer differently, but never once did superbonzo say I was getting closer or that I had the right idea.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|