Assuming n=B-A+1, I need to derive the recurrence relation of this algorithm:
void r开发者_如何学Cecurringalgorithm(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 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)!)
)
精彩评论