简体   繁体   中英

Recurrence equation - Recursion inside for loop

I was trying to solve my university problem about recurrence equations and computational complexity but I can't understand how to set the recurrence equation.

static void comb(int[] a, int i, int max) {
    if(i < 0) {
        for(int h = 0; h < a.length; h++)
            System.out.print((char)(’a’+a[h]));
        System.out.print("\n");
        return;
    }
    for(int v = max; v >= i; v--) {
        a[i] = v;
        comb(a, i-1, v-1);
    }
}

static void comb(int[] a, int n) { // a.length <= n
    comb(a, a.length-1, n - 1);
    return;
}

I tried to set the following equation

                O(n) + c                     if i < 0 
T (n, i, j) = { 
               (j-i) T(n, i-1, j-1)         otherwise

Solving

T(n, i, j) = (j-i) T(n, i-1, j-1) = 
(j-i) (j-1-i+1) T(n, i-2, j-2) = 
(j-i)^k T(n, i-k, j-k)

At this point I'm stuck and I can not figure out how to proceed.

Thanks and sorry for my bad english.

Luigi

With your derivation

T(n, i, j) = ... = (j-i)^k T(n, i-k, j-k)

you are almost done! Just set k = i+1 and you get:

T(n, i, j) = (j-i)^(i+1) T(n,-1,j-i-1) = (j-i)^(i+1) O(n) =
O(n (j-i)^(i+1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM