【问题标题】:Homework - Big O analysis作业 - 大 O 分析
【发布时间】:2012-03-29 12:58:41
【问题描述】:

我的作业涉及大 O 分析,我想我已经掌握了窍门,但我不能 100% 确定。如果我走在正确的轨道上,你们可爱的人会介意看看并告诉我吗?

作业如下。对于问题 1 和 3,我的分析和答案在右边,在 // 标记之后。对于问题2,我的分析和答案在算法类型下面。

提前感谢您的帮助! :-)

1.For each of the following program fragments, give a Big-Oh analysis of the running time in terms of N:
    (a) // Fragment (a)
        for ( int i = 0, Sum = 0; i < N; i++ )      // N operations
            for( int j = 0; j < N; j++ )            // N operations
                Sum++;                              // Total: N^2 operations  =>  O(N^2)

    (b) // Fragment (b)
        for( int i = 0, Sum = 0; i < N * N; i++ )   // N^2 operations
            for( int j = 0; j < N; j ++ )           // N operations
                Sum++;                              // Total: N^3 operations  =>  O(N^3)

    (c) // Fragment (c)
        for( int i = 0, Sum = 0; i < N; i++ )       // N operations
            for( int j = 0; j < i; j ++ )           // N-1 operations
                Sum++;                              // Total: N(N-1) = N^2 – N operations  =>  O(N^2)

    (d) // Fragment (d)
        for( int i = 0, Sum = 0; i < N; i++ )       // N operations
            for( int j = 0; j < N * N; j++ )        // N^2 operations 
                for( int k = 0; k < j; k++ )        // N^2 operations 
                    Sum++;                          // Total: N^5 operations  =>  O(N^5)

2. An algorithm takes 0.5 milliseconds for input size 100. How long will it take for input size 500 if the running time is:
    a. Linear
        0.5 *5 = 2.5 milliseconds

    b. O( N log N)  
        O (N log N) – treat the first N as a constant, so O (N log N) = O (log N)
        Input size 100 = (log 100) + 1 = 2 + 1 = 3 operations
        Input size 500 = (log 500) + 1= 2.7 + 1 = 3.7 ≈ 4 operations
        Input size 100 runs in 0.5 milliseconds, so input size 500 takes 0.5 * (4/3) ≈ 0.67 milliseconds

    c. Quadratic        
        Input size 100 in quadratic runs 100^2 operations =   10,000 operations
        Input size 500 in quadratic runs 500^2 operations = 250,000 operations = 25 times as many
        Input size of 100 runs in 0.5 milliseconds, so input size of 500 takes 25 * 0.5 = 12.5 milliseconds

    d. Cubic
        Input size 100 in quadratic runs 100^3 operations =     1,000,000 operations
        Input size 500 in quadratic runs 500^3 operations = 125,000,000 operations = 125 times as many
        Input size of 100 runs in 0.5 milliseconds, so input size of 500 takes 125 * 0.5 = 62.5 milliseconds


3. Find the Big-O for the following:
    (a) f(x) = 2x^3 + x^2 log x             // O(x^3)
    (b) f(x) = (x^4 – 34x^3 + x^2 -20)      // O(x^4)
    (c) f(x) = x^3 – 1/log(x)               // O(x^3)

4. Order the following functions by growth rate: (1 is slowest growth rate; 11 is fastest growth rate)
__6_ (a) N
__5_ (b) √N
__7_ (c) N^1.5
__9_ (d) N^2
__4_ (e) N log N
__2_ (f) 2/N        
_11_ (g) 2^N
__3_ (h) 37
_10_ (i) N^3
__1_ (j) 1/ N^2
__8_ (k) N^2 /log N


* My logic in putting (j) and (f) as the slowest is that as N grows, 1/N^2 and 2/N decrease, so their growth rates are negative and therefore slower than the rest which have positive growth rates (or a 0 growth rate in the case of 37 (h)). Is that correct?

【问题讨论】:

  • 虽然与编程密切相关,但这对cs.stackexchange.com 来说是一个更好的问题。如果您以后有这样的问题,请直接在那里。

标签: algorithm big-o analysis


【解决方案1】:

我查看了您的问题 1 到 3,看起来还不错。

遵循这些规则并自行检查:

1) 乘法常数可以省略, 示例 50n^2 简化为 n^2

2) 如果 a>b,则 n^a 支配 n^b 示例 n^3 支配 n^2,因此 n^3 + n^2 + n ,简化为 n3

3) 任何指数支配任何多项式 示例 3^n 支配 n^5 示例 2^n 支配 n^2+5n+100

4) 任何多项式都优于任何对数 例 n 占优 (log n)3

至于问题 4,请使用以下指导(从小到大):

Log2 n

【讨论】:

  • 刚刚注意到指数的格式不正确 - 我打算将它们更改为 ^ 符号但我忘记了。我应该用正确的格式重新发布吗?
  • 好的,我刚刚编辑它以显示指数符号。一切看起来都还好吗?谢谢你的帮助! :-)
【解决方案2】:

时间计算(b)的答案是错误的。你不能假设其中一个为常数。所以 nlogn 变为 1log1,这意味着 log1 为 0。所以 0。

所以答案是 100 log100 操作比较 500log500 ...

从最小到最大。 b 是 4,a 是 5。c、e、k 是位置 6 和 7 和 8 的竞争。 你给出的1,2,3个位置是正确的。9,10,11是正确的。

我会检查一些关于 6,7,8 的分析并告诉你..

如果您需要对我的回答进行任何澄清,您可以对此发表评论..

【讨论】:

  • 如何以最佳方式找到第四个问题的答案。 lt n->infinity f(n)/g(n)=infinity then f(n)>g(n).=constant then f(n)=g(n) =0 then f(n)
【解决方案3】:

@op 你能告诉我你为什么考虑 O(nlgn) = O(lg n) 吗?据我了解,您对第二季度 b 部分的分析实际上是对 O(lg n) 算法的分析,要分析 nlgn 算法,您需要将左侧的 n 考虑在内。

【讨论】:

  • 我一直认为左边的 N 会被认为是一个常数。看来我弄错了。这是一次很棒的学习经历! :-)
【解决方案4】:
  1. (a) 正确
    (b) 正确
    (c) 纠正。 0 + 1 + 2 + ... + (n - 1) = n(n - 1) / 2 = 0.5n^2 - 0.5n = O(n^2)
    (d) 正确(和 (c) 一样,里面有 1/2,但复杂度仍然是 O(N^5))

  2. 一个。正确
    湾。设 K 为一步的持续时间。
    K * (100 log 100) = 0.5,所以 K = 7.5 * 10^-4
    K * (500 log 500) = 7.5 * 1-^-4 * 500 log 500 = 3.3737ms

    或者,(500 log 500) / (100 log 100) = 6.7474
    当 n = 500 时,它会慢 6.7474 倍,6.7474 * 0.5ms = 3.3737ms

    c。正确
    d。正确

  3. (a) 正确
    (b) 正确
    (c) 正确

  4. __5_ (a) N
    __4_ (b) √N
    __7_ (c) N^1.5
    _9_ (d) N^2
    __6_ (e) N log N
    __2_ (f) 2/N
    _11_ (g) 2^N
    _3_ (h) 37
    _10_ (i) N^3
    __1_ (j) 1 / N^2
    __8_ (k) N^2 /log N

我同意 (f) 和 (j) 的定位。但是,您应该知道它们不会“在野外”出现,因为每个算法都至少有一个步骤,因此无法击败 O(1)。更详细的解释见Are there any O(1/n) algorithms?

【讨论】:

  • 谢谢。这正是我需要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多