java - To minimize heavy use of recursion? -
java - To minimize heavy use of recursion? -
i quite new in programming , java. have created simple java code generation of s-permutation matrices. s- permutation matrices square matrices have 1 in each row, each column , each block 1 time e.g.
1 0 0 * 0 0 0 * 0 0 0 0 0 0 * 1 0 0 * 0 0 0 0 0 0 * 0 0 0 * 1 0 0 *********************** 0 1 0 * 0 0 0 * 0 0 0 0 0 0 * 0 1 0 * 0 0 0 0 0 0 * 0 0 0 * 0 1 0 *********************** 0 0 1 * 0 0 0 * 0 0 0 0 0 0 * 0 0 1 * 0 0 0 0 0 0 * 0 0 0 * 0 0 1 above illustration of 9 x 9 s-permutation matrix.
4 x 4 illustration
1 0 * 0 0 0 0 * 1 0 *********** 0 1 * 0 0 0 0 * 0 1 i made java code 4 x 4 , generating 4 x 4 s-permutation matrices 16 in number. same logic when did 9 x 9 programme giving matrices , showing stackoverflow error.
these 9 x 9 s-permutation matrices 46656 in number. programme giving 2000 matrices , showing stackoverflow error. asked of friends , searched on google , think either logic of programme @ fault or because of heavy utilize of recursion in program.
would 1 please help me regarding , tell me how can minimize utilize of recursion. read on site 1 way of using loops there no way of using loops that. kindly tell me there other way it?
program gonna share sec programme made , giving around 300 matrices..http://pastebin.com/2lbccce9
i think should go different way of representing matrix.
for example, instead of creating array lists of array lists, seek represent rows numbers. know each row has 1 column "1", , rest zero. instead of representing such row array list, utilize index of column 1:
1 0 0 0 0 0 0 0 0 → 0 0 1 0 0 0 0 0 0 0 → 1 0 0 1 0 0 0 0 0 0 → 2 0 0 0 1 0 0 0 0 0 → 3 0 0 0 0 1 0 0 0 0 → 4 0 0 0 0 0 1 0 0 0 → 5 0 0 0 0 0 0 1 0 0 → 6 0 0 0 0 0 0 0 1 0 → 7 0 0 0 0 0 0 0 0 1 → 8 now, legal matrix permutation of these numbers, example,
1 0 2 4 5 8 6 7 3 is representation of matrix
0 1 0 0 0 0 0 0 0 → 1 1 0 0 0 0 0 0 0 0 → 0 0 0 1 0 0 0 0 0 0 → 2 0 0 0 0 1 0 0 0 0 → 4 0 0 0 0 0 1 0 0 0 → 5 0 0 0 0 0 0 0 0 1 → 8 0 0 0 0 0 0 1 0 0 → 6 0 0 0 0 0 0 0 1 0 → 7 0 0 0 1 0 0 0 0 0 → 3 so simple array of 9 integers can represent whole matrix. , matrix guaranteed fulfill constraints on row , column uniqueness (do understand why?)
so seek generate permutations of these 9 numbers - it's much easier problem.
write method accepts such permutation parameter, , checks whether fulfills constraints on blocks.
private boolean checkblocks( int[] matrix, int blockwidth ) { // code checking here } inside method can reconstruct matrix array of arrays or whatever help check constraint, , check it, , homecoming true if blocks ok, , false if not.
now, have generate permutation of array [ 0 1 2 3 4 5 6 7 8 9 ], pass each of them checkblocks method, , maintain ones returned true.
the recursion permutations of 9 numbers no more 9 deep, , such little arrays, i'm sure have no problem of stack overflow.
java algorithm
Comments
Post a Comment