What's the time complexity of the following code? - function

What's the time complexity of the following code?
a = 2;
while (a <= n)
{
for (k=1; k <= n; k++)
{
b = n;
while (b > 1)
b = b / 2;
}
a = a * a * a;
}
I'm struggling with the outer while loop, which is loglogn, I can't understand why. How would the time complexity change if the last line was a = a * a * a * a;?
the for loop is O(n), and inner one is O(logn).
So in total, O(n*logn*loglogn)

a values would be:
a = 2 2^3 2^9 2^27 2^81 ...
and so on.
Now let's assume that the last value of a is 2^(3^k)
Where k is the number of iterations of the outer while loop.
For simplicity let's assume that a = n^3, so 2^(3^k) = n^3
So 3^k = 3*log_2(n) => k = log_3(3log_2(n)) = š›©(loglogn)
If the last line was a = a * a * a * a the time-complexity would remain š›©(loglogn) because k = log_4(4log_2(n)) = š›©(loglogn).

the loop is running n times and the inner loop has time complexity is log n so total time complexity is O(n log n)

Related

How does LU decomposition with partial pivoting work?

I am using the method in which initially the elements on the main diagonal of L are set to ones (think that is Doolittleā€™s method, but not sure because I have seen it named differently). I know there tons of documentations, papers and books but I could not find simple examples that were not using Gauss elimination for finding L.
Partial Pivoting, as compared to full pivoting, uses row interchanging only as compared to full pivoting which also pivots columns. The primary purpose of partial pivoting as shown below in the picture and the code is to swap the rows to find the maximum u there as to avoid dividing by a very small one in that for loop which would cause a large condition number.
If you try a naive implementation of the LU decomposition and some ill-conditioned matrix like some arbitrary diagonally dominant matrix it should explode.
function [L,U,P] = my_lu_piv(A)
n = size(A,1);
I = eye(n);
O = zeros(n);
L = I;
U = O;
P = I;
function change_rows(k,p)
x = P(k,:); P(k,:) = P(p,:); P(p,:) = x;
x = A(k,:); A(k,:) = A(p,:); A(p,:) = x;
x = v(k); v(k) = v(p); v(p) = x;
end
function change_L(k,p)
x = L(k,1:k-1); L(k,1:k-1) = L(p,1:k-1);
L(p,1:k-1) = x;
end
for k = 1:n
if k == 1, v(k:n) = A(k:n,k);
else
z = L(1:k-1,1:k -1)\ A(1:k-1,k);
U(1:k-1,k) = z;
v(k:n) = A(k:n,k)-L(k:n,1:k-1)*z;
end
if k<n
x = v(k:n); p = (k-1)+find(abs(x) == max(abs(x))); % find index p
change_rows(k,p);
L(k+1:n,k) = v(k+1:n)/v(k);
if k > 1, change_L(k,p); end
end
U(k,k) = v(k);
end
end

What is the time complexity of the following pseudocode?

XYZ(a, b, c, m, n){
For p = 1 to m do
For q=p to n do
c[p,q] = a[p,q] + b[p,q];}
I think it is n + n-1 + n-2 +.....+(n-m+1). But I am not sure. Is it this or m*n?
Let's simplify your code :
For p from 1 to m
For q from p to n
Do something
Assuming the Do something part is done in constant time, what determines the time complexity of the code are the two loops. The outer loop runs m times, while the inner loop runs n-p, with p going from 1 to m.
If m >= n, the Do something part is repeated n+(n-1)+...+1 = n*(n+1)/2 = nĀ²/2 + n/2 = O(nĀ²) times.
Otherwise, if n > m, it's repeated n+(n-1)+...+(n-m+1) = (n*(n+1) - (n-m)*(n-m+1))/2 = 1/2 * (nĀ² + n - nĀ² + 2*n*m - n - mĀ² + m) = O(2*n*m - mĀ²) = O(nĀ²) times.
In any case, O(nĀ²) is a right answer, but if n >> m, a more precise answer is O(n*m).

How to find the time complexity of nested for-loop

What is the time complexity for the nested loops shown below:
1)
for (int i = 1; i <=n; i += 2) {
for (int j = 1; j <=n; j += 2) {
// some O(1) expressions
}
}
2)
for (int i = 1; i <=n; i += 3) {
for (int j = 1; j <=n; j += 3) {
// some O(1) expressions
}
}
In general:
for (int i = 1; i <=n; i += c) {
for (int j = 1; j <=n; j += c) {
// some O(1) expressions
}
}
Is is really this the following? O(nc)
Your algorithm will execute (n / c) * (n /c) iterations. We're dividing, because we are skipping c characters for each iteration. See that:
for (var i = 0; i <= n; i = i + 1)
Will have n / 1 iterations
for (var i = 0; i <= n; i = i + 2)
Will have n / 2 iterations
*Note that the result will be floored. That is, if n = 3 and c = 2, it will execute only one time (floor(3 / 2) == 1)
So, we can generalize it to be
(n / c)2
= (n2/c2)
= 1/c2 * n2
Remember that Big O is only interested in the rate of change. Since c is a constant, it is ignored from the calculation.
So, the result is:
O(1/c2 * n2) = O(n2)
For the general case, the inner loop has O(n) and the outer loop has O(n). Therefore, for each iteration of the outside loop, the inner loop iterates n times (c does not matter for order of complexity and should be treated as if it is 1). If the outer loop iterates n times, the total number of iterations in the inner loop is n*n, or O(n^2).
Imagine there are 10 chairs (n here)
in one for loop you are iterating over all the chairs, let say you sit on all the chairs, so in total you need to sit 10 times to sit on all the chairs for a given loop.
Now imagine you sit on first chair and ask your friend to sit on the other chairs one by one including your chair, so in total your friend has to sit on 10 chairs.
Now you choses the second chair, and again ask you friend to sit on each chair again, so in total he again has to sit on 10 chairs.
Similarly you can choose the 3rd,4th... chair and so on, so in total your friend has to sit on 10 chairs for each of the chair you choose.
10 + 10 + ... = 100 times
which is equivalent to 10^2 = 100
So the complexity is O(n^2), where n is the number of chairs.

Detect frequencies in a buffer along the time

if I record a series of frequencies beeps into a buffer, for example:
15kHz for 50ms, 17k for 50 ms and goes on, is there any way to "go" along the time plain and to decode this freqs(with goertzel or something)?
Hey, this is an update, I've added a code that shows how I find the first delimiter in the sound buffer that I check. If I record 5 seconds of a buffer(I record into a stream buffer and not a file) The first snippet takes something like 30 seconds to analyze the index where the start delimiter starts at. I thinks it is very newbie...must find a better solution. thanks
(every delimiter is 0.2 seconds duration) and it's like that - Start delimiter = 12KHz, 1's = 13k, 0's = 14k, End delimiter = 15k
double max_power = 0;
int max_power_index = 0;
double DelimiterSamplesCount = SampleRate * DelimiterTime;
float[] samples32array = samples32.ToArray();
//Searching For Delimiter
for (int i = 0; i < (samples32array.Length); i++) //Delimiter Samples Length = SampleRate*DelimiterTimeLength,( i.e: 44100*0.2=8820 samples)
{
if ((i + (int)DelimiterSamplesCount - 1) > samples32array.Length) break;
double power = Goertzel.GoertzelFilter(samples32array, StartDelimiterFreq, i, i + (int)DelimiterSamplesCount - 1);
if(power > max_power)
{
max_power = power;
max_power_index = i;
}
}
My Goertzel is like that:
public static double GoertzelFilter(float[] samples, double freq, int start, int end)
{
double sPrev = 0.0;
double sPrev2 = 0.0;
int i;
double normalizedfreq = freq / 44100;
double coeff = 2 * Math.Cos(2 * Math.PI * normalizedfreq);
for (i = start; i < end; i++)
{
double s = samples[i] + coeff * sPrev - sPrev2;
sPrev2 = sPrev;
sPrev = s;
}
double power = sPrev2 * sPrev2 + sPrev * sPrev - coeff * sPrev * sPrev2;
return power;
}
If you know the set of frequencies and the durations, then a set of sliding Goertzel filters is a good start to building a simple demodulator. Comparing and scanning for for a peak difference between these filters is a better decision criteria than just checking for a certain magnitude output.

openacc say Segmentation fault when split big data

As i have big data to malloc in GPU, i have to split it.Like follows, to split temp1 and temp2 from start to end once:
for (int start = 0; start < total; start += step) {
int end = start + step > total?total:start+step;
fprintf(stderr, "total %ld start :%ld end :%ld\n", total, start, end);
#pragma acc data pcopyin(sum[0:n_sample], num[0:n_sample*total], lognn[0:preFact])
#pragma acc data copy(temp1[start*n_array1:end*n_array1], temp2[start*n_array2:end*n_array2])
#pragma acc kernels loop independent
for (int index = start; index < end; ++index) {
unsigned long long * t1 = temp1 + index * n_array1;
unsigned long long * t2 = temp2 + index * n_array2;
// fprintf(stderr, "use %d\t", index*n_array1);
int k = count / 32;
int l = count / 64;
t1[k] <<= 2;
t2[l] <<= 1;
int x = num[index * n_sample + i];
int y = num[index * n_sample + j];
}
}
but I always be told Segmentation fault when first loop is complete and begin to run second loop.
Is the index var should be [0:end-start]? or should do some sync when loop complete?
thanks!!
The data clause does not have to be [0:end-start]. The lower bound and the upper bound can be expressions. You must ensure, however that lower_bound < upper_bound