How can I find the minimum number of lines needed to cover all the zeros in a 2 dimensional array?

ε祈祈猫儿з 提交于 2020-01-02 07:11:31

问题


I'm trying to make a decent implementation of the hungarian algorithm however I'm stuck at how to find the minimum number of lines that cover all the zeros in an array

also I need to know these lines to make some computations later

here is the explanation:

http://www.ams.jhu.edu/~castello/362/Handouts/hungarian.pdf

in step 3 it says

Use as few lines as possible to cover all the zeros in the matrix. There is no easy rule to do this - basically trial and error.

what does trial and error mean in terms of computation? If I have for example an 2d array of 5 rows and 5 columns then

The first row can cover all the zeros, the first and second, the first row and first column, etc etc too many combinations

isn't there something more efficient than this?

thanks in advance


回答1:


You need to implement bipartite matching algorithm here. You have two partitions in the graph- the vertices in one of them represent the rows and the vertices in the other one represent the columns in the table. There is an edge between rowi and columnj iff there is a 1 in cell (i,j). Then you create maximum bipartite matching. After the last iteration of the bipartite matching algorithm you need to figure out which vertices are connected via a incremental path with the source for your matching. An incremental path is path using only edges with positive capacity. You should have picture like:

         row_1                  col_1
        /                             \
       / - row_2                col_2 -\
source  - ....     some_edges           \ sink
       \                                / 
        \ - row_n                col_n /
                                 .... /
                                 col_m

After you find the maximum bipartite matching, find which rows and columns are reachable via positive-capacity edges from sink. Now the minimum number of rows and columns you need to scratch is found using the following algorithm - you cross out all the rows that are not reachable from the source and all the columns that are reachable and this is your optimal solution.

Hope this answer helps you.




回答2:


I'm not quite sure why they told you to do trial and error. The Hungarian algorithm, however, does not take exponential time. Take a look at wikipedia, which walks you through an example of how to figure out the minimum number of lines (look at Step 3):

http://en.wikipedia.org/wiki/Hungarian_algorithm#Matrix_interpretation

The article also includes links to implementations, and some online course notes which give more complete (although also more technical) descriptions of the Hungarian algorithm than the one you're using.




回答3:


Trial and error means O((n+m)!) complexity.

At most you will only need to pick min(n,m) lines, as selecting all rows or columns will cover all 0s.

I would implement a dynamic programming algorithm to solve this step for large problems.



来源:https://stackoverflow.com/questions/10074270/how-can-i-find-the-minimum-number-of-lines-needed-to-cover-all-the-zeros-in-a-2

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