There are several versions to this problem related to the availability of items. In particular, the 0-1 version represents a situation where only one item of each type is available or can be selected. The unbounded case, which we consider here assumes that there are n types of items and that there are ifinitely many items of each type. The raw input data for this version consists then of the following:
z(U):= max { v1x1 + v2x2 + ... + vnxn} subject to: u1x1 + u2x2 + ... + unxn <= U x1,...,xn are non-negative integers |
The volumes u1,...,un of the items as well as the total volume of the knapsack, U, are assumed to be non-negative integers. Recall that, as indicated above, the "unbounded" version of the knapsack problem allows you to select as many items of each type as you wish, subject to the total volume constraint. The JavaScript code is a naive implementation of the following famous dynamic programming functional equation:
|
z(s) = max { vi + z(s - ui) : i = 1,2,...,n, ui<=s} , u* <= s <=U z(s) = 0 , s < u*:= min {ui: i=1,2,...,n} |