Permutation Combination Help required...............(2D array combs prob.)

I have an array of object.

I want to perform the permutation combination on 2D array to fetch the all possible combinations of n(row)-5(column-fix) elements.

suppose i have 5 rows and 5 columns in the table then it should generate all possible combinations i.e(120) without repeating the elements. result should be such that from each column only one elements would be selected at a time. each individual combination will have exact 5 elemnts. Sequence of elements will not be considered.

The permutation should be the maximal possible combination of the attributes with each other.

Object[][] data={{ "A1","B1","C1" ,"D1","E1" },{ "A2","B2","C2" ,"D2","E2" },{ "A3","B3","C3" ,"D3","E3" },{ "A4","B4","C4" ,"D4","E4" },{ "A5","B5","C5" ,"D5","E5" }};

Can i get some help or code snippet to generate this?

For Example:

Result should be like this each (combination must be unique):

1st column will always have A's, 2nd column will always have B's, 3rd C's, 4th D's and 5th E's......

A1B1C1D1E1....Correct combs

A1B2C1D1E1

A2B1C1D1E1

A2B2C1D1E1

.........................

B1A1C1D1E1...wrong combination( At 1st place only A should be entertain)

........................ and so on....

[1283 byte] By [spanse@DCa] at [2007-11-27 5:40:54]
# 1

imagine counting in base 5

because you are in base 5, every digit can only range between 0 and 4.

as you count upward you get every possible digit showing up in every possible place.

120 is the wrong number. There are 5 ways to choose the first number and 5 ways to choose the second number and 5 ways to choose the third ...

there are 5^5 = 3125 combinations.

so count from zero to 3124, decode in base 5, and the 5 digits will be your column indicies.

What? you don't know how to decode in base 5? You'd really rather burn some stack space and do a cool recursive algorithm. OK

How do you fill in 5 slots with all possible choices? You choose a value for a single slot (like slot 5) and then fill in th 4 remaining slots with all possible choices.

Object[] slots = new Object[5];

fillSlot(int n){

if (n <0) {

dumpOutResult(slots);

} else {

for(int i = 0; i<5; i++){

slots[n] = data[i][n]; // n, the colum is fixed, we vary the row.

fillSlot(n-1);

}

}

}

and of course the recursive algorithm is just shorthand for the longer

for(col0 = 0; col0<5; col0++){

for(col1 = 0; col1<5; col1++){

for(col2 = 0; col2<5; col2++){

for(col3 = 0; col3<5; col3++){

for(col4 = 0; col4<5; col4++){

dumpOut(data[col0][0], data[col1][1], data[col2][2], data[col3][3], data[col4][4]);

}

}

}

}

}

marlin314a at 2007-7-12 15:17:50 > top of Java-index,Other Topics,Algorithms...