views:

269

answers:

1

Assuming n=B-A+1, I need to derive the recurrence relation of this algorithm:

void recurringalgorithm(int *a, int A, int B){
  if (A == B){
    for (int j=0;j<B;j++){
      cout<<a[j];  
    }
    cout<<endl;
    return;
  }
  for (int i=A;i<B;i++){
    dosomething(a[A],a[i]);
    recurringalgorithm(a,A+1,B);
    dosomething(a[A],a[i]);
  }
}

Help?

+3  A: 

Assume the complexity of your recursive algorithm is h(A,B).

From your code you can split h into 2 cases:

h(A,B) = { complexity-of-if-branch          if A = B
         { complexity-of-rest-of-the-code   otherwise

The "complexity-of-if-branch" is trivial. For "complexity-of-rest-of-the-code", since it involves recurringalgorithm, you'll need to include h again.

For instance, if the function is defined like

function hh(A,B) {
    for (var i = A+1; i < B; ++ i)
        hh(i, B);
}

Then the complexity will be

hh(A,B) = hh(A+1, B) + hh(A+2, B) + ... + hh(B-1, B)

You can compare this with your code to generalize.

(BTW, the complexity is h(A,B) = O(B * (B-A)!))

KennyTM
thanks, so the complexity-of-if-branch is negligible in calculating the overall big-o complexity?
zebraman
@zebraman: No, the "complexity-of-if-branch" is responsible of the "B" factor in `O(B * (B-A)!)`.
KennyTM
Whenever I see a "!" in a complexity, I cry... it's fortunate it's only homework!
Matthieu M.