简体   繁体   English

O(2m + n)和O(kn)的时间复杂度

[英]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 例如,假设有一个函数迭代大小为m两次的数组和一个大小为n的数组

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)? 将其时间复杂度写为O(2m + n)= O(m + n)是否正确,因为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. 而对于另一个问题,如果我有一个迭代大小为n的数组的函数,k次,其中k是在调用函数之前从输入计算的。

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? 可以说它的时间复杂度是O(k * n)= O(n),原因与第一个问题相同吗?

The first one is correct because the number 2 is constant, therefore it makes no difference to time coplexity. 第一个是正确的,因为数字2是恒定的,因此它对时间复杂性没有影响。

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) . 如果将k视为永不改变的常数,则时间复杂度为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. 然而,如果k是可变的,那么时间复杂度严格地是O(kn) ,因为k可以增加或减少,因此它在渐近时间复杂度上产生差异。

Therefore, in your case, the complexity is strictly O(kn) . 因此,在您的情况下,复杂性严格为O(kn)

Is it correct to write its time complexity as O(2m+n) = O(m+n), since O(2m) = O(m)? 将其时间复杂度写为O(2m + n)= O(m + n)是否正确,因为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) 大表达式的术语可以分成单独的大表达式,因此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? 可以说它的时间复杂度是O(k * n)= O(n),原因与第一个问题相同吗?

No, because k is not a constant. 不,因为k不是常数。

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 在第一个有3个循环的函数中,其中两个从(零)到(m)开始,第三个函数从(零)到(n)开始,所以这个函数的时间复杂度为o(2m + n) = o (m + n) = o(n) ,这种情况称为“线性时间复杂度”,这意味着当(m)或(n)的值增加时,时间复杂度线性增加,因此第一个函数的时间复杂度为真

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) 在第二个具有两个嵌套循环的函数中,内部循环从(零)开始到(n)和重复(k)次,例如if(k = 3)和(n = 2),这意味着内部循环开始从(零)到(2)并重复3次,所以时间复杂度为o(2 + 2 + 2)= o(2 * 3)= o(n k),因此第二函数的时间复杂度为o (n k)

Let's talk about this from the definition of big O: 让我们从大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). 我们说f(n)= O(g(n)),如果对于任何正n 0,存在常数C = C(n 0),这样对于任何n> n 0,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). 所以,在你的第一种情况下,如果我们选择一个常数C,如2,我们有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). 但是对于第二种情况,对于你选择的任何一个C,存在这样一个k来使要求失败,所以第二种情况不能是O(n)。 It's in fact O(kn). 实际上是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) (如果变量k被调节,你可以说它是O(n),在这种情况下你可以选择一个大于k的上限的C)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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