This week's book giveaway is in the Cloud/Virtualizaton forum.We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Logical Help needed (All possible combinations)

Kunal Sathe
Ranch Hand
Posts: 49
Hello,

I am trying to write a java method which takes a hashmap as the input. The hashmap looks as follows:

Key Value
**** ******
Q1 V11,V12,V13,V14....V1n (This is a collection)
Q2 V21,V22,V23,V24....V2n (This is a collection)
.
.
.
.
Qm Vm1,Vm2,Vm3,Vm4....Vmn (This is a collection)

The method should process the hashmap and make all possible combinations as follows:

Combination1: V11, V21, V31.....Vm1
Combination2: V11, V21, V32, V41.......Vm1

and so on.

So basically I am trying to make all possible combinations of the values.
The method will return a collection of these combinations.

I will really appreciate if anybody could shed some light on the logic. I am totally boggled at the moment.

Thanks,
Kunal

Stefan Wagner
Ranch Hand
Posts: 1923
Your question isn't quiete clear to me.

Qx: Vx1, Vx2, ... Vxn

Can you write down all possible combinations for x=2 and n=3?

Repeating of values is prohibited?
Number of elements in collection is allways n?
Elements are allways sorted ascending in Collection?

Chris Johnston
Ranch Hand
Posts: 85
I am not totally sure what it is you want here, but what ever it is, it sounds really expensive with an operation that will probably border on O(n^2) complexity. The larger your hashmap and the larger the collections contained within that hashmap, the longer it is going to take to process the probabilities.

Can you be a little more clear as to what you are trying to do?

Kunal Sathe
Ranch Hand
Posts: 49
Stefan and Chris,

For x=2 and n=3 the inputted HashMap would look like this

Key Value
*** *****
Q1 V11 V12 V13
Q2 V21 V22 V23

Now all possible combinations are:
Combination 1: V11, V21
Combination 2: V11, V22
Combination 3: V11, V23
Combination 4: V12, V21
Combination 5: V12, V22
Combination 6: V12, V23
Combination 7: V13, V21
Combination 8: V13, V22
Combination 9: V13, V23

No combination can ever be repeated.
The number of elements in a valid combination should always be equal to x=2.

Now, the hashmap may also look like this:

Key Value
*** *****
Q1 V11 V12
Q2 V21 V22 V23

Here the value of n is not the same for each collection. This can also be one case.

The elements in the collection may or may not be sorted.

Thanks,
Kunal

M Beck
Ranch Hand
Posts: 323
it wasn't that long ago that a very similar question got asked here... here is the thread.