Skip to content

Conversation

@AliAlimohammadi
Copy link
Contributor

Description

This PR adds an implementation of Catalan Numbers using dynamic programming.

The Catalan numbers are a fundamental sequence in combinatorics that appear in numerous counting problems, including:

  • Counting valid parentheses combinations (e.g., ()() vs ())()
  • Counting different ways to fully parenthesize expressions
  • Counting full binary trees with a given number of leaves
  • Counting Dyck words

Algorithm Overview:
The implementation uses dynamic programming with the recurrence relation:

  • Base cases: C(0) = C(1) = 1
  • Recurrence: C(n) = Σ(C(i) × C(n-i-1)) for i from 0 to n-1

Time Complexity: O(n²)
Space Complexity: O(n)

The function computes all Catalan numbers from C(0) through C(upper_limit) and returns them as a vector.

References:

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist:

  • I ran below commands using the latest version of rust nightly.
  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I added my algorithm to the corresponding mod.rs file within its own folder, and in any parent folder(s).
  • I added my algorithm to DIRECTORY.md with the correct link.
  • I checked CONTRIBUTING.md and my code follows its guidelines.

Note: All tests run quickly (well under 300ms) as they only compute Catalan numbers up to C(10).

@codecov-commenter
Copy link

codecov-commenter commented Dec 5, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.48%. Comparing base (eaf895d) to head (be79f44).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #961      +/-   ##
==========================================
+ Coverage   95.40%   95.48%   +0.08%     
==========================================
  Files         334      336       +2     
  Lines       21576    21836     +260     
==========================================
+ Hits        20585    20851     +266     
+ Misses        991      985       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AliAlimohammadi
Copy link
Contributor Author

@siriak, This PR is ready to be merged.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new dynamic programming implementation for computing Catalan numbers, a fundamental sequence in combinatorics used for counting problems such as valid parentheses combinations, binary trees, and Dyck words. The implementation uses the standard recurrence relation with O(n²) time complexity and O(n) space complexity.

Key Changes:

  • Added catalan_numbers.rs with a well-documented dynamic programming implementation
  • Updated module exports in mod.rs to include the new function
  • Added entry to DIRECTORY.md for documentation

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/dynamic_programming/catalan_numbers.rs New implementation of Catalan numbers using dynamic programming with comprehensive tests and documentation
src/dynamic_programming/mod.rs Added module declaration and public export for catalan_numbers, maintaining alphabetical order
DIRECTORY.md Added entry for Catalan Numbers algorithm (indentation issue noted)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

AliAlimohammadi and others added 3 commits December 5, 2025 14:52
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Contributor Author

@AliAlimohammadi AliAlimohammadi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed.

@AliAlimohammadi AliAlimohammadi changed the title feat: Add Catalan numbers implementation using dynamic programming feat: add Catalan numbers implementation using dynamic programming Dec 5, 2025
Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@siriak siriak merged commit 2d1bfc4 into TheAlgorithms:master Dec 6, 2025
7 checks passed
@AliAlimohammadi AliAlimohammadi deleted the feat/catalan_numbers branch December 6, 2025 01:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants