2018-11-11 02:39:53 +08:00
|
|
|
/** Print all the Catalan numbers from 0 to n, n being the user input.
|
|
|
|
|
|
|
|
* A Catalan number satifies the following two properties:
|
|
|
|
* C(0) = C(1) = 1; C(n) = sum(C(i).C(n-i-1)), from i = 0 to n-1
|
|
|
|
* Read more about Catalan numbers here:
|
|
|
|
https://en.wikipedia.org/wiki/Catalan_number
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
int *cat; // global array to hold catalan numbers
|
|
|
|
|
|
|
|
unsigned long int catalan_dp(int n)
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
/** Using the tabulation technique in dynamic programming,
|
2018-11-11 02:39:53 +08:00
|
|
|
this function computes the first `n+1` Catalan numbers
|
|
|
|
|
|
|
|
Parameter
|
|
|
|
---------
|
|
|
|
n: The number of catalan numbers to be computed.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
cat[n]: An array containing the first `n+1` Catalan numbers
|
|
|
|
*/
|
|
|
|
|
|
|
|
// By definition, the first two Catalan numbers are 1
|
2019-08-21 10:10:08 +08:00
|
|
|
cat[0] = cat[1] = 1;
|
2018-11-11 02:39:53 +08:00
|
|
|
|
|
|
|
// Compute the remaining numbers from index 2 to index n, using tabulation
|
2019-08-21 10:10:08 +08:00
|
|
|
for (int i = 2; i <= n; i++)
|
|
|
|
{
|
|
|
|
cat[i] = 0;
|
|
|
|
for (int j = 0; j < i; j++)
|
|
|
|
cat[i] += cat[j] * cat[i - j - 1]; // applying the definition here
|
|
|
|
}
|
2018-11-11 02:39:53 +08:00
|
|
|
|
|
|
|
// Return the result
|
2019-08-21 10:10:08 +08:00
|
|
|
return cat[n];
|
2018-11-11 02:39:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
int n;
|
|
|
|
cout << "Enter n: ";
|
|
|
|
cin >> n;
|
2018-11-11 02:39:53 +08:00
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
cat = new int[n + 1];
|
2018-11-11 02:39:53 +08:00
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
cout << "Catalan numbers from 0 to " << n << " are:\n";
|
|
|
|
for (int i = 0; i <= n; i++)
|
2018-11-11 02:39:53 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
cout << "catalan (" << i << ") = " << catalan_dp(i) << endl;
|
|
|
|
// NOTE: Since `cat` is a global array, calling `catalan_dp`
|
2018-11-11 02:39:53 +08:00
|
|
|
// repeatedly will not recompute the the values already computed
|
2019-08-21 10:10:08 +08:00
|
|
|
// as in case of pre-computed values, the array will simply return them,
|
2018-11-11 02:39:53 +08:00
|
|
|
// instead of recomputing them.
|
|
|
|
}
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
return 0;
|
2018-11-11 02:39:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Sample Test Case:
|
|
|
|
|
|
|
|
$ cd "Dynamic Programming"
|
|
|
|
$ g++ Catalan-Numbers.cpp
|
|
|
|
$ ./a.exe
|
|
|
|
|
|
|
|
Enter n: 5
|
|
|
|
Catalan numbers from 0 to 5 are:
|
|
|
|
catalan (0) = 1
|
|
|
|
catalan (1) = 1
|
|
|
|
catalan (2) = 2
|
|
|
|
catalan (3) = 5
|
|
|
|
catalan (4) = 14
|
|
|
|
catalan (5) = 42
|
|
|
|
|
|
|
|
*/
|