• Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Logical Help needed (All possible combinations)

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stefan and Chris,

Sorry about the confusion.
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
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it wasn't that long ago that a very similar question got asked here... here is the thread.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic