The solution is optimal, but complexity increases to O(n!).
For every found subgroup, one transaction is spared. When I find a subgroup, I calculate transactions in the subgroup using basic algorithm described above. Implementations of combination generators are available. I solve it by generating all combinations of members and checking if sum of balances in subgroup equals zero. But it's not optimal, because it doesn't detect subgroups which can settle up internally.įinding subgroups which can settle up internally is hard. It also minimizes amount of money in transactions. With this, number of transactions cannot be worse than n-1. At least one of them ends up with zero balance and is excluded from further calculations. Then I always take the poorest and richest and transaction is made. I sort members according to balance increasingly. Balance is what he paid minus what he should pay. It works like this: From payments, I compute balance for each member. I have basic algorithm which solves the problem with n-1 transactions, but it's not optimal. My algorithm calculates minimum required number of transactions and you can setup "transaction tolerance" which can reduce transactions even further (you don't care about $1 transactions) Try it out, it's called Settle Up: At the end it calculates "who should send how much to whom". You can input expenses during the trip, it even recommends you "who should pay next".
How to split a receipt in neat receipt android#
I have created an Android app which solves this problem.