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:
- take a sub-subset from the subset
1, - add one more sub-subset from the subset
2maintaining the list of ‘unique’ elements acquired so far (the check on the ‘unique’ list is skipped if the sub-subset contains the*element); - Repeat
2untilNis 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.
These are some pointers (not a complete code) that can take you to right direction probably:
[{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)[{X,Y} || X1 <- [[c],[d],[e,f]], X <- X1, Y1 <- [[a,b],[c,d]], Y <- Y1].L = ["a", "b", "a"]., you can anytime simply dosets: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.