简体   繁体   中英

Time complexity of O(2m+n) and O(kn)

For example lets say there is a function that iterates array of size m two times and an array of size n one time

func(){
   for(i = 0; i < m; i++){//do something}
   for(i = 0; i < n; i++){//do something}
   for(i = 0; i < m; i++){//do something}
}

Is it correct to write its time complexity as O(2m+n) = O(m+n), since O(2m) = O(m)?

And for another question, if I have a function that iterates array of size n, k-times where k is calculated from the input before the function is called.

func(int k){
    for(int i = 0; i < k; i++){
        for(int j = 0; j < n; j++){}
    }
}

Is it ok to say that its time complexity is O(k*n) = O(n) for the same reason as in the first question?

The first one is correct because the number 2 is constant, therefore it makes no difference to time coplexity.

However the second one could be both wrong and right. If you treat k as a constant which would never change, then the time complexity is O(n) . However, if k is variable then the time complexity is strictly O(kn) , since k can increase or decrease, therefore it makes a difference in asymptotic time complexity.

Therefore, in your case, the complexity is strictly O(kn) .

Is it correct to write its time complexity as O(2m+n) = O(m+n), since O(2m) = O(m)?

Yes. Terms of a big-oh expression can be separated into individual big-oh expressions, so O(2m+n) = O(2m) + O(n) = O(m) + O(n) = O(m+n)

Is it ok to say that its time complexity is O(k*n) = O(n) for the same reason as in the first question?

No, because k is not a constant.

in the first function that's have 3 loops, two of them start from (zero) to (m) and the third one start from (zero) to (n), so the time complexity of this function o(2m+n) = o(m+n) = o(n) , this case called "Linear time complexity" that's mean that when value of (m) or (n) increasing , the time complexity increasing linearly , so the time complexity of the first function is true

in the second function that's have two nested loop the inside loop start from (zero) to (n) and repeated (k)times, for example if (k = 3) and (n = 2) , that's mean that the inside loop start from (zero) to (2) and repeated 3 times , so the time complexity is o(2+2+2) = o ( 2*3 ) = o (n k) , so the time complexity of the second function is o(n k)

Let's talk about this from the definition of big O:

We say f(n) = O(g(n)), if for any positive n₀, there exists a constant C = C(n₀), such that for any n > n₀, it's true that f(n) ≤ Cg(n).

So, in your first case, if we pick a constant C like 2, we have O(2m+n) ≤ O(2m+2n) = O(m+n).

But for the second case, for any C you pick, there exists such a k to make the requirement fail, so the second case cannot be O(n). It's in fact O(kn). (You may say it's O(n) if variable k is regulated, in which case you can pick a C bigger than the upper bound of k)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM