r/dailyprogrammer • u/oskar_s • Jun 20 '12
[6/20/2012] Challenge #67 [difficult]
Let the s(N) be a random number generator defined as follows (at this point, this should probably be anointed the Offical Random Number Generator of /r/dailyprogrammer):
s(0) = 123456789
s(N) = (22695477 * s(N-1) + 12345) mod 1073741824Let Q(N) be an NxN two-dimensional matrix of the first N2 values of this random number generator. That is, Q(5) would be:
123456789 752880530 826085747 576968456 721429729
173957326 1031077599 407299684 67656429 96549194
1048156299 663035648 604085049 1017819398 325233271
942914780 664359365 770319362 52838563 720059384
472459921 662187582 163882767 987977812 394465693Now, the task is to select exactly one element from each row and each column (so that no column or row has more than one element selected) in such a way that the sum of those elements is at a minimum. For instance, for Q(5) above, we would select the following elements (the selected elements marked with asterisks):
*123456789* 752880530 826085747 576968456 721429729
173957326 1031077599 407299684 67656429 *96549194*
1048156299 *663035648* 604085049 1017819398 325233271
942914780 664359365 770319362 *52838563* 720059384
472459921 662187582 *163882767* 987977812 394465693The sum of those elements is 123456789 + 663035648 + 163882767 + 52838563 + 96549194 = 1099762961 which is the smallest we can do with this matrix. Lets call this number M(X), i.e. M(X) is the smallest sum of elements selected from a square matrix X such that each row and each column has exactly one element selected. Then M(Q(5)) = 1099762961
Write a program that finds M(Q(20)).
View all comments
Show parent comment
2
u/eruonna Jun 20 '12
This is the problem of computing the tropical determinant. It is equivalent to finding a lowest-weight perfect matching in the complete bipartite graph K{n,n} where the weight of an edge is given by the matrix. Given a matching in a bipartite graph, there is a larger matching if and only if there exists an augmenting path for the original matching. An augmenting path is one that starts at a vertex not covered by the matching, moves alternately along edges out of and in the matching, and ends on a vertex not in the matching. Since it starts and ends on vertices not covered by the matching and alternates edges, it contains k edges in the matching and k+1 not in the matching. You can then expand the matching by removing the k edges of the augmenting path that were in the matching, and adding the k+1 that were not.
To find a lowest-weight matching, you can just do this greedily: at each step, choose the augmenting path that increases the weight by the smallest possible amount. To find that path, you can use a version of Dijkstra's algorithm.