programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Devaka Cooray
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Jeanne Boyarsky
• Tim Cooke
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Mikalai Zaikin
• Carey Brown
Bartenders:

permutation combination problem

Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
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?

Wanderer
Posts: 18671
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
Thanks for your help....
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.

 I knew I would regret that burrito. But this tiny ad has never caused regrets: a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com