Skip to content

Commit cfba6e1

Browse files
authored
Merge pull request #255 from rahulkumaran/catalan
Added 2 different implementations for Catalan Numbers
2 parents cc26632 + 099e891 commit cfba6e1

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
The two different programs in this folder contain the same implementation!
2+
The programs basically are a python implementation of the Catalan Numbers.
3+
They're similar to Fibonacci (very slightly)
4+
5+
The two different implementations vary only in terms of time taken!<br>
6+
Basically we can implement in 3 different ways!<br>
7+
8+
* Recursively - `Exponential time complexity`<br>
9+
* Dynamic Programming - `O(n^2)`<br>
10+
* Binomial Coefficient - `O(n)` <br><br>
11+
12+
Each of the above 3 implementations have a descending order of time complexities from (1) to (3) <br><br>
13+
14+
The Binomial implementation has the best time complexity while the Recursive implementation has the worst time complexity<br>
15+
In this folder we have the Binomial and Recursive implementations of Catalan Numbers.<br><br>
16+
17+
Catalan Numbers have a lot of applications in Combinatorics. Some of them are listed below:
18+
* Cn is the number of standard Young tableaux whose diagram is a 2-by-n rectangle. In other words, it is the number of ways the numbers 1, 2, ..., 2n can be arranged in a 2-by-n rectangle so that each row and each column is increasing. As such, the formula can be derived as a special case of the hook-length formula.
19+
* Cn is the number of ways that the vertices of a convex 2n-gon can be paired so that the line segments joining paired vertices do not intersect. This is precisely the condition that guarantees that the paired edges can be identified (sewn together) to form a closed surface of genus zero (a topological 2-sphere).
20+
* Cn is the number of semiorders on n unlabeled items.
21+
* In chemical engineering Cn-1 is the number of possible separation sequences which can separate a mixture of n components.<br><br>
22+
23+
### Sources :
24+
(1) Click [here](https://www.geeksforgeeks.org/program-nth-catalan-number/) to know more about Catalan number implementations and its theory.<br>
25+
(2) Click [here](https://en.wikipedia.org/wiki/Catalan_number) to know more about the applicatations of Catalan Numbers in Combinatorics
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def binCoeff(n, k): #Finds the binomial coefficient
2+
if (k > n - k): #As binCoeff(n,k)=binCoeff(n,n-k)
3+
k = n - k
4+
res = 1 #Initialising Result
5+
for i in range(k):
6+
res = res * (n - i)
7+
res = res / (i + 1)
8+
return res #The binomial coefficient is returned
9+
10+
def catalan(n): #Function that finds catalan numbers
11+
c = binCoeff(2*n, n) #Finding value of c by calling the binCoeff function
12+
return c/(n + 1) #This is the final catalan number
13+
14+
if __name__=='__main__':
15+
print "The 10th catalan number is:",catalan(9)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def catalan_numbers(n):
2+
if n <=1 : #The result will be 1, if the function takes an argument that's equal to or less than 1
3+
return 1
4+
res = 0 #Result has been initialised to 0
5+
for i in range(n):
6+
res += catalan_numbers(i) * catalan_numbers(n-i-1) #The recursive function call
7+
return res
8+
9+
if __name__=='__main__':
10+
print "The 10th catalan number is:",catalan(9)

0 commit comments

Comments
 (0)