先验且渐进的复杂度级别

发布于 2024-11-01 19:11:43 字数 673 浏览 3 评论 0原文

如何确定以下程序代码的先验复杂度和渐近复杂度?

#include<stdio.h>


int br_nacina_zaba(int br_lopoca, int tren_poz, int korak) {  
    if (korak == 18) return 0;
    else if (tren_poz == br_lopoca) return 1;
    else if (tren_poz <= 0 && korak != 0) return 0;
    else if (tren_poz > br_lopoca) return 0;
    else return
               br_nacina_zaba(br_lopoca, tren_poz + 2, korak + 1)
             + br_nacina_zaba(br_lopoca, tren_poz + 3, korak + 1)
             + br_nacina_zaba(br_lopoca, tren_poz - 2, korak + 1)
             + br_nacina_zaba(br_lopoca, tren_poz - 3, korak + 1);
}

所以我需要知道函数br_nacina_zaba(n,0,0)的复杂度。

How to determine the a priori and asymptotic complexity of following program code?

#include<stdio.h>


int br_nacina_zaba(int br_lopoca, int tren_poz, int korak) {  
    if (korak == 18) return 0;
    else if (tren_poz == br_lopoca) return 1;
    else if (tren_poz <= 0 && korak != 0) return 0;
    else if (tren_poz > br_lopoca) return 0;
    else return
               br_nacina_zaba(br_lopoca, tren_poz + 2, korak + 1)
             + br_nacina_zaba(br_lopoca, tren_poz + 3, korak + 1)
             + br_nacina_zaba(br_lopoca, tren_poz - 2, korak + 1)
             + br_nacina_zaba(br_lopoca, tren_poz - 3, korak + 1);
}

So I need to know the complexity of function br_nacina_zaba(n,0,0).

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

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

发布评论

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

评论(2

冰葑 2024-11-08 19:11:43

在我看来,br_nacina_zaba(n,0,0) 的时间复杂度为 O(1)。在函数的第一个 LOC 中,(四元)调用树的最大深度限制为 19:

korak 在每次递归调用中递增。如果您从 korak=0 开始,并在每个递归步骤中最多调用该函数 4 次,则最多会有 4^18 次递归调用。 4^18 不依赖于 n,因此该函数的复杂度为 O(1)。

In my opinion, br_nacina_zaba(n,0,0) is in O(1). The maximum depth of the (quaternary) call tree is limited by 19 in the function's first LOC:

korak gets incremented in each recursive call. If you start with korak=0 and call the function at most 4 times in each recursive step, you'll have at most 4^18 recursive calls. 4^18 doesn't depend on n, hence the function is in O(1).

客…行舟 2024-11-08 19:11:43

我不知道你的意思是“函数的复杂性”,但我在键盘上运行你的函数( http://codepad.org /jFUW1ATj )并得到这个结果

br_nacina_zaba(1, 0, 0) was called 5 times.
br_nacina_zaba(2, 0, 0) was called 5 times.
br_nacina_zaba(3, 0, 0) was called 9 times.
br_nacina_zaba(4, 0, 0) was called 77 times.
br_nacina_zaba(5, 0, 0) was called 33445 times.
br_nacina_zaba(6, 0, 0) was called 1048573 times.
br_nacina_zaba(7, 0, 0) was called 15530681 times.

I don't know what you mean "complexity of function", but I run your function on codepad ( http://codepad.org/jFUW1ATj ) and got this result

br_nacina_zaba(1, 0, 0) was called 5 times.
br_nacina_zaba(2, 0, 0) was called 5 times.
br_nacina_zaba(3, 0, 0) was called 9 times.
br_nacina_zaba(4, 0, 0) was called 77 times.
br_nacina_zaba(5, 0, 0) was called 33445 times.
br_nacina_zaba(6, 0, 0) was called 1048573 times.
br_nacina_zaba(7, 0, 0) was called 15530681 times.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文