A priori and asymptotic complexity level
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)
.
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).
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.
精彩评论