I've looked at multiple forums and can't seem to find exactly what I'm looking for. I want to generate all possible solutions where I choose one number from each set of numbers, where a solution contains all numbers present and does not A) duplicate numbers and B) pull multiple numbers from one set. For an easy example, consider I have these five sets:
A: {1, 2, 3, 4, 5}
B: {1, 2, 4}
C: {2, 3, 5}
D: {1, 3, 4, 5}
E: {3, 4}
Possible solutions will look something like:
{A:1, B:2, C:3, E:4, D:5}
{D:1, C:2, E:3, B:4, A:5}
...
etc.
As you can see, all numbers 1-5 are used exactly once and all sets A-E are featured exactly once. I know there are less than 120 solutions here (5!), because I cannot have a solution with E2 or D2 or C4, etc.
For concise examples such as this I can do the work manually, but I'm interested in doing this for a group of 50 sets, each containing up to 50 numbers, all within 1-50. Some categories will look like A, where the full set of numbers in the range are used, and some will look like E, where only a few numbers are used (for example, Set X may be simply {11, 19}).
So, if each set had all 50 numbers, I would have 50! (I think?) possible solutions. In my case, though, I know I can rule out any solution where Set X contributes a number other than 11 or 19, which significantly cuts down my solution space, especially when most of these categories will only have a few possible contributions.
Is there a simple way to generate solutions, or at least calculate the number of solutions?
I'm thinking I could:
1) sort all the sets from smallest length to largest length
2) choose a number from the first set (which may only have 1 possible choice)
3) eliminate that number from the remaining sets
4) move to the next set: If it has `0` elements, there is no possible solution using the previous selection of numbers
5) Repeat steps `1-4` until all sets have contributed a number: that is a solution (either print in full or add 1)
In theory, I would have (n1)*(n2-1)*(n3-1)*...*(1) possible solutions (where n1, n2, etc. is the size of that set), but where I'm getting tripped up is when I factor in the times when sets do and don't take numbers that other sets have, because I can't have duplicates. For instance, given Set A has every number: {1, 2, ..., 50} and Set B only has numbers 1-25: {1, 2, ..., 25}, if I choose a number >25 from Set A, then I have 25 possible choices from Set B. If I choose a number <=25 from Set A, then I only have 24 possible choices from Set B. I don't know how to factor in this uncertainty in my calculations, especially when I extrapolate this trend to more than a few sets of numbers.
I have a feeling that the sheer number of possible solutions would be too much for my computer to handle, but I'd at least like to know how many there are. I have all 50 sets of numbers if that would be useful in calculations. Any help would be appreciated with this. Thanks!