Question

I wanted an algorithm to eliminate the sides of a cyclic directed graph or mostly a Tournament and output a tree kind of structure with minimum number of sides.

The elimination should be based on the weight of the sides, as described below in as a simple real world example.

If there are three friends A,B,C. Take a scenario of borrowing & returning money between each other.

Person A has to transfer Person B - $10. Person B has to transfer Person C - $20. Person C has to transfer Person A - $20.

In the final settlement to minimize the number of transfers between each other we can rearrange the above to something like "Person B will transfer Person A - $10" and everything is settled.

I am looking for some algorithm which will work for any number of nodes when weight of each side and direction is given.

Given that the graph can be rearranged and chances are high that the graph might be a "Tournament" where each node is connected to all the nodes in the graph, what would be the best approach I follow.

Was it helpful?

Solution

n - 1 edges is pretty easy to obtain. First compute everyone's "net worth", put the borrowers in one list, put the lenders in another, and then repeatedly make a transfer from the borrower at the front of the line to the lender, removing one or both if they are saturated (returned to zero).

The preceding algorithm is a 2-approximation. Minimizing the number of transfers means maximizing the number of doubly-saturating transfers, which is a knapsack-like problem that's at least weakly NP-hard. There's an exponential-time dynamic program that might be suitable for small n (naive is more like n^n).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top