'What algorithm to use to determine minimum number of actions required to get the system to "Zero" state?
This is kind of more generic question, isn't language-specific. More about idea and algorithm to use.
The system is as follows:
It registers small loans between groups of friends. Alice and Bill are going to lunch, Bill's card isn't working, so Alice pays for his meal, $10.
The next day Bill and Charles meet each other on a railway station, Chales has no money for ticket, so Bill buys him one, for $5.
Later that day Alice borrows $5 from Charles and $1 from Bill to buy her friend a gift.
Now, assuming they all registered that transactions in the system, it looks like this:
Alice -> Bill $10
Bill -> Alice $1
Bill -> Charles $5
Charles -> Alice $5
So, now, only thing that needs to be done is Bill giving Alice $4 (he gave her $1 and Charlie transferred his $5 to Alice alredy) and they're at the initial state.
If we scale that to many diffrent people, having multiple transaction, what would be the best algorithm to get as little transactions as possible?
Solution 1:[1]
Intuitively, this sounds like an NP-complete problem (it reduces to a problem very like bin packing), however the following algorithm (a modified form of bin packing) should be pretty good (no time for a proof, sorry).
Net out everyone's positions, i.e. from your example above:
Alice = $4 Bill = $-4 Charles = $0
Sort all net creditors from highest to lowest, and all debtors from lowest to highest, then match by iterating over the lists.
At some point you might need to split a person's debts to net everything out - here it is probably best to split into the biggest chunks possible (i.e. into the bins with the most remaining space first).
This will take something like O(n log n) (again, proper proof needed).
See the Partition Problem and Bin Packing for more information (the former is a very similar problem, and if you limit yourself to fixed precision transactions, then it is equivalent - proof needed of course).
Solution 2:[2]
I have created an Android app which solves this problem. You can input expenses during the trip, it even recommends you "who should pay next". At the end it calculates "who should send how much to whom". 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:
https://market.android.com/details?id=cz.destil.settleup
Description of my algorithm:
I have basic algorithm which solves the problem with n-1 transactions, but it's not optimal. It works like this: From payments, I compute balance for each member. Balance is what he paid minus what he should pay. I sort members according to balance increasingly. Then I always take the poorest and richest and transaction is made. At least one of them ends up with zero balance and is excluded from further calculations. With this, number of transactions cannot be worse than n-1. It also minimizes amount of money in transactions. But it's not optimal, because it doesn't detect subgroups which can settle up internally.
Finding subgroups which can settle up internally is hard. I solve it by generating all combinations of members and checking if sum of balances in subgroup equals zero. I start with 2-pairs, then 3-pairs ... (n-1)pairs. Implementations of combination generators are available. When I find a subgroup, I calculate transactions in the subgroup using basic algorithm described above. For every found subgroup, one transaction is spared.
The solution is optimal, but complexity increases to O(n!). This looks terrible but the trick is there will be just small number of members in reality. I have tested it on Nexus One (1 Ghz procesor) and the results are: until 10 members: <100 ms, 15 members: 1 s, 18 members: 8 s, 20 members: 55 s. So until 18 members the execution time is fine. Workaround for >15 members can be to use just the basic algorithm (it's fast and correct, but not optimal).
Source code:
Source code is available inside a report about algorithm written in Czech. Source code is at the end and it's in English:
http://www.settleup.info/files/master-thesis-david-vavra.pdf
Solution 3:[3]
I found a practical solution which I implemented in Excel:
find out who ows the most
let that person pay the complete amount he ows to the one who should get the most
that makes the first person zero
repeat this proces taken into account that one of (n-1) persons has a changed amount
It should result in a maximum of (n-1) transfers and the nice thing about it is that no one is doing more than one payment. Take the (modified) example of jrandomhacker:
a=-5 b=25 c=-20 d=3 e=-2 f=-1 (sum should be zero!)
c -> b 20. result: a=-5 b=5 c=0 d=3 e=-2 f=-1
a -> b 5 result: a=0 b=0 c=0 d=3 e=-2 f=-1
e -> d 2 result a=0 b=0 c=0 d=1 e=0 f=-1
f -> d 1
Now, everyone is satisfied and no one is bothered by making two or more payments. As you can see, it is possible that one person recieves more than one payment (person d in this example).
Solution 4:[4]
I have designed a solution using a somewhat different approach to the ones that have been mentioned here. It uses a linear programming formulation of the problem, drawing from the Compressed Sensing literature, especially from this work by Donoho and Tanner (2005).
I have written a blog post describing this approach, along with a tiny R package that implements it. I would love to get some feedback from this community.
Cheers.
Solution 5:[5]
Well, the first step is to totally ignore the transactions. Just add them up. All you actually need to know is the net amount of debt a person owes/owns.
You could very easily find transactions by then creating a crazy flow graph and finding max flow. Then a min cut...
Some partial elaboration: There is a source node, a sink node, and a node for each person. There will be an edge between every pair of nodes except no edge between source node and sink node. Edges between people have infinite capacity in both directions. Edges coming from source node to person have capacity equal to the net debt of the person (0 if they have no net debt). Edges going from person node to sink node have capacity equal to the net amount of money that person is owed (0 if they have no net owed).
Apply max flow and/or min cut will give you a set of transfers. The actual flow amount will be how much money will be transfered.
Solution 6:[6]
Only if someone owes more than 2 people, whom also owe to the same set, can you reduce the number of transactions from the simple set.
That is, the simple set is just find each balance and repay it. That's no more than N! transactions.
If A owes B and C, and some subset of B C owe each other, so B owes C, then instead of: A -> B, A -> C (3 transactions). You'd use: A -> B, B -> C (2 transactions).
So in other words you are building a directed graph and you want to trim vertices on order to maximize path length and minimize total edges.
Sorry, I don't have an algorithm for you.
Solution 7:[7]
You should be able to solve this in O(n) by first determining how much each person owes and is owed. Transfer the debts of anyone who owes less than he is owed to his debtors (thus turning that person into an end point). Repeat until you can't transfer any more debts.
Solution 8:[8]
This is the code I wrote to solve this type of a problem in Java. I am not 100% sure if this gives the least number of transactions. The code's clarity and structure can be improved a lot.
In this example:
- Sarah spent $400 on car rental. The car was used by Sarah, Bob, Alice and John.
John spent $100 on groceries. The groceries were consumed by Bob and Alice.
import java.util.*; public class MoneyMinTransactions { static class Expense{ String spender; double amount; List<String> consumers; public Expense(String spender, double amount, String... consumers){ this.spender = spender; this.amount = amount; this.consumers = Arrays.asList(consumers); } } static class Owed{ String name; double amount; public Owed(String name, double amount){ this.name = name; this.amount = amount; } } public static void main(String[] args){ List<Expense> expenseList = new ArrayList<>(); expenseList.add(new Expense("Sarah", 400, "Sarah", "John", "Bob", "Alice")); expenseList.add(new Expense("John", 100, "Bob", "Alice")); //make list of who owes how much. Map<String, Double> owes = new HashMap<>(); for(Expense e:expenseList){ double owedAmt = e.amount/e.consumers.size(); for(String c : e.consumers){ if(!e.spender.equals(c)){ if(owes.containsKey(c)){ owes.put(c, owes.get(c) + owedAmt); }else{ owes.put(c, owedAmt); } if(owes.containsKey(e.spender)){ owes.put(e.spender, owes.get(e.spender) + (-1 * owedAmt)); }else{ owes.put(e.spender, (-1 * owedAmt)); } } } } //make transactions. // We need to settle all the negatives with positives. Make list of negatives. Order highest owed (i.e. the lowest negative) to least owed amount. List<Owed> owed = new ArrayList<>(); for(String s : owes.keySet()){ if(owes.get(s) < 0){ owed.add(new Owed(s, owes.get(s))); } } Collections.sort(owed, new Comparator<Owed>() { @Override public int compare(Owed o1, Owed o2) { return Double.compare(o1.amount, o2.amount); } }); //take the highest negative, settle it with the best positive match: // 1. a positive that is equal to teh absolute negative amount is the best match. // 2. the greatest positive value is the next best match. // todo not sure if this matching strategy gives the least number of transactions. for(Owed owedPerson: owed){ while(owes.get(owedPerson.name) != 0){ double negAmt = owes.get(owedPerson.name); //get the best person to settle with String s = getBestMatch(negAmt, owes); double posAmt = owes.get(s); if(posAmt > Math.abs(negAmt)){ owes.put(owedPerson.name, 0.0); owes.put(s, posAmt - Math.abs(negAmt)); System.out.println(String.format("%s paid %s to %s", s, Double.toString((posAmt - Math.abs(negAmt))), owedPerson.name)); }else{ owes.put(owedPerson.name, -1 * (Math.abs(negAmt) - posAmt)); owes.put(s, 0.0); System.out.println(String.format("%s paid %s to %s", s, Double.toString(posAmt), owedPerson.name)); } } } } private static String getBestMatch(double negAmount, Map<String, Double> owes){ String greatestS = null; double greatestAmt = -1; for(String s: owes.keySet()){ double amt = owes.get(s); if(amt > 0){ if(amt == Math.abs(negAmount)){ return s; }else if(greatestS == null || amt > greatestAmt){ greatestAmt = amt; greatestS = s; } } } return greatestS; } }
Solution 9:[9]
If you take states as nodes of graph then you will be able to use shortest path algorithm to know the answer.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jwoolard |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | mbiron |
| Solution 5 | |
| Solution 6 | Adam Luter |
| Solution 7 | Elie |
| Solution 8 | Prakash_se7en |
| Solution 9 |
