1

I was recently practicing dynamic programming and came across the nth-Catalan problem. After looking at the problem and the formula for the Catalan numbers, I was confused about how to code the solution dynamically. I decided to look at this problem mathematically and derived the formula:

(1) C_n = (4n - 2) / (n + 1) * C_{n - 1}

By doing so I was easily able to translate it into code:

//Not sure if this solution is dynamic or not.
int ln = 1;
for(int i = 2; i <= n; i++){
    ln = ln * (4*i - 2) / (i + 1);
}
return ln;

After implementing my solution and checking it, I looked at the dynamic approach to solve the problem. I was surprised to see that it followed a recursive formula that is derived from the initial formula:

(2) C_n = sum(n, i=0) (C_i * C_{n - i - 1})

This program runs at a time complexity of O(N^2) as opposed to my method with a complexity of O(N).

Looking on the internet, I found that the equation I derived is the simplified recursive formula. However, even after finding multiple coding solutions, I was unable to find any that used the simplified formula to solve the nth-Catalan problem in O(N) time.

The closest I got is a O(N) method that uses the formula:

(3) C_n = (2n choose n)/(n + 1)

Is there a reason why the simplified formula (1) is not used in any of the solutions I found?

  • Your algorithm is definitely better than the quadratic one, so well done. I guess your question isn't really answerable, but one reason might be that it's not a practical problem -- the catalan numbers grow extremely quickly so computing for example C_1000 (598 digits) is not really useful. – Paul Hankin May 01 '23 at 13:22
  • In a sense, eqn (3) is fully simplified, while yours is one of several formulas that can be easily derived from it. – Matt Timmermans May 01 '23 at 14:02

1 Answers1

0

I decided to look at this problem mathematically and derived the formula: (1) C_n = (4n - 2) / (n + 1) * C_{n - 1}

This is the formula that is also provided on Wikipedia in the article on Catalan number:

The Catalan numbers satisfy the recurrence relations enter image description here

Your question then states:

even after finding multiple coding solutions, I was unable to find any that used the simplified formula to solve the nth-Catalan problem in O(N) time.

It is a known method to calculate Catalan numbers with code. For instance:

  • Rosetta Code provides code snippets for a multitude of programming languages, many using this iterative solution (or using tail recursion to achieve the same). For instance, for Python it has:

    @memoize
    def catR2(n):
        return 1 if n == 0 else (
            ((4 * n - 2) * catR2(n - 1)) // (n + 1)
        )
    
  • GeeksForGeeks lists 5 solutions, and the 4th one uses this iterative solution:

    a) initially set cat_=1 and print it
    b) run a for loop i=1 to i<=n
            cat_ *= (4*i-2)
            cat_ /= (i+1)
            print cat_
    c) end loop and exit   
    
  • On StackOverflow it is referenced in several Q&A, like here, here, here, here, et al.

Is there a reason why the simplified formula (1) is not used in any of the solutions I found?

It certainly is used, and makes a good choice. A tiny(!) downside could be that one needs to first multiply to a bigger number before dividing it. This means that if one uses fixed-size integers (like 64bit), the calculation limit is reached earlier than if only summing and multiplication is used.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Thank you for your answer! I couldn't seem to find the resources you linked during my research but now that I know it has helped me a lot. It also seems that I overlooked the 4th solution mentioned in GeeksforGeeks. – Captaincanadap May 01 '23 at 15:50