Just looking for a bit of direction, I realise that the example given is possible to solve using brute force iteration, but I am looking for a more elegant (ie. mathematical
This is related to the n Queens problem, except that you do not care about the diagonal and you have weighted solutions. As the Queens problem, you can solve it by (multiple) backtracking.
I.e., once you find a solution you remember its weight, mark the soulution as invalid, and start over. The (a) solution with the highest weight wins.
As Matthias indicated you should use backtracking.
Your problem is almost identical to the Assignment problem, which can e.g. be solved by the Hungarian algorithm in polynomial time.
Note that the assignment problem is usually a minimization problem, but multiplying your matrix with -1 and adding some constant should make the method applicable. Further, there is no formal tie-braking condition, for case of multiple optimal solutions. However, the method yields you a solution having the optimal sum. Let m be the minimum summand. Modify your matrix by setting all entries less or equal to m to zero and solve again. Either you get a solution with the same sum that is better than the last one. If not, the previous solution was already optimal.
Ouch. This algorithm is wrong; there is no proof because it's wrong and therefore it's impossible to prove that it's correct. ;) I'm leaving it here because I'm too attached to delete it entirely, and it's a good demonstration of why you should formally prove algorithms instead of saying "this looks right! There's no possible way this could fail to work!"
I'm giving this solution without proof, for the time being. I have a proof sketch but I'm having trouble proving optimal substructure for this problem. Anyway...
Given a square array of numbers, select as many "non-overlapping" numbers as possible so that the sum of the selected numbers is maximised. "Non-overlapping" means that no two numbers can be from the same row or the same column.
A
be a square array of n by n
numbers.Aij
denote the element of A
in the i
th row and j
th column.S( i1:i2, j1:j2 )
denote the optimal sum of non-overlapping numbers for a square subarray of A
containing the intersection of rows i1
to i2
and columns j1
to j2
.Then the optimal sum of non-overlapping numbers is denoted S( 1:n , 1:n )
and is given as follows:
S( 1:n , 1:n ) = max { [ S( 2:n , 2:n ) + A11 ]
[ S( 2:n , 1:n-1 ) + A1n ]
[ S( 1:n-1 , 2:n ) + An1 ]
[ S( 1:n-1 , 1:n-1 ) + Ann ] }
(recursively)
Note that S( i:i, j:j ) is simply Aij.
That is, the optimal sum for a square array of size n
can be determined by separately computing the optimal sum for each of the four sub-arrays of size n-1
, and then maximising the sum of the sub-array and the element that was "left out".
S for |# # # #|
|# # # #|
|# # # #|
|# # # #|
Is the best of the sums S for:
|# | | #| |# # # | | # # #|
| # # #| |# # # | |# # # | | # # #|
| # # #| |# # # | |# # # | | # # #|
| # # #| |# # # | | #| |# |
The recursive algorithm above suggests a recursive solution:
def S(A,i1,i2,j1,j2):
if (i1 == i2) and (j1==j2):
return A[i1][j1]
else:
return max ( S( A, i1+1, i2, j1+1, j2) + A[i1][j1] ],
S( A, i1+1, i2, j1, j2-1) + A[i1][j2] ],
S( A, i1, i2-1, j1+1, j2) + A[i2][j1] ],
S( A, i1, i2-1, j1, j2-1) + A[i2][j2] ], )
Note that this will make O(4^n)
calls to S()
!! This is much better than the factorial O(n!)
time complexity of the "brute force" solution, but still awful performance.
The important thing to note here is that many of the calls are repeated with the same parameters. For example, in solving a 3*3 array, each 2*2 array is solved many times.
This suggests two possible solutions for a speedup:
S()
cache results so that it only needs to S(A,i1,i2,j1,j2)
once for each i1,i2,j1,j2
. This means that S()
only needs to calculate O(n^3)
results - all other requests will be fufilled from cache. (This is called memoising.)n*n
array, and working down through successively smaller subproblems, start at the bottom with the smallest possible subproblems and build up to the n*n
case. This is called dynamic programming. This is also O(n^3)
, but it's a much faster O(n^3)
because you don't have to hit a cache all the time.The dynamic programming solution proceeds somewhat like:
S()
, the optimal sum. It doesn't tell you which numbers actually make up that sum. You get to add in your own method of backtracing your path to the solution.2,2
vs. 1,3
will be broken in favour of having all the individual numbers be as large as possible (so that 2,2
wins.) I believe you can define max()
to break ties in favour of the largest numbers possible, and that will do what you want, but I can't prove it.Dynamic programming is a powerful technique for devising fast algorithms for any problem which exhibits two properties:
If the problem has optimal substructure, and the problem breaks down into slightly smaller problems - say a problem of size n
breaks down into subproblems of size n-1
- then the problem can be solved by dynamic programming.
If you can split the problem into much smaller chunks - say chopping a problem of size n
into halves, each of size n/2
- that's divide and conquer, not dynamic programming. Divide and conquer solutions are generally very fast - for example binary search will find an element in a sorted array in O(log n)
time.