worst-case complexity of algorithms [closed] - algorithm

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the worst-case complexity of the following algorithms, where the complexity of the {sequence of code} is O(1)? so I'm looking for worse case complexity of the code written here if the rest is O(1)...
a)1: for (i= 0; i < n; i++)
2: {sequence of code}
for this one I've got 0(n)
b) 1: int i = 0;
2: if (i > =0)
3: {sequence of code}
I got o(1)
the rest I don't understand.
c) 1: for (i = 0; i < n; i++ )
2: for (j = n; j >0; j--)
3: {sequence of code}
d) 1:for (i = n; i <n+10; i++ )
2: {sequence of code}
e) 1:for (i = 1; i <= n; i++)
2: {
3: for (j = 1; j <= n; j++)
4: {sequence of code}
5: for (k = 1; k <= 2n; k++)
6: {sequence of code}
7: }

Yes, O(n).
Yes, O(1).
The outer loop executes N times. The inner loop executes N times for each iteration of the outer loop.
The loop executes a constant number (10) of times...
The outer sequence of code is executed inside two nested loops, just about like in 3. The inner sequence of code is nested inside yet another loop, also executing N times for each iteration of the next loop.

Related

Big O Time Complexity for nested loops

What is the time complexity for the below code?
int i = 0;
while(i*i <=N) {
for(int j = 0; j <=N; j++) {
for(int k = 0; k <=N; k++, i++) {
//O(1) operation
}
}
i++;
}
In nested loops if the outer loop 1 takes O(1) time and inner loop 2 takes O(logn) time and inner loop 3 takes O(n).
Then the total T.C. is O(1)O(logn)O(n) = O(nlogn). Is it true?
Please explain.
tl;dr: This code runs in O(n^2).
Detailed answer:
The outer "loop": while (i*i <= M) is a distraction.
Because i increases itself for each iteration of the most inner loop - it means by the time you re-evaluate the condition in it, the value of i is going to be N*N. This means, the outer loop is always repeating itself only once.
Now, once you ignore it, it is easy to see that the time complexity of the remaining code is O(N^2), since it's basically can be rewritten as:
int i = 0;
if (i * i <= N) { // Since the while loop does not repeat more than once
for(int j = 0; j <=N; j++) {
for(int k = 0; k <=N; k++, i++) {
//O(1) operation
}
}
i++;
}
Note: This answer assume no overflows of the variables, and specifically i does not overflow
Big O Time Complexity for nested loops
The operation "//O(1) operation" is executed (N+1)^2 times. And the number of times the calculations done by the loops themselves (e.g. performing the test j<=N) is also square (N^2+a*N+b).
So the time complexity is O(N^2).
You can test that by extending your program the following way:
int i=0;
int count=0;
while(i*i <= N)
{
for(int j=0; j<=N; j++)
{
for(int k=0; k<=N; k++, i++)
{
count++;
}
}
i++;
printf("i after the while() loop: %d\n",i);
}
printf("Number of steps: %d\n",count);
You test your program with different values of N and you can see that:
i is (N+1)^2+1 after the first pass of the while() loop. This means that the condition i*i<=N is equal to (N+1)^4 + 2*N^2 + 3*N <= (-2). And this is always false for N>=0.
The operation count++ (representing ourt O(1) operation) is done (N+1)1^2 times.
Time Complexity
Such kind of questions are typically asked on the CS Stack Exchange web site, not on StackOverflow.
StackOverflow is intended for questions about writing computer programs only.
(Note that StackOverflow is part of the StackExchange network and you can use all StackExchange web sites with your user account.)

Big O notation for Following Loops [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What will be the Big Oh of these portion of codes:
int sum = 0;
for(int i = 1; i < N; i *= 2)
for(int j =0; j <i; j++)
sum++;
And
int sum = 0;
for(int i = 0; i < N; i *= 2)
for(int j =0; j <i; j++)
sum++;
My Attempt:
According to me both have time complexity equal to O(n^2), because here we will multiply n with n which is equal to n^2. Am I correct? Or doing some mistake?
For the first portion of code:
1st loop will go from 1 to n with variable i going as
1, 2, 4, 8, 16.... n
and in the second loop j goes from 0 to i so the time complexity will be
O(1 + 2 + 4 + 8 + 16.... n) = O(2n - 1) = O(n)
and as for the second portion of code
i starts from 0, it always be 0 because you are multiplying it by 2. Its an infinite loop.

worst case runtime of the double for loop

Can someone please explain how the worst case running time is O(N) and not O(N^2)in the following excercise. There is double for loop, where for every i we need to compare j to i , sum++ and then increment and again repeat the operation until reach N.
What is the order of growth of the worst case running time of the following code fragment
as a function of N?
int sum = 0;
for (int i = 1; i <= N; i = i*2)
for (int j = 0; j < i; j++)
sum++;
Question Explanation
The answer is : N
The body of the inner loop is executed 1 + 2 + 4 + 8 + ... + N ~ 2N times.
I think you already stated the answer in your question -- the inner loop is executed 2N times, which is O(N). In asymptotic (or big-O) notation any multiples are dropped because for very, very large values, the graph of 2N looks just like N, so it isn't considered significant. In this case, the complexity of the problem is equal to the number of times "sum++" is called, because the algorithm is so simple. Does that make sense?
Complexity doesn't depends upon number of nested loops
it is O(Nc):
Time complexity of nested loops is equal to the number of times theinnermost statement is executed.For example the following sample loops have O(N2) time complexity
for (int i = 1; i <=n; i += c) {
for (int j = 1; j <=n; j += c) {
// some O(1) expressions
}
}
for (int i = n; i > 0; i += c) {
for (int j = i+1; j <=n; j += c) {
// some O(1) expressions
}

How to answer these Big-O homework challenges? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to see if I have these Big O questions right:
Determine the Big-O of the following:
a. for (i = 0; i < N; i++){
sequence of statements
}
for (j = 0; j < 1000000000*M; j++){
sequence of statements
}
This is O(NM) correct?
b. for (i = 0; i < N; i++) {
for (j = 0; j < i; j++) {
sequence of statements
}
}
for (k = 0; k < N; k++) {
sequence of statements
}
Is this O(n^4)?
c. for (i = 0; i < N; i++) {
for (j = i; j < i*i; j++) {
sequence of statements
}
I'm kinda stuck on this one....O(N^5)? or O(N^4) ?
a) No, first one is incorrect because both the for-loops are independent. The first for loop iterates for N times, whereas the second for-loop iterates for 1000000000*M times.
If f1(n) = O(g1(n)) and f2(n) = O(g2(n)), then f1 + f2 = O(|g1| + |g2|).
Check this Wikipedia link on Big O notation to know why the above.
So, overall time complexity = O(|N| + |M|).
b) The nested loop's time complexity comes out to be 1 + 2 + ... + N = N * (N+1)/2 = O(N2).
And, k-variable guided loop's complexity is O(N).
So, overall time complexity in this case is O(N2).
c) The 3rd case is somewhat complex.
When N = 2, the total iteration of both-loops = 0.
When N = 3, the total iteration of both-loops = 2.
When N = 4, the total iteration of both-loops = 2 + 6 = 8.
When N = 5, the total iteration of both-loops = 2 + 8 + 12 = 22.
...
When N = N(equals), the total iteration of both-loops = 2 + 8 + 22 + ... + (N-1)*(N-2) =
So, the total complexity
= 2 + 8 + 22 + ... + (N^2 - 3*N + 2)
= 1/3 * (N-2) * (N-1) * N
Check this link to know how it was derived
= O(N^3).
So, overall time complexity = O(N3).

Big O Algorithm Analysis

I have to analyze the Big O complexity for the below code fragments:
a)
// loop 1
for(int i = 0; i < n; i++)
// loop 2
for(int j = i; j < n; j++)
sum++;
b)
// loop 1
for(int i = 0; i < n; i++)
// loop 2
for(int j = i + 1; j > i; j--)
// loop 3
for(int k = n; k > j; k--)
sum++;
I'm not sure how to do so any help provided will be greatly appreciated. Thanks.
To analize Big-Oh complexity you have to try to count how many basic operations are made by your code.
In your first loop:
for(int i = 0; i < n; i++)
for(int j = i; j < n; j++)
sum++;
How many times is sum++ called?
The first loop happens n times, and in each one of these, the second loop happens around n times.
This gives you around n * n operations, which is equivalent to a complexity of O(n^2).
I'll let you work out the second one.
The first is straight forward (using the tools of the 2nd code snap, which is a bit trickier) - I'll focus on the 2nd code snap.
Big O notation is giving asymptotic upper bound to the number of ops the algorithm do.
Let's assume each inner iteration do 1 op, and let's neglect the counters and overhead of looping.
Denote T(n) total number of ops done in the program.
It is clear that the program has NO MORE ops then:
// loop 1
for(int i = 0; i < n; i++)
// loop 2
for(int j = i+1; j > i; j--) //note a single op in here, see (1) for details
// loop 3
for(int k = n; k > 0; k--) //we change k > j to j > 0 - for details see (2)
sum++;
(1) Since j is initialized as i+1, and is decreased each iteration, after the first iteration of loop2, you will get j == i, and the condition will yield false - thus - a single iteration is done
(2) The original loop iterates NO MORE then n times (since j >= 0) - thus the "new program" is "not better" then the old one (in terms of upper bounds).
Complexity of the simplified program
The total complexity of the above program is O(n^2), since loop1 and loop3 repeat n times each, and loop2 repeats exactly once.
If we assume single command is done each inner loop - the total number of commands which are done is then n^2.
Conclusion:
Since the new program is doing n^2 "ops" (according to the assumptions) and the original is "not worse then the new" - it is doing T(n) <= n^2 steps.
From definition of big O notation (with c=1, and for every N) - you can conclude the program is O(n^2)

Resources