Here's a recursive function that hopefully achieves what you want. It uses no Tuples
or Subsets
so it shouldn't be too bad on the memory. I hope your valid tuples are extremely uncommon, because even $25^{25}$ is a ridiculously huge number.
ClearAll@findTuples;
findTuples[e_] := findTuples[e, {}];
findTuples[e_ /; Length[e] == 1, v_] := List /@ First@e;
findTuples[e_, verboten_] := Module[{r, v},
Flatten[Function[f,
v = Cases[Union[verboten, Flatten@f], Except[0]];
r = Fold[DeleteCases[#1, {_?IntegerQ ..., #2, _?IntegerQ ...},
Infinity] &, Rest@e, v];
Prepend[#, f] & /@ findTuples[r, v]
] /@ First@e, 1]
];
The idea is to maintain a list of forbidden numbers, and use those to filter the list as we go. At each step we pick each member of the first element in turn, use that to filter the rest of the list, then prepend to valid tuples of the filtered rest. If we have reached the end of the list, just give those back in a list.
findTuples[e1] // Column
{{0,0},{0,0},{0,0},{0,0},{0,0}}
{{0,0},{0,0},{0,0},{4,1},{0,0}}
{{0,0},{0,0},{0,0},{4,2},{0,0}}
{{0,0},{0,0},{0,0},{4,3},{0,0}}
{{0,0},{0,0},{3,1},{0,0},{0,0}}
{{0,0},{0,0},{3,1},{4,2},{0,0}}
{{0,0},{0,0},{3,2},{0,0},{0,0}}
{{0,0},{0,0},{3,2},{4,1},{0,0}}
{{0,0},{2,1},{0,0},{0,0},{0,0}}
{{0,0},{2,1},{0,0},{4,3},{0,0}}
Here's a slightly faster version.
ClearAll@findTuples;
findTuples[e_] := findTuples[e, {}];
findTuples[e_ /; Length[e] == 1, v_] := List /@ First@e;
findTuples[e_, verboten_] :=
If[Min[Length /@ e] > 0,
Module[{r, v, o, s, i},
o = Ordering[e, All, Length[#1] < Length[#2] &];
i = InversePermutation@o;
s = e[[o]];
#[[i]] & /@ Flatten[Function[f,
v = Cases[Flatten@f, Except[0]];
r = Fold[DeleteCases[#1, {#2, _} | {_, #2}, {2}] &, Rest@s, v];
Prepend[#, f] & /@ findTuples[r, Union[verboten, v]]
] /@ First@s, 1]
],
{}];
If there are zero choices somewhere in the rest, it skips that whole branch. As suggested by @anderstood, it sorts by Length
at each step, and then unsorts the results at the end. The filtering of forbidden elements has been improved: assuming we are always dealing with pairs of integers, and only filtering on the new forbidden numbers.
BUT it's still going to get slow as the combinations increase. If OP wants to provide sample data, or how it is generated, then maybe it could be tailored to that. I believe you have no hope to filter $100^{100}$ combinations.
e1
or with{{0, 0}, {0, 0}, {2, 1}, {0, 0}, {3, 1}, {3, 2}, {0, 0}, {4, 1}, {4, 2}, {4, 3}, {0, 0}}
? $\endgroup${{0,0},{2,1},{0,0},{4,1},{0,0},{1,5}}
be kept (if it could be formed frome1
)? In other word, is the condition that number should not appear exactly twice, or twice or more? $\endgroup$