Question

Consider the following recursive algorithm Algorithm S(n) if n==1 return 1 else return S(n-1) + n*n*n...

Consider the following recursive algorithm

Algorithm S(n)

if n==1 return 1

else return S(n-1) + n*n*n

1)What does this algorithm compute?

2) Set up and solve a recurrence relation for the number of times the algorithm's basic operation is executed.

3) How does this algorithm compare with the non-recusive algorithm for computing thius function in terms of time efficeincy and space effeciency?

Homework Answers

Answer #1

1. ) This algo. computes summation of (1^3 + 2^3+ 3^3 +...n^3) which is equal to {n*(n+1)/2}^2.

2.) Recurrence relation:

t(n) = t(n-1) + n*n*n

= t(n-2) + (n-1)^3 + n^3 = t(n-3) + (n-2)^3 + (n-1)^3 + n^3

= .. = t(n-k) + (n-k)^3 + ..+ (n-2)^3 + (n-1)^3 + n^3

= t(1) + 1^3 + 2^3 + ...+(n-2)^3 + (n-1)^3 + n^3

= {n*(n+1)/2}^2.

3.) Non-recursive algo, will have same time complexity that of recursive version. But recuresive algo. will have approx. n stack calls. So, space complexity will be of O(n) while in non-recursive case, it requires no extra variable therefore, space complexity will be O(1).

Hope it helps, do give your response.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
a. Design a non-recursive algorithm for computing an (discussed in the class). What is the basic...
a. Design a non-recursive algorithm for computing an (discussed in the class). What is the basic operation? How many times is the algorithm’s basic operation executed? b. Using an = a*an-1 (discussed in the class) to design a recursive algorithm for computing an . What is the basic operation? Set up and solve a recurrence relation for the number of times that algorithm's basic operation is executed. c. Using an = a*(a(n-1)/2) 2 (n is odd integer) and an =...
Consider the following recursive algorithm. Algorithm Test (T[0..n − 1]) //Input: An array T[0..n − 1]...
Consider the following recursive algorithm. Algorithm Test (T[0..n − 1]) //Input: An array T[0..n − 1] of real numbers if n = 1 return T[0] else temp ← Test (T[0..n − 2]) if temp ≥ T[n − 1] return temp else return T[n − 1] a. What does this algorithm compute? b. Set up a recurrence relation for the algorithm’s basic operation count and solve it.
Consider the following recursive algorithm for computing the sum of the first ? cubes: ? (?)...
Consider the following recursive algorithm for computing the sum of the first ? cubes: ? (?) = 13 + 23 + ⋯+ ? 3 . Algorithm S(n) //Input: A positive integer n //Output: The sum of the first n cubes if n = 1 return 1 else return S(n-1) + n * n * n
Design a recursive algorithm to compute 3^n based on the formula 3^n=3^(n−1) + 3^(n−1) + 3^(n−1)....
Design a recursive algorithm to compute 3^n based on the formula 3^n=3^(n−1) + 3^(n−1) + 3^(n−1). Also do the recurrence relation.
Give a recursive algorithm to solve the following recursive function.    f(0) = 0;    f(1)...
Give a recursive algorithm to solve the following recursive function.    f(0) = 0;    f(1) = 1; f(2) = 4; f(n) = 2 f(n-1) - f(n-2) + 2; n > 2 Solve f(n) as a function of n using the methodology used in class for Homogenous Equations. Must solve for the constants as well as the initial conditions are given.
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n ==...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n == 0)    return 0; else    return n * f(n - 1); } A. 120 B. 60 C. 1 D. 0 10 points    QUESTION 2 Which of the following statements could describe the general (recursive) case of a recursive algorithm? In the following recursive function, which line(s) represent the general (recursive) case? void PrintIt(int n ) // line 1 { // line 2...
Consider the following recursive equation s(2n) = 2s(n) + 3; where n = 1, 2, 4,...
Consider the following recursive equation s(2n) = 2s(n) + 3; where n = 1, 2, 4, 8, 16, ... s(1) = 1 a. Calculate recursively s(8) b. Find an explicit formula for s(n) c. Use the formula of part b to calculate s(1), s(2), s(4), and s(8) d Use the formula of part b to prove the recurrence equation s(2n) = 2s(n) + 3
Below is C code and Python code for an algorithm. C code: void foo( int n,...
Below is C code and Python code for an algorithm. C code: void foo( int n, int A, int B, int C ) { if( n==1 ) { printf("%d to %d\n",A,B); return; } foo( n-1, A, C, B ); printf("%d to %d\n",A,B); foo( n-1, B, C, A ); Python code: def foo(n , A, B, C): if n==1: print A, "to", B return foo(n-1, A, C, B) print A, "to", B foo(n-1, B, C, A) Let Hn be the number...
Solve the following recurrence relation, subject to the basis. S(1) = 2 S(n) = S(n –...
Solve the following recurrence relation, subject to the basis. S(1) = 2 S(n) = S(n – 1) + 2n please explain how you solved this, thank you!
Solve the following recurrence relation, subject to the basis. S(1) = 2 S(n) =2S(n/2) + 2n
Solve the following recurrence relation, subject to the basis. S(1) = 2 S(n) =2S(n/2) + 2n