简体   繁体   English

是n log n = O(n)且是n log n = Omega(n)

[英]Is n log n = O(n) and is n log n = Omega(n)

Hey guys i have been asked the above 2 questions with an explanation as to why. 嗨,有人问我上述两个问题,并解释了原因。 I'm quite confused, i know that the time it takes for an O(nlogn) algorithm to run grows faster then a linear O(n) algorithm but not quite sure the answer to these. 我很困惑,我知道O(nlogn)算法运行所花的时间要比线性O(n)算法快,但是不确定这些问题的答案。 I'm fairly sure that n log n is not equivalent to O(n) but im not quite sure how to explain it. 我相当确定n log n不等于O(n),但是我不太确定如何解释它。 (i don't think we need to do an exact proof) (我认为我们不需要做确切的证明)

Claim: n*log(n) is not O(n) Proof: the proof is by contradiction. 声明: n*log(n)不是O(n)证明:证明是矛盾的。 Suppose that n*log(n) were O(n) . 假设n*log(n)O(n) Then by the definition of O there must exist constants n0 and c such that for all n > n0 , n*log(n) <= c * n . 然后根据O的定义,必须存在常数n0c ,使得对于所有n > n0n*log(n) <= c * n Dividing both sides by n gives log(n) <= c . 将两侧除以n得出log(n) <= c However, there is no constant c such that log(n) <= c for all n > n0 ; 但是,没有常数c使得log(n) <= c对于所有n > n0 consider the sequence n = 2^0, 2^1, 2^2, …, 2^k, … This is a contradiction; 考虑序列n = 2^0, 2^1, 2^2, …, 2^k, …这是一个矛盾。 so, the supposition was incorrect. 因此,该假设是不正确的。 In other words, n*log(n) cannot be O(n) . 换句话说, n*log(n)不能为O(n)

Claim: n*log(n) is Omega(n) Proof: the proof is direct. 声明: n*log(n)Omega(n)证明:证明是直接的。 Choose n0 = 1 and c = 1 . 选择n0 = 1c = 1 Then n*log(n) >= n = 1*n = c*n , as required. 然后,根据需要n*log(n) >= n = 1*n = c*n

Simple answer: 简单答案:

With ignoring some details you can say that f(n) = O(g(n)) means f(n) <= g(n) (not an actual inequality but just in terms of growth). 忽略一些细节,您可以说f(n)= O(g(n))意味着f(n)<= g(n)(不是实际的不平等,而仅是增长方面的)。 Also, you can say that f(n)=Omega(g(n)) means f(n) >= g(n). 同样,您可以说f(n)= Omega(g(n))意味着f(n)> = g(n)。 (We can extend this to Theta, little o and little omega as well). (我们可以将其扩展到Theta,o和o)。

So basically, if you have an intuition that nlogn is a bigger function than n, then you can say: n log n = Omega(n) or equivalently n = O(n log n). 因此,基本上,如果您直觉nlogn是比n大的函数,那么您可以说:n log n = Omega(n)或等效地n = O(n log n)。 But not the other way around. 但并非相反。

Therefore, nlogn=O(n) is a wrong statement. 因此,nlogn = O(n)是错误的语句。

Sophisticated answer: 复杂的答案:

If you want to prove that f(n)=O(g(n)), then you need to find certain constants that satisfy the Big-O definition. 如果要证明f(n)= O(g(n)),则需要找到满足Big-O定义的某些常数。 You can look it up but I'm not gonna get into the details of that. 您可以查找一下,但我不会详细说明。

However, if you want to prove that f(n) is not in O(g(n)), then simply said you want to prove that f(n) <= g(n) is a wrong statement. 但是,如果要证明f(n)不在O(g(n))中,则简单地说,就是要证明f(n)<= g(n)是错误的陈述。 As a result, it should be the case that f(n) > g(n). 结果,应该是f(n)> g(n)的情况。 Now, you can express the strictly larger (smaller) comparison as little-o or little-omega. 现在,您可以将严格较大(较小)的比较表示为little-o或little-omega。 So, to prove that f(n) is not in O(g(n)) you should actually prove that f(n) > g(n) which translates to f(n) = little-omega(g(n)). 因此,要证明f(n)不在O(g(n))中,您实际上应该证明f(n)> g(n)转换为f(n)= little-omega(g(n)) 。 To prove little o and little omega relationships you have to use limits. 为了证明很少的o关系和很小的omega关系,您必须使用限制。

A very good reference to learn about these formal proofs in details is "Introduction to Algorithms, Third Edition" by Cormen. 要详细了解这些形式证明,一个很好的参考是Cormen的“算法简介,第三版”。

n logn is not O(n) because it increases faster than n meaning that n is not an upper bound for it. n logn不是O(n)因为它比n增加快,这意味着n不是它的上限。

On the other hand n log n is Ω(n) because n is a lower bound for it. 另一方面, n log nΩ(n)因为n是其下限。 The definition of Ω is that for f(n)=Ω(g(n)) means that for any c there exists an n such that 0<=cg(n)<=f(n) . Ω的定义是for f(n)=Ω(g(n)) means that for any c there exists an n such that 0<=cg(n)<=f(n)

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

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