Bob's Sale (reordering of pairs with constraints to minimize sum of products) I've asked this question on Stack Overflow a while ago: Problem: Bob's sale. Someone suggested posting the question here as well.

Someone has already asked a question related to this problem here - Minimum weight subforest of given cardinality - but as far as I understand it doesn't help me with my problem. The highest-rated answer on StackOverflow is also worth looking at.

Here's the verbatim copy of my StackOverflow question. It's probably inadequately formulated for this site (heck, I feel inadequately uneducated just asking it here), so feel free to edit it:



Note: this is an abstract rewording of a real-life problem regarding ordering records in a SWF file. A solution will help me improve an open-source application.

Bob has a store, and wants to do a sale. His store carries a number of products, and he has a certain integer quantity of units of each product in stock. He also has a number of shelf-mounted price labels (as many as the number of products), with the prices already printed on them. He can place any price label on any product (unitary price for one item for his entire stock of that product), however some products have an additional restriction - any such product may not be cheaper than a certain other product.

You must find how to arrange the price labels, such that the total cost of all of Bob's wares is as low as possible. The total cost is the sum of each product's assigned price label multiplied by the quantity of that product in stock.



Given:


N &ndash; the number of products and price labels
Si, 0≤i&lt;N &ndash; the quantity in stock of product with index i (integer)
Pj, 0≤j&lt;N &ndash; the price on price label with index j (integer)
K &ndash; the number of additional constraint pairs
Ak, Bk, 0≤k&lt;K &ndash; product indices for the additional constraint


Any product index may appear at most once in B. Thus, the graph formed by this adjacency list is actually a set of directed trees.



The program must find:


Mi, 0≤i&lt;N &ndash; mapping from product index to price label index (PMi is price of product i)


To satisfy the conditions:


PMAk ≤ PMBk, for 0≤k&lt;K
Σ(Si &times; PMi) for 0≤i&lt;N is minimal




Note that if not for the first condition, the solution would be simply sorting labels by price and products by quantity, and matching both directly.

Typical values for input will be N,K&lt;10000. In the real-life problem, there are only several distinct price tags (1,2,3,4).



Here's one example of why most simple solutions (including topological sort) won't work:

You have 10 items with the quantities 1 through 10, and 10 price labels with the prices $\$$1 through $\$$10. There is one condition: the item with the quantity 10 must not be cheaper than the item with the quantity 1.

The optimal solution is:

Price, $   1  2  3  4  5  6  7  8  9 10
Qty        9  8  7  6  1 10  5  4  3  2


with a total cost of $\$$249. If you place the 1,10 pair near either extreme, the total cost will be higher.
This is a follow-up on Gero's answer.  The idea is to modify his construction to show strong NP-hardness.

Instead of choosing $t_i=(2b+1)^i$, chose $t_i=i$.  Now, you have to modify the argument that a solution with prize $P=2b\sum_{1\leq i \leq k} t_i$ implies that there exists a 3-partition. 

Take an arbitrary shelf order.  Do the accounting in the following way: distribute $w_i-1$ units of quantity of the root-product to its leaf-products.  Then every product has quantity 2.  By definition of the constraints, this does not shift to a higher price.
After this shifting, the price will be exactly $P$.
If the shifting moved some quantity to a lower prize, the original prize was strictly larger than $P$.

Hence, it is only possible to achieve the claimed prize, if all leaf-products have the same prize as their root-product, which means that there exists a 3-partition.

Citing the result from a SWAT 2010 paper this argument shows that even with unary encoding of the numbers and $k$ different price tags, a running time of $f(k)\cdot n^{O(1)}$ would violate "standard complexity assumptions".
This makes the hinted at dynamic programming with a running time of $n^{O(k)}$ look not so bad.



Also cross-posted to the original stack-overflow question.