confused about the time complexity of the follwing func. A good explanation would be helpful - algorithm

If the the first loop runs for n+1 times.
second loop runs for n(n+1) times.
third loop will run for ??? it has n^2+1 one relation with with with the second loop i guess but how about with the first one
somefunction(n) {
c = 0
for (i = 1 to n*n)
for (j = 1 to n)
for (k = 1 to 2*j)
c = c+1
return c
}

The first loop has O(n**2) iterations.
The second loop has O(n) iterations.
The third loop has O(n) iterations as well, since j is steadily increasing towards n.
(It's a little easier to see if you sum up the number of times c = c + 1 executes for the two inner loops combined. The inner loop runs 2 times for j = 1, 4 for j = 2, ..., and 2*n times for j = n. 2 + 4 + .. + 2*n = O(n**2).)
You can then (loosely speaking) multiply the three values together to get a total bound of O(n**4).

Related

How to do this nested for loop time complexity?

I'm trying to figure out this time complexity:
for(i=0; i<=(n/2)-1; i++){
for (j=i+1; j<=(n/2)-1; j++){
--some O(1) thing--
}
}
The outer loop I understand to be on its own O(n/2.) However with the inner loop as well I can't wrap my brain around how to break down how many times O(1) executes.
If the inner one stared j=0 I could do n/2(inner) * n/2(outer) = O(n^2) time complexity right? However since j depends on i, I'm thinking some type of summation is involved from i+1 to n/2 but i can't figure out how to set it up...
Basically I need help kind of visualizing how many times it loops, and how to set the summation up. Thank you! :)
Assuming that m = n/2. You will see that in inner loop, j will iterater over range m-1, m-2, m-3,... 1. Summing all of that will be 1+2+..+m-1 = (m-1)*m/2 = O(m^2)
Premise
For simplicity, let us call m = n / 2 - 1. The outer loop runs from 0 to m. The inner loop from i + 1 to m.
Iteration counting
We need to count how often the inner statement which you labeled O(1) is executed. That is, how often the inner loop runs in total, as executed by the outer loop. So let us take a look.
The first iteration of the outer loop generates m - 1 iterations of the inner loop. The second generates m - 1, then m - 2, m - 3, m - 4, ..., 2, 1, 0.
That means that the O(1) statement is, in total, executed:
(m - 1) + (m - 2) + (m - 3) + ... + 2 + 1 + 0
That is the sum from 0 up to m - 1
sum_{i = 0}^{m - 1} i
which can be simplified to
(m^2 - m) / 2
Substitute back
Let us now substitute back m = n / 2 - 1, we get
((n / 2 - 1)^2 - (n / 2 - 1)) / 2
After simplifying, this is
n^2/8 - 3n/4 + 1
Big-O
For Big-O, we observe that it is smaller than
n^2 - 0 + n^2
= 2n^2
Which, by definition is O(n^2).
As you see, this bound is also tight. So we also receive Omega(n^2) which also concludes Theta(n^2).

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.

Determining the time complexity

Given the following pseudo code for an array A
x = 0
for i = 0 to n - 1
for j = i to n - 1
if A[i] > A[j]:
x = x + 1
return x
how do I determine the running time?
I would say it's (n - 1)*(n - 1) = n^2 - 2n - 1 = O(n^2).
I'm not quite sure how to work with the if loop though.
yes O(n^2), just sum the number of iterations in the inner loop:
n + (n - 1) + (n - 2) + ... 1 = [ n x (n + 1) ] / 2
and if is not a loop, it is a control structure. generally you just count the number of times the condition is checked without considering the condition. The condition may be important if there is another loop in the if body.
how to count the iterations of the inner loop:
when i = 0 the inner loop runs n times, then ends
then i = 1 the inner loop runs n - 1 times, then ends
then i = 2 the inner loop runs n - 2 times, then ends
....
when i = n - 2 inner loop runs 1 times
when i = n - 1 inner loop runs 0 times
so all we need to do is to add the number of iterations of the inner loop:
n + (n - 1) + (n - 2) + ... 1 = [ n x (n + 1) ] / 2
#perreal is totally right about the order:
n*(n+1)/2 => O(n^2)
About the "if" part, it doesn't really matter. (I write this to answer to this part)
Lets say doing checking if takes c1 time, and doing the x=x+1 takes c2 time. You will have
(c1 | c1+c2)* n*(n+1)/2
And since you can ignore the constants from the order, it is from
O(n^2)
Actually, saying "this algorithm has a O(n^2)time complexity" suggests implicitly that it's a worst case complexity.
In a naive way, you can just count the number of times each instruction is executed in the worst case. if A[i] > A[j]: might be considered as an instruction as well, so first you don't necessarily have to ask yourself when the condition is true.
2*(n-1)*(n-1) is a majorant of the number of instructions executed in the inner-most loop, and more precisely:
2(n + (n-1) + ... + 1) = n(n+1) = O(n^2)
Even if it's not important with the O-notation, there are several arrays for which the condition is always true (thus these are the worst case for this algorithm). For example:
n n-1 n-2 ... 2 1

Selection sort CLRS - Correctness of the reasoning

I'm a self-taught computer science student.
Now I'm reading CLRS and I did the 2.2-2 exercise, it's about Selection Sort.
First array subscript is 1.
The pseudocode that I wrote is:
FOR i=1 to A.length - 1
FOR j=i+1 to A.length
IF A[j] < A[i]
new_index=j
IF new_index > i
tmp = A[i]
A[i] = A[new_index]
A[new_index] = A[i]
My reasoning is:
the first loop test is executed n times (1, 2, 3 ... n). When i becomes n the loop stops. So line 5 is executed (n-1) times and so on.
Then I think that the second loop test is executed (n^2 + n - 2)/2 times. When initial j = 2, it assumes: 2, 3, 4, 5 ... (n + 1), the loop test is executed n times, when j = 3 the loop test is executed (n - 1) times and so on. So when j = n the loop test is executed 2 times.
For this reason the second loop test is executed:
2 + 3 + 4 + 5 + ... + n = [(n-1)*(n+2)] / 2 = (n^2 + n - 2) / 2.
So the inner if of the second loop is executed: 1 + 2 + 3 + 4 + ... + (n-1) = [(n-1)*n] / 2.
Before this written I read a lot of hypothetical solutions but no one is equal to mine. So I'd like to know if my reasoning is wrong.
I hope that I written all details in good form.
The pseudocode is right and your analysis is along the right lines, but your solution has some mistakes in its reasoning.
Some Tips
Then I think that the second loop test is executed (n^2 + n - 2)/2 times
It's executed n(n-1)/2 times. See the solution below.
When initial j = 2, it assumes: 2, 3, 4, 5 ... (n + 1), the loop test is executed n times,
Remember the code is: FOR j=i+1 to A.length, so when the inner for loop starts at j = 2, it goes up to j = A.length, i.e. it goes up to j = n. In other words, it executes repeatedly for j = 2, 3, 4, ..., n and so executes a total of n - 1 times, not n times!
For this reason the second loop test is executed: 2 + 3 + 4 + 5 + ... + n = [(n-1)*(n+2)] / 2 = (n^2 + n - 2) / 2. So the inner if of the second loop is executed: 1 + 2 + 3 + 4 + ... + (n-1) = [(n-1)*n] / 2.
Suppose the body of the if statement of the second loop is always executed (the if condition is always true), then it should execute the same number of times as the second loop test. So I don't follow your reasoning here. And as for the summation, to find number of iterations, you need to add up the following:
j = 1: executes (n-1) times
j = 2: executes (n-2) times
j = 3: executes (n-3) times
...
j = n: executes 1 time
So you need to add up: (n-1) + (n-2) + (n-3) + ... + 1 = n(n-1)/2
Solution
The inner for loop executes exactly C(n, 2) = n(n-1)/2 times. The loop produces all pairs (i,j) such that 1 <= i < j <= n – which from combinatorics is n-choose-2, or C(n, 2).
The if statement body (new_index=j) executes at most n(n-1)/2 times – we don't know exactly how many because we don't know what the values in A are – so we don't know how many times A[j] < A[i]. It could execute zero times (consider the case when the array is sorted). But nevertheless, we find an upper bound by assuming the condition is always true. In this worst case, the if statement's body executes the same number of times as the inner for loop – which from the previous bullet point is n(n-1)/2
By similar reasoning, the other if statement body (the one performing the swap) executes at most n times.
So overall, the algorithm performs exactly n(n-1)/2 iterations of the inner for loop iterations having ϴ(1) work – and exactly n iterations of the if statement performing a swap in ϴ(1) time. This gives a time complexity of ϴ(n^2).

Nested Loops Asymptotic analysis

Need help figuring this hw example out guys.
the example states 0(n) running time.
I see that the outter loop is O(logn)
I cant figure out how to describe the inner loop in relation to n.
Help is much appreciated.
for (int i = 1; i <= N; i = i*2) // log n
for (int j = 0; j < i; j++) // less than n i don't know how to describe the growth
sum++;
Answer:: 0(n)
It might be easiest to analyze the runtime of the piece of code in the aggregate. On the first iteration of the outer loop, the inner loop will run 1 time. On the second iteration of the outer loop, the inner loop will run 2 times. On the third iteration of the outer loop, the inner loop will run 4 times. More generally, on the kth iteration of the outer loop, the inner loop will run 2k times. The outer loop stops as soon as i becomes greater than N, which happens after log2 N iterations.
If we sum up the total work done, we'll see that it's
1 + 2 + 4 + 8 + ... + 2log2 N = 2log2 N + 1 - 1 = 2n - 1
(This uses the fact that 1 + 2 + 4 + 8 + ... + 2k = 2k+1 - 1). Therefore, the total work done for the entire piece of code (that is, including both loops) is O(n).
The inner loop is O(n) time because it will go through each element once. If i is 1000, 1000 times, if i is 10000, 10000 times, etc.

Resources