knapsack-problem

Why solving Knapsack problem is not considered as linear programming?

心不动则不痛 提交于 2019-12-21 03:49:30
问题 Why isn't the knapsack problem included under the category of linear programming algorithms in spite of the fact that the Knapsack problem statement seems similar to the problems in linear programming ? 回答1: Knapsack can be written as an integer linear programming program. Unlike normal linear programming, this problem requires that variables in the solution are integers. Linear programming is known to be solvable in polynomial time, while integer linear programming is NP-complete. Exercise

Knapsack algorithm with an additional property

孤街醉人 提交于 2019-12-20 12:43:28
问题 When there's 1 property, I do understand what's going on in there. I'm having a problem with understanding knapsack problem when there's more than 1 property. I have to write a program that uses knapsack algorithm with a 2 properties. Teacher told us, It has to be done in a 3d array. I can't imagine how would such array look like. Let's say here's my input: 4 3 4 // number of records below, 1st property of backpack, 2nd property of backpack 1 1 1 // 1st property, 2nd property, cost 1 2 2 //

Knapsack algorithm with an additional property

爱⌒轻易说出口 提交于 2019-12-20 12:43:12
问题 When there's 1 property, I do understand what's going on in there. I'm having a problem with understanding knapsack problem when there's more than 1 property. I have to write a program that uses knapsack algorithm with a 2 properties. Teacher told us, It has to be done in a 3d array. I can't imagine how would such array look like. Let's say here's my input: 4 3 4 // number of records below, 1st property of backpack, 2nd property of backpack 1 1 1 // 1st property, 2nd property, cost 1 2 2 //

Efficient table for Dynamic Programming in Haskell

亡梦爱人 提交于 2019-12-20 08:45:07
问题 I've coded up the 0-1 Knapsack problem in Haskell. I'm fairly proud about the laziness and level of generality achieved so far. I start by providing functions for creating and dealing with a lazy 2d matrix. mkList f = map f [0..] mkTable f = mkList (\i -> mkList (\j -> f i j)) tableIndex table i j = table !! i !! j I then make a specific table for a given knapsack problem knapsackTable = mkTable f where f 0 _ = 0 f _ 0 = 0 f i j | ws!!i > j = leaveI | otherwise = max takeI leaveI where takeI

Generating the power set of a list

前提是你 提交于 2019-12-19 04:14:37
问题 I have to write a brute-force implementation of the knapsack problem. Here's the pseudocode: computeMaxProfit(weight_capacity) max_profit = 0 S = {} // Each element of S is a weight-profit pair. while true if the sum of the weights in S <= weight_capacity if the sum of the profits in S > max_profit update max_profit if S contains all items // Then there is no next subset to generate return max generate the next subset S While the algorithm is fairly easy to implement, I haven't the slightest

Algorithm design: can you provide a solution to the multiple knapsack problem?

感情迁移 提交于 2019-12-18 21:18:19
问题 I am looking for a pseudo-code solution to what is effectively the Multiple Knapsack Problem (optimisation statement is halfway down the page). I think this problem is NP Complete so the solution doesn't need to be optimal, rather if it is fairly efficient and easily implemented that would be good. The problem is this: I have many work items, with each taking a different (but fixed and known) amount of time to complete. I need to divide these work items into groups so as to have the smallest

Knapsack algorithm restricted to N-element solution

丶灬走出姿态 提交于 2019-12-18 18:08:23
问题 This excerpt from the CRAN documentation for the adagio function knapsack() functions as expected -- it solves the knapsack problem with profit vector p , weight vector w , and capacity cap , selecting the subset of elements with maximum profit subject to the constraint that the total weight of selected elements does not exceed the capacity. library(adagio) p <- c(15, 100, 90, 60, 40, 15, 10, 1) w <- c( 2, 20, 20, 30, 40, 30, 60, 10) cap <- 102 (is <- knapsack(w, p, cap)) How can I add a

DP algorithm for bounded Knapsack?

故事扮演 提交于 2019-12-18 13:17:13
问题 The Wikipedia article about Knapsack problem contains lists three kinds of it: 1-0 (one item of a type) Bounded (several items of a type) Unbounded (unlimited number of items of a type) The article contains DP approaches for 1. and 3. types of problem, but no solution for 2. How can the dynamic programming algorithm for solving 2. be described? 回答1: Use the 0-1 variant, but allow repetition of an item in the solution up to the number of times specified in its bound. You would need to maintain

0/1 Knapsack Dynamic Programming Optimazion, from 2D matrix to 1D matrix

有些话、适合烂在心里 提交于 2019-12-18 12:06:00
问题 I need some clarification from wikipedia: Knapsack, on the part This solution will therefore run in O(nW) time and O(nW) space. Additionally, if we use only a 1-dimensional array m[W] to store the current optimal values and pass over this array i+1 times, rewriting from m[W] to m[1] every time, we get the same result for only O(W) space. I am having trouble understanding how to turn a 2D matrix into a 1D matrix to save space. In addition, to what does rewriting from m[W] to m[1] every time

How to ensure Java threads run on different cores

别来无恙 提交于 2019-12-17 22:51:30
问题 I am writing a multi-threaded application in Java in order to improve performance over the sequential version. It is a parallel version of the dynamic programming solution to the 0/1 knapsack problem. I have an Intel Core 2 Duo with both Ubuntu and Windows 7 Professional on different partitions. I am running in Ubuntu. My problem is that the parallel version actually takes longer than the sequential version. I am thinking this may be because the threads are all being mapped to the same kernel