Inclusion–exclusion principle
In combinatorics, the inclusion–exclusion principle (commonly referred to as PIE) is a counting technique which generalizes the familiar method of obtaining the number of elements in the union of two finite sets; symbolically expressed as where Template:Mvar and Template:Mvar are two finite sets and Template:Math indicates the cardinality of a set Template:Mvar (which may be considered as the number of elements of the set, if the set is finite). The formula expresses the fact that the sum of the sizes of the two sets may be too large since some elements may be counted twice. The double-counted elements are those in the intersection of the two sets and the count is corrected by subtracting the size of the intersection.
The inclusion-exclusion principle, being a generalization of the two-set case, is perhaps more clearly seen in the case of three sets, which for the sets Template:Mvar, Template:Mvar and Template:Mvar is given by This formula can be verified by counting how many times each region in the Venn diagram figure is included in the right-hand side of the formula. In this case, when removing the contributions of over-counted elements, the number of elements in the mutual intersection of the three sets has been subtracted too often, so must be added back in to get the correct total.
Generalizing the results of these examples gives the principle of inclusion–exclusion. To find the cardinality of the union of Template:Mvar sets:
- Include the cardinalities of the sets.
- Exclude the cardinalities of the pairwise intersections.
- Include the cardinalities of the triple-wise intersections.
- Exclude the cardinalities of the quadruple-wise intersections.
- Include the cardinalities of the quintuple-wise intersections.
- Continue, until the cardinality of the Template:Mvar-tuple-wise intersection is included (if Template:Mvar is odd) or excluded (Template:Mvar even).
The name comes from the idea that the principle is based on over-generous inclusion, followed by compensating exclusion. This concept is attributed to Abraham de Moivre (1718),[1] although it first appears in a paper of Daniel da Silva (1854)[2] and later in a paper by J. J. Sylvester (1883).[3] Sometimes the principle is referred to as the formula of Da Silva or Sylvester, due to these publications. The principle can be viewed as an example of the sieve method extensively used in number theory and is sometimes referred to as the sieve formula.[4]
As finite probabilities are computed as counts relative to the cardinality of the probability space, the formulas for the principle of inclusion–exclusion remain valid when the cardinalities of the sets are replaced by finite probabilities. More generally, both versions of the principle can be put under the common umbrella of measure theory.
In a very abstract setting, the principle of inclusion–exclusion can be expressed as the calculation of the inverse of a certain matrix.[5] This inverse has a special structure, making the principle an extremely valuable technique in combinatorics and related areas of mathematics. As Gian-Carlo Rota put it:Template:Sfn
"One of the most useful principles of enumeration in discrete probability and combinatorial theory is the celebrated principle of inclusion–exclusion. When skillfully applied, this principle has yielded the solution to many a combinatorial problem."
Formula
In its general formula, the principle of inclusion–exclusion states that for finite sets Template:Math, one has the identity Template:NumBlk

This can be compactly written as or
In words, to count the number of elements in a finite union of finite sets, first sum the cardinalities of the individual sets, then subtract the number of elements that appear in at least two sets, then add back the number of elements that appear in at least three sets, then subtract the number of elements that appear in at least four sets, and so on. This process always ends since there can be no elements that appear in more than the number of sets in the union. (For example, if there can be no elements that appear in more than sets; equivalently, there can be no elements that appear in at least sets.)
In applications it is common to see the principle expressed in its complementary form. That is, letting Template:Mvar be a finite universal set containing all of the Template:Math and letting denote the complement of Template:Math in Template:Mvar, by De Morgan's laws we have
As another variant of the statement, let Template:Math be a list of properties that elements of a set Template:Mvar may or may not have, then the principle of inclusion–exclusion provides a way to calculate the number of elements of Template:Mvar that have none of the properties. Just let Template:Math be the subset of elements of Template:Mvar which have the property Template:Math and use the principle in its complementary form. This variant is due to J. J. Sylvester.[1]
Notice that if you take into account only the first Template:Math sums on the right (in the general form of the principle), then you will get an overestimate if Template:Math is odd and an underestimate if Template:Math is even.
Examples
Counting derangements
A more complex example is the following.
Suppose there is a deck of Template:Mvar cards numbered from Template:Math to Template:Mvar. Suppose a card numbered Template:Mvar is in the correct position if it is the Template:Mvarth card in the deck. How many ways, Template:Mvar, can the cards be shuffled with at least 1 card being in the correct position?
Begin by defining set Template:Math, which is all of the orderings of cards with the Template:Mvarth card correct. Then the number of orders, Template:Mvar, with at least one card being in the correct position, Template:Mvar, is
Apply the principle of inclusion–exclusion,
Each value represents the set of shuffles having at least Template:Mvar values Template:Math in the correct position. Note that the number of shuffles with at least Template:Mvar values correct only depends on Template:Mvar, not on the particular values of . For example, the number of shuffles having the 1st, 3rd, and 17th cards in the correct position is the same as the number of shuffles having the 2nd, 5th, and 13th cards in the correct positions. It only matters that of the Template:Mvar cards, 3 were chosen to be in the correct position. Thus there are equal terms in the Template:Mvarth summation (see combination).
is the number of orderings having Template:Mvar elements in the correct position, which is equal to the number of ways of ordering the remaining Template:Math elements, or Template:Math. Thus we finally get:
A permutation where no card is in the correct position is called a derangement. Taking Template:Math to be the total number of permutations, the probability Template:Mvar that a random shuffle produces a derangement is given by a truncation to Template:Math terms of the Taylor expansion of Template:Math. Thus the probability of guessing an order for a shuffled deck of cards and being incorrect about every card is approximately Template:Math or Template:Math.
A special case
The situation that appears in the derangement example above occurs often enough to merit special attention.[6] Namely, when the size of the intersection sets appearing in the formulas for the principle of inclusion–exclusion depend only on the number of sets in the intersections and not on which sets appear. More formally, if the intersection has the same cardinality, say Template:Math, for every Template:Mvar-element subset Template:Mvar of Template:Math, then
Or, in the complementary form, where the universal set Template:Mvar has cardinality Template:Math,
Formula generalization
Given a family (repeats allowed) of subsets Template:Math of a universal set Template:Mvar, the principle of inclusion–exclusion calculates the number of elements of Template:Mvar in none of these subsets. A generalization of this concept would calculate the number of elements of Template:Mvar which appear in exactly some fixed Template:Mvar of these sets.
Let Template:Math. If we define , then the principle of inclusion–exclusion can be written as, using the notation of the previous section; the number of elements of Template:Mvar contained in none of the Template:Math is:
If Template:Mvar is a fixed subset of the index set Template:Mvar, then the number of elements which belong to Template:Math for all Template:Mvar in Template:Mvar and for no other values is:[7]
Define the sets
We seek the number of elements in none of the Template:Math which, by the principle of inclusion–exclusion (with ), is
The correspondence Template:Math between subsets of Template:Math and subsets of Template:Mvar containing Template:Mvar is a bijection and if Template:Mvar and Template:Mvar correspond under this map then Template:Mvar, showing that the result is valid.
In probability
In probability, for events Template:Math in a probability space , the inclusion–exclusion principle becomes for Template:Math for Template:Math and in general which can be written in closed form as where the last sum runs over all subsets Template:Mvar of the indices Template:Math which contain exactly Template:Mvar elements, and denotes the intersection of all those Template:Math with index in Template:Mvar.
According to the Bonferroni inequalities, the sum of the first terms in the formula is alternately an upper bound and a lower bound for the LHS. This can be used in cases where the full formula is too cumbersome.
For a general measure space Template:Math and measurable subsets Template:Math of finite measure, the above identities also hold when the probability measure is replaced by the measure Template:Mvar.
Special case
If, in the probabilistic version of the inclusion–exclusion principle, the probability of the intersection Template:Math only depends on the cardinality of Template:Mvar, meaning that for every Template:Mvar in Template:Math there is an Template:Math such that then the above formula simplifies to due to the combinatorial interpretation of the binomial coefficient . For example, if the events are independent and identically distributed, then for all Template:Mvar, and we have , in which case the expression above simplifies to
(This result can also be derived more simply by considering the intersection of the complements of the events .)
An analogous simplification is possible in the case of a general measure space and measurable subsets of finite measure.
There is another formula used in point processes. Let be a finite set and be a random subset of . Let be any subset of , then
Other formulas
The principle is sometimes stated in the form[8] that says that if then Template:NumBlk
The combinatorial and the probabilistic version of the inclusion–exclusion principle are instances of (Template:EquationNote). Template:Math proof
If one sees a number as a set of its prime factors, then (Template:EquationNote) is a generalization of Möbius inversion formula for square-free natural numbers. Therefore, (Template:EquationNote) is seen as the Möbius inversion formula for the incidence algebra of the partially ordered set of all subsets of Template:Mvar.
For a generalization of the full version of Möbius inversion formula, (Template:EquationNote) must be generalized to multisets. For multisets instead of sets, (Template:EquationNote) becomes Template:NumBlk where is the multiset for which , and
- Template:Math if Template:Mvar is a set (i.e. a multiset without double elements) of even cardinality.
- Template:Math if Template:Mvar is a set (i.e. a multiset without double elements) of odd cardinality.
- Template:Math if Template:Mvar is a proper multiset (i.e. Template:Mvar has double elements).
Notice that is just the of (Template:EquationNote) in case is a set.
Applications
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 Template:Mvar is a bijection from Template:Mvar into itself that has no fixed points. Via the inclusion–exclusion principle one can show that if the cardinality of Template:Mvar is Template:Mvar, then the number of derangements is Template:Math where Template:Math denotes the nearest integer to Template:Mvar; 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: Template:Lang by P. R. de Montmort (1678 – 1719) and was known as either "Montmort's problem" or by the name he gave it, "Template:Lang".[9] The problem is also known as the hatcheck problem.
The number of derangements is also known as the subfactorial of Template:Mvar, written Template:Math. It follows that if all bijections are assigned the same probability then the probability that a random bijection is a derangement quickly approaches Template:Math as Template:Mvar 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 represent the complement of Template:Math with respect to some universal set Template:Mvar such that for each Template:Mvar. Then we have 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.[10]
A well known application of the principle is the construction of the chromatic polynomial of a graph.[11]
Bipartite graph perfect matchings
The number of perfect matchings of a bipartite graph can be calculated using the principle.[12]
Number of onto functions
Given finite sets Template:Mvar and Template:Mvar, how many surjective functions (onto functions) are there from Template:Mvar to Template:Mvar? Without any loss of generality we may take Template:Math and Template:Math, since only the cardinalities of the sets matter. By using Template:Mvar as the set of all functions from Template:Mvar to Template:Mvar, and defining, for each Template:Mvar in Template:Mvar, the property Template:Math as "the function misses the element Template:Mvar in Template:Mvar" (Template:Mvar is not in the image of the function), the principle of inclusion–exclusion gives the number of onto functions between Template:Mvar and Template:Mvar as:[13]
Permutations with forbidden positions
A permutation of the set Template:Math where each element of Template:Mvar is restricted to not being in certain positions (here the permutation is considered as an ordering of the elements of Template:Mvar) is called a permutation with forbidden positions. For example, with Template:Math, 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 Template:Math be the set of positions that the element Template:Mvar is not allowed to be in, and the property Template:Math to be the property that a permutation puts element Template:Mvar into a position in Template:Math, the principle of inclusion–exclusion can be used to count the number of permutations which satisfy all the restrictions.[14]
In the given example, there are Template:Math permutations with property Template:Math, Template:Math permutations with property Template:Math, and no permutations have properties Template:Math or Template:Math as there are no restrictions for these two elements. The number of permutations satisfying the restrictions is thus:
The final 4 in this computation is the number of permutations having both properties Template:Math and Template:Math. There are no other non-zero contributions to the formula.
Stirling numbers of the second kind
The Stirling numbers of the second kind, Template:Math count the number of partitions of a set of Template:Mvar elements into Template:Mvar 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 Template:Mvar-set into Template:Mvar non-empty but distinguishable boxes (ordered non-empty subsets). Using the universal set consisting of all partitions of the Template:Mvar-set into Template:Mvar (possibly empty) distinguishable boxes, Template:Math, and the properties Template:Math meaning that the partition has box Template:Math empty, the principle of inclusion–exclusion gives an answer for the related result. Dividing by Template:Math to remove the artificial ordering gives the Stirling number of the second kind:[15]
Rook polynomials
A rook polynomial is the generating function of the number of ways to place non-attacking rooks on a board Template:Mvar 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 Template:Mvar is any subset of the squares of a rectangular board with Template:Mvar rows and Template:Mvar columns; we think of it as the squares in which one is allowed to put a rook. The coefficient, Template:Math of Template:Math in the rook polynomial Template:Math is the number of ways Template:Mvar rooks, none of which attacks another, can be arranged in the squares of Template:Mvar. For any board Template:Mvar, there is a complementary board consisting of the squares of the rectangular board that are not in Template:Mvar. This complementary board also has a rook polynomial with coefficients
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 Template:Math, so this coefficient is Template:Math. The number of ways to place Template:Mvar non-attacking rooks on the complete Template:Math "checkerboard" (without regard as to whether the rooks are placed in the squares of the board Template:Mvar) is given by the falling factorial:
Letting Template:Math be the property that an assignment of Template:Mvar non-attacking rooks on the complete board has a rook in column Template:Mvar which is not in a square of the board Template:Mvar, then by the principle of inclusion–exclusion we have:[16]
Euler's phi function
Euler's totient or phi function, Template:Math is an arithmetic function that counts the number of positive integers less than or equal to Template:Mvar that are relatively prime to Template:Mvar. That is, if Template:Mvar is a positive integer, then Template:Math is the number of integers Template:Mvar in the range Template:Math which have no common factor with Template:Mvar other than 1. The principle of inclusion–exclusion is used to obtain a formula for Template:Math. Let Template:Mvar be the set Template:Math and define the property Template:Math to be that a number in Template:Mvar is divisible by the prime number Template:Math, for Template:Math, where the prime factorization of
Then,[17]
Dirichlet hyperbola method

The Dirichlet hyperbola method re-expresses a sum of a multiplicative function by selecting a suitable Dirichlet convolution , recognizing that the sum can be recast as a sum over the lattice points in a region bounded by , , and , splitting this region into two overlapping subregions, and finally using the inclusion–exclusion principle to conclude that
Diluted inclusion–exclusion principle
In many cases where the principle could give an exact formula (in particular, counting prime numbers using the sieve of Eratosthenes), the formula arising does not offer useful content because the number of terms in it is excessive. If each term individually can be estimated accurately, the accumulation of errors may imply that the inclusion–exclusion formula is not directly applicable. In number theory, this difficulty was addressed by Viggo Brun. After a slow start, his ideas were taken up by others, and a large variety of sieve methods developed. These for example may try to find upper bounds for the "sieved" sets, rather than an exact formula.
Let Template:Math be arbitrary sets and Template:Math real numbers in the closed unit interval Template:Closed-closed. Then, for every even number Template:Mvar in Template:Math, the indicator functions satisfy the inequality:[18]
Proof of main statement
Choose an element contained in the union of all sets and let be the individual sets containing it. (Note that Template:Math.) Since the element is counted precisely once by the left-hand side of equation (Template:EquationNote), we need to show that it is counted precisely once by the right-hand side. On the right-hand side, the only non-zero contributions occur when all the subsets in a particular term contain the chosen element, that is, all the subsets are selected from . The contribution is one for each of these sets (plus or minus depending on the term) and therefore is just the (signed) number of these subsets used in the term. We then have:
By the binomial theorem,
Using the fact that and rearranging terms, we have and so, the chosen element is counted only once by the right-hand side of equation (Template:EquationNote).
Algebraic proof
An algebraic proof can be obtained using indicator functions (also known as characteristic functions). The indicator function of a subset Template:Mvar of a set Template:Mvar is the function
If and are two subsets of , then
Let Template:Mvar denote the union of the sets Template:Math. To prove the inclusion–exclusion principle in general, we first verify the identity Template:NumBlk for indicator functions, where:
The following function is identically zero because: if Template:Mvar is not in Template:Mvar, then all factors are Template:Math; and otherwise, if Template:Mvar does belong to some Template:Math, then the corresponding Template:Mvarth factor is Template:Math. By expanding the product on the left-hand side, equation (Template:EquationNote) follows.
To prove the inclusion–exclusion principle for the cardinality of sets, sum the equation (Template:EquationNote) over all Template:Mvar in the union of Template:Math. To derive the version used in probability, take the expectation in (Template:EquationNote). In general, integrate the equation (Template:EquationNote) with respect to Template:Mvar. Always use linearity in these derivations.
See also
- Template:Annotated link
- Template:Annotated link
- Template:Annotated link
- Template:Annotated link
- Template:Annotated link
- Template:Annotated link
Notes
References
- Template:Citation
- Template:Citation
- Template:Citation
- Template:Citation
- Template:Citation
- Template:Citation
- Template:Citation
- Template:Springer
- Template:Citation
- Template:Citation
- Template:Citation
- Template:Citation
- Template:Citation
Template:PlanetMath attribution
- ↑ 1.0 1.1 Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harvnb
- ↑ Template:Harv