This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes permutation combination problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "permutation combination problem" Watch "permutation combination problem" New topic
Author

permutation combination problem

sitaram panse
Greenhorn

Joined: May 28, 2007
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

Joined: Jan 30, 2000
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.


"I'm not back." - Bill Harding, Twister
sitaram panse
Greenhorn

Joined: May 28, 2007
Posts: 3
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.
 
jQuery in Action, 2nd edition
 
subject: permutation combination problem
 
Similar Threads
Challenging permutation - combination of strings
ResultSet returns empty when one of the tables is empty!
Don't know which algorithm to use
Logical Help needed (All possible combinations)
Tricky task! Help needed...