问题
I'm given a graph with many seperate components. Each component is bipartite. How do I distribute the vertices into two sets A
and B
such that the difference between the two sets is minimal ?
Example:
1: 1 -> 2 ->3 -> 4 -> 5
2: 6 -> 7 -> 8
The best solution is
A = {1, 3, 5, 7}
B = {2, 4 ,6, 8}
The other (non-optimal) solution is
A = {1, 3, 5, 6, 8}
B = {2, 4, 7}
How do you solve this when the graph has many seperate bipartite components ?
回答1:
It's the Partition Problem, slightly disguised. For every bipartite component, take the difference of the numbers of elements in the independent sets (actually, it's absolute value.) This is the input to the partition problem. For your example it would be [1,1] (from (3-2) and (2-1).)
Now for converting the solution back to graphs. For every graph whose number ended in set S1, put its bigger set in A
(and the smaller in B
), for every graphs whose number ended in S2, put its smaller set in A
(and the larger in B
.) In your example, the solution is S1=[1] and S2=[1]. Going back to the associated graphs you get the optimal solution from your example.
回答2:
This is a variation of partition problem, which is NP complete.
For each component find the number of vertices on either side, say [m,n] and then you have to decide whether to put m in pool A or pool B, such that the difference between the summation of terms in pool A and that in pool B is minimum.
There are existing techniques for solving this sort of problem by dynamic programming and also variations of IPP. But with a large number of components you are doomed by NP completeness alone.
来源:https://stackoverflow.com/questions/5743141/split-vertices-of-disconnected-bipartite-graph-equally