什么是嵌套循环的 Big-O,其中内循环的迭代次数由外循环的当前迭代确定?

发布于 2024-07-11 08:26:35 字数 259 浏览 5 评论 0原文

以下嵌套循环的 Big-O 时间复杂度是多少:

for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
        System.out.println("i = " + i + " j = " + j);
    }
}

仍然是 O(N^2) 吗?

What is the Big-O time complexity of the following nested loops:

for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
        System.out.println("i = " + i + " j = " + j);
    }
}

Would it be O(N^2) still?

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

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

发布评论

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

评论(8

娇女薄笑 2024-07-18 08:26:35

是的,它仍然是 O(n^2),它有一个更小的常数因子,但这并不影响 O 表示法。

Yep, it's still O(n^2), it has a smaller constant factor, but that doesn't affect O notation.

无需解释 2024-07-18 08:26:35

是的。 回想一下 Big-O 的定义:O(f(n)) 根据定义,运行时间 T(n)kf(n) em> 对于一些常数k。 在这种情况下,步数将为(n-1)+(n-2)+...+0,重新排列为0到n-1的总和; 这是

T(n)=(n-1)((n-1)+1)/2

重新排列一下,您可以看到 T(n) 将始终 ≤ 1/2(n²); 根据定义,因此T(n) = O(n²)

Yes. Recall the definition of Big-O: O(f(n)) by definition says that the run time T(n)kf(n) for some constant k. In this case, the number of steps will be (n-1)+(n-2)+...+0, which rearranges to the sum of 0 to n-1; this is

T(n)=(n-1)((n-1)+1)/2.

Rearrange that and you can see that T(n) will always be ≤ 1/2(n²); by the definition, thus T(n) = O(n²).

-残月青衣踏尘吟 2024-07-18 08:26:35

如果忽略 System.out.println,它是 N 平方。 如果你假设它所花费的时间在其输出中是线性的(当然,它很可能不是),我怀疑你最终会得到 O ( (N^2) * log N) 。

我提到这一点并不是为了挑剔,只是想指出,在计算复杂性时,您不仅需要考虑明显的循环 - 您还需要考虑您所调用的复杂性。

It's N squared if you ignore the System.out.println. If you assume that the time taken by that will be linear in its output (which it may well not be, of course), I suspect you end up with O ( (N^2) * log N).

I mention this not to be picky, but just to point out that you don't just need to take the obvious loops into account when working out complexity - you need to look at the complexity of what you call as well.

飘逸的'云 2024-07-18 08:26:35

让我们跟踪每次迭代中每个循环执行的次数。

for (int i = 0; i < N; i++) {  // outer loop
    for (int j = i + 1; j < N; j++) {  // inner loop
        System.out.println("i = " + i + " j = " + j);
    }
}

在外循环的第一次迭代 (i = 0) 中,内循环执行 N - 1 次。

在外循环的第二次迭代 (i = 1) 中,内循环执行 N - 2 次。

在外循环的第三次迭代 (i = 2) 中,内循环执行 N - 3 次。



在外循环的第 N - 2 次迭代 (i = N - 3) 中,内循环执行 2 次。

在外循环的第 N - 1 次迭代 (i = N - 2) 中,内循环执行 1 次。

在外循环的最后(N)迭代中 (i = N - 1),内循环执行 0 次。

因此,该代码执行的总次数为

N - 1 + N - 2 + N - 3 + … + 2 + <代码>1 + <代码>0

= <代码>0 + <代码>1 + <代码>2 + … + N - 3 + N - 2 + N - 1

将其代入自然数求和公式中,

= (N - 1)((N - 1) + 1) / 2

= (N - 1)(N) / 2

= ((N^2) - N) / 2

= O(N^2),假设 System.out.println 在常数时间内执行 O(1)

——————

另外,请看看这些

  1. https://stackoverflow.com/a/71805214/17112163
  2. https://stackoverflow.com/a/71146522/17112163
  3. https://stackoverflow.com/a/69821878/17112163
  4. https://stackoverflow .com/a/72046825/17112163
  5. https://stackoverflow.com/a/72046933/17112163

Let us trace the number of times each loop executes in each iteration.

for (int i = 0; i < N; i++) {  // outer loop
    for (int j = i + 1; j < N; j++) {  // inner loop
        System.out.println("i = " + i + " j = " + j);
    }
}

In the first iteration of the outer loop (i = 0), the inner loop executes N - 1 times.

In the second iteration of the outer loop (i = 1), the inner loop executes N - 2 times.

In the third iteration of the outer loop (i = 2), the inner loop executes N - 3 times.

.
.
.

In the N - 2th iteration of the outer loop (i = N - 3), the inner loop executes 2 times.

In the N - 1th iteration of the outer loop (i = N - 2), the inner loop executes 1 time.

In the last (Nth) iteration of the outer loop (i = N - 1), the inner loop executes 0 times.

Therefore, the total number of times this code executes is

N - 1 + N - 2 + N - 3 + … + 2 + 1 + 0

= 0 + 1 + 2 + … + N - 3 + N - 2 + N - 1

Substituting this in the Sum of Natural numbers Formula,

= (N - 1)((N - 1) + 1) / 2

= (N - 1)(N) / 2

= ((N^2) - N) / 2

= O(N^2), assuming System.out.println executes in constant time O(1).

——————

Also, do take a look at these

  1. https://stackoverflow.com/a/71805214/17112163
  2. https://stackoverflow.com/a/71146522/17112163
  3. https://stackoverflow.com/a/69821878/17112163
  4. https://stackoverflow.com/a/72046825/17112163
  5. https://stackoverflow.com/a/72046933/17112163
瞳孔里扚悲伤 2024-07-18 08:26:35

如果 N = 10,则迭代次数将为:10+9+8+7+6+5+4+3+2+1。 (这是:十次迭代加九次迭代加八次迭代......等等)。

现在,您需要计算加法可以得到 N 次(在我的示例中,N = 10):

1:(10), 2:(9+1), 3:(8+2), 4: (7+3)、5:(6+4)。 这是 5 次...并且仍然是 5 次迭代。

现在您知道有 5 个十 + 5:

10(5) + 5

就 f(N)(或 n)而言,我们可以轻松看出:

f(n) = n(n/2) + n/2 = (n^2)/2 + n/2 = (n^2 + n)/2...这正是这些嵌套循环的复杂性。

但是,考虑到 Big O 的渐近行为,我们可以去掉 f(n) 中不太重要的值,即单个 n 和分母。

结果:O(n^2)

If you had N = 10, your iterations would be: 10+9+8+7+6+5+4+3+2+1. (this is: ten iterations plus nine iterations plus eight iterations... etc.).

Now, you need to find into the addition how many times you can get a N (N = 10 in my example):

1:(10), 2:(9+1), 3:(8+2), 4:(7+3), 5:(6+4). Which is 5 times... and still remains 5 iterations.

Now you know that you have five tens + 5:

10(5) + 5

In terms of f(N) (or n), we can easily see that this would be:

f(n) = n(n/2) + n/2 = (n^2)/2 + n/2 = (n^2 + n)/2... this is exactly the complexity of these nested loop.

But, considering the asymptotic behavior of Big O, we can get rid of the less significant values of f(n), which are the single n and the denominator.

Result: O(n^2)

南城旧梦 2024-07-18 08:26:35

是的,它是N平方。 如果我没记错的话,实际步数是 1 到 N 的总和,即 0.5*(N - 1)^2。 Big O 只考虑最高指数而不考虑常数,因此,这仍然是 N 平方。

Yes, it would be N squared. The actual number of steps would the sum of 1 to N, which is .5*(N - 1)^2, if I'm not mistaken. Big O only takes into account the highest exponant and no constants, and thus, this is still N squared.

书信已泛黄 2024-07-18 08:26:35

AFAIL,从内循环开始到外循环是计算嵌套循环复杂性的充分方法。
输入图片此处描述

AFAIL, being begun from inner loop through outer ones is adequate way for calculation of nested loop complexity.
enter image description here

坚持沉默 2024-07-18 08:26:35

对于给定的程序:

for (int i = 0; i < n; i++)
    for (int j = i; j < n; j++)
        println(...);

考虑 n = 3:

i 的值为 0、1 和 2。

For i = 0: println will execute 3 times
for i = 1: println will execute 2 times
for i = 2: println will execute 1 times

因此 println 函数将执行 3 + 2 + 1 次,即 n (n+1)/2 次。

因此 O(n(n+1)/2) = O(n^2)。

For the given program:

for (int i = 0; i < n; i++)
    for (int j = i; j < n; j++)
        println(...);

Consider n = 3:

i will have the values 0, 1 and 2.

For i = 0: println will execute 3 times
for i = 1: println will execute 2 times
for i = 2: println will execute 1 times

Thus the println function will execute 3 + 2 + 1 times, i.e. n(n+1)/2 times.

Hence O(n(n+1)/2) = O(n^2).

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