I have a few sets, say 5 of them
{1,2,3}
{2,4}
{1,2}
{4,5}
{2,3,5}
Here, I need to choose at least 3 elements from any three sets(One element per set). Given that if an element is already selected, then it cannot be selected again.
Also check if any solution exists or not.
Eg
set {1,2,3} -> choose 1
set {2,4} -> choose 2
set {1,2} -> cannot choose since both 1 and 2 are chosen.
set {2,5} -> can only choose 5
Is there a formula or steps to mathematically achieve this? Simple explanation would be appreciated.
One possibility (which may be overkill) is an integer linear program. Create a binary variable $x_{ij}$ for each combination of a set index $i$ (1 to 5 in your example) and an element $j$ from that set. So, for instance, $x_{53}=1$ means the element 3 was selected from set 5. For indexing purposes let $I=\lbrace1,\dots,5\rbrace$ be the set indices and $J_i$ the elements in set $i$. You need a constraint $$\sum_{j\in J_i} x_{ij} \le 1$$ for each set $i$ to restrict to at most one selection from that set, as well as a constraint $$\sum_i \sum_j x_{ij} = 3$$to select the correct number of elements. Finally, to avoid selecting the same value twice, you need a constraint $$\sum_{i:j\in J_i} x_{ij} \le 1$$ for each distinct element $j$ in the union of the sets. If you have a preference among feasible solutions, you can try adding it as an objective function. Otherwise, just minimize 0 and grab the first feasible solution
You can also solve the problem using a constraint programming (CP) solver. I can't write a formulation because CP languages and solvers are not a standardized as IP solvers in terms of what they support, but this problem should be doable with an CP solver.
Also, you can set it up as a graph problem, using a layered digraph. Each layer corresponds to one of the original five sets, plus one terminal layer containing a node representing problem satisfaction. Among the nonterminal layers, each node consists of the elements of the corresponding set that are still available after removing elements already used on the path to that node, plus a counter for how many elements have been selected. If the element count reaches 3, there is an arc to the terminal node. Otherwise, there is an arc from each node to any node in the next layer that is feasible.
All these will find a feasible solution. The question specifies picking "at least three elements" but does not specify whether more than three is better, or whether all solutions are needed (as opposed to one solution).