<p>I also posted this on your original question on Stack Overflow:</p>

<hr>

<p>The problem is NP-complete for the general case. This can be shown via a reduction of 3-partition (which is a still strong NP-complete version of bin packing).</p>

<p>Let <em>w<sub>1</sub>, ..., w<sub>n</sub></em> be the weights of objects of the 3-partition instance, let <em>b</em> be the bin size, and <em>k = n/3</em> the number of bins that are allowed to be filled. Hence, there is a 3-partition if objects can be partitioned such that there are exactly 3 objects per bin.</p>

<p>For the reduction, we set N=<em>kb</em> and each bin is represented by <em>b</em> price labels of the same price (think of P<sub><em>i</em></sub> increasing every <em>b</em>th label). Let <em>t<sub>i</sub></em>, 1≤<em>i</em>≤<em>k</em>, be the price of the labels corresponding to the <em>i</em>th bin.
For each <em>w<sub>i</sub></em> we have one product S<sub><em>j</em></sub> of quantity <em>w<sub>i</sub> + 1</em> (lets call this the root product of <em>w<sub>i</sub></em>) and another <em>w<sub>i</sub> - 1</em> products of quantity 1 which are required to be cheaper than S<sub><em>j</em></sub> (call these the leave products).</p>

<p>For <em>t<sub>i</sub></em> = (2b + 1)<sup>i</sup>, 1≤<em>i</em>≤<em>k</em>, there is a 3-partition if and only if Bob can sell for <em>2b</em>Σ<sub>1≤<em>i</em>≤<em>k</em></sub> <em>t<sub>i</sub></em>:<ul>
<li>If there is a solution for 3-partition, then all the <em>b</em> products corresponding to objects <em>w<sub>i</sub></em>, <em>w<sub>j</sub></em>, <em>w<sub>l</sub></em> that are assigned to the same bin can be labeled with the same price without violating the restrictions.
Thus, the solution has cost <em>2b</em>Σ<sub>1≤<em>i</em>≤<em>k</em></sub> <em>t<sub>i</sub></em> (since the total quantity of products with price <em>t<sub>i</sub></em> is <em>2b</em>).<br>
<li>Consider an optimal solution of Bob's Sale.
First observe that in any solution were more than 3 root products share the same price label, for each such root product that is "too much" there is a cheaper price tag which sticks on less than 3 root products. This is worse than any solution were there are exactly 3 root products per price label (if existent).<br>
Now there can still be a solution of Bob's Sale with 3 root labels per price, but their leave products do not wear the same price labels (the bins sort of flow over).
Say the most expensive price label tags a root product of <em>w<sub>i</sub></em> which has a cheaper tagged leave product. This implies that the 3 root labels <em>w<sub>i</sub></em>, <em>w<sub>j</sub></em>, <em>w<sub>l</sub></em> tagged with the most expensive price do not add up to <em>b</em>. Hence, the total cost of products tagged with this price is at least <em>2b+1</em>.<br>
Hence, such a solution has cost <em>t<sub>k</sub>(2b+1)</em> + some other assignment cost. Since the optimal cost for an existent 3-partition is <em>2b</em>Σ<sub>1≤<em>i</em>≤<em>k</em></sub> <em>t<sub>i</sub></em> , we have to show that the just considered case is worse. This is the case if <em>t<sub>k</sub> > 2b</em> Σ<sub>1≤<em>i</em>≤<em>k-1</em></sub> <em>t<sub>i</sub></em> (note that it's <em>k-1</em> in the sum now). Setting <em>t<sub>i</sub></em> = (2b + 1)<sup>i</sup>, 1≤<em>i</em>≤<em>k</em>, this is the case. This also holds if not the most expensive price tag is the "bad" one, but any other.
</ul></p>

<p>So, this is the destructive part ;-) However, if the number of different price tags is a constant, you can use dynamic programming to solve it in polynomial time.</p>

CC BY-SA 2.5
2011-02-16T18:41:57.527Z
2011-02-16T18:41:57.527Z
Gero Greiner
<p>This is a follow-up on <a href="https://cstheory.stackexchange.com/questions/4932/bobs-sale-reordering-of-pairs-with-constraints-to-minimize-sum-of-products/4965#4965">Gero's answer</a>.  The idea is to modify his construction to show strong NP-hardness.</p>

<p>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. </p>

<p>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$.</p>

<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.</p>

<p>Citing the result from a <a href="http://dx.doi.org/10.1007/978-3-642-13731-0_25" rel="nofollow noreferrer">SWAT 2010 paper</a> 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.</p>

<hr>

<p>Also cross-posted to the original stack-overflow question.</p>

CC BY-SA 2.5
2011-02-17T14:49:22.087Z
I can't accept two answers, so I'll just have to thank you for the insight :)
Vladimir Panteleev
CC BY-SA 2.5
2011-02-17T02:19:11.060Z
2011-02-17T02:45:52.707Z
2017-04-13T12:32:28.053Z
Riko Jacob
<p>This sounds like a gaming theory question. In that case, a very simple brute-force solution is:</p>

<p>Let us assume the constraints represent some invariants of the form </p>

<pre>
S->AkSBk|AkBkS|SAkBk
</pre>

<p>The solution is to keep adding the constraints first, and then elements. 
Eg: Let us say n = 10 and there are 2 constraints, A1B1 and A2B2.
Then, there are three children to the root node (level 2). Each of these 3 nodes will have 7 children level 3, each of 21 have 6 at level 4, etc., Essentially you are running through all possible combinations.</p>

<pre>                A1B1                  --- Level 1 
               / |  \
              /  |   \
             /   |    \
            /    |     \
    A1A2B2A1   A1B1A2B2  A2B2A1B1     --- Level 2</pre>

<p>and grow the tree. Though on the outset it looks like a horrible solution, I feel you could abandon chasing very expensive leaves by using some heuristics and pruning...</p>

CC BY-SA 2.5
2011-02-15T17:08:07.937Z
2011-02-15T17:08:07.937Z
CMR
<p>I've asked this question on Stack Overflow a while ago: <a href="https://stackoverflow.com/questions/4898511/problem-bobs-sale">Problem: Bob's sale</a>. Someone suggested posting the question here as well.</p>

<p>Someone has already asked a question related to this problem here - <a href="https://cstheory.stackexchange.com/questions/4904/minimum-weight-subforest-of-given-cardinality">Minimum weight subforest of given cardinality</a> - 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.</p>

<p>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:</p>

<hr>

<p><em>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.</em></p>

<p>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.</p>

<p>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.</p>

<hr>

<p>Given:</p>

<ul>
<li>N &ndash; the number of products and price labels</li>
<li>S<sub><em>i</em></sub>, 0≤<em>i</em>&lt;N &ndash; the quantity in stock of product with index <em>i</em> (integer)</li>
<li>P<sub><em>j</em></sub>, 0≤<em>j</em>&lt;N &ndash; the price on price label with index <em>j</em> (integer)</li>
<li>K &ndash; the number of additional constraint pairs</li>
<li>A<sub><em>k</em></sub>, B<sub><em>k</em></sub>, 0≤<em>k</em>&lt;K &ndash; product indices for the additional constraint

<ul>
<li>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.</li>
</ul></li>
</ul>

<p>The program must find:</p>

<ul>
<li>M<sub><em>i</em></sub>, 0≤<em>i</em>&lt;N &ndash; mapping from product index to price label index (P<sub>M<sub><em>i</em></sub></sub> is price of product <em>i</em>)</li>
</ul>

<p>To satisfy the conditions:</p>

<ol>
<li>P<sub>M<sub>A<sub><em>k</em></sub></sub></sub> ≤ P<sub>M<sub>B<sub><em>k</em></sub></sub></sub>, for 0≤<em>k</em>&lt;K</li>
<li>Σ(S<sub><em>i</em></sub> &times; P<sub>M<sub><em>i</em></sub></sub>) for 0≤<em>i</em>&lt;N is minimal</li>
</ol>

<hr>

<p>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.</p>

<p>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).</p>

<hr>

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

<p>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.</p>

<p>The optimal solution is:</p>

<pre><code>Price, $   1  2  3  4  5  6  7  8  9 10
Qty        9  8  7  6  1 10  5  4  3  2
</code></pre>

<p>with a total cost of $\$$249. If you place the 1,10 pair near either extreme, the total cost will be higher.</p>

CC BY-SA 2.5
2011-02-15T01:18:43.473Z
Erm, the preformatted block for the example at the bottom got mungled, and I'm not sure how to fix it (StackOverflow's Markdown syntax and <pre> tags don't seem to work here).
Vladimir Panteleev
CC BY-SA 2.5
2011-02-15T01:41:03.007Z
The markup for the preformatted block was not recognized because dollar signs were treated as TeX delimiter (although I do not know why TeX markup ruins the markup for the preformatted block).  Because [there does not seem to be a “correct” way to escape dollar signs](http://meta.cstheory.stackexchange.com/questions/492/how-to-escape-latex-markdown), I fixed it in an ad-hoc way.
Tsuyoshi Ito
CC BY-SA 2.5
2011-02-15T03:05:36.487Z
what is the question? you want an (efficient) algorithm for finding an optimal solution? hardness? approximate solution?
Marcos Villagra
CC BY-SA 2.5
2011-02-15T10:48:32.940Z
@Ito, thanks. @Marcos - sorry, I'm looking for an algorithm to solve this problem, hopefully fast enough so that I can implement it in my project. There are many ideas for an approximate solution, so an exact solution would be preferred.
Vladimir Panteleev
CC BY-SA 2.5
2011-02-15T16:14:25.730Z
Which way does the constraint between $A_k$ and $B_k$ go? Do you require the price of $A_k$ to be no higher than, or no lower than, that of $B_k$? Might be useful to know, given the “no duplicates” constraint on $B$. (Then again, maybe I missed it in the explanation…)
Magnus Lie Hetland
CC BY-SA 2.5
2011-02-15T18:22:02.810Z
Magnus, it's specified a bit lower (the first condition the solution must satisfy).
Vladimir Panteleev
CC BY-SA 2.5
2011-02-15T22:35:49.047Z
For what it's worth, I think the related question (http://cstheory.stackexchange.com/q/4904/751) considers the case where the prices consist of k ones and N−k zeroes.
mhum
CC BY-SA 2.5
2011-02-17T14:46:59.140Z
Thank you for the help and insight, everyone. By further studying the underlying practical problem I had found an adequate solution, but I couldn't have done it without your help.
Vladimir Panteleev
<ds.algorithms><np-hardness><tree><optimization><application-of-theory>
Bob's Sale (reordering of pairs with constraints to minimize sum of products)
Vladimir Panteleev
# Bob's Sale (reordering of pairs with constraints to minimize sum of products)

(Asked by: username_0 on 2011-02-15)

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 – the number of products and price labels
Si, 0≤ii (integer)
Pj, 0≤jj (integer)
K – the number of additional constraint pairs
Ak, Bk, 0≤k
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≤iMi is price of product i)

To satisfy the conditions:

PMAk ≤ PMBk, for 0≤k
Σ(Si × PMi) for 0≤i


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<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.

**Tags:** ds.algorithms, np-hardness, tree, optimization, application-of-theory
> **Comments:**
> **username_0**: Erm, the preformatted block for the example at the bottom got mungled, and I'm not sure how to fix it (StackOverflow's Markdown syntax and <pre> tags don't seem to work here).  
> **username_1**: The markup for the preformatted block was not recognized because dollar signs were treated as TeX delimiter (although I do not know why TeX markup ruins the markup for the preformatted block).  Because [there does not seem to be a “correct” way to escape dollar signs](http://meta.cstheory.stackexchange.com/questions/492/how-to-escape-latex-markdown), I fixed it in an ad-hoc way.  
> **username_2**: what is the question? you want an (efficient) algorithm for finding an optimal solution? hardness? approximate solution?  
> **username_0**: @Ito, thanks. @Marcos - sorry, I'm looking for an algorithm to solve this problem, hopefully fast enough so that I can implement it in my project. There are many ideas for an approximate solution, so an exact solution would be preferred.  
> **username_3**: Which way does the constraint between $A_k$ and $B_k$ go? Do you require the price of $A_k$ to be no higher than, or no lower than, that of $B_k$? Might be useful to know, given the “no duplicates” constraint on $B$. (Then again, maybe I missed it in the explanation…)  
> **username_0**: Magnus, it's specified a bit lower (the first condition the solution must satisfy).  
> **username_4**: For what it's worth, I think the related question (http://cstheory.stackexchange.com/q/4904/751) considers the case where the prices consist of k ones and N−k zeroes.  
> **username_0**: Thank you for the help and insight, everyone. By further studying the underlying practical problem I had found an adequate solution, but I couldn't have done it without your help.  

---

## 3 answers

### Answer by username_5 on 2011-02-16. Score: 6 | Accepted answer

I also posted this on your original question on Stack Overflow:

---

The problem is NP-complete for the general case. This can be shown via a reduction of 3-partition (which is a still strong NP-complete version of bin packing).

Let *w1, ..., wn* be the weights of objects of the 3-partition instance, let *b* be the bin size, and *k = n/3* the number of bins that are allowed to be filled. Hence, there is a 3-partition if objects can be partitioned such that there are exactly 3 objects per bin.

For the reduction, we set N=*kb* and each bin is represented by *b* price labels of the same price (think of P*i* increasing every *b*th label). Let *ti*, 1≤*i*≤*k*, be the price of the labels corresponding to the *i*th bin.
For each *wi* we have one product S*j* of quantity *wi + 1* (lets call this the root product of *wi*) and another *wi - 1* products of quantity 1 which are required to be cheaper than S*j* (call these the leave products).

For *ti* = (2b + 1)i, 1≤*i*≤*k*, there is a 3-partition if and only if Bob can sell for *2b*Σ1≤*i*≤*k* *ti*:

* If there is a solution for 3-partition, then all the *b* products corresponding to objects *wi*, *wj*, *wl* that are assigned to the same bin can be labeled with the same price without violating the restrictions.
  Thus, the solution has cost *2b*Σ1≤*i*≤*k* *ti* (since the total quantity of products with price *ti* is *2b*).  
  * Consider an optimal solution of Bob's Sale.
    First observe that in any solution were more than 3 root products share the same price label, for each such root product that is "too much" there is a cheaper price tag which sticks on less than 3 root products. This is worse than any solution were there are exactly 3 root products per price label (if existent).  
    Now there can still be a solution of Bob's Sale with 3 root labels per price, but their leave products do not wear the same price labels (the bins sort of flow over).
    Say the most expensive price label tags a root product of *wi* which has a cheaper tagged leave product. This implies that the 3 root labels *wi*, *wj*, *wl* tagged with the most expensive price do not add up to *b*. Hence, the total cost of products tagged with this price is at least *2b+1*.  
    Hence, such a solution has cost *tk(2b+1)* + some other assignment cost. Since the optimal cost for an existent 3-partition is *2b*Σ1≤*i*≤*k* *ti* , we have to show that the just considered case is worse. This is the case if *tk > 2b* Σ1≤*i*≤*k-1* *ti* (note that it's *k-1* in the sum now). Setting *ti* = (2b + 1)i, 1≤*i*≤*k*, this is the case. This also holds if not the most expensive price tag is the "bad" one, but any other.

So, this is the destructive part ;-) However, if the number of different price tags is a constant, you can use dynamic programming to solve it in polynomial time.


---

### Answer by username_6 on 2011-02-17. Score: 7

This is a follow-up on [Gero's answer](https://cstheory.stackexchange.com/questions/4932/bobs-sale-reordering-of-pairs-with-constraints-to-minimize-sum-of-products/4965#4965). 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](http://dx.doi.org/10.1007/978-3-642-13731-0_25) 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.

> **Comments:**
> **username_0**: I can't accept two answers, so I'll just have to thank you for the insight :)  

---

### Answer by username_7 on 2011-02-15. Score: 0

This sounds like a gaming theory question. In that case, a very simple brute-force solution is:
Let us assume the constraints represent some invariants of the form 

S->AkSBk|AkBkS|SAkBk

The solution is to keep adding the constraints first, and then elements. 
Eg: Let us say n = 10 and there are 2 constraints, A1B1 and A2B2.
Then, there are three children to the root node (level 2). Each of these 3 nodes will have 7 children level 3, each of 21 have 6 at level 4, etc., Essentially you are running through all possible combinations.
                A1B1                  --- Level 1 
               / |  \
              /  |   \
             /   |    \
            /    |     \
    A1A2B2A1   A1B1A2B2  A2B2A1B1     --- Level 2
and grow the tree. Though on the outset it looks like a horrible solution, I feel you could abandon chasing very expensive leaves by using some heuristics and pruning...


---

