algorithmknapsack-problemcomputation-theorynp

Searching Algorithm: Product Knapsack Problem with goal to find lowest product above a certain threshold?


I'm looking for people with some algorithmic knowledge who can help me out with this.

I'm currently programming a constrained tree search in java, where I want to have a certain amount of leaf nodes that is just bigger than a given threshold T.

I'm boiling down the problem (which is most likely NP complete) to this:

Problem

Input: a threshold T, and a List of entries L = [e1, e2, ..., en], where for every ei (where 1 <= i <= n) there is a defined arbitrary maximum weight wMaxi and a minimum weight wMini = 1. (wMax_i >= wMin_i)

Output: a choice of weights wi, so that:

  • wMaxi >= wi >= wMini = 1
  • the product W of wi is greater than threshold (W > T)
  • the product of wi is lowest possible or if not possible is linearly constrained in T (W is in O(T) / W < k * T)

Notes

I tried to approximate the solution when defining this problem with float numbers: every entry would be able to have a weight of ceil(logn(T)) but this approximation doesn't work as the max weights are distributed quite unevenly.

Furthermore I read some articles on solving the product knapsack problem, but they were interested with achieving a max weight.

I'm also not so sure if this problem can be reduced to the knapsack problem, as the threshold is quite tricky to handle and I deal with products.

Edit: I ended up brute forcing it by iteratively and evenly increasing weights until I'm above T.

Edit:

Example data:

Solution (nearest Threshold):

Solution (as balanced as possible, while also near):


Solution

  • I have an algorithm similar to your brute force idea.

    brute forcing it by iteratively and evenly increasing weights until I'm above T.

    Let's do this, but using binary search instead. The algorithms goes something like this.

    1. Sort by wMaxi
    2. Define f(x) as the product of wi where wi = min(wMaxi, x). See below on how to compute f(x)
    3. Binary search to find k where f(k) <= T and f(k+1) > T
    4. Find the optimal i to "split" the list into k and k+1. So wj = min(wMaxi, k) if j < i, and wj = k+1 if j >= i. We can to find i such that the product is just above T with either linear or binary search

    To compute f(x), a naive solution would suffice. But we can also compute the prefix product pi, binary search for i where wMaxi < x and wMaxi+1 >= x. Then f(x) is equal to pi plus x to the power of the number of elements left (watch out for off by 1 error).

    The product is less than 2 * T. Also note that with this algorithm, the problem (where the product is not minimized but the variance/standard deviation of the weights is minimized) is not NP complete.