Se si riformula il problema in modo leggermente diverso (ma equivalente), un algoritmo diventa più ovvio:
Sono coinvolte parti: persone e un ristorante. Diciamo essere la quantità di denaro partito dovrei avere dopo il pasto è finito e pagato. Ad esempio, se Alice ha $ 36 e deve $ 25, Bob ha $ 12 e deve $ 11 e Carl ha $ 30 e deve $ 25, diciamo che è il ristorante e ha:
That is, when the meal is over the restaurant should have $61, Alice should have $11, Bob should have $1 and Carl should have $5.
Now let enumerate all of the bills involved. For example:
The denominations of the bills do not matter, but I have chosen denominations of paper US currency for this example because they are familiar.
We are seeking to minimize the number of bills that change hands, so we associate a "cost" with person leaving with bill by using a matrix . Entries of 0 in this matrix indicate which bills each party starts with ( for all because the restaurant starts with no bills).
Continuing our example:
indicates that Alice started with $1, $5, $10, $20, Bob started with $1, $1, $5, $5, and Carl started with $10 and $20.
Again, the goal is to minimize the number of bills that change hands. In other words:
The first constraint says that the solution can only assign a particular bill to one party, and the second ensures that everyone pays the appropriate amount.
This is the 0,1 INTEGER PROGRAMMING problem and is NP-complete (see [Karp 1972]). The Wikipedia page about linear programming has information on different algorithms that can be used for these types of problems.
There are potentially multiple optimal solutions; by hand the first solution to the example I came up with was:
which means Alice pays exactly $5 and $20, Bob pays exactly $1, $5 and $5, and Carl overpays $10 and $20 and then removes a $5 from the table.
I also used the Mixed Integer Linear Program module of the Sage Math system which has the ability to use different solver backends (GLPK, COIN, CPLEX, or Gurobi). The first solution it gave was
which is almost the same except that Carl took the "other" $5 that Bob put on the table.
Formulating the problem in this way satisfies all the properties you list (you can extrapolate which bills end up where from and the solution matrix ). The exception is perhaps #4 which was discussed in the comments of the question. It is not clear to me what you would like to do in the situation that there is no feasible solution to the set of linear equations:
Identify a subset of people that can pay the reduced total? Or perhaps a subset of people which could still pay the entire bill, i.e. they pay for their friend.
Your final statement makes it seem like you are interested in the case that the denominations of the bills are fixed, this doesn't change the problem however.
In any case, there is also an solution in which every person pays with a credit card.