Find minimum sum of elements of n*n 2D matrix such that I have to choose one and only one element from each row and column ? Eg
4 12
6 6
If I choose 4
from row 1
I cannot choose 12
from row 1
also from column 1
,
I can only choose 6 from row 2 column 2.
So likewise minimum sum would be 4 + 6 = 10
where 6
is from second row second column
and not 6 + 12 = 18
where 6
is from second row first column
also 4 + 12
is not allowed since both are from same row
I thought of brute force where once i pick element from row and column I cannot pick another but this approach is O(n!)
.
Theorem: If a number is added to or subtracted from all of the entries of any one row or column of the matrix, then elements selected to get required minimum sum of the resulting matrix are the same elements selected to get required minimum sum of the original matrix.
The Hungarian Algorithm (a special case of min-cost flow problem) uses this theorem to select those elements satisfying the constraints given in your problem :
See this example for better understanding.
Both O(n4) (easy and fast to implement) and O(n3) (harder to implement) implementations are explained in detail here.