views:

936

answers:

7

I can't solve a problem; can someone help me?

What is the Big O notation for the below statement:-

for (int i=2;i<=n;i=i*4)
    sum++;
+9  A: 

Once i grows exponentially, it is O(log(n)).

If n is 16 times larger, it is expected to run the loop only two more times than it would.

Samuel Carrijo
but the problem is the i start from 2 and grows with time 4.
It doesn't matter for the big O notation. It grows as 4^i = n. The 2 and 4 are just constants in this case
Samuel Carrijo
Samuel is correct. The number of iterations is the answer to this question "How many times must 2 be multiplied by 4 in order to exceed n?" The answer is (plus-or-minus 1, cuz' being sloppy is ok when you're doing a big-O calculation!) ceiling(log base 4 of n/2), so the complexity is big-O of log(n)/log(4) - 1/2, which is O(log(n)).
Steve Kass
i is being increased in a multiplicative manner so it is growing faster than in a linear style,e.g. note that if you double n, the loop only repeats one more time and not a multiple of n times.
JB King
@Steve Tks for better explaining. The lack of mathematical tools in SO just kept me from trying to deliver a full explanation on this
Samuel Carrijo
+3  A: 

Once you know that the starting point and multiplier for i are > 1, their exact values make no difference in big-O terms (they only translate into constants added to or multiplying the core component, O(log N), and such constants are neglected in big-O reasoning -- that's the core point of big-O reasoning, after all!!!).

Alex Martelli
+3  A: 

Try counting the number of loops for a few (small) values of n, then graphing the result (n on horizontal axis, loop count on vertical axis). Playing around with a problem is great when you're first learning.

Depending on which values you pick for n, you may not see the pattern. For instance, the loop count is the same for n=10 and n=20. Considering when the loop count will change will also reveal a pattern which can tell you the big-O timing.

Once you have a better understanding of algorithm timing, you won't need to go through this somewhat time-consuming procedure. You'll be able to figure out the big-O timing algebraically through code analysis.

outis
-1: Graphing the values for small n doesn't tell you what happens for large n, and O notation is about what happens for large n.
Stephen C
hey it is acutally a workable idea. use a calculator or a computer to count the number of iterations for small, large and extremely large n, then plot on a linear scale.
Midhat
The idea was to teach the OP to fish, rather than giving hir a fish. The main problem is that my suggestion might lead the OP to pick values that are too close together to see the pattern.
outis
@Midhat: except that that is now what the answer says. It says "a few (small) values of n". In non-trivial examples, that will give you the wrong answer. For example suppose that the loop count is ~= N + 1/1000 * N * log(N). You'll need to go to rather large N for the real answer to be evident on the graph.
Stephen C
@outis: you don't teach someone to fish by giving them dynamite. In this case, the OP needs to learn to understand/do the Math, and so do you by the sounds of it :-)
Stephen C
@Stephen: except that the loop count isn't N+N*log(N)/1000. For this question, a few small values (n=2,8,32,128) works. And don't be catty.
outis
lol@ dynamite. reminds me of the Jocks @ http://bit.ly/abtJ1
Midhat
@outis: The problem is that you are teaching the OP a technique that won't work in general. It is really unfortunately that the technique "works" here, since that may convince the OP and other unfortunates that yours is a good technique.
Stephen C
@Stephen: my answer isn't so much about general techniques as it is about having the OP play around with the code to get a gut-level understanding of timing. It's a stepping stool intended to help the OP get a fingerhold on this particular problem. By the time a more complex function such as you describe comes around, zhe'll have a better understanding of big-O. I'll make this bit of pedagogy explicit.
outis
+4  A: 

The way I think of big O notation is how long does it take to complete, which is complexity. For example if you have a bubble sort, as you go to items to be sorted it take approximately n*n operations to complete, which is O(N^2).

For binary search, as you increase n in size, you have log2(n) opertions to find the value. Since the number of operation is in terms of log then O(log N) for binary search (where log is log of 2).

For what you have, has increase you have N number of operations (this is even if you offseting) as it is inreasing in a linear fashion, which is O(N). This is the notation for linear search as it may take n/2 options an average to find a value, it is still O(N).

I would look at Wikipedia on O(N) notation. It has a more techinal explaination, and more big O notation information.

Glenn
Good general explanation, but this is not particularly linear :P It's `O(log N)`.
Thorarin
You are correct. I went back and looked at the code and I mistook the multiply for addition. You are correct it is O(log n)
Glenn
Thanks for pointing that out.
Glenn
I suppose it's easy an easy to make mistake if you see something of the form `y = x * 4`, the assignment looks somewhat like a linear function.
Thorarin
+1  A: 

Since you are doing this as a homework exercise, what you really need to do is go back to first principles; i.e. the mathematical definition of O notation. Work out how many simple computation steps there are as 'n' increases, work out the limit algebraically, and then proceed to the answer.

In practice, most folks estimate the 'O' complexity, based on knowledge of classical examples and a experience. And quite often they get it wrong.

Stephen C
A: 

Assuming that "sum++" is constant which is a pretty reasonable assumption the algorithm is O(log4 n).

Because the loop goes from 2 to n, you know it is at most O(n). However, because your incremented is multiplied by 4 every loop, there is exponentially less time spent in the loop.

Unknown
Except there is no log4 in O notation. It's just log, the base pulls out as a constant and O notation uses no constants.
Loren Pechtel
@Loren: actually that is a common misconception by people who haven't studied CS. The formal description of Big O notation makes no requirements that you must simplify the equation. See http://en.wikipedia.org/wiki/Big_O_notation#Formal_definition. This is in fact, why algorithms such as http://en.wikipedia.org/wiki/Karatsuba_algorithm does not make common simplifications such as the one you describe.
Unknown
A: 

The first 4 values of i are 2, 8, 32, 128 so the formula that shows how many iterations that loops will go through is:

((log(n)/log(2))/2)+0.5

Kelly French