复杂度算法递归关系

时间:2016-01-22 23:15:20

标签: algorithm math complexity-theory recurrence master-theorem

int function(int n){
if (n<=1)
 return 1;
else 
 return (2*function(n/2));
}

运行时间的递归关系T(n)是什么?为什么?

2 个答案:

答案 0 :(得分:1)

您可以直接计算:它是最近的2 ^ n,最大或相等。

你计算L = log2(n),你取2 ^ L,或2 ^(L + 1)

复杂度为O(log2 N):log2 N次操作。

答案 1 :(得分:1)

该算法的复杂性函数将是

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

应用master-theorem,我们会得到

a = 1
b = 2
c = 0 (1 = n^0)

log b(A) = log2(1) = 0 = 0 c, thus case 2
apply values and the result is O(log n).

正如@guillaume已经正确陈述的那样,通过使用线性函数可以更容易地解决这个问题。

相关问题