Question

Consider the Catalan numbers P(n) described here Show a recursion-dependence tree for P(5) according to the...

Consider the Catalan numbers P(n) described here

  1. Show a recursion-dependence tree for P(5) according to the recurrence relation.
  2. Design a bottom-up dynamic programming algorithm to compute P(n) based on the recurrence relation (without using the formula P(n) = (2n−2)!/(n!(n−1)!)). Analyze its asymptotic runtime efficiency.
  3. Design a top-down memoized algorithm to compute P(n) based on the recurrence relation (without using the formula P(n) = (2n−2)!/(n!(n−1)!)). Analyze its asymptotic runtime efficiency.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

As stated, nth Catalan number is given by P(n) = (2n-2)!/(n!(n-1)!)

Some terms of the Catalan numbers sequence using above formula are:

1, 1, 2, 5, 14, 42, ..... so on

On analysing some of the terms of obtained by the above formula, we find that, P(n+1) =   and p(0) = 1 . We can use this recurrent relation in our algorithm.

(a.) Recursion-dependence tree for P(5) is shown in the image below:

note: due to lack of space i have not drawn the nodes having reverse order multiplications. for ex, i have shown node p(0)p(4) but not p(4)p(0), those nodes will also be in the tree. I have shown these nodes by multiplying the reverse of missing node by 2.

(b.) In bottom-up DP (also called tabulation method), we start from the base case and keep calculating values one by one using the previously obtained values until we reach the desired case. For ex, if we need to find p(n), and we have p(0)=1, we will calculate , p(1), p(2), p(3),.......p(n) in the given order using previous values.

Bottom-up DP algorithm pseudo code:

catalan (n): //function to find the nth catalan number declare array c[n+1] //declare array 'c' of size n+1 to store values c[0] <- 1 //base case c[1] <- 1 //base case for i <- 2 to n do //filling c array from index 0 to index n c[i] <- 0 for j <- 0 to i-1 do c[i] <- c[i] + c[j] * c[i-j-1] //calculating the sum end for end for return c[n] //return nth index of c array as it gives the value of P(n)

Time complexity of this algorithm = O()

(c.) In top-down DP (also called memoization method), we start from the problem(desired case) instead of the base case and use the value stored in the table for its subproblems to solve the problem. If the subproblem is not solved then we solve the subproblem first. For ex, if we need to find p(n), we can find it using p(n-1),p(n-2).....p(1), if we dont have value for p(n-1), we calculate it first in the similar manner.

Top-down DP algorithm pseudo code:

declare array c[n+1]and initialize it with 0 catalan (n): //function to calculate nth catalan number if n = 0 then return 1 if n = 1 then return 1 if c[n] != 0 then //Check if we have already calculated return c[n] //the value and stored in c array for i <- 0 to n-1 do //if the value is not calculated already c[n] = c[n] + catalan(i) * catalan(n-i-1) //using recursive formula to calculate it end for return c[n] 

Time complexity of this algorithm = O()

Add a comment
Know the answer?
Add Answer to:
Consider the Catalan numbers P(n) described here Show a recursion-dependence tree for P(5) according to the...
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
  • Consider the Catalan numbers P(n) described here (it is also described on page 372 of the...

    Consider the Catalan numbers P(n) described here (it is also described on page 372 of the book). Show a recursion-dependence tree for P(5) according to the recurrence relation. Design a bottom-up dynamic programming algorithm to compute P(n) based on the recurrence relation (without using the formula P(n) = (2n−2)!/(n!(n−1)!)). Analyze its asymptotic runtime efficiency. Design a top-down memoized algorithm to compute P(n) based on the recurrence relation (without using the formula P(n) = (2n−2)!/(n!(n−1)!)). Analyze its asymptotic runtime efficiency.

  • 2. (40 pts) Let A, B, and C be three strings each n characters long. We want to compute the longest subsequence that is...

    2. (40 pts) Let A, B, and C be three strings each n characters long. We want to compute the longest subsequence that is common to all three strings. (a) Let us first consider the following greedy algorithm for this problem. Find the longest common subsequence between any pair of strings, namely, LCS(A, B) LCS(B, C), LCS(A, C). Then, find the longest common subsequence between this LCS and the 3rd string. That is, supposing that the longest common pair wise...

  • 2. (40 pts) Let A, B, and C be three strings each n characters long. We want to compute the longest subsequence th...

    2. (40 pts) Let A, B, and C be three strings each n characters long. We want to compute the longest subsequence that is common to all three strings. (a) Let us first consider the following greedy algorithm for this problem. Find the longest common subsequence between any pair of strings, namely, LCS(A, B). LCS(B,C), LCS(A, C). Then, find the longest common subsequence between this LCS and the 3rd string. That is, supposing that the longest common pair wise subsequence...

  • need the answer to b not a. thanks! 2. (40 pts) Let A, B, and C be three strings each n characters long. We want t...

    need the answer to b not a. thanks! 2. (40 pts) Let A, B, and C be three strings each n characters long. We want to compute the longest subsequence that is common to all three strings. (a) Let us first consider the following greedy algorithm for this problem. Find the longest common subsequence between any pair of strings, namely, LCS(A, B). LCS(B,C), LCS(A, C). Then, find the longest common subsequence between this LCS and the 3rd string. That is,...

  • We are given a color picture consisting of an m?n array AŒ1::m;1::n? of pixels, where each...

    We are given a color picture consisting of an m?n array AŒ1::m;1::n? of pixels, where each pixel specifies a triple of red, green, and blue (RGB) intensities. Sup- pose that we wish to compress this picture slightly. Specifically, we wish to remove one pixel from each of the m rows, so that the whole picture becomes one pixel narrower. To avoid disturbing visual effects, however, we require that the pixels removed in two adjacent rows be in the same or...

  • Assignment 2 In this assignment, you will write two short programs to solve problems using recursion....

    Assignment 2 In this assignment, you will write two short programs to solve problems using recursion. 1. Initial Setup Log in to Unix. Run the setup script for Assignment 2 by typing: setup 2 2. Towers of Hanoi Legend has it that in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another. The initial stack had 64 disks threaded onto one peg and arranged from bottom to top by...

  • JAVA 3 LECTURE REVIEW PLEASE NEED ANSWERS ASAP. DUE IN AN HOUR!!! Question 12 points The...

    JAVA 3 LECTURE REVIEW PLEASE NEED ANSWERS ASAP. DUE IN AN HOUR!!! Question 12 points The best-case performance for a shell sort is: --- O(1) O(n2)   O(n) O(n log n)   Signaler cette question Question 22 points The best-case performance for an array of n items using insertion sort is: --- O(n2)   O(n) O(1) there is no best-case Signaler cette question Question 3 2 points A recursive method that processes a chain of linked nodes --- uses the first node in...

  • What is the role of polymorphism? Question options: Polymorphism allows a programmer to manipulate objects that...

    What is the role of polymorphism? Question options: Polymorphism allows a programmer to manipulate objects that share a set of tasks, even though the tasks are executed in different ways. Polymorphism allows a programmer to use a subclass object in place of a superclass object. Polymorphism allows a subclass to override a superclass method by providing a completely new implementation. Polymorphism allows a subclass to extend a superclass method by performing the superclass task plus some additional work. Assume that...

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