难以解决递归方程

时间:2013-12-18 04:04:54

标签: c++ algorithm recursion recurrence

这是我正在使用的等式(这是我过去的考试问题,我错了):

void foo(float[] array, int start, int end){
    if((end-start) <= 1) return;

    int x = (end-start) / 5;
    int y = 2*x;
    int z = 4*x;

    foo(array,start,start+y);
    for(index = y; index <z; index++){
            array[index]++;
    }
    foo(array,start+z,end);
}

我将如何为此提出递推方程式? 我不确定我应该使用的符号,因为函数#recurrences取决于end-start的值......

T(1)= 1
T(N)= _ ___ + __ _ _ + _ ____

1 个答案:

答案 0 :(得分:1)

for notation simplicity, lets call N = end-start
then:
 foo(array,start,start+y);  // T(2/5 * N)
 for(index = y; index <z; index++)  // 2/5 * N
 foo(array,start+z,end);  // T(N/5)

T(N) = T(2/5 * N) + 2/5 * N + T(N/5)

足够接近吗?

相关问题