Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# permutation combination problem

sitaram panse
Greenhorn
Posts: 3
JTable with 5 column and n rows.
After filling the table with values i've got an array of object of cell values .
I want to perform the permutation combination on this Object[] [] 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" }};

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....
Can i get some help or code snippet to generate this?

Jim Yingst
Wanderer
Sheriff
Posts: 18671
Normally permutations and combinations are two different things, and it's confusing to refer to this as a "permutation combination". What you have here seems to be a combinations problem, with additional constraints, so I'll just use the term combinations.

It looks to me like there are not 120 combinations here, but 3125. That is, there are 5 choices for the first positions, 5 choices for the second, 5 for the third, etc. 5 * 5 * 5 * 5 * 5 = 3125. Since each position is drawn from a separate sample set (1st column is always an A, 2nd always a B, etc) there is no need to divide by anything to eliminate duplicates.

It seems like it should be fairly straightforward to solve this with some nested for loops - five of them. Or you could do this with a recursive solution, if you prefer. Whichever makes more sense to you. I'd prefer the nested loops:

For simplicity, while working on this you may want to start with a smaller table, e.g. 2 x 2 or 3 x 3. Because it may be difficult to check 3125 rows for accuracy. But checking 4 or 27 is easier.

sitaram panse
Greenhorn
Posts: 3
Actualy i also tried the same with one D array.
I have a 2D array like Object[n][m] where m=5 (Fixed), but n could be anything.
so the combination of elements should be generated according to no. of rows.

For Example:

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" }};

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....

I'll appriciate your help on this.