|
15 | 15 | -- ... |
16 | 16 | -- } |
17 | 17 | -- In the returned output, each item will receive a new attribute 'p' |
18 | | --- referring to how much (percentage) of the item has been added into |
| 18 | +-- referring to how much (percentage) of the item has been added into |
19 | 19 | -- the knapsack. |
20 | 20 |
|
21 | 21 | -- Performs fractional Knapsack. Fractional here means we can |
22 | 22 | -- select a portion of a item. With that respect, this implementation |
23 | 23 | -- is greedy. |
24 | | --- items : an array of items (this array will be sorted regarding |
25 | | --- their benefits in decreasing order) |
| 24 | +-- items : an array of items (see note before). This array will be |
| 25 | +-- sorted regarding their benefits in decreasing order. |
26 | 26 | -- capacity: the maximum capacity of the knapsack |
27 | | --- returns : an array of items |
| 27 | +-- returns : 1. an array of items |
| 28 | +-- 2. the maximum profit |
28 | 29 | local function fractionalKnapsack(items, capacity) |
29 | 30 | table.sort(items, function(a, b) return a.b > b.b end) |
30 | | - local inKnapsack = {} |
| 31 | + local inKnapsack, profit = {}, 0 |
31 | 32 | while capacity > 0 do |
32 | 33 | local max_item = pop(items) |
33 | 34 | max_item.p = max_item.w > capacity and capacity/max_item.w or 1 |
34 | 35 | max_item.b = max_item.p * max_item.b |
35 | 36 | max_item.w = max_item.p * max_item.w |
36 | 37 | capacity = capacity - max_item.w |
37 | 38 | table.insert(inKnapsack, max_item) |
| 39 | + profit = profit + max_item.b |
38 | 40 | end |
39 | | - return inKnapsack |
| 41 | + return inKnapsack, profit |
| 42 | +end |
| 43 | + |
| 44 | + |
| 45 | +-- Performs standard 0/1 Knapsack, meaning that an item is either |
| 46 | +-- picked or not. This implementation uses dynamic programming. |
| 47 | +-- items : an array of items (see note before). |
| 48 | +-- capacity: the maximum capacity of the knapsack |
| 49 | +-- returns : 1. an array of items |
| 50 | +-- 2. the maximum profit |
| 51 | +local function integerKnapsack(items, capacity |
| 52 | + -- Get the count of items |
| 53 | + local numOfItems = #items |
| 54 | + |
| 55 | + -- Auxiliary tables for dynamic search and selected items tracking |
| 56 | + local V, K = {}, {} |
| 57 | + |
| 58 | + -- Inits auxiliary tables with 0's. Note that although |
| 59 | + -- Lua's arrays start at 1, we start looping at 0 |
| 60 | + for i = 0, numOfItems do |
| 61 | + V[i], K[i] = {}, {} |
| 62 | + for w = 0, capacity do |
| 63 | + V[i][w], K[i][w] = 0, 0 |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + -- Dynamic search |
| 68 | + for i = 1, numOfItems do |
| 69 | + local item = items[i] |
| 70 | + for w = 0, capacity do |
| 71 | + if item.w < w |
| 72 | + and (item.b + V[i - 1][w - item.w] > V[i - 1][w]) then |
| 73 | + V[i][w] = item.b + V[i-1][w - item.w] |
| 74 | + K[i][w] = 1 |
| 75 | + else |
| 76 | + V[i][w] = V[i - 1][w] |
| 77 | + K[i][w] = 0 |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + -- Process auxiliary tables to identify |
| 83 | + -- selected items and evaluate the profit |
| 84 | + local inKnapsack, profit = {}, 0 |
| 85 | + for i = numOfItems, 1, -1 do |
| 86 | + local item = items[i] |
| 87 | + if K[i][capacity] == 1 then |
| 88 | + table.insert(inKnapsack, item) |
| 89 | + capacity = capacity - item.w |
| 90 | + profit = profit + item.b |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + return inKnapsack, profit |
40 | 95 | end |
41 | 96 |
|
42 | 97 | return { |
43 | 98 | fractional = fractionalKnapsack, |
| 99 | + integer = integerKnapsack |
44 | 100 | } |
45 | 101 |
|
46 | 102 |
|
0 commit comments