Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

algorithm to determine minimum payments amongst a group

The Problem

I was recently asked to calculate the money owed amongst a group of people who went on a trip together and came upon an interesting problem: given that you know the amounts that each person owes another, what is a general algorithm to consolidate the debts between people so that only the minimum number of payments needs to be made? Take this as an example:

  • Mike owes John 100
  • John owes Rachel 200
  • Mike owes Rachel 400

We can remove a payment between Mike and John by reformulating the debts like this:

  • Mike owes John 0
  • John owes Rachel 100
  • Mike owes Rachel 500

I did the math by hand since it was easy enough, but then the programmer in me was itching to figure out a general algorithm to do it for an arbitrarily large group. This seems like a graph algorithm to me, so I'll reformulate this as a graph:

Viewed as a Graph

  • The vertices are the people in the group
  • The edges are directed and weighted by the amount owed. For example, an edge from Mike to Rachel with weight 500 means that Mike owes Rachel 500.
  • Constraint: the net sum of weights for each node must remain unchanged.
  • The goal is to find a graph with the minimum number of edges that still satisfy the constraint.
like image 303
alanlcode Avatar asked Jul 22 '09 04:07

alanlcode


People also ask

What is Splitwise algorithm?

Simplify debts (a.k.a. “debt simplification”) is a feature of Splitwise that restructures debt within groups of people. It does not change the total amount that anyone owes, but it makes it easier to pay people back by minimizing the total number of payments.

How do you know your minimum payment?

The easiest ways to find your minimum payment each month are to look at your mailed billing statement or log in to your credit card account online and go to the payment tab or most recent billing statement. If necessary, you can also contact your bank over the phone to ask what your minimum payment is for the month.

How does the minimum payment on a credit card work?

A credit card minimum payment is the lowest amount you can pay every month while keeping your account in good standing. Making at least the minimum payment on your credit cards every billing cycle ensures that you do not get stuck with late fees, penalty APRs or derogatory marks on your credit report.


1 Answers

It does not suffice to just figure out the receivers and givers. While I think this strategy is on the right track it also does not ensure an algorithm to find the least possible amount of payments.

For Example,

  • Person A owes 25
  • Person B owes 50
  • Person C owes 75
  • Person D is owed 100
  • Person E is owed 50

While it is obvious that this can be done in 3 pays (A and C to D, B to E). I can't think of an efficient algorithm that will satisfy this for all problem sets.

Better Example,

  • Person A owes 10
  • Person B owes 49
  • Person C owes 50
  • Person D owes 65
  • Person E is owed 75
  • Person F is owed 99

If we took the greedy approach of having person D pay to F we would wind up with a sub-optimal solution as opposed to the optimal(A&D to E, B&C to F).

This problem has a lot of similarity with the Bin Packing Problem which has been proven to be NP-hard. The only difference being that we have multiple bins of varying sizes and the condition that the total space in all bins is equal to the total size of all items. This leads me to believe that the problem is likely NP-hard but with the added constraints it may be possible to solve in polynomially time.

like image 85
ccipriano Avatar answered Oct 22 '22 09:10

ccipriano