0

recently I have been reading about the Splitwise problem where one has a group of people with debts between each other, and the goal is to settle these debts with the minimal number of transactions. It can also be modeled as a directed weighted graph which edges are to be reduced.

The solution I encountered most often was a greedy iterative algorithm that, firstly, calculates the net outcome of every person (money he is owed - money he owes), and then repeats the following:

1. take max. debtor X and max. creditor Y
2. if X owes more than Y is owed
     then X pays Y Y's value
          reduce X's debt by Y's value
          set Y's value to 0
     else X pays Y X's value
          reduce Y's value by X's debt
          set X's debt to 0

...until everyone's value is 0.

My question(s):

  • Is that algorithm really optimal in the transaction amount it produces? If yes, how can this be proved?
  • If not, what is a counterexample to this algorithm's optimality, i.e., a situation where the debts can be minimised with less transactions than the ones it outputs?
alistairv
  • 159
  • 9

1 Answers1

1

It looks like this algorithm isn't optimal. Consider the case [-3, -2, -2, 3, 4], where positive indicates creditor and negative indicates debtor. Using the algorithm described, we require four transaction operations to knock out all the debts:

>> [-3, -2, -2, 3, 4]
>> [0, -2, -2, 3, 1]    ([0] pays [4])
>> [0, 0, -2, 1, 1]     ([1] pays [3])
>> [0, 0, -1, 1, 0]     ([2] pays [4])
>> [0, 0, 0, 0, 0]      ([2] pays [3])

But you can see that the debts can be cleared via three transactions: the person who owes $3 pays the person credited $3, and then the two who owe $2 each pay the person owed $4.

Actually, I believe the goal of the algorithm described is to minimize the total amount of money transacted, rather than the number of transactions, which it does and which can be proven (though I won't attempt that here).

aeternalis1
  • 675
  • 4
  • 7
  • Thank you. Regarding what the algo minimises: It does suffice for me that it is not optimal regarding number of transactions. – alistairv Mar 29 '20 at 00:02