[英]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?
以下哪项是可能的?
- f( n ) = Θ(log n )
f( n ) = Θ(log n )
- f( n ) = Θ( n )
f( n ) = Θ( n )
- f( n ) = Θ( n 2 )
f( n ) = Θ( n 2 )
- 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 n
和n^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) 项不能为负数。
(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))
中,必须满足以下条件:
(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:下面我们可以看到所有功能并排绘制:
f(n)
f(n)
log n
log n
n
n
n^2
n^2
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 n
、 n
和n^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:下面我们可以看到两个这样的常量:
f(n)
f(n)
1 * n^2
1 * n^2
4 * n^2
4 * n^2
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.