Time Complexity for loop - algorithm

I was trying to find the time complexity for a for loop. below is loop detail.
for(int i=N;i>1;i=i/2)
{
for(int k=0;k<i;k++){
sum++;
}
}
Below is any find for the problem. Please correct me if i am going worng.
Inner loop will be exceute N+N/2+N/4+N/8....
so tn=ar^(n-1). So replacing Tn=1, a=N and r=1/2
1=N(1/2)^(n-1)
therefore
1/2N=(1/2)^n
So sum of inner loop is a GP. Sn=a(1-r^n)/(1-r)
Replacing a=N,r=1/2, we get
Sn=N(1-(1/2N))/(1-1/2)
therefore Sn=2N-1
I am not sure if complexity is N.
Please help
Thanks.

Below is the formal way (Sigma Notation) to infer the order of growth related to your algorithm (corroborated with experiments, using C's MinGW2.95 compiler).

Related

Complexity of 2 for loops inside 1 for loop

for(int i=1;i<=n*n;i++)
{
for(int j=1;j<=i/2;j++)
{
s=s+i+j;
}
k=1;
while(k<j)
{
s=s+k;
k=k*2;
}
}
So I know that the complexity is O(n^4), but I don't quite get it how to get there. I know that the first loop has n*n so it's more. However, 2 for's inside another usually mean O(n^2), so I just have (n^2)^2, due to the first n * n? Or the two fors inside mean an n each? The second for runs only for even values of i and the thrid one the same, maybe that counts. Please help. I'm confused because I remember some example that 2 fors inside another for is O(n^2*logn). If anyone cares to explain about that, I'll be thankful.
So your first guess is pretty much right. The outer for (i) will go for n^2 iterations. The inside for (j) will cost at most n^2 as well in single iteration. while can be in this case ignored (you can use n^2 as very rough upper bound for it). So you have n^2 x n^2 and that's your n^4.

How to derived solution by Big'O Notation with Algorithm Efficiency?

I was been given with this algorithm and I was told to provide its efficiency in Big'O Notation and explained the derivation.
I have tried to research about Big'O Notation and algorithm efficiency but still I am not able to solve the following question. These are the algorithms given:
i= 100
loop( i > 10)
num = 1;
loop( num < = 1000)
num = i*num
end loop
i= i /2;
end loop
I hope anyone could help me with this question, this is an exercise not homework.
Thank you.
if initial value of i - N, here 100, and max allowed value of num - M is the input, then the answer is log(N)*log(M).
So, outer loops will be executed log(N, 2) times. And inner loop will be executed log(M, current_value_of_i) times.
EDIT: to answer SilverArcher's comment.
During one iteration of the outer loop, the inner loop can't be executed more than log(1000,11) times.
Notice that this code always does exactly the same thing every time - there's no inputs you can change to cause it to run for a longer or shorter amount of time. As a result, the time complexity is O(1), since the runtime is independent of the size of the input.

Big O sum of integers runtime

I have am trying to learn Big O and am confused on an algorithm I just came across. The algorithm is:
void pairs(int[] array){
for (int i=0; i < array.length; i++){
for (int j=i+1; j<array.length; j++){
System.out.println(array[i]+","+array[j]);
}
}
}
I think the first for loop is O(n) and I know the second for loop is O(1/2*n(n+1)). The answer to the problem was that the run time for the function is O(n^2). I simplified O(1/2*n(n+1)) to O(1/2*(n^2+n)). So I'm confused because I thought that you needed to multiply the two run time terms since the for loop is nested, which would give O(n) * O(1/2*(n^2+n)). I simplified this to O(1/2n^3 + 1/2n^2). From what I understand of Big O, you only keep the largest term so this would reduce to O(n^3). Can anyone help me out with where I went wrong? Not sure how the answer is O(n^2) instead of O(n^3).
When you say the inner loop is O(1/2*n(n+1)), you are actually describing the big-O complexity of both loops.
To say that the outer loop has complexity O(N) basically means its body runs N times. But for your calculation of the inner loop's complexity, you already took account of all iterations of the outer loop, because you added up the number of times the inner loop runs over all iterations of the outer loop. If you multiply again by N, you would be saying that the outer loop itself is re-run another N times.
Put another way, what your analysis shows is that the inner loop body (the System.out.println call) runs 1/2*n(n+1) times overall. That means the overall complexity of the two-loop combination is O(1/2*n(n+1)) = O(n^2). The overall complexity of the two-loop combination describes how many times the innermost code is run.
your mistake is counting the second loop as O(1/2n^2)...
first, you can clearly see it is capped to N-1 (when j = 0)
first loop is clearly N
Second loop is MAX of N-1...
threrefore, O(N^2)...
if we examine it little more,
second loop will run N-1 when i=0,
then N-2 for i=1,
and ONE single time for i=n-1,
this is 1/2n(n-1) = 1/2n^2 - 1/2n = O(n^2)
Notice this includes all iteration of the outer loop too!

Determining big-O of a given algorithm

I am given this algorithm (pseudo code, not in any specific language):
foo1(l,m,n){
for ( i = 1; i < l, i++){
for( j = 1; j < m ; j++){
for ( k = 1; k < n; k++){
//Constant time inner loop
}
}
}
}
I am trying to find the number of times he inner loop runs in respect to l, m, and n and come up with a function for it. I also am trying to figure out the big-O notation for the algorithm.
Looking at the algorithm, I was thinking that the inner loop would run l*m*n times. I came up with this because for example, if l,m,n were 3, 6, 9 respectively, then the inner loop would run(9*6*3) times. So the function that would return the number of times the inner loop runs would be something like:
f = l*m*n
Now the big-O is where I am struggling with (not necessarily with this problem) but I wanted to get some further insight as so how to tackle big-O problems to best determine the right big-O for an algorithm.
For this specific case, I was thinking that the big-O would be n^3 but that is just based on a guess really. How do I go about figuring out what the big-O actually is for this problem, and more generally for other algorithms I may encounter?
You are on the right track of understanding big-O. Above pseudo code indeed has complexity of O( lmn ) As you are probably looking for some references, I would like that you have a look at the following awesome post on stack overflow itself:
Plain English explanation of Big-O
In my opinion,this is one of the best guide to get you started with Big-O concepts.
If you delve further into depth follow this MIT Lecture .This will surely give you a nice ride about Big-O concepts in detail. I think these two references will clear lot of your concepts and will definitely help building your solid understanding.
Happy Leaning!

What is the bigOh or running time of the following algorithm

What is the running time of the following algorithm in bigO.
for(int i=1;i<=n;i++){
for(int j=i;j<=n;j++){
for(int k=j; k<=n;k++){
for(int l=k; l<=n;l++){
...
}
}
}
}
This algorithm seems to be n^4. Of course, from the theoretical perspective (without any compiler considerations).
N^4. The fractional part doesn't count.
A formula for such a collection of dependent loops, can be inferred like the following:
Where c (constant) is the number of operations inside the innermost loop, n is the number of elements, and r is the number of nested loops.
In the question's case:
Another methodology, efficient but tedious, is to use Sigma notation:
O(N^4) is the cost.
each for nested is N^
so essentially N * N * N * N = N^4
CS610, Algorithm Development, NJIT.
My graduate coursework is actually coming in handy.
my answer is O(N^4)...because there are four "for loops"..and it is easy to judge the runing time of this algo...thanks !

Resources