How to check subsets efficiently

334 Views Asked by At

Suppose we have a 5-bit 0-1 string "01001" and a set of 5-bit 0-1 strings {"01010","10111","01011"}. Define a relation on string a and b: $a \subseteq b$ if and only if all bits 1 in a exist in b in the same position. For example. 01001$\subseteq$01011 because all bits 1 in 01001 also exist in 01011 in the same position. Here is the question: Could we design a pattern, like a encoding function F($a$,$S$) to use bit operation to encode all strings in the string set $S$, such that whenever a new string $a$ comes, we can check whether $a$ is a subset of one of the string in the string set $S$ in $\textbf{one operation}$, $\textbf{in which we don't need to iterative every element in S}$. We just need to know whether it is a subset and we don't care which set is $a$'s superset. In the example, F(01001,{01010,10111,01011}) = 1 because 01001 is a subset of one element in {01010,10111,01011}.

1

There are 1 best solutions below

2
On

To check if $a \subseteq b$ you can check whether $a \& b =a$. Therefore you can try something like $$F(a,S)=OR_{b \in S} (a\&b=a)$$