-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path14-01-knapsack.rb
315 lines (270 loc) · 8.36 KB
/
14-01-knapsack.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
puts "max profit knapsack - each item only once"
puts "Recursive"
def max_profit(profits, weights, capacity, len = profits.length)
return 0 if capacity.zero? || len.zero?
if capacity >= weights[len - 1]
[profits[len - 1] + max_profit(profits, weights, capacity - weights[len - 1], len - 1), max_profit(profits, weights, capacity, len - 1)].max
else
max_profit(profits, weights, capacity, len - 1)
end
end
p max_profit [4, 5, 3, 7], [2, 3, 1, 4], 5
puts "Recursive with memo"
def max_profit_memo(profits, weights, capacity, len = profits.length, cache = {})
return cache[[capacity, len]] if cache.key?([capacity, len])
return 0 if capacity.zero? || len.zero?
if capacity >= weights[len - 1]
cache[[capacity, len]] = [profits[len - 1] + max_profit_memo(profits, weights, capacity - weights[len - 1], len - 1, cache), max_profit_memo(profits, weights, capacity, len - 1, cache)].max
else
cache[[capacity, len]] = max_profit_memo(profits, weights, capacity, len - 1, cache)
end
end
p max_profit_memo [4, 5, 3, 7], [2, 3, 1, 4], 5
puts "DP with no optimization"
def max_profit_dp_no_op(profits, weights, capacity)
dp = (profits.length + 1).times.collect { (capacity + 1).times.collect { 0 } }
1.upto(profits.length) do |len|
1.upto(capacity) do |cap|
dp[len][cap] = if cap >= weights[len - 1]
[dp[len - 1][cap], profits[len - 1] + dp[len - 1][cap - weights[len - 1]]].max
else
dp[len - 1][cap]
end
end
end
dp[profits.length][capacity]
end
p max_profit_dp_no_op [4, 5, 3, 7], [2, 3, 1, 4], 5
p max_profit_dp_no_op [1, 6, 10, 16], [1, 2, 3, 5], 7
p max_profit_dp_no_op [1, 6, 10, 16], [1, 2, 3, 5], 6
puts "DP with some optimization"
def max_profit_dp_some_op(profits, weights, capacity)
prev = (capacity + 1).times.collect { 0 }
cur = (capacity + 1).times.collect { 0 }
1.upto(profits.length) do |len|
1.upto(capacity) do |cap|
cur[cap] = if cap >= weights[len - 1]
[prev[cap], profits[len - 1] + prev[cap - weights[len - 1]]].max
else
prev[cap]
end
end
prev, cur = cur, prev
end
prev[capacity]
end
p max_profit_dp_some_op [4, 5, 3, 7], [2, 3, 1, 4], 5
puts "DP with max optimization"
def max_profit_dp_max_op(profits, weights, capacity)
max_weight = weights.max
dp = (1 + capacity).times.collect { 0 }
1.upto(profits.length) do |len|
capacity.downto(1) do |cap|
next if cap < weights[len - 1]
dp[cap] = [dp[cap], profits[len - 1] + dp[(cap - weights[len - 1])]].max
end
end
dp[capacity]
end
p max_profit_dp_max_op [4, 5, 3, 7], [2, 3, 1, 4], 5
# require "benchmark"
# Benchmark.bm(7) do |x|
# x.report("recursive:") { 100_000.times { max_profit [4, 5, 3, 7, 7, 7, 7], [2, 3, 1, 4, 4, 4, 4], 5 } }
# x.report("memo:") { 100_000.times { max_profit_memo [4, 5, 3, 7, 7, 7, 7], [2, 3, 1, 4, 4, 4, 4], 5 } }
# x.report("dp_no_op:") { 100_000.times { max_profit_dp_no_op [4, 5, 3, 7, 7, 7, 7], [2, 3, 1, 4, 4, 4, 4], 5 } }
# x.report("dp_some_op:") { 100_000.times { max_profit_dp_some_op [4, 5, 3, 7, 7, 7, 7], [2, 3, 1, 4, 4, 4, 4], 5 } }
# x.report("dp_max_op:") { 100_000.times { max_profit_dp_max_op [4, 5, 3, 7, 7, 7, 7], [2, 3, 1, 4, 4, 4, 4], 5 } }
# end
puts "Equal subsets"
puts "Recursive:"
def equal_subset_possible(arr)
sum = arr.sum
return false if sum.odd?
equal_subset(arr, sum / 2)
end
def equal_subset(arr, target, len = arr.length)
return target.zero? if len.zero?
if target >= arr[len - 1]
equal_subset(arr, target - arr[len - 1], len - 1) || equal_subset(arr, target, len - 1)
else
equal_subset(arr, target, len - 1)
end
end
p equal_subset_possible [1, 2, 3, 4]
p equal_subset_possible [1, 1, 3, 4, 7]
p equal_subset_possible [2, 3, 4, 6]
puts "DP:"
def equal_subset_possible(arr)
sum = arr.sum
return false if sum.odd?
equal_subset(arr, sum / 2)
end
def equal_subset(arr, target, len = arr.length)
dp = (target + 1).times.collect { false }
dp[0] = true
1.upto(arr.length) do |len|
(target).downto(0) do |target|
next if target < arr[len - 1]
dp[target] = dp[target] || dp[target - arr[len - 1]]
end
end
dp[target]
end
p equal_subset_possible [1, 2, 3, 4]
p equal_subset_possible [1, 1, 3, 4, 7]
p equal_subset_possible [2, 3, 4, 6]
p equal_subset_possible [0, 0, 0, 0]
puts
def equal_subset(arr, target, len = arr.length)
prev = (target + 1).times.collect { false }
cur = (target + 1).times.collect { false }
prev[0] = true
1.upto(arr.length) do |len|
0.upto(target) do |target|
if target >= arr[len - 1]
cur[target] = prev[target] || prev[target - arr[len - 1]]
else
cur[target] = prev[target]
end
end
prev, cur = cur, prev
end
prev[target]
end
p equal_subset_possible [1, 2, 3, 4]
p equal_subset_possible [1, 1, 3, 4, 7]
p equal_subset_possible [2, 3, 4, 6]
p equal_subset_possible [0, 0, 0, 0]
puts
def equal_subset(arr, target, len = arr.length)
dp = (arr.length + 1).times.collect { [false] * (target + 1) }
dp[0][0] = true
1.upto(arr.length) do |len|
0.upto(target) do |target|
if target >= arr[len - 1]
dp[len][target] = dp[len - 1][target] || dp[len - 1][target - arr[len - 1]]
else
dp[len][target] = dp[len - 1][target]
end
end
end
res = []
arr.length.downto(1) do |len|
if dp[len][target] != dp[len - 1][target]
res.push(arr[len - 1])
target -= arr[len - 1]
end
end
res
end
p equal_subset_possible [1, 2, 3, 4]
p equal_subset_possible [1, 1, 3, 4, 7]
p equal_subset_possible [2, 3, 4, 6]
p equal_subset_possible [0, 0, 0, 0]
puts
puts "Subset sum"
def subset_sum(arr, target)
dp = (arr.length + 1).times.collect { (target + 1).times.collect { false } }
dp[0][0] = true
1.upto(arr.length) do |len|
1.upto(target) do |target|
if target >= arr[len - 1]
dp[len][target] = dp[len - 1][target] || dp[len - 1][target - arr[len - 1]]
else
dp[len][target] = dp[len - 1][target]
end
end
end
len, res = arr.length, []
while len > 0 && dp[len][target]
if !dp[len - 1][target]
res << arr[len - 1]
target -= arr[len - 1]
end
len -= 1
end
res
end
def subset_sum(arr, target)
dp = (target + 1).times.collect { false }
dp[0] = true
1.upto(arr.length) do |len|
target.downto(0) do |target|
next if target < arr[len - 1]
dp[target] ||= dp[target - arr[len - 1]]
end
end
dp[target]
end
p subset_sum [1, 2, 3, 7], 6
p subset_sum [1, 2, 7, 1, 5], 10
p subset_sum [1, 3, 4, 8], 6
puts "Minimum subset difference"
def min_subset_diff(arr)
target = arr.sum / 2
dp = (target + 1).times.collect { false }
max_target_possible = 0
dp[0] = true
1.upto(arr.length) do |len|
target.downto(0) do |target|
if target >= arr[len - 1]
dp[target] ||= dp[target - arr[len - 1]]
end
if len == arr.length && dp[target] && target > max_target_possible
max_target_possible = target
end
end
end
arr.sum - (2 * max_target_possible)
end
p min_subset_diff [1, 2, 3, 9]
p min_subset_diff [1, 2, 7, 1, 5]
p min_subset_diff [1, 3, 100, 4]
puts "Count of subsets"
def count_of_subsets(arr, target)
dp = (arr.length + 1).times.collect { (1 + target).times.collect { 0 } }
dp[0][0] = 1
1.upto(arr.length) do |len|
0.upto(target) do |target|
if arr[len - 1] > target
dp[len][target] = dp[len - 1][target]
else
dp[len][target] = dp[len - 1][target] + dp[len - 1][target - arr[len - 1]]
end
end
end
dp[arr.length][target]
end
p count_of_subsets [1, 1, 2, 3], 4
p count_of_subsets [1, 2, 7, 1, 5], 9
puts
def count_of_subsets(arr, target)
dp = (1 + target).times.collect { 0 }
dp[0] = 1
1.upto(arr.length) do |len|
target.downto(0) do |target|
if arr[len - 1] <= target
dp[target] += dp[target - arr[len - 1]]
end
end
end
dp[target]
end
p count_of_subsets [1, 1, 2, 3], 4
p count_of_subsets [1, 2, 7, 1, 5], 9
puts "Target Sum"
def target_sum(arr, sum)
total_sum = arr.sum
target = [(total_sum + sum) / 2, (total_sum - sum) / 2].min
dp = (target + 1).times.collect { 0 }
dp[0] = 1
1.upto(arr.length) do |len|
target.downto(0) do |target|
if arr[len - 1] <= target
dp[target] += dp[target - arr[len - 1]]
end
end
end
dp[target]
end
p target_sum [1, 1, 2, 3], S = 1