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:
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)
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):
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.
f(x)
as the product of wi where wi = min(wMaxi, x). See below on how to compute f(x)
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.