knapsack-problem

Knapsack 0-1 path reconstruction (which items to take) [duplicate]

情到浓时终转凉″ 提交于 2021-02-19 05:37:20
问题 This question already has answers here : How to find which elements are in the bag, using Knapsack Algorithm [and not only the bag's value]? (4 answers) Closed 6 days ago . I know how to solve knapsack 0-1 problem with dynamic programming approach, but I am having troubles figuring out which items to take without compromising the complexity of O(N * C) (N items, C capacity). Any ideas (I would prefer a bottom-up approach)? 回答1: Suppose, right now you're storing results in array bool[] a ,

Cases where the greedy algorithm fails the 0-1 knapsack p‌r‌o‌b‌l‌e‌m

空扰寡人 提交于 2021-02-16 15:17:29
问题 I'm looking for a case where the greedy algorithm of picking items of the highest value/weight ratio that has weight < max weight and putting it in the knapsack first won't work. Does anyone have examples? Because otherwise, the worst case for greedy would be O(nlogn) (nlogn to sort in descending value/weight and n to go through it) while the dynamic programming way's worst case would be O(nW), making greedy faster when logn < W. 回答1: Consider a backpack with capacity 4, and items with the

Calculate a rough estimate for shipping box size

守給你的承諾、 提交于 2021-02-16 08:37:56
问题 I'm trying to find the best way to calculate the box size needed for shipping. I have 3 shipping containers with different sizes. I have the product's width, length, depth, and mass defined in the database. I would like to know how to find the smallest amount of boxes needed to ship, and also the smallest dimensions of those boxes given the number of items in the cart. My current 'idea' is to find the maximum width of the entire products array, the select a box according to it, and then split