简体   繁体   English

为给定的运行时 function f(n)=O(n^2)+nlog(n) 找到可能的大 theta?

[英]Finding possible big theta for a given runtime function f(n)=O(n^2)+nlog(n)?

Suppose that f( n ) = O( n 2 ) + n log n .假设 f( n ) = O( n 2 ) + n log n Which of the following are possible?以下哪项是可能的?

  1. f( n ) = Θ(log n ) f( n ) = Θ(log n )
  2. f( n ) = Θ( n ) f( n ) = Θ( n )
  3. f( n ) = Θ( n 2 ) f( n ) = Θ( n 2 )
  4. f( n ) = Θ( n 3 ) f( n ) = Θ( n 3 )

I am a little confused about the runtime function, because of the included O( n 2 ).由于包含 O( n 2 ),我对运行时 function 有点困惑。 I believe the answers are 2 and 3, because each of them can be multiplied by a number to reach the O( n 2 ).我相信答案是 2 和 3,因为它们中的每一个都可以乘以一个数字来达到 O( n 2 )。 Specifically, Θ( n 2 ) can be multiplied by 1 to reach the upper bound O( n 2 ), and Θ( n ) can be multiplied by n to reach the upper bound O( n 2 ).具体来说,Θ( n 2 )可以乘以1达到上界O( n 2 ),而Θ( n )可以乘以n达到上界O( n 2 )。

Am I correct?我对么?

I guess the only correct answer is (3).我想唯一正确的答案是(3)。 O(n^2) is any function that grows as fast as n^2 or slower. O(n^2)是任何 function,其增长速度与n^2一样快或更慢。 n log n = O(n^2) , so O(n^2) + n log n is any function that is asymptotically "between" n log n and n^2 . n log n = O(n^2) ,所以O(n^2) + n log n是任何 function 渐近“介于” n log nn^2之间。 Among all Thetas in your question only the third one fits into these bounds.在您问题中的所有 Thetas 中,只有第三个符合这些界限。

f(n) = O(n^2) + nlogn means that there's ag(n) in O(n^2) such that f(n) = g(n) + nlogn. f(n) = O(n^2) + nlogn 表示 O(n^2) 中有 ag(n) 使得 f(n) = g(n) + nlogn。

g(n) in O(n^2) means that |g(n)| O(n^2) 中的 g(n) 表示 |g(n)| < cn^2 for some positive constant c, and all large enough n. < cn^2 对于一些正常数 c,并且所有足够大的 n。 The absolute value ||绝对值 || bars in the definition allow for the possibility that g(n) is negative.定义中的条允许 g(n) 为负的可能性。

This means 1, 2, 3 can be the right answer.这意味着 1、2、3 可能是正确答案。 f(n) can't be negative because it describes a running time, but there's no reason why the O(n^2) term can't be negative. f(n) 不能为负数,因为它描述了运行时间,但没有理由 O(n^2) 项不能为负数。

  1. g(n) = logn - nlogn is O(n^2), and g(n) + nlogn = logn. g(n) = logn - nlogn 为 O(n^2),并且 g(n) + nlogn = logn。
  2. g(n) = n - nlogn is O(n^2), and g(n) + nlogn = n g(n) = n - nlogn 是 O(n^2),并且 g(n) + nlogn = n
  3. g(n) = n^2 - nlogn is O(n^2), and g(n) + nlogn = n^2 g(n) = n^2 - nlogn 是 O(n^2),并且 g(n) + nlogn = n^2
  4. is not possible.不可能。

(Note the question is phrased in terms of big-theta, but it's possible for f(n) to match the bounds exactly in 1, 2, and 3). (请注意,这个问题是用大θ 来表达的,但 f(n) 有可能与 1、2 和 3 中的界限完全匹配)。

3 is the only solution if you assume the O(n^2) term to be non-negative.如果您假设 O(n^2) 项为非负数,则 3 是唯一的解决方案。

Make sure you go back to the definition of big theta when answering these types of questions.在回答这些类型的问题时,请确保您 go 回到大θ的定义。 For a function f to be in Θ(g(n)) of a function g a few things have to be true:对于 function f要在 function gΘ(g(n))中,必须满足以下条件:

  1. There exists a constant k 1 that is greater than 0存在一个大于 0 的常数 k 1
  2. There exists a constant k 2 that is greater than 0存在一个大于 0 的常数 k 2
  3. There exists some start value, n 0存在某个起始值,n 0
  4. For all n > n 0 , k 1 * g(n) <= f(n) <= k 2 * g(n)对于所有 n > n 0 , k 1 * g(n) <= f(n) <= k 2 * g(n)

(4) basically means that as f(n) grows, k 1 * g(n) grows slower and k 2 * g(n) grows faster for the same n. (4) 基本上意味着随着 f(n) 的增长,对于相同的 n,k 1 * g(n) 增长较慢,而 k 2 * g(n) 增长较快

Luckily the relationship between these functions is really easy to see when they are plotted beside each other:)幸运的是,当它们并排绘制时,这些函数之间的关系真的很容易看出:)

Below we can see all the functions plotted alongside one another:下面我们可以看到所有功能并排绘制:

  • blue is f(n)蓝色是f(n)
  • green is log n绿色是log n
  • purple is n紫色是n
  • black is n^2黑色是n^2
  • red is n^3红色是n^3

绘制所有函数

based on this plot, we can immediately discard log n , n , and n^3 because there are no two constants we could bound these functions by that would grow in such a way that it bound f(n) as n grows.基于这个 plot,我们可以立即丢弃log nnn^3 ,因为没有两个常数可以约束这些函数,它们会随着n的增长而约束f(n)

n^2 however looks promising.然而, n^2看起来很有希望。 We just need to find two constants that allow n^2 to bound the growth of f(n) .我们只需要找到允许n^2限制f(n)增长的两个常数。

Below we can see two such constants:下面我们可以看到两个这样的常量:

  • blue is f(n)蓝色是f(n)
  • green is 1 * n^2绿色是1 * n^2
  • purple is 4 * n^2紫色是4 * n^2

1*n^2 和 4*n^2 与 f(n) 一起绘制

By finding these constants we can say definitively that n^2 + n (log n) is Θ(n^2)通过找到这些常数,我们可以明确地说n^2 + n (log n)Θ(n^2)

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

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