algorithm to determine minimum payments amongst a group

后端 未结 8 727
庸人自扰
庸人自扰 2020-12-05 12:09

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

8条回答
  •  感情败类
    2020-12-05 12:35

    While I concur with @Andrew that turning this into a graph problem is probably overcomplicated, I'm not sure his approach yields the minimal number of transactions. It's how you'd solve the problem in real life to save yourself a headache; just pool the money.

    A few steps that seem 'right':

    • Remove all individuals with zero debt; they don't need to send or receive money from anyone.
    • Pair all givers and receivers with identical amounts owed/owing. Since the minimal connectivity per node of non-zero debt is 1, their transactions are already minimal if they just pay each other. Remove them from the graph.
    • Starting with the individual with the largest amount to pay back, create a list of all receivers with owed less than that amount. Try all combinations of payment until one is found that satisfies most receivers with one transaction. 'Save' the surplus debt remaining.
    • Move on to the next largest giver, etc.
    • Allocate all the surplus debt to the remaining receivers.

    As always, I'm afraid I'm pretty sure about the first two steps, less sure about the others. In any case, it does sound like a textbook problem; I'm sure there's a 'right' answer out there.

提交回复
热议问题