Question

In: Computer Science

Consider the following recursive algorithm for computing the sum of the first n squares: S(n) =...

Consider the following recursive algorithm for computing the sum of the first n squares: S(n) = 12 +22 +32 +...+n2 . Algorithm S(n) //Input: A positive integer n //Output: The sum of the first n squares if n = 1 return 1 else return S(n − 1) + n* n a. Set up and solve a recurrence relation for the number of times the algorithm’s basic operation is executed. b. How does this algorithm compare with the straightforward non-recursive algorithm for computing this function?

Solutions

Expert Solution

Algorithm is

S(n)

{

if ( n==1 )

return 1

else

return S(n-1 ) + n*n

Now Let's first set The Recurrence Relation

As function have recursion and it is calling itself by passing n-1 elements only once

1.Lets say S(n) takes T(n) time then S(n-1) will take T(n-1) time

2. Along with recursion we have a constant c i.e. by If condition and one addition , multiplication operation in else

So we get Overall Recurrence As

T(n) = {

1 , when n=1

T(n-1) + c , when n>1

}

Note : Don't take S(n-1) + n*n as T(n-1) + n^2  because that n^2 apart from recursion means that there is some code that is taking n^2 units of time but in our case we didn't have such code we have only constants .

Hence we get the Recurrence relation

Now Lets Solve The Relation using Iterative method

T(n) = T(n-1) + c

= T(n-2) + c+ c= T(n-2) + 2c

= T(n-3 ) + c + 2c = T(n-3) + 3c

Observing the pattern after doing k times we get

T(n-k) + kc

Now assume n-k = 1 so k = n-1 we get

T(1) = 1

Now We have

1 + (n-1) c

= n.c = O(n)

c is the Time taken by all basic operations to run . We get n.c so it means that all basic operations are repeated n times .

So it The Time Taken by the recursive function S(n) is O(n)

Compare it with Non -Recursive approach

In Non- Recursive Algorithm

  1. Take Value of n for user
  2. Declare a sum variable that will store sum of all squares
  3. Make a for loop that runs from 1 to n and add sum with i^2 = i*i

Time Complexity : As Loop is running for n times from 1 to n so all the statement inside loop also run for n times so all the basic basic operations are repeated n times .

In terms of Time Complexity we can use any of the approach either Recursive or Non-Recursive but in terms of writing and understanding non - recursive is always the best one .

So this is how we can analyze both approaches and then do the comparisons

Thank You

If u like the answer do Upvote it and have any doubt ask in comments


Related Solutions

a. Design a recursive algorithm for computing 3n for any nonnegative integer n that is based...
a. Design a recursive algorithm for computing 3n for any nonnegative integer n that is based on the formula 3n = 3n−1 + 3n−1 + 3n−1 b. Set up a recurrence relation for the number of additions made by the algorithm and solve it. c. Draw a tree of recursive calls for this algorithm and count the number of calls made by the algorithm. d. Is it a good algorithm for solving this problem?
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try...
For each given integer n, determine if n is a sum of two squares. (You do...
For each given integer n, determine if n is a sum of two squares. (You do not have to find the squares.) (a) n = 19 (b) n = 45 (c) n = 99 (d) n = 999 (e) n = 1000
For each given integer n, determine if n is a sum of two squares. (You do...
For each given integer n, determine if n is a sum of two squares. (You do not have to find the squares.) (a)n= 19 (b)n= 45 (c)n= 99 (d)n= 999 (e)n=1000
Gram method for computing least squares approximate solution. Algorithm 12.1 in the textbook uses the QR...
Gram method for computing least squares approximate solution. Algorithm 12.1 in the textbook uses the QR factorization to compute the least squares approximate solution xˆ = A†b, where the m × n matrix A has linearly independent columns. It has a complexity of 2mn2 flops. In this exercise we consider an alternative method: First, form the Gram matrix G = AT A and the vector h = AT b; and then compute xˆ = G−1h (using algorithm 11.2 in the...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Give a recursive algorithm to compute a list of all permutations of a given set S....
Give a recursive algorithm to compute a list of all permutations of a given set S. (That is, compute a list of all possible orderings of the elements of S. For example, permutations({1, 2, 3}) should return {〈1, 2, 3〉, 〈1, 3, 2〉, 〈2, 1, 3〉, 〈2, 3, 1〉, 〈3, 1, 2〉, 〈3, 2, 1〉}, in some order.) Prove your algorithm correct by induction.
Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
2. Give a recursive algorithm to compute the product of two positive integers, m and n,...
2. Give a recursive algorithm to compute the product of two positive integers, m and n, using only addition and subtraction. Java Language...
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 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 b) 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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT