Algorithm to simplify a weighted directed graph of debts

后端 未结 3 1457
南旧
南旧 2020-12-02 17:52

I\'ve been using a little python script I wrote to manage debt amongst my roommates. It works, but there are some missing features, one of which is simplifying unnecessarily

3条回答
  •  暖寄归人
    2020-12-02 18:22

    I've actually encountered this problem in exactly the same situation as you :)

    I think krlmlr's various solution don't quite solve the problem exactly. I'll have a think about how to solve it exactly (in the minimum-edges sense), but in the meantime, a practical alternative solution to your problem is to invent a new person, Steve:

    1. Steve is not actually a person. Steve is just a bucket, with a piece of paper attached to it.
    2. Everyone calculates the net amount that they owe (or are owed, if negative), and writes it on the piece of paper, alongside their name.
    3. Anyone whose net position is that they owe money gives that net amount of money to Steve when they can, and crosses off their name.
    4. Everyone whose net position is that they are owed money takes that money from Steve when they see Steve has it, and crosses off their name.

    If a person who owes money can't pay all of it at once, they can just give Steve what they can currently afford, and take this amount off the total-owing figure against their name. Likewise if you are owed more money than Steve currently has on hand, you can take all of the money he currently has, and take that amount off the total-owed against your name.

    If everyone agrees at the start to pay Steve only the full amount, then every net-ower makes exactly one deposit, and every net-owed person make exactly one withdrawal (although this may require multiple checks on Steve to see whether he currently has sufficient cash on hand). The good thing about Steve is that he's always around, and is never too busy to sort out finances. Unfortunately he's very gullible, so Alice, Bob and Charlie need to already trust one another not to take advantage of him.

提交回复
热议问题