O 表示法帮助

发布于 2024-08-08 23:42:16 字数 4414 浏览 1 评论 0原文

我被这周的课堂作业困住了,这是我真的很想学习的一个主题,所以这一次我想我应该做额外的阅读!

该方法已为我们提供,我只是编写一些测试用例。这是我的知识变得有点模糊的地方。如果时间增加,那么我低估了我相信的复杂性?在这种情况下,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));
}

I am getting stuck with the class work we got this week and its a subject i really want to learn so for once i thought i would do the additional reading!!!!

The method is provided for us and i am just writign some test cases. This is where my knowledge gets a little hazy. If the time increases then i am underestimatign the complexity i believe? in this case n^3 isnt enough and n^4 is too much, hence the gradual reduction to 0.

That means there is a compelxity that lies between the 2, and this is where log n comes in as log n is a value less than n? but this is as far as my knowledge goes

i was really hoping someone could clear this confusion up for me with a better explanation than whats on the lecture slides as they make no sence to me at all, thanks

/**
 * 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" );

    }        

Here are the complexity Methods

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));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北陌 2024-08-15 23:42:16

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

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

Keep in mind that big-O notation describes the behavior as the number of items approaches infinity. As such, you shouldn't expect to see an exact fit when dealing with almost any practical amount of computation. In fact, you don't necessarily see an exact fit under any circumstances -- it may approach a fit asymptotically, but even when (from a practical viewpoint) the number involved is really large, it's still not a very close fit. For as small of numbers as you're using for part of your test (e.g., 5, 10, 100) the fit will often be extremely poor even at best.

From a viewpoint of timing, most implementations of Java make life substantially more difficult as well. The problem is that most JVMs will interpret the first few (where "few" is rather loosely defined) iterations of some code, before they decide it's being executed often enough to be worth compiling to more optimized machine code. The numbers you're using are almost certainly small enough that in some cases you're timing interpreted code and in others compiled code (and somewhere in there, getting an execution that includes the time taken to compile the code as well). This has no real effect on the accuracy of big-O notation, but (especially for small number) can and will have a substantial effect on how closely your timing comes to fitting what big-O would predict.

手长情犹 2024-08-15 23:42:16

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

这意味着 2 之间有一个复杂性,这就是 log n 的用武之地,因为 log n 是一个小于 n 的值?

这句话不是一个问题,而是一个陈述:< strong>你在这里问什么?

如果你问log(n)是什么,那么它就是数字p,当10(表示为log 10) 或 e(当谈论自然对数时)被提升到该幂(即 10p, ep) 产生 n。因此,随着 n 的增加,它的上升速度非常缓慢(实际上与指数增长正好相反):

log10(10) is 1 (101 == 10 )
log10(100) 为 2 (102 == 100)
log10(1000) is 3 (103 == 1000)

如果您已经知道这一切,我们深表歉意。

The only question-mark in your question appears at the end of this sentence:

That means there is a compelxity that lies between the 2, and this is where log n comes in as log n is a value less than n?

That sentence is not a question, it is a statement: what are you asking here?

If you are asking what log(n) is, then it is the number p which, when 10 (denoted log10) or e (when talking about the natural logarithm) is raised to that power (i.e. 10p, ep) yields n. Hence it goes up very slowly as n increases (it is the exact opposite of an exponential increase in fact):

log10(10) is 1 (101 == 10)
log10(100) is 2 (102 == 100)
log10(1000) is 3 (103 == 1000)

Apologies if you already knew all this.

稚然 2024-08-15 23:42:16

在这种情况下 n^3 不够

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

in this case n^3 isnt enough

That's not true. The outer loop in Five runs exactly n times. For each value of i, the inner loop runs exactly i² times, so the number of steps the outer loop does is the sum of i² while i runs from 0 to n-1, which is n/6 - n²/2 + n³/3 (simple to prove with induction). This is a polynomial of third degree, therefore it is O(n³).

零崎曲识 2024-08-15 23:42:16

恐怕您没有正确解决问题:盲目地测试函数只能让您到目前为止。

O() 表示法实际上就像是说,对于非常大的 x 值,函数会在时间 (aO(x)) 内完成,其中 a 是任意常数(可以是 0.00001 也可以是 6305789932)。

我们看一下代码:内循环执行了i2次,而(外循环)执行了n次,i从0到n。

现在,执行内部运算(sum++)Sumi=1,ni2
根据维基百科的智慧,它变成 (*):

然后是时候应用 O() 表示法了。对于大 n(例如 10100),n3 压倒 n2 甚至更多 n1,因此你只需丢弃它们:O(*) = O(n3),这是练习的解决方案。

华泰

I'm afraid you're not approaching the problem correctly: blindly testing against functions will only get you so far.

The O() notation is actually like saying, for a really big value of x, the function completes in time(aO(x)), where a is an arbitrary constant (can be 0.00001 as well as 6305789932).

Let's look at the code: the inner loop is executed i2 times, whereas (outer loop) is executed n times, with i from 0 to n.

Now, the inner operation (sum++) is executed Sumi=1,ni2,
which, by wikipedian wisdom becomes (*):

Then it's time to apply the O() notation. For a big n (say 10100), n3 overwhelms n2 and even more n1, so you just discard them: O(*) = O(n3), which is the solution to the excercise.

HTH

土豪我们做朋友吧 2024-08-15 23:42:16

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

因此处理的语句总数可以为 --
总和 = 0 + 1 + 4 + 9 + .... + mm + ... +(n-1)(n-1)
总和 = 1 + 4 + 9 + .... + mm + ... +(n-1)(n-1)
这些是自然数的平方
前 N 个自然数的和可以计算为 - N(N+1)(2N+1) / 6
在我们的例子中 N=n-1
所以总和 = (n-1)(n)(2n-1) / 6
总和 = (nn -n) (2n -1) /6
总和 = (2n.nn - 2n.n - nn -n) /6
总和 = (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。

Try to understand it like this-
We need to find the number of times the loops will execute to find the time complexity.
The sum here also represents the same number, that is why you can use it in place of time in your complexity functions. This assumption is based on the assumption that each processing of a statement takes a constant time.
If we count the number of times the loops run-
for i = 0
the inner loop runs 0 times
for i = 1
the inner loop runs 1 time
for i = 2
the inner loop runs 4 times
for i = 3
the inner loop runs 9 times
so for i = m
the inner loop runs m*m times

So the total number of statements processed can be found as --
sum = 0 + 1 + 4 + 9 + .... + mm + ... +(n-1)(n-1)
sum = 1 + 4 + 9 + .... + mm + ... +(n-1)(n-1)
These are squares of natural numbers
sum of first N natural numbers can be found as - N(N+1)(2N+1) / 6
in our case N=n-1
so 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

Now, the Big O will only consider the highest order of n..
So your complexity is of the order of n^3

Now your time complexity function for n^3 will take exactly this number and divide it by n^3
so your sum would be like 1/3 - 1/2n^-1 -1/6n^-2.

and for n^4 , an even smaller number, which will get even smaller as n increases which explains gradual reduction to 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文