Fix 0/1 Knapsack traceback crash and incorrect item selection#2109
Open
danielalanbates wants to merge 1 commit intotrekhleb:masterfrom
Open
Fix 0/1 Knapsack traceback crash and incorrect item selection#2109danielalanbates wants to merge 1 commit intotrekhleb:masterfrom
danielalanbates wants to merge 1 commit intotrekhleb:masterfrom
Conversation
The traceback phase of solveZeroOneKnapsackProblem() had two bugs: 1. Accessed knapsackMatrix[itemIndex - 2] without bounds checking, causing "Cannot read property of undefined" when itemIndex was 1. 2. Used incorrect logic that pushed prevItem instead of currentItem when an item was included, leading to wrong selected items. Replaced with the standard DP traceback: if dp[i][w] != dp[i-1][w], item i was included. Also added check for the first item (row 0). Added test case from issue trekhleb#248 that previously crashed. Fixes trekhleb#248
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes two bugs in
solveZeroOneKnapsackProblem()traceback phase:Crash:
knapsackMatrix[itemIndex - 2][weightIndex]accessed out-of-bounds whenitemIndex === 1, causingTypeError: Cannot read property of undefined.Wrong items selected: The traceback pushed
prevIteminstead ofcurrentItemwhen including an item, producing incorrect results even when the crash didn't occur.Changes
Replaced the buggy traceback logic with the standard 0/1 knapsack DP traceback:
dp[i][w] !== dp[i-1][w], itemiwas included — push it and reduce weight.Added a test case using the exact input from #248 that previously crashed.
Testing
All 8 tests pass (7 existing + 1 new):
should solve 0/1 knapsack problemshould solve 0/1 knapsack problem regardless of items ordershould solve 0/1 knapsack problem with no single-weight-1 item (issue #248)[NEW]should solve 0/1 knapsack problem with impossible items setshould solve 0/1 knapsack problem with all equal weightsFixes #248
This PR was created with AI assistance to help this open-source project. Happy to make any adjustments!