Skip to content

Commit

Permalink
Add Knapsack exercise (#1644)
Browse files Browse the repository at this point in the history
  • Loading branch information
fpsvogel authored Mar 18, 2024
1 parent d408ec0 commit 7e8509d
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 0 deletions.
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,18 @@
"enumeration"
],
"difficulty": 8
},
{
"slug": "knapsack",
"name": "Knapsack",
"uuid": "91b585ce-b476-4c0d-aa2b-c1487a6e466f",
"practices": [],
"prerequisites": [
"numbers",
"arrays",
"enumeration"
],
"difficulty": 8
}
]
},
Expand Down
55 changes: 55 additions & 0 deletions exercises/practice/knapsack/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Hints

## A starting point: brute-force recursion

If you're stuck, a good starting point is a brute-force recursive solution.
You can see it sketched out in the first half of the article ["Demystifying the 0-1 knapsack problem: top solutions explained"](demystifying-the-knapsack-problem).

## Dynamic programming: what is it?

For a more efficient solution, you can improve your recursive solution using *dynamic programming*, which is introduced in the second half of [the above-mentioned article](demystifying-the-knapsack-problem).

For a more general explainer, see the video ["5 Simple Steps for Solving Dynamic Programming Problems"](solving-dynamic-programming-problems)

## Dynamic programming and the knapsack problem

If you need a more visual walkthrough of how to apply dynamic programming to the knapsack problem, see the video ["0/1 Knapsack problem | Dynamic Programming"](0-1-knapsack-problem).

Also worth mentioning is [this answer](intuition-of-dp-for-knapsack-problem) to a question on Reddit, *"What is the intuition behind Knapsack problem solution using dynamic programming?"*.
Here is the answer in full:

> The intuition behind the solution is basically like any dynamic programming solution: split the task into many sub-tasks, and save solutions to these sub-tasks for later use.
>
> In this case the sub task is to **"Try to fit x items into a knapsack of a smaller size"** instead of trying all possible variations in the whole thing right away.
>
> The idea here is that at any point you can ask, *"Does this item fit into the sack at all?"*
> If not, you repeat by looking at a bigger portion of the sack until you reach the whole size of it.
> If the item still doesn't fit, then it's simply not part of any solution.
>
> If it does fit, however, then there are two options.
> Either the maximum value for that portion of the sack is achieved without the item, or with the item.
> If the former is true then we can just take the previous solution because we already tried the previous items.
> (For example, if we try item 4 and it doesn't increase our maximum then we can just use our previous solution for items 1-3.)
> If the latter is true then we put item 4 in, which takes some value off of our capacity.
> The remaining capacity gets filled with a previous solution.
> How?
> Well, we already tried smaller capacities beforehand, so there should be a solution for that smaller, in this case remaining, capacity.
>
> So the idea is to split the entire knapsack problem into smaller knapsack problems.
> Instead of testing 10 items with capacity 50, you first try (after the trivial case of 0) 1 item and capacity 10, 20, 30, 40 and 50 (or however many sub tasks you want to create) and then take another item and start again at capacity 10.
>
> If you see item 1 fits into capacity 20+, then all these slots in the table now contain this value.
> Then you look at item 2 from capacity 10-50 again.
> Let's assume item 2 fits into capacity 20 as well.
> Then now you check whether it is a new maximum or not, and if it is, then you update the table.
> Now you look at capacity 30 for item 2.
> You see that item 2 fits; this means 10 capacity would remain if you take it.
> However there, as of now, was no item that fits into 10 capacity, thus the solution remains the same as before.
> At 40 this changes: you now realize that even if you include item 2 there are 20 capacity remaining, thus you can fill that space with the previous solution, which was item 1.
> Thus for 40 capacity, as of now, the optimal solution is to take item 1 and 2.
> And so on.
[demystifying-the-knapsack-problem]: https://www.educative.io/blog/0-1-knapsack-problem-dynamic-solution
[solving-dynamic-programming-problems]: https://www.youtube.com/watch?v=aPQY__2H3tE
[0-1-knapsack-problem]: https://www.youtube.com/watch?v=cJ21moQpofY
[intuition-of-dp-for-knapsack-problem]: https://www.reddit.com/r/explainlikeimfive/comments/junw6n/comment/gces429
35 changes: 35 additions & 0 deletions exercises/practice/knapsack/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Instructions

In this exercise, let's try to solve a classic problem.

Bob is a thief.
After months of careful planning, he finally manages to crack the security systems of a high-class apartment.

In front of him are many items, each with a value (v) and weight (w).
Bob, of course, wants to maximize the total value he can get; he would gladly take all of the items if he could.
However, to his horror, he realizes that the knapsack he carries with him can only hold so much weight (W).

Given a knapsack with a specific carrying capacity (W), help Bob determine the maximum value he can get from the items in the house.
Note that Bob can take only one of each item.

All values given will be strictly positive.
Items will be represented as a list of items.
Each item will have a weight and value.

For example:

```none
Items: [
{ "weight": 5, "value": 10 },
{ "weight": 4, "value": 40 },
{ "weight": 6, "value": 30 },
{ "weight": 4, "value": 50 }
]
Knapsack Limit: 10
```

For the above, the first item has weight 5 and value 10, the second item has weight 4 and value 40, and so on.

In this example, Bob should take the second and fourth item to maximize his value, which, in this case, is 90.
He cannot get more than 90 as his knapsack has a weight limit of 10.
17 changes: 17 additions & 0 deletions exercises/practice/knapsack/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": ["fpsvogel"],
"files": {
"solution": [
"knapsack.rb"
],
"test": [
"knapsack_test.rb"
],
"example": [
".meta/example.rb"
]
},
"blurb": "Given a knapsack that can only carry a certain weight, determine which items to put in the knapsack in order to maximize their combined value.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Knapsack_problem"
}
25 changes: 25 additions & 0 deletions exercises/practice/knapsack/.meta/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This solution uses dynamic programming to memoize solutions to overlapping
# subproblems, so that they don't need to be recomputed. It's essentially a
# recursive solution that remembers best-so-far outputs of previous inputs. The
# algorithm has a time complexity of O(n * W), where n is the number of items
# and W is the knapsack's maximum weight.
class Knapsack
def initialize(max_weight)
@max_weight = max_weight
end

def max_value(items)
# e.g. max_values[3] is the maximum value so far for a maximum weight of 3.
max_values = Array.new(@max_weight + 1, 0)

items.each do |item|
@max_weight.downto(item.weight) do |weight|
value_with_item = max_values[weight - item.weight] + item.value

max_values[weight] = [max_values[weight], value_with_item].max
end
end

max_values[@max_weight]
end
end
36 changes: 36 additions & 0 deletions exercises/practice/knapsack/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[a4d7d2f0-ad8a-460c-86f3-88ba709d41a7]
description = "no items"
include = false

[3993a824-c20e-493d-b3c9-ee8a7753ee59]
description = "no items"
reimplements = "a4d7d2f0-ad8a-460c-86f3-88ba709d41a7"

[1d39e98c-6249-4a8b-912f-87cb12e506b0]
description = "one item, too heavy"

[833ea310-6323-44f2-9d27-a278740ffbd8]
description = "five items (cannot be greedy by weight)"

[277cdc52-f835-4c7d-872b-bff17bab2456]
description = "five items (cannot be greedy by value)"

[81d8e679-442b-4f7a-8a59-7278083916c9]
description = "example knapsack"

[f23a2449-d67c-4c26-bf3e-cde020f27ecc]
description = "8 items"

[7c682ae9-c385-4241-a197-d2fa02c81a11]
description = "15 items"
7 changes: 7 additions & 0 deletions exercises/practice/knapsack/knapsack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
=begin
Write your code for the 'Knapsack' exercise in this file. Make the tests in
`knapsack_test.rb` pass.
To get started with TDD, see the `README.md` file in your
`ruby/knapsack` directory.
=end
127 changes: 127 additions & 0 deletions exercises/practice/knapsack/knapsack_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
require 'minitest/autorun'
require_relative 'knapsack'

class KnapsackTest < Minitest::Test
Item = Data.define(:weight, :value)

def test_no_items
# skip
max_weight = 100
items = []
expected = 0
actual = Knapsack.new(max_weight).max_value(items)

assert_equal expected, actual,
"When there are no items, the resulting value must be 0."
end

def test_one_item_too_heavy
skip
max_weight = 10
items = [Item.new(weight: 100, value: 1)]
expected = 0
actual = Knapsack.new(max_weight).max_value(items)

assert_equal expected, actual,
"When there is one item that is too heavy, the resulting value must be 0."
end

def test_five_items_cannot_be_greedy_by_weight
skip
max_weight = 10
items = [
Item.new(weight: 2, value: 5),
Item.new(weight: 2, value: 5),
Item.new(weight: 2, value: 5),
Item.new(weight: 2, value: 5),
Item.new(weight: 10, value: 21)
]
expected = 21
actual = Knapsack.new(max_weight).max_value(items)

assert_equal expected, actual,
"Do not prioritize the most valuable items per weight when that would " \
"result in a lower total value."
end

def test_five_items_cannot_be_greedy_by_value
skip
max_weight = 10
items = [
Item.new(weight: 2, value: 20),
Item.new(weight: 2, value: 20),
Item.new(weight: 2, value: 20),
Item.new(weight: 2, value: 20),
Item.new(weight: 10, value: 50)
]
expected = 80
actual = Knapsack.new(max_weight).max_value(items)

assert_equal expected, actual,
"Do not prioritize the items with the highest value when that would " \
"result in a lower total value."
end

def test_example_knapsack
skip
max_weight = 10
items = [
Item.new(weight: 5, value: 10),
Item.new(weight: 4, value: 40),
Item.new(weight: 6, value: 30),
Item.new(weight: 4, value: 50)
]
expected = 90
actual = Knapsack.new(max_weight).max_value(items)

assert_equal expected, actual,
"A small example knapsack must result in a value of 90."
end

def test_eight_items
skip
max_weight = 104
items = [
Item.new(weight: 25, value: 350),
Item.new(weight: 35, value: 400),
Item.new(weight: 45, value: 450),
Item.new(weight: 5, value: 20),
Item.new(weight: 25, value: 70),
Item.new(weight: 3, value: 8),
Item.new(weight: 2, value: 5),
Item.new(weight: 2, value: 5)
]
expected = 900
actual = Knapsack.new(max_weight).max_value(items)

assert_equal expected, actual,
"A larger example knapsack with 8 items must result in a value of 900."
end

def test_fifteen_items
skip
max_weight = 750
items = [
Item.new(weight: 70, value: 135),
Item.new(weight: 73, value: 139),
Item.new(weight: 77, value: 149),
Item.new(weight: 80, value: 150),
Item.new(weight: 82, value: 156),
Item.new(weight: 87, value: 163),
Item.new(weight: 90, value: 173),
Item.new(weight: 94, value: 184),
Item.new(weight: 98, value: 192),
Item.new(weight: 106, value: 201),
Item.new(weight: 110, value: 210),
Item.new(weight: 113, value: 214),
Item.new(weight: 115, value: 221),
Item.new(weight: 118, value: 229),
Item.new(weight: 120, value: 240)
]
expected = 1458
actual = Knapsack.new(max_weight).max_value(items)

assert_equal expected, actual,
"A very large example knapsack with 15 items must result in a value of 1458."
end
end

0 comments on commit 7e8509d

Please sign in to comment.