Question

I have a set S. It contains N subsets (which in turn contain some sub-subsets of various lengths):

1. [[a,b],[c,d],[*]]
2. [[c],[d],[e,f],[*]]
3. [[d,e],[f],[f,*]]
N. ...

I also have a list L of 'unique' elements that are contained in the set S:

a, b, c, d, e, f, *

I need to find all possible combinations between each sub-subset from each subset so, that each resulting combination has exactly one element from the list L, but any number of occurrences of the element [*] (it is a wildcard element).

So, the result of the needed function working with the above mentioned set S should be (not 100% accurate):

- [a,b],[c],[d,e],[f];
- [a,b],[c],[*],[d,e],[f];
- [a,b],[c],[d,e],[f],[*];
- [a,b],[c],[d,e],[f,*],[*];

So, basically I need an algorithm that does the following:

  1. take a sub-subset from the subset 1,
  2. add one more sub-subset from the subset 2 maintaining the list of 'unique' elements acquired so far (the check on the 'unique' list is skipped if the sub-subset contains the * element);
  3. Repeat 2 until N is reached.

In other words, I need to generate all possible 'chains' (it is pairs, if N == 2, and triples if N==3), but each 'chain' should contain exactly one element from the list L except the wildcard element * that can occur many times in each generated chain.

I know how to do this with N == 2 (it is a simple pair generation), but I do not know how to enhance the algorithm to work with arbitrary values for N.

Maybe Stirling numbers of the second kind could help here, but I do not know how to apply them to get the desired result.

Note: The type of data structure to be used here is not important for me.

Note: This question has grown out from my previous similar question.

Was it helpful?

Solution

These are some pointers (not a complete code) that can take you to right direction probably:

  1. I don't think you will need some advanced data structures here (make use of erlang list comprehensions). You must also explore erlang sets and lists module. Since you are dealing with sets and list of sub-sets, they seems like an ideal fit.
  2. Here is how things with list comprehensions will get solved easily for you: [{X,Y} || X <- [[c],[d],[e,f]], Y <- [[a,b],[c,d]]]. Here i am simply generating a list of {X,Y} 2-tuples but for your use case you will have to put real logic here (including your star case)
  3. Further note that with list comprehensions, you can use output of one generator as input of a later generator e.g. [{X,Y} || X1 <- [[c],[d],[e,f]], X <- X1, Y1 <- [[a,b],[c,d]], Y <- Y1].
  4. Also for removing duplicates from a list of things L = ["a", "b", "a"]., you can anytime simply do sets:to_list(sets:from_list(L)).

With above tools you can easily generate all possible chains and also enforce your logic as these chains get generated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top