Whats the total BigO complexity of an algorithm like the one below:
function void a(int n, int p) {
for(i = 0; id < n; i++){
print(i)
for(j=0; j < p; j++){
print(i+j)
}
}
}
A formal and efficient way is to use the following methodology:
Where c is a constant.
So, it´s the value instead of (logarithmic) length.
If i have n=5 and p=10, how much prints would this give?
50. This means n*p is correct
Think about it... you do n times p loops. So O(n*p).
Related
I'm wondering why is the tightest Big-Oh Complexity of the following code O(n^4) instead of O(n^5):
int sum = 0;
for(int i=1; i<n ; i++){ //O(n)
for(int j=1; j<i*i; j++){ // O(n^2)
if(j%i == 0)
for(int k=0; k<j; k++) //O(n^2)
sum++;
}
}
Can anyone help me with this?
Thank you!
From math we know that a number x has at most 2*x^0.5 divisors.
So if statement will be executed j^0.5 times in worst case
which equals to n since largest j is n*n
It backs to the inner-most if condition. As j%i == 0 is only true for j = {i, 2*i, 3*i, ..., i * i}, just i times the second loop will run in O(i^2). Hence, the tight complexity is O(n^4).
I have a function test(arr, x) where arr is the array of finite size and x is the finite positive number. I have two nested for loops as below:
function test(arr, x) {
for(i=0; i < arr.length; i++) { //executes arr.length times
for(j=1; j <= x; j++) { // executes x times
//code goes here
}
}
}
Question:
What is the time complexity for this case? I'm confused between O(n) and O(n^2).
Any help will be appreciated. Thanks in advance!
for the following code:
for(i=0;i<5;i++)
for(j=2;j<n;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=a[i][k]*b[k][j];
}
I would say the run time is theta(n^3), as I see in the k loop, there is two n (n^3) and on the other loop, another n, making it n^3. Would this be right or what did I miss.
Thank you
Here is your code, formatted:
for (i=0; i < 5; i++) {
for (j=2; j < n; j++) {
c[i][j] = 0;
for (k=0; k < n;k++)
c[i][j] = a[i][k]*b[k][j];
}
}
The outer loop in i only iterates 5 times, and so can just be treated as a constant penalty as far as complexity is concerned. The inner two loops in j and k are independent of each other, and both are O(n). We can therefore just multiple the complexities to get O(n^2) for the overall running time as a function of n.
I have a question about this algorithm, I am having hard time in understanding the complexity
While(input) {
...
array to check
...
for (int i=0; i< arraysize; i++) {
creation first subarray;
creation second subarray;
}
for (int i=0; i < firstsubarraysize; i++) {
addinput to array;
for( int j = 0; j < secondsubarraysize; j++) {
calculate array[j] * input;
}
}
}// endwhile
So, considering input a M variable and the array is N, the big O would be O(m(nlogn)) or O(n2)?
The sub arrays are not always n\2.
I apologize If I'm not very clear.
Generally, for each nested loop, you will have one N in the big O for time complexity.
Here assume the outer most loop while run M times, then there are two nested for inside of it. So the total is O(M N^2).
I'm still learning about complexity measurement using the Big O Notation, was wondering if I'm correct to say that following method's complexity is O(n*log4n), where the "4" is a subscript.
public static void f(int n)
{
for (int i=n; i>0; i--)
{
int j = n;
while (j>0)
j = j/4;
}
}
Yes, You are correct, that the complexity of the function is O(n*log_4(n))
Log_4(n) = ln(n) / ln(4) and ln(4) is a constant, so if Your function has complexity O(n*log_4(n)) it is also true, that it has a complexity of O(n*ln(n))
Did you mean
public static void f(int n)
{
for (int i=n; i>0; i--)
{
int j = i; // Not j = n.
while (j>0)
j = j/4;
}
}
?
In that case, too you are correct. It is O(nlogn). Using the 4 as subscript is correct too, but it only makes it more cumbersome to write.
Even with the j=n line, it is O(nlogn).
In fact to be more accurate you can say it is Theta(n logn).
yes you are right, complexity is n* log4(n)