I want to determine the time complexity of the below code 👇
function random(a){
let i;
let num=Math.floor((Math.random()*(a+1)))
return num;
}
function fuc1(n){
let i;
if(n<=0){
alert("condition false ")
return 0;
}else{
i=random(n-1);
console.log("thisn")
return fuc1(i)+fuc1(n-1-i);
}
}
fuc1(6)
I added that alert
statement, in order to count how many times 0 is returned. This number of executions would be useful to determine the time complexity.
Although there is a random aspect involved, I notice that the alert("condition false ")
always executes 7 times. I don’t understand this. Why is it always the same 7 even though the arguments to the recursive calls are randomly determined?
2
Answers
Your function basically sums up random numbers until it reaches 0
The function creates a recursion tree where
n=0
represents a leaf node in that tree (it is a base case of the recursion), and any other (unsigned integer) value forn
represents an internal node in that recursion tree.An example:
The shape of that tree could have been different, as it is determined by the random number generator, but the invariants are that:
it is always a full binary tree, i.e. every node has either 2 children or no children at all. This follows from the fact that there are either no recursive calls made (the
if
case) or two (theelse
case), never one isolated call.the sum of the arguments in
fuc1(i)+fuc1(n-1-i)
is alwaysn-1
. So when we first execute withn=6
(which is the root node), the direct children of that node have values whose sum is 5. It could have been 0 and 5, 1 and 4, 2 and 3, …etc, but always summing up to 5.Number of internal nodes in recursion tree
Now we can prove that the number
n
represents the number of internal nodes that the recursion tree will have.We can prove that by induction:
We already established that 0 represents a leaf (the
if
condition), and so it is true thatn
represents the number of internal nodes, i.e. there are none.If we assume that the premise is true for all unsigned integers between 0 and
n-1
(inclusive), let’s see what we can say aboutn
:The two recursive calls have arguments that sum up to
n-1
. So using the inductive assumption we know that these two calls each produce a recursion tree with a number of internal nodes that is equal to the argument they get. By consequence the total number of internal nodes generated by the two calls together is equal ton-1
. Add to that the root of the recursion tree, and we haven
internal nodes.And so we have proved that
n
represents the number of internal nodes of the recursion tree.Full binary trees with
n
internal nodes haven+1
leaf nodes, and this is why you always get 7 cases where theif
block is executed (and you see the alert that many times).Time Complexity
If we remove the printing/alerting from your code, the time complexity is directly related to the total executions of the function, which corresponds to the number of nodes (internal and leaves) of the recursion tree. From the above proof we know that this recursion tree has
2n+1
nodes, and so the algorithm has a time complexity of O(n).