Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a bipartite graph with two sets of vertices.

Needs["Combinatorica`"]
g = CompleteGraph[3, 3];
edgeWeights = {{53, 96, 37}, {47, 87, 41}, {60, 92, 36}};
g1 = SetEdgeWeights[g, Flatten[edgeWeights]];

I want to find the optimal assignment that minimizes the total cost (sum of assigned edges). For this I use:

BipartiteMatchingAndCover[g1]

The answer I get is {{1, 5}, {2, 6}, {3, 4}}, which tells me that vertex 1 is assigned to vertex 5, 2 is assigned to 6 and 3 is assigned to 4. These assignments correspond to a total cost of 96+41+60 = 197. I would have expected the answer to be {{1,6},{2,4},{3,5}} which corresponds to a total cost of 37+47+92=176. It seems like the function is finding the assignments associated with the maximum cost instead of the minimum cost. I tried using 1/edgeWeights as edge weights, but got the same result.

Any ideas?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I think you're right about BipartiteMatchingAndCover returning a maximal matching. Instead of reciprocals, use negatives:

Needs["Combinatorica`"]
g = Combinatorica`CompleteGraph[3, 3];
edgeWeights = {{53, 96, 37}, {47, 87, 41}, {60, 92, 36}};
g1 = Combinatorica`SetEdgeWeights[g, -Flatten[edgeWeights]];

Combinatorica`BipartiteMatchingAndCover[g1]
Extract[edgeWeights, First[%] /. x_ /; x > 3 :> x - 3]
Total[%]
(*
  {{{1, 4}, {2, 5}, {3, 6}}, {-93, -87, -92, 40, 0, 56}}
  {53, 87, 36}
  176
*)

Note: There seem to be two optimal solutions.

share|improve this answer
    
Great. Thanks for your help MichelE2 ! –  riveralebron Jul 14 at 18:58
    
@riveralebron You're welcome. :) –  Michael E2 Jul 14 at 19:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.