Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Optimized Recursive Knapsack with Memoization #12550

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

adityakrmishra
Copy link

Summary
This PR improves the recursive implementation of the Knapsack Problem by adding memoization, reducing the time complexity from O(2ⁿ) to O(n × max_weight).

Changes Made:
✅ Added Memoization using a dictionary to store previously computed results.
✅ Optimized Recursion by avoiding redundant calculations.
✅ Improved Function Readability with clear parameter names and better docstrings.
✅ Maintained the Same Functionality but with better efficiency.
✅ Retained Doctests to ensure correctness.

Performance Improvement:
The previous approach was exponential O(2ⁿ) and inefficient for large inputs.
The new approach is O(n × max_weight), making it significantly faster.
Test Cases:
The function has been tested with sample cases:

assert knapsack([1, 2, 4, 5], [5, 4, 8, 6], 4, 5, 0) == 13
assert knapsack([3, 4, 5], [10, 9, 8], 3, 25, 0) == 27
Issue Reference:
Closes [#IssueNumber] (if applicable).

Additional Notes:
Looking forward to feedback! 🚀

adityakrmishra and others added 2 commits January 31, 2025 16:20
- Added memoization to avoid redundant calculations
- Improved function readability with better parameter naming
- Ensured correct handling of empty inputs
- Reduced time complexity from O(2ⁿ) to O(n * max_weight)
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Jan 31, 2025
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Jan 31, 2025
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Jan 31, 2025
@adityakrmishra
Copy link
Author

hey freinds review it please for adding it in github repository

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
awaiting reviews This PR is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant