O表示法帮助

时间:2009-10-18 13:48:59

标签: java algorithm big-o time-complexity

我对本周的课堂作业感到困惑,这是我真正想要学习的课程,因为有一次我以为我会做额外的阅读!!!!

该方法是为我们提供的,我只是写了一些测试用例。这是我的知识有点朦胧的地方。如果时间增加,那么我低估了我相信的复杂性?在这种情况下,n ^ 3不够,n ^ 4太多,因此逐渐减少到0.

这意味着2之间有一个强制性,这就是log n进来的地方,因为log n是一个小于n的值?但据我所知,这是

我真的希望有人可以通过一个更好的解释清除这种混乱,而不是演讲幻灯片,因为它们根本没有给我任何感谢,谢谢

/**
 * Number 5
 */
public int Five(int n)
{
    int sum=0; 
    for(int i=0; i<n; i++){
        for(int j=0; j<i*i; j++){
            sum++;
        }
    }

    return sum;
}

   public void runFive()
    {
// Test n^2 complexity 
//         System.out.println("We are passing the value 5, This returns us an n value of " + Five(5) + " , With a time complexity of " + complexityN2(Five(5), 5) + " This is to test the value of 5 in a n^2 test" );
//         System.out.println("We are passing the value 10, This returns us an n value of " + Five(10) + " , With a time complexity of " + complexityN2(Five(10), 10) + "This is to test the value of 10 in a n^2 test" );
//         System.out.println("We are passing the value 100, This returns us an n value of " + Five(100) + " , With a time complexity of " + complexityN2(Five(100), 100) + "This is to test the value of 100 in a n^2 test" );
//         System.out.println("We are passing the value 1000, This returns us an n value of " + Five(1000) + " , With a time complexity of " + complexityN2(Five(1000), 1000) + "This is to test the value of 1000 in a n^2 test" );
//         System.out.println("We are passing the value 10000, This returns us an n value of " + Five(10000) + " , With a time complexity of " + complexityN2(Five(10000), 10000) + "This is to test the value of 10000 in a n^2 test" );

// Test n^3 complexity          
//         System.out.println("We are passing the value 5, This returns us an n value of " + Five(5) + " , With a time complexity of " + complexityN3(Five(5), 5) + " This is to test the value of 5 in a n^3 test" );
//         System.out.println("We are passing the value 10, This returns us an n value of " + Five(10) + " , With a time complexity of " + complexityN3(Five(10), 10) + "This is to test the value of 10 in a n^3 test" );
//         System.out.println("We are passing the value 100, This returns us an n value of " + Five(100) + " , With a time complexity of " + complexityN3(Five(100), 100) + "This is to test the value of 100 in a n^3 test" );
//         System.out.println("We are passing the value 1000, This returns us an n value of " + Five(1000) + " , With a time complexity of " + complexityN3(Five(1000), 1000) + "This is to test the value of 1000 in a n^3 test" );
//         System.out.println("We are passing the value 10000, This returns us an n value of " + Five(10000) + " , With a time complexity of " + complexityN3(Five(10000), 10000) + "This is to test the value of 10000 in a n^3 test" );
//         

//Test n^4 complexity
        System.out.println("We are passing the value 5, This returns us an n value of " + Five(5) + " , With a time complexity of " + complexityN4(Five(5), 5) + " This is to test the value of 5 in a n^3 test" );
        System.out.println("We are passing the value 10, This returns us an n value of " + Five(10) + " , With a time complexity of " + complexityN4(Five(10), 10) + "This is to test the value of 10 in a n^3 test" );
        System.out.println("We are passing the value 100, This returns us an n value of " + Five(100) + " , With a time complexity of " + complexityN4(Five(100), 100) + "This is to test the value of 100 in a n^3 test" );
        System.out.println("We are passing the value 1000, This returns us an n value of " + Five(1000) + " , With a time complexity of " + complexityN4(Five(1000), 1000) + "This is to test the value of 1000 in a n^3 test" );
        System.out.println("We are passing the value 10000, This returns us an n value of " + Five(10000) + " , With a time complexity of " + complexityN4(Five(10000), 10000) + "This is to test the value of 10000 in a n^3 test" );

    }        

以下是复杂性方法

public double complexityN2(double time, double n)
{
    return time / (n * n);
}

public double complexityN3(double time, double n)
{
    return time / (n * n * n);
}

 public double complexityN4(double time, double n)
{
    return time / (n * n * n * n);
}

public double complexityLog(double time, double n)
{
    return time / (Math.log(n) * (n*n));
}

5 个答案:

答案 0 :(得分:5)

请记住,big-O表示法描述了项目数量接近无穷大时的行为。因此,在处理几乎任何实际的计算量时,您不应期望看到精确的拟合。事实上,在任何情况下你都不一定能看到完全拟合 - 它可能渐近地接近拟合,但即使(从实际角度来看)所涉及的数量非常大,它仍然不是非常贴合。对于您用于部分测试的数字(例如,5,10,100)而言,即使充其量,拟合通常也会非常。

从时间的角度来看,Java的大多数实现也使生活变得更加困难。问题在于,大多数JVM会解释某些代码的前几个(其中“很少”是相当松散定义的)迭代,然后才决定它们的执行频率足以值得编译为更优化的机器代码。你使用的数字几乎肯定足够小,在某些情况下,你是定时解释代码和其他编译代码(在那里的某个地方,获得执行,包括编译代码所花费的时间)。这对big-O表示法的准确性没有实际影响,但是(特别是对于小数字)可以并且将对你的时间与拟合大O的预测的接近程度产生重大影响。

答案 1 :(得分:4)

问题中唯一的问号出现在这句话的末尾:

  

这意味着2之间有一个强制性,这就是log n的来源,因为log n是一个小于n的值?

这句话不是问题,而是一个声明:你在这里问什么?

如果你问log(n)是什么,那么它是数字p,当10(表示为log 10 )或e时(说话时)关于自然对数)被提升到该幂(即10 p ,e p )得到n。因此,当n增加时它会非常缓慢地上升(这与事实上的指数增长完全相反):

log 10 (10)为1(10 1 == 10)
log 10 (100)是2(10 2 == 100)
log 10 (1000)为3(10 3 == 1000)

如果您已经知道这一切,请道歉。

答案 2 :(得分:4)

  

在这种情况下n ^ 3不够

那不是真的。 Five中的外循环恰好运行n次。对于i的每个值,内部循环精确地运行i²次,因此外部循环所执行的步数是i²的总和,而i从0运行到n-1,即n / 6 - n²/ 2 +n³/ 3(简单用感应证明)。这是三度多项式,因此它是O(n³)。

答案 3 :(得分:3)

我担心你没有正确接近这个问题:盲目地测试功能只会让你到目前为止。

O()符号实际上就像是说,对于x的一个非常大的值,函数在时间上完成(aO(x)),其中a是任意常量(可以是0.00001以及6305789932)。 / p>

让我们看看代码:内部循环执行i 2 次,而(外部循环)执行n次,i从0到n。

现在,执行内部操作(sum ++)Sum i = 1,n i 2 , 其中,wikipedian智慧变为(*):

然后是时候应用O()表示法了。对于大n(比如10 100 ),n 3 压倒n 2 甚至更多n 1 ,所以你只需丢弃它们:O(*)= O(n 3 ),这是练习的解决方案。

HTH

答案 4 :(得分:0)

尝试像这样理解 - 我们需要找到循环执行的次数以找出时间复杂度。 此处的总和也表示相同的数字,这就是为什么您可以在复杂度函数中使用它来代替时间的原因。该假设基于以下假设:语句的每个处理都需要一个恒定的时间。 如果我们计算循环运行的次数 - 因为i = 0 内循环运行0次 因为i = 1 内循环运行一次 因为i = 2 内循环运行4次 因为我= 3 内循环运行9次 所以对于i = m 内环运行m * m次

因此,处理的语句总数可以是 - sum = 0 + 1 + 4 + 9 + .... + m m + ... +(n-1)(n-1) sum = 1 + 4 + 9 + .... + m m + ... +(n-1)(n-1) 这些是自然数的正方形 前N个自然数之和可以是 - N(N + 1)(2N + 1)/ 6 在我们的例子中N = n-1 所以sum =(n-1)(n)(2n-1)/ 6 sum =(n.n -n)(2n -1)/ 6 sum =(2n.n.n - 2n.n - n.n -n)/ 6 sum =(2n ^ 3 -3n ^ 2 -n)/ 6 sum = 1 / 3n ^ 3 - 1 / 2n ^ 2 -1 / 6n

现在,Big O只会考虑n的最高顺序。 因此,您的复杂性大约为n ^ 3

现在你的时间复杂度函数n ^ 3将精确地取这个数并除以n ^ 3 所以你的总和就像是1/3 - 1 / 2n ^ -1 -1 / 6n ^ -2。

和n ^ 4,一个更小的数字,随着n的增加会变得更小,这可以解释为逐渐减少为0.