[英]Can O(N+NM) be simplified to O(NM)?
Suppose that N
and M
are two parameters of an algorithm. 假设N
和M
是算法的两个参数。 Is the following simplification correct? 以下简化是否正确?
O(N+NM) = O[N(1+M)] = O(NM)
In other words, is it allowed to remove the constant in such a context? 换句话说,在这种情况下是否允许删除常量?
Obviously, you cannot get rid of the N
term if M=0
. 显然,如果M=0
,则您不能摆脱N
项。 so let's assume M>0
. 因此,假设M>0
。 Take a constant k > 0
such that 1<=kM
(if M
is integer, k=1
, otherwise take a constant c
such that 0 < c <= M
an take k=1/c
). 取常数k > 0
使得1<=kM
(如果M
是整数,则k=1
,否则取常数c
使得0 < c <= M
a取k=1/c
)。 We have 我们有
N+NM = N(1+M)
<= N(kM+M) ; 1<=kM
= (k+1)NM
∊ O(NM)
On the other hand, 另一方面,
NM <= N+NM ∊ O(N+NM)
Hence the equality. 因此,平等。
TL;DR Yes TL; DR是
Explanation 说明
By the definition of the Big-Oh notation, if a term inside the O(.) is provably smaller than a constant times another term for all sufficiently large values of the variable, then you can drop the smaller term. 根据Big-Oh表示法的定义,如果对于变量的所有足够大的值,O(。)内部的项证明是小于常数乘以另一个项的常数,则可以删除较小的项。
You can find a more precise definition of Big-Oh here , but some example consequences are that: 您可以在此处找到Big-Oh的更精确定义,但是一些示例结果是:
O(1000*N+N^2) = O(N^2) since N^2 will dwarf 1000*N as soon as N>1000 O(1000 * N + N ^ 2)= O(N ^ 2),因为只要N> 1000,N ^ 2就会使1000 * N相形见war
O(N+M) cannot be simplified O(N + M)无法简化
O(N+NM) = O(NM) since N+NM < 2(NM) as soon as N>1 and M>1 O(N + NM)= O(NM),因为只要N> 1和M> 1,N + NM <2(NM)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.