Finding the Frequency and big O for this code segment - data-structures

Can anyone explain how to analyze this code segment?
I can't understand how to get the frequency count for second line.
Thanks in advance

You need to understand Time Complexity and Space complexity to get a good understanding of analysing code like you have posted above. You can start from here
To briefly describe your code, there are two loops: outer loop and inner loop. The outer loop will run n times. Now, this is how the inner loop will work:
First j will be equal to 1. Then the inner loop will run for n times, just like the outer loop. So, j = 1 and then the control will enter in the inner loop so x = 1,3,4,6.... First, x will be equal to 1. Then, x = x + 2 will make x = 3. Then, x will be incremented by one and x will be equal to 4 and so on.
Once this inner loop finishes, we move onto the outer loop again and j will be equal to 2. Now again control will go back inside the inner loop. so x = 1,3,4,6... once again. This will keep on happening until j = n.
Then the outer loop will terminate and the control will move out of both these loops.

Related

How many times is the process command repeated in the code snippet? Why?

This snippet of code is not related to a specific language and is actually about the effect of two four loops on the final result.
For(int i=0;i<n-i;i++)
for(int i=0;i<n-i;i++)
process;
How many time is the "process" command repeated in the code?
Why?
Lets assume that
For is a typo for for
process; is actually an allowed syntax for a statement
the inner int i creates a local variable which is not in conflict with the outer i
all inner i refer to the same variable
Then the both loops will iterate until 2*i <= n, i.e. n/2 times, give or take 1 for odd n.
Being nested and independent (see assumptions above) it will result in process; being executed (n * n) / 4 times.
Keeping all the assumptions intact according to 1, we can generalize in little mathy version as following.
Number of times "process" command repeated = ceil(n/2) * ceil(n/2)
This result is irrespective of even or odd. Let me explain a little, when we try to solve the inequality as given in the loop condition i < n - i, it gives i < n / 2. If we put an even number in place of n, then both loop will run n/2 times. For odd number (say 11), if we closely see the inner loop then it will run for all integers from 0 including 5 (similarly for outer loop) and total number of times it runs is 6. On the other hand if that number was 10 then the loop couldn't have run for 5 as per the loop condition and total number of times it runs is 5. Now come to our condition i < n / 2, put 11 and 10, it will result 5.5 and 5 respectively. Hence in general number times loop run is ceil(n/2).

Triple Nested For-loop with two independent outer loops and one dependent inner loop

I have the following sequence of loops.
TripleLoop(int n)
for i <- 1 to n
for j <- 1 to n
for k <- j to n
do num <- j + i
return num
I know the two outer loops run "n" times.
Thus, we have n * n = n^2 for the two outer loops.
However, the third inner loop depends on the variable "j".
How do I begin to solve these types of nested dependent for-loops?
I'm not sure if I should multiply, or add the third inner loop to the two outer loops.
Can someone help me out here?
Well the inner loop (the one with k as iterator) is executed n-j+1 times, since it starts at j and ends with n.
The total number of steps the middle for loop thus performs is the sum of steps per iteration for j, so that means that the total number of times we run the body of the inner for loop is:
n
---
\ n * (n + 1)
/ n - j + 1 = -------------
--- 2
j=1
so after one iteration of the outer loop (the one with i as an iterator), we have n*(n+1)/2 steps.
In total, our algorithm will thus run the body of the inner loop for a total of n * n * (n+1)/2 times. Since the outer loop runs n times, and the number of steps in the body of that loop does not depend on the value of i itself.
If we consider the num <- j + 1 part to run in constant time (well strictly speaking summing up huge numbers can not be done in constant time), then this is thus an O(n3) algorithm.

How to calculate big O notation for this nested loop?

if I have for example this simple code
for (i =1;i<=n;i++)
for (j=1 ;j<=i;j++)
count++;
for this line
for (i =1;i<=n;i++)
if I say that the time for 'i' to get a value is T then i will increase n+1 times since the condition is i<=n so the time for increasing i is (n+1)*T the condition will be asked n+1 times so lets say that the time needed to check the condition is T aswell then the total time for it to complete is (n+1)*T and i++ will be executed n times because when the condition is asked if i(in this case i is n+1) <=n it will be false so it wont increase i so the total time for executing this single loop would be (n+1)*T+(n+1)T+nT or (n+1+n+1+n)*T = (3n+2)T so big O for this case would be n
but I dont know how to calculate for the second loop I was thinking if it would be n[(3n+2)*T] and big O for this would be n^2 but I am not too sure if you dont understand what I am saying or if I made a mistake with first loop too if you can please explain in details how to I calculate it for that code .
First loop will execute n times, second loop i times, for each i from the outer loop. At the beginning, i=1, so the inner loop will have only one iteration, then i=2, i=3.. until i reaches the value n. Therefore, the total number of iterations is 1 + 2 + 3 + ... + n = n * (n + 1) / 2, which gives O(n^2).

Why is the bigO of this algorithm m^2*n?

I am trying to determine why the bigO of this algorithm is m^2*n, and why the innermost loop is executing in m^2*n steps.
int m=10, n=15;
int inLoop = 0, midLoop = 0, outLoop = 0;
for(int i=1;i<=m;i++)
{
outLoop++;
for(int j=1;j<=2*i-1;j++)
{
midLoop++;
for(int k=1;k<=n;k++)
{
inLoop++;
}
}
}
System.out.println("Out Loop " + outLoop);
System.out.println("Mid Loop " + midLoop);
System.out.println("Inner Loop " + inLoop);
When I run this, I get that the inner loop runs 1500 times, the middle loop 100 times, and the outermost loop 10 times.
Before running this code I thought that this code ran the first loop m times, the second loop m^2 times, and the last loop n times, which with these values would result in the inner loop output to be 15,000.
Apparently the algorithm seems to be executing the innermost loop in m^2 * n steps as opposed to the m^3*n steps I believed it would be.
summation(2i - 1) as i starts at 1 and ends at m is:
2*summation(i) - summation(1) = 2 * (m+1)/2 * m - m = O(m^2)
This is only for the outer and middle loop
The inner loop is straight forward resulting in O(n * m^2)
I think that the idea is clear, that you count how often each loop is repeated, and that it's rather the details that are unclear. Now, in order to ease these things, you can separate these loops and conquer them separately.
For the outermost loop, it is pretty clear that it runs m times. That is, its complexity is Θ(m x), with x being the complexity of what's inside. For the innermost loop, the case is also pretty simple, it only depends on the value of n, which is constant, so its complexity is Θ(n).
The middle loop is the one that is a bit more complicated. Its complexity depends on i, but i is not constant but the loop variable of the outer loop. However, you can use the average as replacement. In this case the average is pretty simple, which you can visualize if you draw a checkboard. In the first iteration of the outer loop, i=1, so j will only take a single value 1. In the second iteration, i=2 and j=1..3. In the third, it's j=1..5 and so on. If you draw these underneath each other, you get a triangle-like shape. It has the width 1 at the top, the width 2m-1 at the bottom and a height of m. It's area is therefore ((2m-1)+1)/2=m.
Putting this together, you have complexities Θ(m) for the outer and middle loop and Θ(n) for the inner loop, making it Θ(m²n) overall.

How to find the running time of a specific procedure?

For each of the procedures below, let T (n) be the running time. Find the order of T (n)
(i.e., find f(n) such that T (n) ∈ (f(n)).
Procedure Fum(int n):
for i from 1 to n do
y ← 1/i
x ← i
while x > 0 do
x ← x − y
end while
end for
I know how to find run times of simple functions but since this is a nested loop where the inner loop depends on a variable from the outer loop, I'm having trouble.
It should be 1+4+9+...+n^2 = n(n+1)(2n+1)/6, or simply O(n^3), for this case.
For each step in the for-loop, it will run i^2 times for the while. Given x=i;y=1/i;, it will take i^2 (as x=y*i^2) times for x to reach x<=0 by decreament step x=x-y.
For i, it will be 1,2,...,n, summing them up, you will get 1+4+9+...n^2 = n(n+1)(2n+1)/6.
First, lets consider the runtime of the inner loop:
We want to figure out how many times the inner loop runs, in terms of i.
That is, we want to solve for f(i) in x-f(i)y = 0. If we sub in x = i, and y = 1/i, we get f(i) = i^2.
We know the outer loop will run exactly n times, so then, we get the total number of times the inner loop will be run:
= 1 + 4 + 9 + ... + n^2
This sum is equal to n(n+1)(2n+1)/6, which is O(n^3)

Resources