I need to create and solve a recurrence relation for the worst-case analysis for the following psuedocode. I am counting the number additions (not including the for loop counter) as my basic operation.
I am assuming n=2^k.
Here is the progress I have made... Base Case: T(n<=4) = 1
W(n)=W(2^k)=additions to calculate answer+additions in next recursion+addition in for loop W(2^k) = 2 + W(2^(k-2)) + (2^k) - 2 = W(2^(k-2)) + (2^k)
I use back substitution and get the following recurrence relation...
for the jth recursive call W(2^k) = W(2^(k-2j)) + (2^k) + sum(t=1,j,2^(k-2(t-1)))
I know that I can simplify this because I take W(2^(k-2j)) = W(4) and solve for j to see how many recursive steps the code takes. In this case, j=(k/2) - 1. Reducing the recurrence gives me...
W(2^k) = 1 + (2^k) + sum(t=1,j,2^(k-2(t-1))).
Reducing the summation gives me...
W(2^k) = 1 + (2^k) + (2^k)*(2^2)*sum(t=1,j,2^(-2t)) or
W(n) = 1 + n + 4n*sum(t=1,j,2^(-2t))
What I cannot simplify is the summation. In lectures, we may have a summation of sum(i=1,n,2^i), which would be 2^(n+1)-1, but this one is different.
int function calc(int n) {
int num,answer;
if(n<=4) {return n+10;}
else {
num=calc(n/4);
answer=(num+num+10);
for(int i=2;i<=n-1;i++) {
answer=answer+answer;
}
return answer;
}
}
Any help would be appreciated. This assignment is due tonight. Thanks
Testing several values...
For geometric sums https://en.wikipedia.org/wiki/Geometric_series