首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >作业-大O分析

作业-大O分析
EN

Stack Overflow用户
提问于 2012-03-13 13:16:58
回答 4查看 10.1K关注 0票数 3

我的作业涉及大O分析,我想我已经掌握了它的诀窍,但我不是百分之百确定。你们这些可爱的人会不会介意看一眼,告诉我我是不是走对了路?

作业如下所示。对于问题1和3,我的分析和答案在//符号之后的右侧。对于问题2,我的分析和答案低于算法类型。

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

代码语言:javascript
运行
复制
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?
EN

回答 4

Stack Overflow用户

发布于 2012-03-13 13:39:10

我看了你的问题1到3,它看起来没问题。

遵循以下规则,自己检查:

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

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

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

4)任何多项式支配任何对数例如n支配(log )3

至于问题4,请使用下面的指南(从最小到最大):

Log2 n

票数 2
EN

Stack Overflow用户

发布于 2012-03-13 14:46:35

时间计算的(b)项的答案是错误的。您不能假设n中的一个,因为constant.So 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以上的分析,然后让你知道..

如果你对我的回答有任何疑问,你可以在上面发表评论。

票数 1
EN

Stack Overflow用户

发布于 2012-03-13 15:47:02

@op你能告诉我为什么你认为O(nlgn) = O(lg )吗?据我所知,您对Q2的分析b部分实际上是对O(lg )算法的分析,要分析nlgn算法,您需要将左边的n计算在内。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9678728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档