O(n^6) is
probably correct. Its only correct if your recursive call condition is not dependend from N.
Simplified: When calculating the big-O notation, a recursive call that is independend of any big-O-variable count as the amount the function would have without the recursion. There is no additional O(n) for the "recursion on its own" involved. (where should this come from? Any normal function call is just O(1) and a recursive call is just a function call in the end.)
In this case you have:
- 2 cascaded for-loops in solveFunction
- within that another 2 cascaded for loops in recursiveFunction.
- finally another 2 cascaded for loops for the recursion of recursiveFunction.
Since all are cascaded into each other, they are multiplied: O(n^2) * O(n^2) * O(n^2) = O(n^6)
The long story is, that this is only true if the recursion condition is O(1), in other words: the number of times the function calls itself is negletable and not dependend of any big-O-variable. You said, if the function calls itself 5 times, so the cost would be actually O(5), but in typical big-O-notations, constants are skiped out.
You can
not skip out this, if the number of self recursions is dependend from N. An very typical example where the recursion condition is dependend is binary search. Take an classic recursive binary search algorithm:
1 2 3 4 5 6 7 8 9
|
// don't take this too seriously: there are probably tons of bugs in this code ;)
bool binarySearch(int start, int end, int value)
{
if (start == end) return false; // no place to search left
pivot = searchData[start + (end-start)/2]; // look at the middle of our search data
if (value == pivot) return true; // found the value
if (value < pivot) return binarySearch(start, end/2, value); // its in the lower half
return binarySearch(start + (end-start+1)/2, end, value); // its in the upper half
}
|
This is a recursive function too, containing no loop at all, but if the size of the search data here is not neglatable, the condition under which the function calls itself is not neglatable either! Instead, it depends on the number of calls to the function already done. On the first call its almost certain calls itself again, only 1 out of N times (if N is the size of the search data) it does
not call itself. On second iteration, this probability is doubled, then doubled again and so on. Hence, you end up with the well-known O(log(N)) for binary search.
Ciao, Imi.
PS: Also, in your example you assume that the While-loop is O(1). This means the number of iterations until eof is true is negletable! If your while loop does loop approximately the same times as your for loops (depends on the same magnitude of iterations as the for loop), then it becomes O(n^7).
PPS: O(n^6) or O(n^7) is only true, if
all the for loops are looping over the same (non-neglatable) size. Simple counter example: If one of the loops looks like
for (int i = 0; i < 2; ++i)
then it clearly does not contribute another O(n), but only O(2) (which usually is simplified to O(1))