-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Added Fractional Knapsack Algorithm #725
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
Closed
Closed
Changes from all commits
Commits
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,78 @@ | ||
/* 0-1 knapsack problem | ||
|
||
items: an array of {w: v:} (where 'w' stands for weight, and 'v' stands for value) | ||
capacity: a positive integer number | ||
Will return max sum value that can reach, and the chosen subset to add up to the value. | ||
Example: | ||
var items = [{w:3,b:10},{w:1,b:3},{w:2,b:9},{w:2,b:5},{w:1,b:6}]; | ||
var capacity = 6; | ||
console.log(knapsack(items, capacity)); | ||
will return | ||
{ maxValue: 25, | ||
subset: [ { w: 1, v: 6 }, { w: 2, v: 9 }, { w: 3, v: 10 } ] } | ||
*/ | ||
|
||
function knapsack(items, capacity) { | ||
var memo = [] | ||
|
||
// Filling the sub-problem solutions grid. | ||
for (var i = 0; i < items.length; i++) { | ||
// Variable 'cap' is the capacity for sub-problems. In this example, 'cap' ranges from 1 to 6. | ||
var row = [] | ||
for (var cap = 1; cap <= capacity; cap++) { | ||
row.push(getSolution(i, cap)) | ||
} | ||
memo.push(row) | ||
} | ||
|
||
return getLast() | ||
|
||
function getLast() { | ||
var lastRow = memo[memo.length - 1] | ||
return lastRow[lastRow.length - 1] | ||
} | ||
|
||
function getSolution(row, cap) { | ||
const NO_SOLUTION = { maxValue: 0, subset: [] } | ||
|
||
var col = cap - 1 | ||
var lastItem = items[row] | ||
|
||
var remaining = cap - lastItem.w | ||
|
||
var lastSolution = row > 0 ? memo[row - 1][col] || NO_SOLUTION : NO_SOLUTION | ||
|
||
var lastSubSolution = | ||
row > 0 ? memo[row - 1][remaining - 1] || NO_SOLUTION : NO_SOLUTION | ||
|
||
if (remaining < 0) { | ||
return lastSolution | ||
} | ||
|
||
var lastValue = lastSolution.maxValue | ||
var lastSubValue = lastSubSolution.maxValue | ||
|
||
var newValue = lastSubValue + lastItem.v | ||
if (newValue >= lastValue) { | ||
var _lastSubSet = lastSubSolution.subset.slice() | ||
_lastSubSet.push(lastItem) | ||
return { maxValue: newValue, subset: _lastSubSet } | ||
} else { | ||
return lastSolution | ||
} | ||
} | ||
} | ||
|
||
// test | ||
var items = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove these statements and add a test file instead. |
||
{ w: 70, v: 135 }, | ||
{ w: 73, v: 139 }, | ||
{ w: 77, v: 149 }, | ||
{ w: 80, v: 150 }, | ||
{ w: 82, v: 156 }, | ||
{ w: 87, v: 163 }, | ||
{ w: 90, v: 173 } | ||
] | ||
|
||
var capacity = 750 | ||
console.log(knapsack(items, capacity)) |
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.