I need some help to understand analysing recursive algorithms. I quickly made this algorithm up, and would like to know what the complexity is:
int FunctionExampple( A1, A2, ... An )
{
product = 1;
if( n == 2)
{
product = multi(A1, A2);
}
else
{
product = multi(A1, FunctionExample( A2, A3, ..., An ) );
}
return product;
}
Now assuming the function multi takes O(n^1.59) time, what would the complexity be? Would it remain O(n^1.59) or would the recursive calls make it O(n^1.59 * n ) to account for the number of recursive calls? Thanks guys.
PS: I just wrote this up quickly, and syntax and all that stuff doesn’t matter.
The parameter ‘n’ in O(n1.59) measures the size(s) of the arguments to ‘multi’, not the number of arguments. So what is crucial is how the size of an output from ‘multi’ relates to the sizes of its inputs. E.g. if the result from ‘multi’ is twice the size of any of its arguments, then a call multi(A, multi(B, C)) where A, B, C are of size n is O(n1.59 + (2n)1.59), and if you then chain multiple calls to multi in this fashion you get exponential growth. On the other hand, if ‘multi’ returns values that are of the same size as its inputs, then you get O(k n1.59) where k is the number of arguments to FunctionExample and n is their (largest) size.
So it depends on how ‘multi’ behaves. E.g. there would be a huge difference if it would be multiplication versus multiplication within a finite field, as for the latter the result wouldn’t grow from the inputs, whereas for unlimited integers the result grows in size.