Question

2. Consider the function george (int n) computed by the following recursive C++ code. int george (int n) assert (n >= 0) if (

0 0
Add a comment Improve this question Transcribed image text
Answer #1

See, we will follow bottom up approach in building memoization algorithm for this problem. Bottom up approach means, first we will calculate base values and then then we will calculate higher values from those base values. We call it memoization because we avoid repetitive calculation of base values again and again by saving those base values in an array.

Lets have insight of problem-

Note- Below image is showing just one recursive call of george(4) function.

geogc)We can see that george(2) and george(1) are calculated twice during recursive call. we are unnecessarily calculating george(2) and george(1) again and again as we go more deep into recursive tree.

So one efficient approach is , we can store values which are calculated once into an array. So in future we can use that value without doing redundant computation again and again.

We will declare an array where arrar[i] will store value of grorge(i).

Algorithm-

1. Declare an array of size n.

2. Fill base values array[0]=george(0)=1 and array[1]=george(1)=1.

3. For any value of n>2, ans=2*array[(n+1)/2]+ 2*array[n/2]+2*array[(n-1)/2]+2*array[(n/2)-1].

4. Store value calculated for george(n) at array[n].

Complexity is O(n) because for any value greater than 2, we are roughly accessing n times contents of array and array access is possible in constant time (O(1)).

As we have seen that we have built array form base values, that's why this approach is called bottom up approach.

Add a comment
Know the answer?
Add Answer to:
2. Consider the function george (int n) computed by the following recursive C++ code. int george ...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 3. Recursive Program (6 points) Consider the following recursive function for n 1: Algorithm 1 int...

    3. Recursive Program (6 points) Consider the following recursive function for n 1: Algorithm 1 int recurseFunc(int n) If n 0, return 1. If n 1, return 1 while i< n do while j <n do print("hi") j 1 end while i i 1 end while int a recurse Func(n/9); int b recurse Func (n/9) int c recurse Func (n/9) return a b c (1) Set up a runtime recurrence for the runtime T n) of this algorithm. (2) Solve...

  • Q5 (25pts) Consider the code: int foo(int N){ if (N <= 3) return 2; int res1 = 3*foo(N-4); int...

    Q5 (25pts) Consider the code: int foo(int N){ if (N <= 3) return 2; int res1 = 3*foo(N-4); int res2 = foo(N-2); return res1-res2; } a) (6 points) Write the recurrence formula for the time complexity of this function (including the base cases) for N>=0. You do NOT need to solve it. b) (5 points) Draw the tree that shows the function calls performed in order to compute foo(8) (the root will be foo(8) and it will have a child...

  • 3) [16 points totall Consider the following algorithm: int SillyCalc (int n) { int i; int Num, answer; if (n &lt...

    3) [16 points totall Consider the following algorithm: int SillyCalc (int n) { int i; int Num, answer; if (n < 4) 10; return n+ else f SillyCalc(Ln/4) answer Num Num 10 for (i-2; i<=n-1; i++) Num + = answer + answer; answer return answer } Do a worst case analysis of this algorithm, counting additions only (but not loop counter additions) as the basic operation counted, and assuming that n is a power of 2, i.e. that n- 2*...

  • This is for C in Linux: Problem 1: Given the following recursive function: void recursiveFunction( int...

    This is for C in Linux: Problem 1: Given the following recursive function: void recursiveFunction( int m ) printf("%d", m); if( m <= 0 ) return; if( n + 2 == 0 ) recursiveFunction( m - 1); else recursiveFunction( m - 2); What is the output when the following functions are called with these parameters? recursiveFunction( 5 ); recursiveFunction( 10 ); recursiveFunction( 0);

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

    Consider the following recursive algorithm for computing the sum of the first n cubes: S(n) = 13 + 23 + … + n3. (a) Set up a recurrence relation for the number of multiplications made by this algorithm. (b) Provide an initial condition for the recurrence relation you develop at the question (a). (c) Solve the recurrence relation of the question (a) and present the time complexity as described at the question number 1. Algorithm S n) Input: A positive...

  • PROBLEM 4: Consider the recursive C++ function below: void foo(unsigned int n) {     if(n==0)        ...

    PROBLEM 4: Consider the recursive C++ function below: void foo(unsigned int n) {     if(n==0)         cout << "tick" << endl;     else {         foo(n-1);         foo(n-1);         foo(n-1);     } } 4.A: Complete the following table indicating how many “ticks” are printed for various parameters n. Unenforceable rule: derive your answers “by hand” -- not simply by writing a program calling the function. n number of ticks printed when foo(n) is called 0 1 2 3 4 4.B:...

  • *Program is in C* Write a recursive function to compute a^b for integers a and b....

    *Program is in C* Write a recursive function to compute a^b for integers a and b. For the recursive use the following equality a^b = a^b - 1 * a and a^0 = 1. What does the following recursive function do? int mystery(int a, int b) {if (b == 1) return (a); else return (a + mystery(a, b - 1));}

  • Q5. [5 marks] Consider the following recursive function: int Test (int number) //Line 1 //Line 2...

    Q5. [5 marks] Consider the following recursive function: int Test (int number) //Line 1 //Line 2 if (number == 0) //Line 3 return number; //Line 4 else //Line 5 return (number + Test (number - 1)); //Line 6 //Line 7 a. Identify the base case. b. Identify the general case. c. If Test (0) is a valid call, what is its value? If not, explain why. d. If Test (5) is a valid call, what is its value? If not,...

  • (a) Consider the following C++ function: 1 int g(int n) { 2 if (n == 0)...

    (a) Consider the following C++ function: 1 int g(int n) { 2 if (n == 0) return 0; 3 return (n-1 + g(n-1)); 4} (b) Consider the following C++ function: 1 bool Function (const vector <int >& a) { 2 for (int i = 0; i < a. size ()-1; i ++) { 3 for (int j = i +1; j < a. size (); j ++) { 4 if (a[i] == a[j]) return false; 5 6 } 7 return...

  • The code for a recursive function 'mystery' appears below. Assume we passed the following array as...

    The code for a recursive function 'mystery' appears below. Assume we passed the following array as x[]: int x[] = { 10, 20, 25, 25 }; How would we call mystery() so the return value is 1? int mystery(const int x[], int n, int mysteryValue) { int count = 0; if (n <= 0) return 0; else { if (x[n - 1] == mysteryValue) count = 1; return mystery(x, n - 1, mysteryValue) + count; } } 1mystery(x, 4, 0)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT