I have three lists of nodes. sources, sinks, and pipes. there is a directed weighted graph from sources to pipes to sinks. Sources are only connected to pipes and pipes only to sinks. But sources are not directly connected to sinks. Pipes are zero-sum, meaning that the sum of the weights that come to each pipe from sources is equal to the sum of the edges that go from that pipe to sinks.
I would like to add the minimum number of edges to this graph from sinks back to sources so that sinks and sources also become zero-sum. I know this problem is np-complete I'm interested to see if there is any good polynomial approximation to this problem that would work in real life.
In simpler words: I have a list of sinks and sources. Each sink has a negative number and each source has a positive number so that the sum of all the numbers in the nodes of the graph are zero(no edges so far). I would like to add the minimum number of edges to this graph so that the sum of the weights of the edges going out/in to each node becomes equal to the number on that node.
Example:
Given:
a0: 3
a1: 1
a2: 5
b0: -2
b1: -5
b2: -2
We get to:
a0-b0: 2
a0-b2: 1
a1-b2: 1
a2-b1: 5
Check of correctness:
a0: 3 = 2+1
a1: 1 = 1
a2: 5 = 5
b0: -2 = -2
b1: -5 = -5
b2: -2 = -1 + -1
This gives a sub-optimum solution with less than n-1 edges.
Here is a sample run:
Here is an illustration of how it works:
Here is a comparison between sorted and unsorted solution with this algorithm: