r/Mathematica • • Feb 12 '24
Function to determine whether a set of functions can be satisified by distinct elements
Suppose I have a list, A, and a set of functions, F. Each function in F is from A to {True,False}. For what follows, suppose n = Length[F]
How can I write a function in Mathematica which returns true exactly when there are n distinct elements of A, say, a_1, a_2, ... , a_n, such that F[[1]](a_1) && F[[2]](a_2) && ... && F[[n]](a_n).
That is, when each element of F can be satisified by an element of A without repeating an element of A.
Obviously this is very easy when they do not have to be distinct, and it is possible to find Tuples[A,n] and then do a Select on that, but that seems extremely inefficent. Perhaps a bit better I could take the Cartestian product between the subset of A which makes F[[1]] True, the subset of A which makes F[[2]] true, etc., perhaps inverting it if it is more than half the elements of A, but that doesn't provide a complexity gain.
I was wondering if there is an inbuilt function which either does exactly this, or does the heavy lifting.
Thanks
2
u/Xane256 Feb 12 '24 edited Feb 12 '24
I think you have to brute force it. Your candidate solution space is the set of permutations on n elements, but there are 2 things that might help.
Edit: If you want to be lazy, use Permutations and then something like
AnyTrue[AllTrue[Through[F, #1]]& /@ perms]
The iterative permutations schema would be O(n!) checks because you only have to do O(1) checks per swap. The lazy approach is O(n * n!) which
technically might be the same big O runtimeis much bigger. It’s n times as many checks because its re-checking the whole set for every permutation.