Question

static int F(int n) { if (n < 1) return 1; else return 2*F(n-1)+n; } (a)...

static int F(int n) {

if (n < 1)

return 1;

else

return 2*F(n-1)+n;

}

(a) [7 points] Give the recurrence that describes the running time of F(int n).

(b) [8 points] Solve the recurrence equation from (a).

Note: For (b), you must show your work

Homework Answers

Answer #1

a) Let the function T(n) denote the number of elementary operations performed by the function call F(int n).

We identify two properties of T(n).

  • Since n<1 is computed using a fixed number of operations k1, T(1) = k1 (here n<1 value =1 base case)
  • If n > 1 the function will perform a fixed number of operations k2, and in addition, it will make a recursive call to F(n-1). This recursive call will perform T(n-1) operations. In total, we get T(n) = 2F(n-1)+n

b) Just looking at the recurrence we can guess that T(n) is something like 2n. we can verify that with first few values of T(n), we discover that they are each n less than a power of two.

It looks like T(n) = 2 n − n might be the right answer. Let’s check.

T(0) = 1 = 20− 0

ie T=1 so it satisfies

T(n) = 2T(n − 1) + n

= 2(2 n−1 − n) + n [induction hypothesis]

= 2 n − n [algebra]

hence solved

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
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an argument and returns the sum of every other int from n down to 1. For example, the call sumEveryOther(10) should return 30, since 10 + 8 + 6 + 4 + 2 = 30. The call sumEveryOther(9) should return 25 since 9 + 7 + 5 + 3 + 1 = 25. Your method must use recursion.
Consider the following two methods: public static boolean isTrue(int n){        if(n%2 == 0)          ...
Consider the following two methods: public static boolean isTrue(int n){        if(n%2 == 0)           return true;        else           return false; } public static int Method(int[] numbers, int startIndex) { if(startIndex >= numbers.length)        return 0; if (isTrue(numbers[startIndex]))      return 1 + Method(numbers, startIndex + 1); else      return Method(numbers, startIndex + 1); } What is the final return value of Method() if it is called with the following parameters: numbers = {1, 2, 2, 3, 3,...
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...
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...
Given the following function: int bar(int n) {     if( n < 0 )         return...
Given the following function: int bar(int n) {     if( n < 0 )         return -2;     else if (n <= 1)         return 2;     else       return (3 + bar(n-1) + bar(n-2)); } What is returned by bar (4)? Show all the steps
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?
Write down the run time of the following recursive functions as a recurrence relation: int f(...
Write down the run time of the following recursive functions as a recurrence relation: int f( int n ) { if ( n <= 1 ) { return 1; } return f( n – 1 ) + f( n – 1 ); }
What is the time complexity ? int fact(int n) { if( n==1) {return 1;} n=n*fact(n-1); return...
What is the time complexity ? int fact(int n) { if( n==1) {return 1;} n=n*fact(n-1); return n; }
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns...
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns the total number of 1’s in n’s binary representation. Note that the input is n, not its binary representation. For example, A(9) should return 2 as 9’s binary representation is 1001, while A(7) should return 3 since 7 is 111 in binary. Note that your algorithm should have a running time of O(log n). Justify your answer. You need to do the following: (1)...
java data structure question - Consider the following recurrence function" f(1)=1; f(2)=2; f(3)=3; f(4)=2; f(5)=2 f(n)...
java data structure question - Consider the following recurrence function" f(1)=1; f(2)=2; f(3)=3; f(4)=2; f(5)=2 f(n) = 2 * f(n -1) + f(n – 5) for n >= 6 Write a program to demonstrate this recurrence relation (recursive method) and demonstrate it for the values n = 6, 7, 10, and 12; Create a second method that solves the same problem using an iterative solution. Modify your test program to show that both the recursive method and the iterative methods...