Can O(N+NM) be simplified to O(NM)?

。_饼干妹妹 提交于 2021-01-05 06:41:28

问题


Suppose that N and M are two parameters of an algorithm. 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?


回答1:


TL;DR Yes

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.

You can find a more precise definition of Big-Oh here, but some example consequences are that:

  • O(1000*N+N^2) = O(N^2) since N^2 will dwarf 1000*N as soon as N>1000

  • O(N+M) cannot be simplified

  • O(N+NM) = O(NM) since N+NM < 2(NM) as soon as N>1 and M>1




回答2:


Obviously, you cannot get rid of the N term if M=0. so let's assume 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). 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.



来源:https://stackoverflow.com/questions/56957916/can-onnm-be-simplified-to-onm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!