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?
Assume the complexity of your recursive algorithm is
h(A,B).From your code you can split
hinto 2 cases:The “complexity-of-if-branch” is trivial. For “complexity-of-rest-of-the-code”, since it involves
recurringalgorithm, you’ll need to includehagain.For instance, if the function is defined like
Then the complexity will be
You can compare this with your code to generalize.
(BTW, the complexity is
h(A,B) = O(B * (B-A)!))