-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: add Catalan numbers implementation using dynamic programming #961
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
siriak
merged 5 commits into
TheAlgorithms:master
from
AliAlimohammadi:feat/catalan_numbers
Dec 6, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4d0ff69
feat: Add Catalan numbers implementation using dynamic programming
AliAlimohammadi 2cdd868
Update DIRECTORY.md
AliAlimohammadi 2e2ce80
Update src/dynamic_programming/catalan_numbers.rs
AliAlimohammadi 687fd03
Update catalan_numbers.rs with complexity analysis.
AliAlimohammadi be79f44
Update catalan_numbers.rs
AliAlimohammadi 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
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,111 @@ | ||
| //! Catalan Numbers using Dynamic Programming | ||
| //! | ||
| //! The Catalan numbers are a sequence of positive integers that appear in many | ||
| //! counting problems in combinatorics. Such problems include counting: | ||
| //! - The number of Dyck words of length 2n | ||
| //! - The number of well-formed expressions with n pairs of parentheses | ||
| //! (e.g., `()()` is valid but `())(` is not) | ||
| //! - The number of different ways n + 1 factors can be completely parenthesized | ||
| //! (e.g., for n = 2, C(n) = 2 and (ab)c and a(bc) are the two valid ways) | ||
| //! - The number of full binary trees with n + 1 leaves | ||
| //! | ||
| //! A Catalan number satisfies the following recurrence relation: | ||
| //! - C(0) = C(1) = 1 | ||
| //! - C(n) = sum(C(i) * C(n-i-1)), from i = 0 to n-1 | ||
| //! | ||
| //! Sources: | ||
| //! - [Brilliant.org](https://brilliant.org/wiki/catalan-numbers/) | ||
| //! - [Wikipedia](https://en.wikipedia.org/wiki/Catalan_number) | ||
|
|
||
| /// Computes the Catalan number sequence from 0 through `upper_limit`. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `upper_limit` - The upper limit for the Catalan sequence (must be ≥ 0) | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// A vector containing Catalan numbers from C(0) to C(upper_limit) | ||
| /// | ||
| /// # Complexity | ||
| /// | ||
| /// * Time complexity: O(n²) | ||
| /// * Space complexity: O(n) | ||
| /// | ||
| /// where `n` is the `upper_limit`. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// use the_algorithms_rust::dynamic_programming::catalan_numbers; | ||
| /// | ||
| /// assert_eq!(catalan_numbers(5), vec![1, 1, 2, 5, 14, 42]); | ||
| /// assert_eq!(catalan_numbers(2), vec![1, 1, 2]); | ||
| /// assert_eq!(catalan_numbers(0), vec![1]); | ||
| /// ``` | ||
| /// | ||
| /// # Warning | ||
| /// | ||
| /// This will overflow the 64-bit unsigned integer for large values of `upper_limit`. | ||
| /// For example, C(21) and beyond will cause overflow. | ||
| pub fn catalan_numbers(upper_limit: usize) -> Vec<u64> { | ||
| let mut catalan_list = vec![0u64; upper_limit + 1]; | ||
|
|
||
| // Base case: C(0) = 1 | ||
| catalan_list[0] = 1; | ||
|
|
||
| // Base case: C(1) = 1 | ||
| if upper_limit > 0 { | ||
| catalan_list[1] = 1; | ||
| } | ||
|
|
||
| // Recurrence relation: C(i) = sum(C(j) * C(i-j-1)), from j = 0 to i-1 | ||
| for i in 2..=upper_limit { | ||
| for j in 0..i { | ||
| catalan_list[i] += catalan_list[j] * catalan_list[i - j - 1]; | ||
| } | ||
| } | ||
|
|
||
| catalan_list | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_catalan_numbers_basic() { | ||
| assert_eq!(catalan_numbers(5), vec![1, 1, 2, 5, 14, 42]); | ||
| assert_eq!(catalan_numbers(2), vec![1, 1, 2]); | ||
| assert_eq!(catalan_numbers(0), vec![1]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_catalan_numbers_single() { | ||
| assert_eq!(catalan_numbers(1), vec![1, 1]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_catalan_numbers_extended() { | ||
| let result = catalan_numbers(10); | ||
| assert_eq!(result.len(), 11); | ||
| assert_eq!(result[0], 1); | ||
| assert_eq!(result[1], 1); | ||
| assert_eq!(result[2], 2); | ||
| assert_eq!(result[3], 5); | ||
| assert_eq!(result[4], 14); | ||
| assert_eq!(result[5], 42); | ||
| assert_eq!(result[6], 132); | ||
| assert_eq!(result[7], 429); | ||
| assert_eq!(result[8], 1430); | ||
| assert_eq!(result[9], 4862); | ||
| assert_eq!(result[10], 16796); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_catalan_first_few() { | ||
| // Verify the first few Catalan numbers match known values | ||
| assert_eq!(catalan_numbers(3), vec![1, 1, 2, 5]); | ||
| assert_eq!(catalan_numbers(4), vec![1, 1, 2, 5, 14]); | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.