-
Notifications
You must be signed in to change notification settings - Fork 162
Added 2 different implementations for Catalan Numbers #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
24f5deb
Added 2 different implementations for Catalan Numbers
rahulkumaran c29e3a9
Update catalan_binomial.py
rahulkumaran ef9b750
Create README.md
rahulkumaran ad8540e
Update README.md
rahulkumaran 70fb09c
Update README.md
rahulkumaran 9a1fc8c
Update README.md
rahulkumaran e63ac07
Update catalan_binomial.py
rahulkumaran e3cac9d
Update catalan_recursive.py
rahulkumaran 099e891
Update README.md
rahulkumaran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
The two different programs in this folder contain the same implementation! | ||
The programs basically are a python implementation of the Catalan Numbers. | ||
They're similar to Fibonacci (very slightly) | ||
|
||
The two different implementations vary only in terms of time taken!<br> | ||
Basically we can implement in 3 different ways!<br> | ||
|
||
* Recursively - `Exponential time complexity`<br> | ||
* Dynamic Programming - `O(n^2)`<br> | ||
* Binomial Coefficient - `O(n)` <br><br> | ||
|
||
Each of the above 3 implementations have a descending order of time complexities from (1) to (3) <br><br> | ||
|
||
The Binomial implementation has the best time complexity while the Recursive implementation has the worst time complexity<br> | ||
In this folder we have the Binomial and Recursive implementations of Catalan Numbers.<br><br> | ||
|
||
Catalan Numbers have a lot of applications in Combinatorics. Some of them are listed below: | ||
* 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. | ||
* 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). | ||
* Cn is the number of semiorders on n unlabeled items. | ||
* In chemical engineering Cn-1 is the number of possible separation sequences which can separate a mixture of n components.<br><br> | ||
|
||
### Sources : | ||
(1) Click [here](https://www.geeksforgeeks.org/program-nth-catalan-number/) to know more about Catalan number implementations and its theory.<br> | ||
(2) Click [here](https://en.wikipedia.org/wiki/Catalan_number) to know more about the applicatations of Catalan Numbers in Combinatorics |
15 changes: 15 additions & 0 deletions
15
Competitive Coding/Math/Catalan_Numbers/catalan_binomial.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
def binCoeff(n, k): #Finds the binomial coefficient | ||
if (k > n - k): #As binCoeff(n,k)=binCoeff(n,n-k) | ||
k = n - k | ||
res = 1 #Initialising Result | ||
for i in range(k): | ||
res = res * (n - i) | ||
res = res / (i + 1) | ||
return res #The binomial coefficient is returned | ||
|
||
def catalan(n): #Function that finds catalan numbers | ||
c = binCoeff(2*n, n) #Finding value of c by calling the binCoeff function | ||
return c/(n + 1) #This is the final catalan number | ||
|
||
if __name__=='__main__': | ||
print "The 10th catalan number is:",catalan(9) |
10 changes: 10 additions & 0 deletions
10
Competitive Coding/Math/Catalan_Numbers/catalan_recursive.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def catalan_numbers(n): | ||
if n <=1 : #The result will be 1, if the function takes an argument that's equal to or less than 1 | ||
return 1 | ||
res = 0 #Result has been initialised to 0 | ||
for i in range(n): | ||
res += catalan_numbers(i) * catalan_numbers(n-i-1) #The recursive function call | ||
return res | ||
|
||
if __name__=='__main__': | ||
print "The 10th catalan number is:",catalan(9) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where is the coefficient in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@prateekiiest what coefficient are you referring to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The combination C factor