
Incombinatorics, theinclusion–exclusion principle is a counting technique which generalizes the familiar method of obtaining the number of elements in theunion of twofinite sets; symbolically expressed as
whereA andB are two finite sets and |S| indicates thecardinality of a setS (which may be considered as the number of elements of the set, if the set isfinite). 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 theintersection 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 setsA,B andC is given by
This formula can be verified by counting how many times each region in theVenn 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 ofn sets:
The name comes from the idea that the principle is based on over-generousinclusion, followed by compensatingexclusion.This concept is attributed toAbraham de Moivre (1718),[1] although it first appears in a paper ofDaniel da Silva (1854)[2] and later in a paper byJ. 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 thesieve method extensively used innumber theory and is sometimes referred to as thesieve formula.[4]
As finite probabilities are computed as counts relative to the cardinality of theprobability 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 ofmeasure 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. AsGian-Carlo Rota put it:[6]
"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."
In its general formula, the principle of inclusion–exclusion states that for finite setsA1, ...,An, one has the identity
| 1 |

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, lettingS be a finiteuniversal set containing all of theAi and letting denote the complement ofAi inS, byDe Morgan's laws we have
As another variant of the statement, letP1, ...,Pn be a list of properties that elements of a setS may or may not have, then the principle of inclusion–exclusion provides a way to calculate the number of elements ofS that have none of the properties. Just letAi be the subset of elements ofS which have the propertyPi and use the principle in its complementary form. This variant is due toJ. J. Sylvester.[1]
Notice that if you take into account only the firstm<n sums on the right (in the general form of the principle), then you will get an overestimate ifm is odd and an underestimate ifm is even.
A more complex example is the following.
Suppose there is a deck ofn cards numbered from 1 to n. Suppose a card numberedm is in the correct position if it is themth card in the deck. How many ways,W, can the cards be shuffled with at least 1 card being in the correct position?
Begin by defining setAm, which is all of the orderings of cards with themth card correct. Then the number of orders,W, withat least one card being in the correct position,m, is
Apply the principle of inclusion–exclusion,
Each value represents the set of shuffles having at leastp valuesm1, ..., mp in the correct position. Note that the number of shuffles with at leastp values correct only depends onp, 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 then cards, 3 were chosen to be in the correct position. Thus there are equal terms in thepth summation (seecombination).
is the number of orderings havingp elements in the correct position, which is equal to the number of ways of ordering the remainingn − p elements, or (n − p)!. Thus we finally get:
A permutation whereno card is in the correct position is called aderangement. Takingn! to be the total number of permutations, the probabilityQ that a random shuffle produces a derangement is given by
a truncation ton + 1 terms of theTaylor expansion ofe−1. Thus the probability of guessing an order for a shuffled deck of cards and being incorrect about every card is approximatelye−1 or 37%.
The situation that appears in the derangement example above occurs often enough to merit special attention.[7] 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αk = |AJ|, for everyk-element subsetJ of {1, ..., n}, then
Or, in the complementary form, where the universal setS has cardinalityα0,
Given afamily (repeats allowed) of subsetsA1,A2, ...,An of a universal setS, the principle of inclusion–exclusion calculates the number of elements ofS in none of these subsets. A generalization of this concept would calculate the number of elements ofS which appear in exactly some fixedm of these sets.
LetN = [n] = {1,2,...,n}. If we define, then the principle of inclusion–exclusion can be written as, using the notation of the previous section; the number of elements ofS contained in none of theAi is:
IfI is a fixed subset of the index setN, then the number of elements which belong toAi for alli inI and for no other values is:[8]
Define the sets
We seek the number of elements in none of theBk which, by the principle of inclusion–exclusion (with), is
The correspondenceK ↔J =I ∪K between subsets ofN \ I and subsets ofN containingI is a bijection and ifJ andK correspond under this map thenBK =AJ, showing that the result is valid.
Inprobability, for eventsA1, ...,An in aprobability space, the inclusion–exclusion principle becomes forn = 2
forn = 3
and in general
which can be written in closed form as
where the last sum runs over all subsetsI of the indices 1, ...,n which contain exactlyk elements, and
denotes the intersection of all thoseAi with index inI.
According to theBonferroni inequalities, the sum of the first terms in the formula is alternately an upper bound and a lower bound for theLHS. This can be used in cases where the full formula is too cumbersome.
For a generalmeasure space (S,Σ,μ) andmeasurable subsetsA1, ...,An offinite measure, the above identities also hold when the probability measure is replaced by the measureμ.
If, in the probabilistic version of the inclusion–exclusion principle, the probability of the intersectionAI only depends on the cardinality ofI, meaning that for everyk in {1, ..., n} there is anak such that
then the above formula simplifies to
due to the combinatorial interpretation of thebinomial coefficient. For example, if the events areindependent and identically distributed, then for alli, 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 inpoint processes. Let be a finite set and be a random subset of. Let be any subset of, then
The principle is sometimes stated in the form[9] that says that if
then
| 2 |
The combinatorial and the probabilistic version of the inclusion–exclusion principle are instances of (2).
Take,, and
respectively for allsets with. Then we obtain
respectively for all sets with. This is becauseelements of can becontained in other ( with) as well, and the-formula runs exactly through all possible extensions of the sets with other, counting only for the set that matches the membership behavior of, if runs through allsubsets of (as in the definition of).
Since, we obtain from (2) with that
and by interchanging sides, the combinatorial and the probabilistic version of the inclusion–exclusion principle follow.
If one sees a number as a set of its prime factors, then (2) is a generalization ofMöbius inversion formula forsquare-freenatural numbers. Therefore, (2) is seen as the Möbius inversion formula for theincidence algebra of thepartially ordered set of all subsets ofA.
For a generalization of the full version of Möbius inversion formula, (2) must be generalized tomultisets. For multisets instead of sets, (2) becomes
| 3 |
where is the multiset for which, and
Notice that is just the of (2) in case is a set.
Substituteon the right hand side of (3). Notice that appears once on both sides of (3). So we must show that for all with, the terms cancel out on the right hand side of (3). For that purpose, take a fixed such that and take an arbitrary fixed such that.
Notice that must be a set for eachpositive ornegative appearance of on the right hand side of (3) that is obtained by way of the multiset such that. Now each appearance of on the right hand side of (3) that is obtained by way of such that is a set that contains cancels out with the one that is obtained by way of the corresponding such that is a set that does not contain. This gives the desired result.
The inclusion–exclusion principle is widely used and only a few of its applications can be mentioned here.
A well-known application of the inclusion–exclusion principle is to the combinatorial problem of counting allderangements of a finite set. Aderangement of a setA is abijection fromA into itself that has no fixed points. Via the inclusion–exclusion principle one can show that if the cardinality ofA isn, then the number of derangements is [n! / e] where [x] denotes thenearest integer tox; a detailed proof is availablehere and also seethe 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."[10] The problem is also known as thehatcheck problem.
The number of derangements is also known as thesubfactorial ofn, 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 asn grows.
The principle of inclusion–exclusion, combined withDe Morgan's law, can be used to count the cardinality of the intersection of sets as well. Let represent the complement ofAk with respect to some universal setA such that for eachk. Then we have
thereby turning the problem of finding an intersection into the problem of finding a union.
The inclusion exclusion principle forms the basis of algorithms for a number of NP-hard graph partitioning problems, such asgraph coloring.[11]
A well known application of the principle is the construction of thechromatic polynomial of a graph.[12]
The number ofperfect matchings of abipartite graph can be calculated using the principle.[13]
Given finite setsA andB, how manysurjective functions (onto functions) are there fromA toB?Without any loss of generality we may takeA = {1, ...,k} andB = {1, ...,n}, since only the cardinalities of the sets matter. By usingS as the set of allfunctions fromA toB, and defining, for eachi inB, the propertyPi as "the function misses the elementi inB" (i is not in theimage of the function), the principle of inclusion–exclusion gives the number of onto functions betweenA andB as:[14]
Apermutation of the setS = {1, ...,n} where each element ofS is restricted to not being in certain positions (here the permutation is considered as an ordering of the elements ofS) is called apermutation with forbidden positions. For example, withS = {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 lettingAi be the set of positions that the elementi is not allowed to be in, and the propertyPi to be the property that a permutation puts elementi into a position inAi, the principle of inclusion–exclusion can be used to count the number of permutations which satisfy all the restrictions.[15]
In the given example, there are 12 = 2(3!) permutations with propertyP1, 6 = 3! permutations with propertyP2 and no permutations have propertiesP3 orP4 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 propertiesP1 andP2. There are no other non-zero contributions to the formula.
TheStirling numbers of the second kind,S(n,k) count the number ofpartitions of a set ofn elements intok non-empty subsets (indistinguishableboxes). 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 ann-set intok non-empty but distinguishable boxes (ordered non-empty subsets). Using the universal set consisting of all partitions of then-set intok (possibly empty) distinguishable boxes,A1,A2, ...,Ak, and the propertiesPi meaning that the partition has boxAi empty, the principle of inclusion–exclusion gives an answer for the related result. Dividing byk! to remove the artificial ordering gives the Stirling number of the second kind:[16]
Arook polynomial is thegenerating function of the number of ways to place non-attackingrooks on aboard B that looks like a subset of the squares of acheckerboard; that is, no two rooks may be in the same row or column. The boardB is any subset of the squares of a rectangular board withn rows andm columns; we think of it as the squares in which one is allowed to put a rook. Thecoefficient,rk(B) ofxk in the rook polynomialRB(x) is the number of waysk rooks, none of which attacks another, can be arranged in the squares ofB. For any boardB, there is a complementary board consisting of the squares of the rectangular board that are not inB. 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 thatn ≤m, so this coefficient isrn(B). The number of ways to placen non-attacking rooks on the completen ×m "checkerboard" (without regard as to whether the rooks are placed in the squares of the boardB) is given by thefalling factorial:
LettingPi be the property that an assignment ofn non-attacking rooks on the complete board has a rook in columni which is not in a square of the boardB, then by the principle of inclusion–exclusion we have:[17]
Euler's totient or phi function,φ(n) is anarithmetic function that counts the number of positive integers less than or equal ton that arerelatively prime ton. That is, ifn is apositive integer, then φ(n) is the number of integersk in the range 1 ≤k ≤n which have no common factor withn other than 1. The principle of inclusion–exclusion is used to obtain a formula for φ(n). LetS be the set {1, ...,n} and define the propertyPi to be that a number inS is divisible by the prime numberpi, for 1 ≤i ≤r, where theprime factorization of
Then,[18]

TheDirichlet hyperbola method re-expresses a sum of amultiplicative function by selecting a suitableDirichlet convolution, recognizing that the sum
can be recast as a sum over thelattice points in a region bounded by,, and, splitting this region into two overlapping subregions, and finally using the inclusion–exclusion principle to conclude that
In many cases where the principle could give an exact formula (in particular, countingprime numbers using thesieve 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. Innumber theory, this difficulty was addressed byViggo Brun. After a slow start, his ideas were taken up by others, and a large variety ofsieve methods developed. These for example may try to find upper bounds for the "sieved" sets, rather than an exact formula.
LetA1, ...,An be arbitrary sets andp1, ...,pn real numbers in the closed unit interval[0, 1]. Then, for every even numberk in {0, ...,n}, theindicator functions satisfy the inequality:[19]
Choose an element contained in the union of all sets and let be the individual sets containing it. (Note thatt > 0.) Since the element is counted precisely once by the left-hand side of equation (1), 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 thebinomial 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 (1).
An algebraic proof can be obtained usingindicator functions (also known as characteristic functions). The indicator function of a subsetS of a setX is the function
If and are two subsets of, then
LetA denote the union of the setsA1, ...,An. To prove the inclusion–exclusion principle in general, we first verify the identity
| 4 |
for indicator functions, where:
The following function
is identically zero because: ifx is not inA, then all factors are 0−0 = 0; and otherwise, ifx does belong to someAm, then the correspondingmth factor is 1−1=0. By expanding the product on the left-hand side, equation (4) follows.
To prove the inclusion–exclusion principle for the cardinality of sets, sum the equation (4) over allx in the union ofA1, ...,An. To derive the version used in probability, take theexpectation in (4). In general,integrate the equation (4) with respect to μ. Always use linearity in these derivations.
This article incorporates material from principle of inclusion–exclusion onPlanetMath, which is licensed under theCreative Commons Attribution/Share-Alike License.