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

前端 未结 10 671
隐瞒了意图╮
隐瞒了意图╮ 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:14

    Well, the first step is to totally ignore the transactions. Just add them up. All you actually need to know is the net amount of debt a person owes/owns.

    You could very easily find transactions by then creating a crazy flow graph and finding max flow. Then a min cut...

    Some partial elaboration: There is a source node, a sink node, and a node for each person. There will be an edge between every pair of nodes except no edge between source node and sink node. Edges between people have infinite capacity in both directions. Edges coming from source node to person have capacity equal to the net debt of the person (0 if they have no net debt). Edges going from person node to sink node have capacity equal to the net amount of money that person is owed (0 if they have no net owed).

    Apply max flow and/or min cut will give you a set of transfers. The actual flow amount will be how much money will be transfered.

提交回复
热议问题