Maximize sum of table where each number must come from unique row and column

喜夏-厌秋 提交于 2019-11-30 02:24:54

This is the maximum cost bipartite matching problem. The classical way to solve it is by using the Hungarian algorithm.

Basically you have a bipartite graph: the left set is the rows and the right set is the columns. Each edge from row i to column j has cost matrix[i, j]. Find the matching that maximizes the costs.

For starters, you can use dynamic programming.

In your straightforward approach, you are doing exactly the same computation many, many times.

For example, at some point you answer the question: "For the last three columns with rows 1 and 2 already taken, how do I maximize the sum?" You compute the answer to this question twice, once when you pick row 1 from column 1 and row 2 from column 2, and once when you pick them vice-versa.

So don't do that. Cache the answer -- and also cache all similar answers to all similar questions -- and re-use them.

I do not have time right now to analyze the running time of this approach. I think it is O(2^n) or thereabouts. More later maybe...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!