patternModerate
Big O: Nested For Loop With Dependence
Viewed 0 times
withloopnestedbigfordependence
Problem
I was given a homework assignment with Big O. I'm stuck with nested for loops that are dependent on the previous loop. Here is a changed up version of my homework question, since I really do want to understand it:
The part that's throwing me off is the
sum = 0;
for (i = 0; i < n; i++
for (j = 0; j < i; j++)
sum++;The part that's throwing me off is the
j < i part. It seems like it would execute almost like a factorial, but with addition. Any hints would be really appreciated.Solution
So let me clarify a few things, you are interested in the big-O notation - this means upper bound. In other words, it is okay to count more steps than you actually do. In particular, you can modify the algorithm to
So you have two nested loops, each loop runs $n$ times, which gives you an upper bound of $O(n^2)$
Of course, you would like to have a good estimate for the upper bound. So for completion, we want to determine a lower bound. This means its okay to count less steps. So consider the modification
Here, we perform only a some of the loop-iterations. Again we have two nested loops, but this time we have $n/2$ iterations per loop, which shows that we have at least $n^2/4$ additions. In this case we denote this asymptotic lower bound by $\Omega(n^2)$. Since the lower and upper bound coincide, we can even say that $n^2$ is a tight asymptotic bound, and we write $\Theta(n^2)$.
If you want to know more, read here.
...
for (j = 0; j < n; j++)
...So you have two nested loops, each loop runs $n$ times, which gives you an upper bound of $O(n^2)$
Of course, you would like to have a good estimate for the upper bound. So for completion, we want to determine a lower bound. This means its okay to count less steps. So consider the modification
for (i = n/2; i < n; i++)
for (j = 0; j < n/2; j++)
sum++;Here, we perform only a some of the loop-iterations. Again we have two nested loops, but this time we have $n/2$ iterations per loop, which shows that we have at least $n^2/4$ additions. In this case we denote this asymptotic lower bound by $\Omega(n^2)$. Since the lower and upper bound coincide, we can even say that $n^2$ is a tight asymptotic bound, and we write $\Theta(n^2)$.
If you want to know more, read here.
Code Snippets
...
for (j = 0; j < n; j++)
...for (i = n/2; i < n; i++)
for (j = 0; j < n/2; j++)
sum++;Context
StackExchange Computer Science Q#4590, answer score: 12
Revisions (0)
No revisions yet.