## Inclusion-Exclusion

Let $A_1$ and $A_2$ be finite subsets of a set $X$.
If $A_1$ and $A_2$ are disjoint, the addition principle tells us
$|A_1\cup A_2| = |A_1| + |A_2|$.

If they're not disjoint, this "double-counts" the elements of the
intersection; we can fix this by subtracting the size of the intersection,
yielding the general formula
$|A_1\cup A_2| = |A_1| + |A_2| - |A_1\cap A_2|$.

For three finite subsets $A_1$,$A_2$,$A_3$ of some set $X$, similar reasoning
yields
:   $|A_1 \cup A_2 \cup A_3| =$
:       $|A_1| + |A_2| + |A_3| - (|A_1\cap A_2| + |A_2\cap A_3| + |A_1\cap A_3|) + |A_1\cap A_2\cap A_3|$.

Example:
    {\it How many integers in [1,100] are divisible by 2, 5, or 7?}

    Let $D_n := \{ k \in \|Z \cap [1,100] : n | k \}$.

    Note $|D_n| = \lfloor\frac{100}n\rfloor$.

    By inclusion-exclusion,
    :   $|D_2 \cup D_5 \cup D_7| = |D_2| + |D_5| + |D_7|$
    :       $- (|D_2 \cap D_5| + |D_5 \cap D_7| + |D_2 \cap D_7|)$
    :       $+ |D_2 \cap D_5 \cap D_7|$
    :   $= |D_2| + |D_5| + |D_7| - (D_{10} + D_{35} + D_{14}) + D_{70}$
    :   $= 50 + 20 + 14 - (10 + 2 + 7) + 1$
    :   $= 66$.

Theorem [Inclusion-Exclusion Principle]:
    Let $A_1, ..., A_n$ be finite subsets of a set $X$.
    Then
    :   $|A_1\cup...\cup A_n| =$
    :       $\sum_i |A_i|$
    :       $- \sum_{i<j} |A_i \cap A_j|$
    :       $+ \sum_{i<j<k} |A_i \cap A_j \cap A_k|$
    :       $- ...$
    :       $+ (-1)^{n-1} |A_1 \cap ... \cap A_n|$
Proof:
    Let $x (- A_1\cup...\cup A_n$.
    We show that $x$ "contributes 1" to the right hand side.
    Say $x$ is in $m>=1$ of the $n$ sets.
    Then $x$ contributes 1 to $m={m \choose 1}$ of the $|A_i|$,
    to ${m \choose 2}$ of the $|A_i \cap A_j|$,
    to ${m \choose 3}$ of the $|A_i \cap A_j \cap A_k|$,
    and so on.

    So $x$ contributes
    :   $\sum_{k>0} (-1)^{k-1} {m \choose k}$
    :       $= - \sum_{k>0} (-1)^k {m \choose k}$
    :       $= 1 - \sum_{k>=0} (-1)^k {m \choose k}$
    :       $= 1 - 0$
    :       $= 1$

Remark:
    Neat alternative expression:
    :   $|{ \bigcup_i A_i }| = \sum_{ {} != I (= \{1,...,n\} } (-1)^{|I| - 1} |{ \bigcap_{i (- I} A_i }|$.

Bonus:
    Version of the proof using this notation:
    *\[a* |{ \bigcup_i A_i }| &= \sum_{x (= \bigcup_i A_i} 1 \\
    &= \sum_{x (= \bigcup_i A_i} \left( - \sum_{k>0} (-1)^k { \# \{i | x (- A_i\} \choose k} \right)\\
    &= \sum_{x (= \bigcup_i A_i, I(=\{i | x (- A_i\} } (-1)^{|I| - 1} \\
    &= \sum_{\{(x,I) | x (- \bigcup_i A_i, I (= \{1,...,n\}, x (= \bigcap_{i (- I} A_i\}} (-1)^{|I| - 1} \\
    &= \sum_{{} != I (= \{1,...,n\}} (-1)^{|I| - 1} |{ \bigcap_{i (- I} A_i }|
    *a\]*

Example:
    {\it How many strings of 8 letters from the Roman alphabet contain 'j', 'q', 'x', 'y' and 'z'?}
    
    We could do this positively, but it would be fiddly.

    Instead, let's count the number of strings which {\bf don't} contain all of
    these letters, i.e. which omit 'j' or omit 'q' or... .

    Let $O_j$ be the strings which omit 'j', $O_{jq}$ the strings which omit
    'j' #and# 'q', and so on.

    Then by inclusion-exclusion,
    :   $|O_j \cup O_q \cup O_x \cup O_y \cup O_z| = |O_j| + |O_q| + ...$
    :       $- (|O_{jq}| + |O_{jx}| + ...)$
    :       $+ (|O_{jqx}| + |O_{jqy}| + ...)$
    :       $- (|O_{jqxy}| + |O_{jqxz}| + ...)$
    :       $+ |O_{jqxyz}|$

    Now $|O_j| = |O_q| = ... = 25^8$,
    and $|O_{jq}| = |O_{jx}| = ... = 24^8$,
    and so on.

    So
    :   $|O_j \cup O_q \cup O_x \cup O_y \cup O_z| =$
    :       ${5 \choose 1} 25^8 - {5\choose 2}24^8 + {5 \choose 3} 23^8 - {5\choose 4}22^8 + {5 \choose 5} 21^8$

    and the answer to the original question is
    $26^8 - |O_j \cup O_q \cup O_x \cup O_y \cup O_z|$, which comes to
    87408720.


# Combinations of multisets, revisited

Recall:
    The number of $r$-combinations of a multiset with at least $r$ of each of
    its $t$ types is ${ r+t-1 \choose t-1}$.

If there are fewer than $r$ of some of the types, we can use
inclusion-exclusion.

This is clearest if we transform the problem.

An $r$-combination of a multiset
:   $\{ c_1 * a_1, ... , c_t * a_t \}$
corresponds to a solution in non-negative integers of the equation
:   $x_1 + ... + x_t = r$
subject to the constraints
:   $x_1 <= c_1, ..., x_t <= c_t$.

Concrete example:
    I take 8 marbles from a bag containing 3 red marbles, 2 blue marbles, and
    10 green marbles. How many possibilities are there for the numbers of each
    colour I get? Equivalently, what is
    \[ |\{(r,b,g) | r+b+g=8, 0<=r<=3, 0<=b<=2\}| ?\]

So we want to count the number of such solutions,
and we know that the answer is ${ r+t-1 \choose t-1}$ if there are no
constraints.

By the subtraction principle,
the number of solutions in the constrained case is the number in the
unconstrained case minus the number which fail at least one constraint,
:   ${r+t-1 \choose t-1} - |F_1 \cup ... \cup F_t|$,
where $F_i := \{ (x_1,...,x_t) | x_1 + ... + x_t = r, x_i > c_i \}$.

So we can use the inclusion-exclusion principle if we can determine the sizes
of the intersections of the $F_i$.

If $c_i >= r$, then $F_i={}$.
Otherwise, subtracting $c_i+1$ from $x_i$ puts $F_i$ in correspondence with
$\{ (y_1,...,y_t) | y_1 + ... + y_t = r-(c_i+1), y_i >= 0 \}$,
so $|F_i| = {r-(c_i+1) + t - 1 \choose t - 1}$.

Similarly, $|F_i \cap F_j| = {r-(c_i+1)-(c_j+1)+t-1 \choose t-1}$, and so on.

So inclusion-exclusion yields
:   $\sum_{I (= \{1,...,t\}} (-1)^{|I|} { r-(\sum_{i (- I} (c_i+1)) +t-1 \choose t-1 }$.

Marble example:
:   ${8+2 \choose 2} - {8-(3+1)+2 \choose 2} - {8-(2+1)+2 \choose 2} + {8-(3+1)-(2+1)+2\choose 2}$
:       $= {10\choose 2} - {6\choose 2} - {7\choose 2} + {3 \choose 2}$
:       $= 12$

Scrabble example:
    How many 7-tile hands can be drawn from a standard 100-tile bag of
    scrabble tiles?
    Using the above formula, my computer calculates it as
    3199724.

    (for the curious, here's the Haskell code I used to calculate this:
\begin{verbatim}
::: import Math.Combinatorics.Binomial (choose)
::: combs :: Int -> [Int] -> Int
::: combs r cs =
:::    let
:::        t = length cs
:::        subs = subs' [] cs
:::        -- subs': returns relevant subsequences of cs, omitting those which
:::        -- will contribute 0 to the final sum (without this, the algorithm
:::        -- would have complexity exponential in t)
:::        subs' sub [] = [sub]
:::        subs' sub _ | (sum (map (+1) sub) > r) = []
:::        subs' sub (c:cs) = subs' (c:sub) cs ++ subs' sub cs
:::  in sum [ (-1)^(length sub) *
:::         choose (r - sum (map (+1) sub) + t-1) (t-1) | sub <- subs ]
::: scrabbleBag :: [Int]
::: scrabbleBag = concat [ replicate n c | (n,c) <-
:::         [(5,1), (10,2), (1,3), (4,4), (3,6), (1,8), (2,9), (1,12)] ]
::: main :: IO ()
::: main = print $ combs 7 scrabbleBag
\end{verbatim}
)


# Derangements

A _derangement_ is a permutation which leaves nothing in its original position.

e.g.
\\{\tt (5,3,4,2,1)} is a derangement of 
\\{\tt (1,2,3,4,5)}, and
\\"{\tt endgreatmen}" is a derangement of
\\"{\tt derangement}".

$D_n :=$ the number of derangements of a sequence of length $n$,
:   $=$ number of derangements of $(1,2,...,n)$.

We can use inclusion-exclusion to determine $D_n$.

A derangement of $(1,...,n)$ is a permutation $(a_1,...,a_n)$ which
satisfies the conditions $a_1 /= 1, ..., a_n /= n$.

Let $P_i$ be the set of permutations which fail the $i$th of these conditions,
i.e. such that $a_i=i$.

Easily, for $I (= \{1,...,n\}$,
:   $|\bigcap_{i\in I} P_i| = (n - |I|)!$.

So by inclusion-exclusion,
:   $D_n = n! - |\bigcup_i P_i|$
:       $= n! - \sum_{{} /= I (= \{1,...,n\}} (-1)^{|I| - 1} |\bigcap_{i\in I} P_i|$
:       $= n! - \sum_{{} /= I (= \{1,...,n\}} (-1)^{|I| - 1} (n - |I|)!$
:       $= n! - \sum_{i=1}^n (-1)^{i-1} {n\choose i}(n-i)!$
:       $= \sum_{i=0}^n (-1)^i {n\choose i}(n-i)!$
:       $= \sum_{i=0}^n (-1)^i \frac{n!}{i!}$
:       $= n!\sum_{i=0}^n \frac{(-1)^i}{i!}$

Note then that the probability that a random $n$-permutation is a derangement
is
:   $Pr_n = \frac{|D_n|}{n!} = \sum_{i=0}^n \frac{(-1)^i}{i!}$,
so
:   $\lim_{n --> \infty} Pr_n = \sum_{i=0}^\infty \frac{(-1)^i}{i!}$
:       $= e^{-1} ~~ 0.368$

The convergence is very fast;
e.g. $Pr_n ~~ 0.368$ to 3 significant figures for $n>=6$.

Example:
    A deranged scientist removes the heads from a large number of different
    animals and re-attaches them at random. What is the probability that every
    resulting creature is a chimera, i.e. that no head is reattached to its
    own body?
Answer:
    About $e^{-1}$.
