Suppose we wanted to implement a boolean function, such as isRectangle, by making note of its necessary, sufficient, and necessary and sufficient conditions and combining them in different ways. How many unique implementations can we make?
For the example of isRectangle, we have
Let $P = $ "shape $s$ is a rectangle"
$N$: Necessary condition of $P$ (i.e. $P \rightarrow N$)
- $N_1 = $ shape $s$ has four sides
- $N_2 = $ shape $s$ has four angles
$S$: Sufficient condition for $P$ (i.e. $S \rightarrow P$)
- $S_1 = $ shape $s$ is a square
$T$: Necessary and sufficient conditions (i.e. $P \leftrightarrow T$)
- $T_1 = $ shape $s$ has four angles and they are all right angles
One implementation of isRectangle would be:
def isRectangle(s):
if not N1: return False
if not N2: return False
if S1: return True
return T1
but this is just one of many. A second could be:
def isRectangle(s):
if S1: return True
if not N2: return False
return T1
And yet a third could be
def isRectangle(s):
return T1
In general, how many implementations are there of a boolean function, when taking into account necessary conditions, sufficient conditions, and necessary and sufficient conditions?
In general, for a given proposition $P$,
Necessary and Sufficient conditions allow us to do early returns
We can implement $P$ as any of the following
where
where
n$\in N$,s$\in S$, andt$\in T$.Now, to count the number of implementations of this boolean function,
Then any $r$-permutation of $N$ and $S$ conditions (with $r$ ranging from $0$ to $|N| + |S|$) followed by a $T$ condition form an implementation.
The total number of implementations, then$^1$, is $\bigg\lfloor e \cdot \big(|N|+|S|\big)! \bigg\rfloor\ \cdot |T|$
$^1$: See How to compute $\sum_{r=0}^N \frac{N!}{(N-r)!}$? This quantity is the count of $r$-permutations of $N$ items (for $r$ ranging from $0$ to $N$).
Why check a necessary condition or a sufficient condition when all we need to do is check a necessary and sufficient condition? A: In some cases, a $N$ (necessary) condition) or a $S$ (sufficient) condition is easy(ier) to check than a $T$ (necessary and sufficient) condition and if the $N$ condition fails, we can return
Falseearly, or if the $S$ condition succeeds, we can returnTrueearly w/o having to check a more expensive $T$ condition.