Can I use the Hungarian algorithm to find max cost?

后端 未结 2 473
忘了有多久
忘了有多久 2021-02-09 04:29

The Hungarian algorithm solves the assignment problem in polynomial time. Given workers and tasks, and an n×n matrix containing the cost of assigning each worker to a task, it

相关标签:
2条回答
  • 2021-02-09 04:50

    Wikipedia says:

    If the goal is to find the assignment that yields the maximum cost, the problem can be altered to fit the setting by replacing each cost with the maximum cost subtracted by the cost.

    So if I understand correctly: among all the costs you have as input, you find the maximum value. Then you replace each cost x by max - x. This way you still have positive costs and you can run the Hungarian algorithm.

    Said differently: Hungarian tries to minimize the assignment cost. So, if you are looking for the maximum, you can reverse the costs: x -> -x. However, some implementations (don't know if all or any) require positive numbers. So the idea is to add a constant value to each cost in order to have positive numbers. This constant value does not change the resulting affectation.

    0 讨论(0)
  • 2021-02-09 04:50

    As David said in the comment:

    Multiply the cost matrix by -1 for maximization.
    
    0 讨论(0)
提交回复
热议问题