What algorithm to use to determine minimum number of actions required to get the system to “Zero” state?

前端 未结 10 660
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 18:25

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 bet

10条回答
  •  攒了一身酷
    2020-11-30 19:05

    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).

    1. Net out everyone's positions, i.e. from your example above:

      Alice = $4 Bill = $-4 Charles = $0

    2. Sort all net creditors from highest to lowest, and all debtors from lowest to highest, then match by iterating over the lists.

    3. 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).

提交回复
热议问题