想知道算法的时间复杂度

发布于 2025-01-10 13:21:09 字数 429 浏览 3 评论 0原文

在我的工作中,我必须实现一个算法,具体细节并不重要,但我无法对这个特定算法的时间复杂度有一个明确的答案。

简而言之,它看起来像这样:

for(let i = 0; i < 3; i++) {
    for(let j = 0; j < n - 1; j++) {
        for(let k = 0; k < 8; k++ {
            // Do some stuff
        }
    }
}

这个算法的时间复杂度是多少?

一开始我以为会是 O(n^3) 之类的。但我想得越多,我就越认为它实际上更像是一个 0(n)。因为,尽管我们有 3 个 for 循环,但只有一个可以是可变大小的,另外两个是恒定的。

那么这个算法的实际时间复杂度是多少呢?是 O(n^3)、O(n) 还是其他?

In my job I had to implement an algorithm, the specifics details does not matters, but I was unable to have a clear answer about the time complexity of this specific algorithm.

In a nutshell it's look like something like that:

for(let i = 0; i < 3; i++) {
    for(let j = 0; j < n - 1; j++) {
        for(let k = 0; k < 8; k++ {
            // Do some stuff
        }
    }
}

What is the time complexity of this algorithm ?

At the beginning I thought it would be something like O(n^3). But the more I think about, the more I think it's actually more a 0(n). Because, even though we have 3 for loop, only one can be of a variable size, the two other are constant.

So what is the actual time complexity of this algorithm ? Is it O(n^3), O(n) or even something else ?

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

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

发布评论

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

评论(5

微暖i 2025-01-17 13:21:09

是的,时间复杂度为O(n)。你是对的。

总执行次数为 3 * n * 8 = 24*n,其中对于较大的 n 可以忽略 24。所以复杂度是O(n)

Yes, it would be O(n). You are right.

The total number of execution is 3 * n * 8 = 24*n where 24 can be ignored for large n. So the complexity is O(n)

盛夏已如深秋| 2025-01-17 13:21:09

直接答案

除了已经给出的很好的答案之外,我希望您能够描绘每个循环的计算:

在此处输入图像描述

某些隐式规则与:

  • 应用的 常数 k 为 O(1)

示例:3 = O(1)8 = O (1)

  • 给定一个变量 n 和一个常量 k 的高值,关系为已验证:O(n + k) = O(n) + k = O(n)

示例:O(n-1) = O(n) -1 = O(n)

  • 给定变量的高值 n 和常数 k,验证关系:kx O(n) = O(kn) = O(n)

示例:<代码>3x O(n) = O(3n) = O(n)

扩展

让我们改变你的循环,让你有 O(n^3) 正如你想知道的那样:

for(let i = 0; i < n-1; i++) {
    for(let j = 0; j < n - 1; j++) {
        for(let k = 0; k < n-1; k++ {
            // Do some stuff
        }
    }
}

因为每个循环都有复杂度为 O(n),我们有 O(n) x O(n) x O(n) = O(n^3)

Direct answer

Besides great answers already given, I'd like you to picture the calculation over each loop:

enter image description here

Certain implicit rules applied are relative to:

  • The complexity of a constant k is O(1)

example: 3 = O(1), 8 = O(1)

  • Given high values of a variable n and a constant k, the relation is verified: O(n + k) = O(n) + k = O(n)

example: O(n-1) = O(n) -1 = O(n)

  • Given high values of a variable n and a constant k, the relation is verified: k x O(n) = O(kn) = O(n)

example: 3x O(n) = O(3n) = O(n)

Extension

Let's change your loop to have case where you'd have O(n^3) as you were wondering:

for(let i = 0; i < n-1; i++) {
    for(let j = 0; j < n - 1; j++) {
        for(let k = 0; k < n-1; k++ {
            // Do some stuff
        }
    }
}

Because each loop has a complexity of O(n), we have O(n) x O(n) x O(n) = O(n^3)

黑色毁心梦 2025-01-17 13:21:09

代码的时间复杂度为 O(n)

由于外循环迭代固定次数,因此内循环每次迭代 n - 1 次,并且对于其中的每一次,最内层的循环都会迭代恒定的次数。

因此,您得到 3 * (n - 1) * 8 = 24n - 24,并且由于常量被大 O 表示法丢弃,您得到 O(n)

话虽这么说,代码的时间复杂度可能会受到最内层循环内部运行的代码(即 // Do some stuff 部分)的影响。如果您只在那里执行固定数量的操作,则代码复杂性不会受到影响。但是,例如,如果您在其中对大小为 n 的数组进行排序,那么这会将时间复杂度更改为 n * nlogn

The time complexity of the code is O(n):

As the outer loop iterates a constant number of times, for each time its inner loop iterates n - 1 times, and for each of these times, the innermost loop iterates a constant number of times.

Therefore you get 3 * (n - 1) * 8 = 24n - 24 and as constants are discarded for big-O notation, you get O(n).

That being said, the time complexity of the code can be affected by the code that runs inside of the innermost loop (i.e the // Do some stuff part). If you are only doing a constant number of operations there, the code complexity will not be affected. However, if for example, you sort an array of size n in there, then this will change the time complexity to n * nlogn.

完美的未来在梦里 2025-01-17 13:21:09

此代码的时间复杂度为 O(n)。外部和内部循环具有恒定的迭代次数,并且与输入大小 (n) 无关。对于大 O 表示法,常量将被丢弃。

Time complexity for this code is O(n). Outer and inner loops have constant number of iterations and are independent from the input size (n). And constants are discarded for big O notation.

独闯女儿国 2025-01-17 13:21:09

时间复杂度将为O(n),因为 3 和 8 是可以忽略的常量。另外,第二个循环将持续到 n - 1,其中 1 也被忽略。注意:当 n 的值变得非常大时,就会出现对常量的无知。

如果在所有这些循环中调用一个函数,那么对该函数的总调用次数为 n,但精确地说,总调用次数将是

(8 * 3 * n) - ((8 * 3) - 1)

我们在大 O 表示法中忽略的。

C 中尝试以下代码:

#include <stdio.h>

size_t my_calls = 1;

void call_me(void){
    my_calls += 1;
}

int main(void)
{
    size_t n = 1000; // let n = 1000

    for(size_t i = 0; i < 3; i++) 
    {
        for(size_t j = 0; j < n - 1; j++) 
        {
            for(size_t k = 0; k < 8; k++) 
            {
                call_me();
            }
        }
    }
    printf("Total calls %lu\n", my_calls);
    if(my_calls == (8 * 3 * n ) - ((8 * 3) - 1))
        printf("GOOD\n");
    else
        printf("BAD\n");
    return 0;
}

实时演示< /strong>

Time complexity will be O(n), because 3 and 8 are constants which can be ignored. Also, the second loop is going till n - 1, where 1 is also ignored. NOTE: All this ignorance of constants is when n becomes really large in value.

If inside all these loop a function is called, then the total calls to the function is n, but in precise manner total call will be

(8 * 3 * n) - ((8 * 3) - 1)

Which we ignore in big O notation.

Try this code in C:

#include <stdio.h>

size_t my_calls = 1;

void call_me(void){
    my_calls += 1;
}

int main(void)
{
    size_t n = 1000; // let n = 1000

    for(size_t i = 0; i < 3; i++) 
    {
        for(size_t j = 0; j < n - 1; j++) 
        {
            for(size_t k = 0; k < 8; k++) 
            {
                call_me();
            }
        }
    }
    printf("Total calls %lu\n", my_calls);
    if(my_calls == (8 * 3 * n ) - ((8 * 3) - 1))
        printf("GOOD\n");
    else
        printf("BAD\n");
    return 0;
}

LIVE DEMO

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