Compute time complexity of code [duplicate] - algorithm

This question already has answers here:
How can I find the time complexity of an algorithm?
(10 answers)
Closed 5 years ago.
Can someone help me on how to analyze the run-time of the given below pseudocode
for i = 1 to n
k[i] = 0
for i = 1 to n
for j = i to n
k[i] = k[i] +j
I guess that it's time complexity is O(n^2). Please correct me if I am wrong.

The for loop consists of three elements. The assignment 1. The conditional branch. And the increment operation. If you can quantify the execution time for each line, you can calculate the overall time to execute. So for example call k[i] = 0 operation a. k[i] = k[i] +j. Operation b. The for loop assignment operation c. The for loop increment and conditional branch operation d.
This would yield:
(sum(n - i) for i = 1 to n)*(b + d) + (2 + n)c + na.
Which I think would simplify to
~(b + d)*(n^2)/2 for very large values of n. So I would agree it's complexity is O(n^2).

The way to analyze complexity of these nested loops is from the deepest loop.
for i = 1 to n
k[i] = 0
for i = 1 to n
for j = i to n
k[i] = k[i] +j
For the first loop it is very easy to see the operation k[i] = 0 will be performed n times
So order of that is O(N)
Now for the nested loop, loop for j starts from i , where i loops from 1 to n and continues till n.
So the key question to ask how many times the loop is executing.
when i = 1 it will be executing N times
when i = 2 it will be executing N-1 times
...
when i = 1 it will be executing 1 time
so if you sum them all it becomes N + N-1+ ... 1 = N(N-1)/2 = N^2/2 - N/2
So the order of the nested loop is O(N^2/2)- O(N/2) = O(N^2)
Also for the 1st loop the order is O(N)
so the total time complexity id O(N) + O(N^2) = O(N^2)

Related

time complexity (with respect of n input)

I was asked if what time complexity if this:
What is the time complexity (with respect of n) of this algorithm:
k=0
for(i = n / 2 ; i < n ; i++ ) {
for( j=0 ; j < i ; j++)
k = k + n / 2
}
choices was : a. O(n) b. O(n/2) c. O(n log(n) and d. O(n^2)
can have a multiple answers.
i know the algorithm above is d. O(n^2) but i came with with a. O(n) since it is looking for complexity of n only?.
if you are to have this question. how would you answer it.?? im so curious about the answer.
The answer is O(n²).
This is easy to understand. I will try to make you understand it.
See, the outer for loop block is executed n - n/2 = n/2 times.
Of course it depends whether the number n is even or odd. If it's even then the outer loop is executed n/2 times. If it's odd then it's executed for (n-1)/2 times.
But for time complexity, we don't consider this. We just assume that the outer for loop is executed n/2 times where i starts from n/2 and ends at n - 1 (because the terminating condition is i < n and not i <= n).
For each iteration of the outer loop, the inner loop executes i times.
For example, for every iteration, inner loop starts with j = 0 to j = i - 1. This means that it executes i times (not i - 1 times because j starts from 0 and not from 1).
Therefore, for 1st iteration the inner loop is executed i = n / 2 times. i = n / 2 + 1 for 2nd iteration and so on upto i = n - 1 times.
Now, the total no. of times the inner loop executes is n/2 + (n/2 + 1) + (n/2 + 2) + ... + (n - 2) + (n - 1). It's simple math that this sums up to (3n² - n)/2 times.
So, the time complexity becomes O((3n² - n)/2).
But we ignore the n term because n² > n and the constant terms because for every n they will remain the same.
Therefore, the final time complexity is O(n²).
Hope this helps you understand.

How to determine the worst case run time of each program using big-O notation? [duplicate]

This question already has answers here:
How can I find the time complexity of an algorithm?
(10 answers)
Closed 5 years ago.
I am having trouble understanding this question and how to get to the answer. How do you calculate the worst case run time?
The input of the following programs is an array A containing n integers A[ 1 ] · · · A[n]. Bound the worst case run time of each program using big-O notation.
Problem 1:
i = 1, total = 0
while i < n/2:
total = total + A[i]
i=i*2
Problem 2:
total = 0
S = the set {1,2,3,4...n}
for each subset T of S
for each element x in T
total = total + A[x]
Problem 3:
int i = 1, j = 1;
for i = 1 to n:
while (j < n && A[i] < A[j])
j++
The prefix sum of an array of numbers A[ 1 ], . . . , A[n] is a second array B[ 1 ], . . . , B[n] where
B[i] = summation from j=1 to i A[j]
The following problems calculate the prefix sum:
Problem 4:
for i = 1 to n:
B[i] = 0;
for j = 1 to i:
B[i] += A[j]
Problem 5:
B[1] = A[1];
for i = 2 to n:
B[i] = B[i-1] + A[i]
Problem 1:
The algorithm performs a constant time operation (addition) at the point of each access, and that access is made while i<n/2. Since i doubles each time, the condition will no longer hold after log(n/4) steps , so the worst case time complexity is O(log(n)) (logarithmic).
Problem 2:
The algorithm accesses each element of the array (x in pseudocode) as many times as it is in a subset of S, and performs a constant time operation at the point of access. Every element is in 2^(n-1) subsets that that contain itself, and there are n such elements, so the worst case time complexity is O(n * 2^(n)) (exponential).
Problem 3:
Observe that at every time the condition in the while loop is checked, the value of i+j increases by 1, and the value of i+j can never decrease. Since i+j starts at 2 and can never go past 2n+1, the overall complexity of the algorithm is O(n) (linear).
Problem 4:
For a given value of i, the inner loop runs i times. Further, i ranges from 1 to n, so we perform 1+2+3+...+n = n(n+1)/2 constant-time computations (additions), so the overall complexity of the algorithm is O(n^2) (quadratic).
Problem 5:
The algorithm accesses n-1 elements of the array, and performs a constant time operation at the point of each access (addition), so the worst case time complexity is O(n) (linear).

Why does this code has a time complexity of O(N*N)? [duplicate]

This question already has answers here:
How can I find the time complexity of an algorithm?
(10 answers)
Closed 5 years ago.
int a = 0;
for (i = 0; i < N; i++) {
for (j = N; j > i; j--) {
a = a + i + j;
}
}
Why does this code has time complexity of O(NN) ? Are all nested loops with n,m,q,.... bounds have a complexity of O(nm*q....) even though few iterations of the loops will not happen?
The reason for this is that constant factors are ignored in Big-O notation.
Your outer loop runs N times, while the inner loop runs on average N/2 times for each of the outer iterations.
This gives O(N * 1/2 * N) executions of the statements within the inner loop. Since we ignore constant factors, we end up with O(N * N) which is O(N^2).
The reason for omitting constants is simple: Big-O notation is about what happens when N is big. If you look at it this way - O((N^2)/2) - you see that increasing N has much more influence in the term, than whether or not we omit the division by two.
I wouldn't say all nested loops have that complexity, but this one certainly does. The number of iterations through the inner loop is something like this (there might be an off-by-one error):
i = 0: N times
i = 1: N - 1 times
i = 2: N - 2 times
. . .
i = N-1: 1 times
i = N: 0 times
Let's count the total number of times the innermost code is called. In this case, you can figure this out arithmetically. The innermost code is executed about N * (N + 1) / 2 times (this is the sum of elements from 1 to N). This simplifies to 0.5 * N^2 + 0.5 * N and that has a complexity of N^2.

Counting/Calculating Primitive operations of a for loop within a for loop

I have the Algorithm:
Input: X, a 1-D numerical array of size n
Let A = an empty 1-D numerical array of size n
For i = 0 to n-1
Let s = X[0]
For j = 1 to i
Let s = s + X[j]
End For
Let A[i] = s /(i+1)
End For
Output: An n-element array A of numbers such that A[i]
is the average of elements X[0],X[1], … ,X[i]
I am trying to write the T(n) formula and calculate it, how do write the for loop J = 1 to i within the for loop i = 0 to n-1.
what is the T(n) formula?
T(n) is time the algorithm takes to execute. t(n) will be used to compute the big-O ( O(n) ). so at the moment I have T(n) = 2n+2(n-1)+5i(n-1)+6(n-1)+1. as I counted the writes,reads, and operations in the algorithm. I don't know if the formula is write.
I am not clearly understanding your question, but still
how do write the for loop J = 1 to i within the for loop i = 0 to n-1?
The way you have written the loop is OK, its gonna do what you want..
what is the T(n) formula?
You can notice that the algorithm will make the second loop run
0 times when i=0,
1 time when i=1,
2 time when i=2,
.
.
.
and so on..
This will continue till n-1, so the complexity comes up to the sum of 0 + 1 + 2 + .... + n-1 which is n*(n-1)/2. which is asymtotically O(n^2)

What is the O notation of this loop?

I understand that this is O(N^2):
Loop from i=1 to N
Loop from j=1 to N
Do something with i,j
But what about this?
Loop from i=1 to N
Loop from j=1 to i
Do something with i,j
Is it still O(N^2) or O(N log N)? I don't really understand how to tell.
this is also O(N^2).
N(N-1)/2 ~ O(N^2).
i = 1 than j = 1
i = 2 than j = 1 to 2
i = 3 than j = 1 to 3
i = 4 than j = 1 to 4
…….
…
i = N than j = 1 to N
So for total is 1 + 2 + 3 + 4 + …. + N = (N * (N+1))/2 ~ O(N^2).
For second problem, the running time will be O (1/2 N^2), which later becomes O(N^2), as we don't care about the constant in O notation. Usually the log N algorithm involves dividing the subproblem into half-size of the actual size in each iteration. Take for example merge sort. In merge sort, in each iteration it is dividing the size of the array into half.
Also O(n^2).
You have to look at the worst case of how long your code will run.
So first loop runs from 1 to N.
For each iteration of that loop there is second loop, which runs from 1 to i.
And we know that i will be N on the last iteration, hence it will run for O(N*N), which is (N^2)
We ignore constants in big-O notations.
If these concepts are difficult, try googling some tutorials and examples. All you need is some practice, and you will get it.

Resources