简体   繁体   English

在算法分析过程中找到上限有什么不同的行为?

[英]What is it with different behaviours in finding upper bound during algorithm analysis?

I'm learning Algorithm Analysis and Big-O notation. 我正在学习算法分析和Big-O表示法。 There are these exercise examples for reference: 这些练习示例可供参考:

Example-1: Find upper bound for f(n) = 3n + 8 示例1:找到f(n)= 3n + 8的上限

Solution: 3n + 8 ≤ 4n, for all n ≥ 8 解决方案: 3n + 8≤4n,对于所有n≥8

       ∴ 3n + 8 = O(n) with c = 4 and n0 = 8

Another One, 另一个,

Example-2: Find upper bound for f(n) = n^2 + 1 示例2:找到f(n)= n ^ 2 +1的上限

Solution: n^2 + 1 ≤ 2(n^2), for all n ≥ 1 解:对于所有n≥1,n ^ 2 + 1≤2(n ^ 2)

       ∴ n^2 + 1 = O(n^2) with c = 2 and n0 = 1

Now here comes the next example and the one that's bugging me, 现在是下一个示例,它困扰着我,

Example-3: Find upper bound for f(n) = 2n^3 – 2n^2 示例3:找到f(n)= 2n ^ 3 – 2n ^ 2的上限

Solution: 2n^3 – 2n^2 ≤ 2n^3, for all n ≥ 1 解: 2n ^ 3 – 2n ^ 2≤2n ^ 3,对于所有n≥1

       ∴ 2n^3 – 2n^2 = O(n^3 ) with c = 2 and n0 = 1

Why did we use 2n^3 for comparison in the last example? 为什么在上一个示例中使用2n ^ 3进行比较?

Means In every example we used greater values ie In first example we used 4n because the equation has 3n as maximum limit, 均值在每个示例中,我们都使用了更大的值,即在第一个示例中,我们使用了4n,因为方程式有3n作为最大极限,

In second example we used 2(n^2), because n^2 was the maximum in that equation. 在第二个示例中,我们使用2(n ^ 2),因为n ^ 2是该方程式中的最大值。

Now that means in the third equation we should use 3(n^3) instead of 2(n^2). 现在这意味着在第三个方程中,我们应该使用3(n ^ 3)而不是2(n ^ 2)。

Maybe I'm not getting something here, Can you elaborate the missing pieces? 也许我在这里没得到什么,您能详细说明丢失的部分吗?

And what is the need for c and n0 here. 在这里对c和n0的需求是什么。 n0 is the point from which we consider the rate of growth for given algorithm but why c? n0是我们考虑给定算法的增长率的起点,但是为什么是c? I'm new to algorithmic analysis. 我是算法分析的新手。

The terms are replaced so that all the exponents are the same. 替换这些术语,以便所有指数相同。 For an upper-bound, you want to replace them with larger values. 对于上限,您想将它们替换为较大的值。 Increasing the exponent will produce larger values for positive terms, but for negative terms then you can replace them with 0 instead, removing the term. 增加指数将对正项产生较大的值,但对于负项,则可以将其替换为0,将其删除。

For 3n + 8 , 3n + n is an upper-bound because 8 <= n for n > n0 . 对于3n + 8 3n + n是一个上限,因为对于n > n0 8 <= n

For n^2 + 1 , n^2 + n^2 is an upper-bound because 1 <= n^2 for n > n0 . 对于n^2 + 1n^2 + n^2是一个上限,因为当n > n0 1 <= n^2

For 3n^3 - 2n^2 , 3n^3 + 0 is an upper-bound because -2n^2 <= 0 for n > n0 . 对于3n^3 - 2n^2 -2n^2 <= 0 3n^3 + 0是一个上限,因为对于n > n0 -2n^2 <= 0


c and n0 are needed because they're part of the definition of Big-O: cn0是必需的,因为它们是Big-O定义的一部分:

`f(n) = O(g(n))` means that `f(n) <= c.g(n)` for some c and large enough n

By finding values of c and n0 , you can show that some functions fit this definition where c is what you need to multiply g by to make it larger than f . 通过查找cn0值,您可以证明某些函数符合此定义,其中c是将g乘以使其大于f

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

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