What is time complexity of this algorithm? - algorithm

Suppose we have an array w, that contains n integers. By the following definition, and the following pseudo code, please, tell me what is the time complexity of the algorithm w.r.t. n:
idx[i] = max(j) : 1 <= j < i and w[j] < w[i]
alg:
Data: w : array of integers - idx: a pointer to maximum index with the less value.
Date: w is 1based
idx[1] = -1
for i=: 2 to n do
j=i-1
while w[j] > w[i] and j <> -1
{
j = idx[j]
}
idx[i] =j
end

You have 2 loops here -
First loop for loop runs n times. hence its O(n).
Second loop while loop runs each time from (i-1) + (i-2) + (i-3) + .... + (i-n). it runs for (n-1) times. So O(n).
Combines to O(n^2) algo. The other operations are either constant time or O(n) time, hence neglected.

Related

Time complexity of dependent nested loops

I was trying to find the time complexity of this nested loop
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
n--;
x++;
}
}
If there wasn't a n-- it would be n*n , O(n2) right?
But what if n reduces every time second loop runs?
What's the time complexity and big O of this nested loop?
If I consider n = 5, x equals 4, the second loop runs 4 time
The time complexity of the code is O(n). n is reduced by half for every iteration of the outer loop.
So we have n/2 + n/4 + n/8 + n/16 + ... + n/2^k = O(n)
where k is the number of iterations of the outer loop (basically i).
Note that the time complexity is independent of x.
If there wasn't a n-- it would be n*n , O(n2) right?
Yes
Another way to see it's O(n): You only enter the inner loop body if j <= n, and since j is positive, n must also be positive. But you decrease n every time, which you can only do O(n) times (where n is the starting value) and still have n positive.

I'm trying to find the complexity of the following algorithm:

I'm trying to find the complexity of the following algorithm:
count = 1
j=0
for i = 1 to 100 do
count += i
for k = 1 to n do
count *= k
while j < n do
count +=j
j *= 2;
end while
For the first for loop time complexity is constant O(100).
The second for loop has O(n) time complexity.
The while loop also has O(n) time complexity.
So total time complexity is O(100+n+n) = O(n)

Time complexity of nested for loop with inner iteration variable dependent on the outer one

This is the loop structure :
for (int i = 1 ; i < n ; i++) {
for (int j = 0 ; j < n ; j += i) {
// do stuff
}
}
My guess was O(nlogn) as it clearly cannot be O(n^2) since the increment in j is increasing and it clearly cannot be O(n sqrt(n)) since the increment is not that high. But I have no idea how to prove it formally.
Each time complexity of the inner loop is based on the value of i is n/i. Hence, total time would be n + n/2 + n/3 + ... + n/n = n(1+1/2+1/3+...+1/n).
As we know 1+1/2+1/3+...+1/n is a harmonic sereis and asymptotically is log(n). Hence, the algorithm is run in O(nlog(n)).

Time complexity of this simple code

In pseudo-code:
j = 5;
while (j <= n) {
j = j* j* j * j;
}
What is the time complexity of this code?
It is way shorter than O(logn), is there even any reason to go lower than that?
Let's trace through the execution of the code. Suppose we start with initial value j0:
0. j ← j0
1. j ← j0^4
2. j ← [j0^4]^4 = j0^(4^2)
3. j ← [j0^(4^2)]^4 = j0^(4^3)
4. j ← [j0^(4^3)]^4 = j0^(4^4)
...
m. j ← [j0^(4^(m-1))]^4 = j0^(4^m)
... after m loops.
The loop terminates when the value exceeds n:
j0^(4^m) > n
→m > log(4, log(j0, n))
Thus the time complexity is O(m) = O(log log n).
I used help from MathSE to find out how to solve this. The answer is same as another one by #meowgoesthedog, but I understand it the following way:
On every iteration, the value of j is going to increase by its own 4th power. Or, we can look at it from the side of n, that on every iteration n is going to reduce by its 4th root. Hence, the recurrence will look like:
T(n) = 1 + T(n1/4)
For any integer k, with 24k + 1 <= n <= 24k + 1, the recurrence will become:
T(n) = 1 + k
if we go on to assume that the 4th root will always be an integer. It won't matter if it is not as the constant of +/- 1 will be ignored in the Big-O calculation.
Now, since the assumption of 4th root being an integer simplifies things for us, we can try to solve the following equation:
n = 24k,
with the equation yielding k = (Log(Log(n)) - Log(2))/Log(4).
This implies that O(T(n)) = O(Log(Log(n))).

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)

Resources