The inclusion–exclusion principle is widely used and only a few of its applications can be mentioned here.
Counting derangements A well-known application of the inclusion–exclusion principle is to the combinatorial problem of counting all
derangements of a finite set. A
derangement of a set
A is a
bijection from
A into itself that has no fixed points. Via the inclusion–exclusion principle one can show that if the cardinality of
A is
n, then the number of derangements is [
n! /
e] where [
x] denotes the
nearest integer to
x; a detailed proof is available
here and also see
the examples section above. The first occurrence of the problem of counting the number of derangements is in an early book on games of chance: ''Essai d'analyse sur les jeux de hazard'' by P. R. de Montmort (1678 – 1719) and was known as either "Montmort's problem" or by the name he gave it, "
problème des rencontres." The problem is also known as the
hatcheck problem. The number of derangements is also known as the
subfactorial of
n, written !
n. It follows that if all bijections are assigned the same probability then the probability that a random bijection is a derangement quickly approaches 1/
e as
n grows.
Counting intersections The principle of inclusion–exclusion, combined with
De Morgan's law, can be used to count the cardinality of the intersection of sets as well. Let \overline{A_k} represent the complement of
Ak with respect to some universal set
A such that A_k \subseteq A for each
k. Then we have :\bigcap_{i=1}^n A_i = \overline{\bigcup_{i=1}^n \overline{A_i}} thereby turning the problem of finding an intersection into the problem of finding a union.
Graph coloring The inclusion exclusion principle forms the basis of algorithms for a number of NP-hard graph partitioning problems, such as
graph coloring. A well known application of the principle is the construction of the
chromatic polynomial of a graph.
Bipartite graph perfect matchings The number of
perfect matchings of a
bipartite graph can be calculated using the principle.
Number of onto functions Given finite sets
A and
B, how many
surjective functions (onto functions) are there from
A to
B?
Without any loss of generality we may take
A = {1, ...,
k} and
B = {1, ...,
n}, since only the cardinalities of the sets matter. By using
S as the set of all
functions from
A to
B, and defining, for each
i in
B, the property
Pi as "the function misses the element
i in
B" (
i is not in the
image of the function), the principle of inclusion–exclusion gives the number of onto functions between
A and
B as: :\sum_{j=0}^{n} \binom{n}{j} (-1)^j (n-j)^k.
Permutations with forbidden positions A
permutation of the set
S = {1, ...,
n} where each element of
S is restricted to not being in certain positions (here the permutation is considered as an ordering of the elements of
S) is called a
permutation with forbidden positions. For example, with
S = {1,2,3,4}, the permutations with the restriction that the element 1 can not be in positions 1 or 3, and the element 2 can not be in position 4 are: 2134, 2143, 3124, 4123, 2341, 2431, 3241, 3421, 4231 and 4321. By letting
Ai be the set of positions that the element
i is not allowed to be in, and the property
Pi to be the property that a permutation puts element
i into a position in
Ai, the principle of inclusion–exclusion can be used to count the number of permutations which satisfy all the restrictions. In the given example, there are 12 = 2(3!) permutations with property
P1, 6 = 3! permutations with property
P2 and no permutations have properties
P3 or
P4 as there are no restrictions for these two elements. The number of permutations satisfying the restrictions is thus: :4! − (12 + 6 + 0 + 0) + (4) = 24 − 18 + 4 = 10. The final 4 in this computation is the number of permutations having both properties
P1 and
P2. There are no other non-zero contributions to the formula.
Stirling numbers of the second kind The
Stirling numbers of the second kind,
S(
n,
k) count the number of
partitions of a set of
n elements into
k non-empty subsets (indistinguishable
boxes). An explicit formula for them can be obtained by applying the principle of inclusion–exclusion to a very closely related problem, namely, counting the number of partitions of an
n-set into
k non-empty but distinguishable boxes (
ordered non-empty subsets). Using the universal set consisting of all partitions of the
n-set into
k (possibly empty) distinguishable boxes,
A1,
A2, ...,
Ak, and the properties
Pi meaning that the partition has box
Ai empty, the principle of inclusion–exclusion gives an answer for the related result. Dividing by
k! to remove the artificial ordering gives the Stirling number of the second kind: :S(n,k) = \frac{1}{k!}\sum_{t=0}^k (-1)^t \binom k t (k-t)^n.
Rook polynomials A
rook polynomial is the
generating function of the number of ways to place non-attacking
rooks on a
board B that looks like a subset of the squares of a
checkerboard; that is, no two rooks may be in the same row or column. The board
B is any subset of the squares of a rectangular board with
n rows and
m columns; we think of it as the squares in which one is allowed to put a rook. The
coefficient,
rk(
B) of
xk in the rook polynomial
RB(
x) is the number of ways
k rooks, none of which attacks another, can be arranged in the squares of
B. For any board
B, there is a complementary board B' consisting of the squares of the rectangular board that are not in
B. This complementary board also has a rook polynomial R_{B'}(x) with coefficients r_k(B'). It is sometimes convenient to be able to calculate the highest coefficient of a rook polynomial in terms of the coefficients of the rook polynomial of the complementary board. Without loss of generality we can assume that
n ≤
m, so this coefficient is
rn(
B). The number of ways to place
n non-attacking rooks on the complete
n ×
m "checkerboard" (without regard as to whether the rooks are placed in the squares of the board
B) is given by the
falling factorial: :(m)_n = m(m-1)(m-2) \cdots (m-n+1). Letting
Pi be the property that an assignment of
n non-attacking rooks on the complete board has a rook in column
i which is not in a square of the board
B, then by the principle of inclusion–exclusion we have: : r_n(B) = \sum_{t=0}^n (-1)^t (m-t)_{n-t} r_t(B').
Euler's phi function Euler's totient or phi function,
φ(
n) is an
arithmetic function that counts the number of positive integers less than or equal to
n that are
relatively prime to
n. That is, if
n is a
positive integer, then φ(
n) is the number of integers
k in the range 1 ≤
k ≤
n which have no common factor with
n other than 1. The principle of inclusion–exclusion is used to obtain a formula for φ(
n). Let
S be the set {1, ...,
n} and define the property
Pi to be that a number in
S is divisible by the prime number
pi, for 1 ≤
i ≤
r, where the
prime factorization of :n = p_1^{a_1} p_2^{a_2} \cdots p_r^{a_r}. Then, :\varphi(n) = n - \sum_{i=1}^r \frac{n}{p_i} + \sum_{1 \leqslant i
Dirichlet hyperbola method The
Dirichlet hyperbola method re-expresses a sum of a
multiplicative function f(n) by selecting a suitable
Dirichlet convolution f = g \ast h, recognizing that the sum : F(n) = \sum_{k=1}^n f(k) = \sum_{k=1}^n \sum_{xy=k}^{} g(x) h(y) can be recast as a sum over the
lattice points in a region bounded by x \geq 1, y \geq 1, and xy \leq n, splitting this region into two overlapping subregions, and finally using the inclusion–exclusion principle to conclude that : F(n) = \sum_{k=1}^n f(k) = \sum_{k=1}^n \sum_{xy=k}^{} g(x)h(y) = \sum_{x=1}^a \sum_{y=1}^{n/x} g(x)h(y) + \sum_{y=1}^b \sum_{x=1}^{n/y} g(x)h(y) - \sum_{x=1}^a \sum_{y=1}^b g(x)h(y). ==Diluted inclusion–exclusion principle==