-2

"Buy and Resell Problem" is a classical optimization problem. It can be described in the following way:
There are $n$ cities. For each city, the price of products in this city is given (a positive number). Now a person will travel from city 1 to city $n$, one by one. When reaching a city, he can buy exactly one product with the price in this city, or sell exactly one product if he currectly has at least one product. He can also do nothing and then go on to the next city. The problem is how to plan a strategy so that he can earn maximal money.
There is a correct greedy algorithm to this problem. When reaching city $i$, consider all $j$ in $1 ... i-1$, and find a $j$ with lowest price such that he doesn't buy a product in city $j$ (he could have sold a product in city $j$). Then he buys a product in city $j$ and sells it in city $i$. The algorithm runs all $i$ in increasing order, and finally gives the best strategy.
The solution is quite sumple, but how to prove its correctness?

zbh2047
  • 393
  • 1
  • 9
  • 1
    Your question is probably more suitable for https://cs.stackexchange.com/ – kfx Sep 14 '18 at 15:09
  • 1
    Thanks, stackexchange is really more suitable for algorithm. – zbh2047 Sep 14 '18 at 15:37
  • 1
    Actually questions about algorithms are allowed on both sites. However this site (stackoverflow) focuses more on practical programming issues. For algorithms this includes their logic and performance so things like cpu and memory complexity will find many good answers and answerers here. For more formal and “theoretical” questions, such as proofs of correctness, https://cs.stackexchange.com will probably provide more and better answers. You can still get answers to such questions here, but that’s mostly because many of us also have degrees in CS. – RBarryYoung Sep 14 '18 at 16:18
  • Cross-posted: https://stackoverflow.com/q/52334214/781723, https://cs.stackexchange.com/q/97323/755. Please [do not post the same question on multiple sites](https://meta.stackexchange.com/q/64068). Each community should have an honest shot at answering without anybody's time being wasted. – D.W. Sep 20 '18 at 04:29

1 Answers1

2

Ok, so the proof won't be very formal, but I hope it will be enough.

Let's consider city i, where you can buy/sell a product for a price x. You want to sell there a product that was bought cheaper than x in order to earn money. So you want to find the cheapest product in the previous cities that costs less than x and you haven't bought it yet. Let's say its cost is y. If we sell it here, we earn (x - y).

Now we go to the next city, j. What if you can sell that product here for more than x, let's say z? Then you would earn (z - y), which is more than (x - y)... So have you made a wrong decision?

Let's say he can both buy AND sell a product in one city. Then, your decision actually does not matter. If you sell a product in city i, that is for x, you immidiately earn (x - y). Then, you can also buy a product in city i, which will cost you x money. Now, when you get to city j, you can sell it for price (z - x). How much would you earn in total by doing so? Exactly:

x - y + z - x = z - y

That is equal to buying a product for y, then selling it in city j for z.

We can easily see that selling and buying a product in one city gives us exactly the same result that not selling a product in that city at all. So does it matter if you can buy and sell at the same time or not? No, not really. It's just like not selling there at all. We can deny selling a product in city i by just buying another one in i.

What if z is actually smaller than x? If so, you have made a great decision, selling product for z instead of x gives you less.

No matter what the price in j is, you have made a good decision with selling a product in city i. So your algorithm is always making the best choice, that's why it works.

Maras
  • 982
  • 9
  • 15
  • This answer explains very clearly that the given solution is a greedy strategy. However, as the author indicated, it might be far from a rigorous proof. Indeed, it is not trivial to find a rigorous proof. – burnabyRails Sep 19 '18 at 09:24