问题
I am trying to calculate the complexity of the following exponential recursive function.
The isMember() and isNotComputed() functions reduce the number of recursive calls.
The output of this code is a set of A[], B[] which are printed at the initial part of recursive function call.
Would appreciate any inputs on developing a recursive relationship for this problem which would lead to the analysis of this program.
Without the functions isMember(), isNotComputed() this code has the complexity of O(2^N). Empirically (with the above two functions) this code has a complexity of O(|N^2||L|). Where L is the number of recursive calls made, i.e. results generated.
I am trying to calculate the complexity of this code as accurate as possible, so that I can compare the efficiency of this with a set of other algorithms which are similar in nature.
void RecuriveCall(int A[], int ASize, short int B[], int BSize,
int y, short int level) {
int C[OBJECTSIZE];
short int D[ATTRIBUTESIZE];
int CSize, DSize;
PrintResult( A,ASize, B, BSize);
for (int j=y; j<n; j++) {
if (! isMember(j, B, BSize)) {
function1(C,CSize,A,ASize,j);
function2(D,DSize,C, CSize);
if (isNotComputed(B, BSize, D, DSize, j)) {
RecursiveCall(C, CSize,D, DSize, j+1, level+1);
}
}
}
}
// Complexity - O(log N) - Binary Search
bool isMember(int j,short int B[], int BSize) {
int first, mid, last;
first = 0;
last = BSize-1;
if (B[first] == j || B[last] == j) {
return true;
}
mid = (first+last)/2;
while (first <= last) {
if (j == B[mid]) {
return true;
}
else if (j < B[mid])
last = mid-1;
else
first = mid+1;
mid = (first+last)/2;
}
return false;
}
// complexity - O(N)
bool isNotComputed(short int B[], int BSize, short int D[], int DSize,int j) {
if (j==0) {
return true;
}
int r = 0;
while (r<BSize && B[r]<j && r<DSize && D[r]<j) {
if (B[r] != D[r]) {
return false;
}
r=r+1;
}
// Now we can check if either B[] or D[] has extra elements which are < j
if (r<BSize && r < DSize && B[r]>=j && D[r] >=j) {// we know it is okay
return true;
}
if (r==BSize && r==DSize) {
return true;
}
if (r==BSize && r<DSize && D[r] >=j) {
return true;
}
if (r==DSize && r<BSize && B[r] >=j) {
return true;
}
return false;
}
// Complexity - O(N)
void function1(int C[],int &CSize,int A[] ,int ASize,int j) {
int tsize = 0;
for (int r=0;r<ASize;r++)
if (I[A[r]][j]==1)
C[tsize++] = A[r];
CSize = tsize;
}
// Complexity - O(|N||G|) - G - number of objects
void function2(short int B[], int &BSize,int A[], int ASize) {
int i,j;
int c=0;
// Iterate through all attributes
for (j = 0; j < MAXATTRIBUTES; ++j) {
// Iterate through all objects
for (i = 0; i < ASize; ++i)
if (!I[A[i]][j])
break;
if (i == ASize)
B[c++] = j;
}
BSize = c;
}
void main() {
n = MAXATTRIBUTES;
for (int r=0; r<MAXOBJECTS; r++)
A[r] = r;
ASize = MAXOBJECTS;
function2(B, BSize, A, ASize);
RecursiveCall(A, ASize,B, BSize, 0, 0);
}
The answer presented by "mohamed ennahdi el idrissi" addresses how a recursive relationship can be developed.
How do you incorporate the functions isMember() and isNotComputed() functions into this. In essence these reduce the number of recursive calls made significantly. Is there a way of introducing a probabilistic function to represent them? i.e P(f(n))xRecCall(n-1). I have seen the complexity of some algorithms been computed e.g. as O(N^2.48). How do you come with such values?
回答1:
I have tried to adapt the following recurrence relation to your code, see the steps below:
Where n = MAXATTRIBUTES
(constant), and m = ASize
.
来源:https://stackoverflow.com/questions/22468916/analyzing-an-exponential-recursive-function