{
  "id": "inclusion-exclusion-principle",
  "title": "Inclusion–Exclusion Principle",
  "lang": "en",
  "source_lang": "en",
  "summary": "The inclusion–exclusion principle counts unions of overlapping sets by alternately adding and subtracting the sizes of their intersections, with corresponding formulas for probabilities and measures.",
  "category": "mathematics",
  "subcategory": "discrete-logic",
  "type": "concept",
  "aliases": [
    "Principle of inclusion and exclusion",
    "inclusion–exclusion formula",
    "sieve principle"
  ],
  "generated_at": "2026-11-04",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/inclusion-exclusion-principle/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/inclusion-exclusion-principle/",
      "json": "https://aiwiki.page/zh/inclusion-exclusion-principle.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "The inclusion–exclusion principle is a fundamental identity in combinatorics that determines the size of a union of finite sets from the sizes of their intersections. Adding the sizes of the individual sets counts shared elements more than once; subtracting pairwise intersections corrects some of this overcounting, but requires further corrections for higher-order intersections. The resulting alternating sum counts each element of the union exactly once. The same identity applies to probabilities, finite measures, and weighted counting."
    },
    {
      "heading": "Basic formulas",
      "text": "For two finite sets \\(A\\) and \\(B\\),\n\n\\[|A\\cup B|=|A|+|B|-|A\\cap B|,\\]\n\nwhere \\(|A|\\) denotes the cardinality of \\(A\\). An element belonging to both sets appears twice in \\(|A|+|B|\\), so subtracting \\(|A\\cap B|\\) leaves one copy.\n\nFor three finite sets,\n\n\\[\\begin{aligned} |A\\cup B\\cup C| ={}&|A|+|B|+|C|\\\\ &-|A\\cap B|-|A\\cap C|-|B\\cap C|\\\\ &+|A\\cap B\\cap C|. \\end{aligned}\\]\n\nAn element in all three sets is initially counted three times and then subtracted three times. The final term restores its contribution to one.\n\nFor example, suppose three clubs have 30, 25, and 20 members. Their pairwise intersections have 10, 8, and 5 members, and 3 people belong to all three clubs. The number belonging to at least one club is\n\n\\[30+25+20-10-8-5+3=55.\\]\n\nHere each pairwise intersection includes the people who also belong to the third club; it does not mean membership in exactly those two clubs."
    },
    {
      "heading": "General statement",
      "text": "Let \\(A_1,\\ldots,A_n\\) be finite sets, with \\(n\\geq 1\\), and write \\([n]=\\{1,\\ldots,n\\}\\). Then\n\n\\[\\left|\\bigcup_{i=1}^{n}A_i\\right| = \\sum_{\\varnothing\\neq I\\subseteq[n]} (-1)^{|I|+1} \\left|\\bigcap_{i\\in I}A_i\\right|.\\]\n\nEquivalently, grouping terms by the number of sets intersected,\n\n\\[\\left|\\bigcup_{i=1}^{n}A_i\\right| = \\sum_{k=1}^{n}(-1)^{k+1} \\sum_{1\\leq i_1<\\cdots<i_k\\leq n} |A_{i_1}\\cap\\cdots\\cap A_{i_k}|.\\]\n\nThus single-set counts are added, two-set intersections are subtracted, three-set intersections are added, and the signs continue alternating.\n\nA closely related formula counts objects that avoid every listed property. If all \\(A_i\\) are subsets of a finite universe \\(U\\), then\n\n\\[\\left|U\\setminus\\bigcup_{i=1}^{n}A_i\\right| = \\sum_{I\\subseteq[n]} (-1)^{|I|} \\left|\\bigcap_{i\\in I}A_i\\right|,\\]\n\nwith the convention\n\n\\[\\bigcap_{i\\in\\varnothing}A_i=U.\\]\n\nThe empty-index term is therefore \\(|U|\\). This formulation is often used by treating \\(A_i\\) as the set of objects violating the \\(i\\)-th requirement."
    },
    {
      "heading": "Why the formula works",
      "text": "Counting the contribution of one element\n\nSuppose an element belongs to exactly \\(r\\geq 1\\) of the sets. It belongs to precisely \\(\\binom{r}{k}\\) of their \\(k\\)-fold intersections, where \\(\\binom{r}{k}\\) is a binomial coefficient. Its total contribution to the alternating sum is\n\n\\[\\sum_{k=1}^{r}(-1)^{k+1}\\binom{r}{k}=1.\\]\n\nThis follows from the binomial theorem:\n\n\\[0=(1-1)^r =\\sum_{k=0}^{r}(-1)^k\\binom{r}{k}.\\]\n\nEvery element in the union contributes one, and every element outside it contributes zero.\n\nIndicator-function identity\n\nAn equivalent pointwise statement uses the indicator function \\(\\mathbf{1}_A\\), which equals one on \\(A\\) and zero elsewhere:\n\n\\[\\mathbf{1}_{\\bigcup_{i=1}^{n}A_i} = 1-\\prod_{i=1}^{n}(1-\\mathbf{1}_{A_i}).\\]\n\nExpanding the finite product gives\n\n\\[\\mathbf{1}_{\\bigcup_{i=1}^{n}A_i} = \\sum_{\\varnothing\\neq I\\subseteq[n]} (-1)^{|I|+1} \\mathbf{1}_{\\bigcap_{i\\in I}A_i}.\\]\n\nSumming over a finite universe proves the counting formula. Taking expectations or integrating proves the probability and measure versions. This explains why the principle is not confined to counting objects."
    },
    {
      "heading": "Probability and measure",
      "text": "For events \\(A_1,\\ldots,A_n\\) in a probability space,\n\n\\[\\Pr\\left(\\bigcup_{i=1}^{n}A_i\\right) = \\sum_{\\varnothing\\neq I\\subseteq[n]} (-1)^{|I|+1} \\Pr\\left(\\bigcap_{i\\in I}A_i\\right).\\]\n\nThe left-hand side is the probability that at least one event occurs. The identity does not require independence. Independence only provides an additional way to evaluate intersection probabilities.\n\nIf the events are mutually independent, the complementary-event form simplifies to\n\n\\[\\Pr\\left(\\bigcup_{i=1}^{n}A_i\\right) = 1-\\prod_{i=1}^{n}\\bigl(1-\\Pr(A_i)\\bigr).\\]\n\nPairwise independence alone does not generally justify this product when there are three or more events.\n\nIn measure theory, the same alternating formula holds for measurable sets of finite measure. The finite-measure condition avoids undefined expressions involving subtraction of infinite quantities. More generally, integrating the indicator identity against an integrable weight yields weighted inclusion–exclusion."
    },
    {
      "heading": "Partial sums and Bonferroni inequalities",
      "text": "Calculating every intersection may be impractical. Truncating the alternating sum nevertheless gives rigorous bounds, called Bonferroni inequalities.\n\nFor events \\(A_1,\\ldots,A_n\\), define\n\n\\[S_k= \\sum_{1\\leq i_1<\\cdots<i_k\\leq n} \\Pr(A_{i_1}\\cap\\cdots\\cap A_{i_k}),\\]\n\nand\n\n\\[T_m=\\sum_{k=1}^{m}(-1)^{k+1}S_k.\\]\n\nFor \\(1\\leq m\\leq n\\),\n\n\\[\\begin{cases} \\displaystyle \\Pr\\left(\\bigcup_i A_i\\right)\\leq T_m, & m\\text{ odd},\\\\[6pt] \\displaystyle \\Pr\\left(\\bigcup_i A_i\\right)\\geq T_m, & m\\text{ even}. \\end{cases}\\]\n\nThe first two bounds are\n\n\\[S_1-S_2 \\leq \\Pr\\left(\\bigcup_i A_i\\right) \\leq S_1.\\]\n\nThe upper bound \\(\\Pr(\\bigcup_i A_i)\\leq\\sum_i\\Pr(A_i)\\) is commonly called the union bound. It underlies the Bonferroni correction used in multiple hypothesis testing.\n\nThe direction of the bounds follows element by element. For an outcome lying in exactly \\(r\\) events, the truncated contribution is\n\n\\[\\sum_{k=1}^{m}(-1)^{k+1}\\binom{r}{k} = 1-(-1)^m\\binom{r-1}{m},\\]\n\nwhere a binomial coefficient is zero when its lower index exceeds its nonnegative upper index. The error is nonnegative for odd \\(m\\) and nonpositive for even \\(m\\). Analogous bounds hold for finite-set cardinalities and finite measures."
    },
    {
      "heading": "Counting exactly a specified number of properties",
      "text": "Inclusion–exclusion can also recover the number of objects belonging to exactly \\(r\\) sets.\n\nFor subsets \\(A_1,\\ldots,A_n\\) of a finite universe \\(U\\), let\n\n\\[S_k= \\sum_{\\substack{I\\subseteq[n]\\\\|I|=k}} \\left|\\bigcap_{i\\in I}A_i\\right|, \\qquad S_0=|U|.\\]\n\nIf \\(N_r\\) counts objects belonging to exactly \\(r\\) of the sets, then\n\n\\[N_r= \\sum_{k=r}^{n} (-1)^{k-r}\\binom{k}{r}S_k, \\qquad 0\\leq r\\leq n.\\]\n\nAn object belonging to exactly \\(j\\) sets contributes \\(\\binom{j}{k}\\) to \\(S_k\\). The alternating coefficients cancel this contribution unless \\(j=r\\), in which case it contributes one."
    },
    {
      "heading": "Applications",
      "text": "Permutations without fixed points\n\nA derangement is a permutation with no fixed points. For permutations of \\(n\\) objects, let \\(A_i\\) consist of those fixing object \\(i\\). Fixing any specified \\(k\\) objects leaves \\((n-k)!\\) permutations, using the factorial notation.\n\nInclusion–exclusion gives the number \\(D_n\\) of derangements:\n\n\\[D_n= \\sum_{k=0}^{n}(-1)^k\\binom{n}{k}(n-k)! = n!\\sum_{k=0}^{n}\\frac{(-1)^k}{k!}.\\]\n\nConsequently,\n\n\\[\\frac{D_n}{n!}\\longrightarrow e^{-1},\\]\n\nwhere \\(e\\) is Euler’s number. Thus the probability that a uniformly chosen permutation has no fixed points approaches \\(1/e\\).\n\nSurjective functions\n\nConsider functions from an \\(m\\)-element set to an \\(n\\)-element set, with \\(m,n\\geq1\\). A surjective function uses every target value.\n\nLet \\(A_i\\) be the set of functions omitting target value \\(i\\). Omitting a specified collection of \\(k\\) target values leaves \\((n-k)^m\\) functions. Therefore the number of surjections is\n\n\\[\\sum_{k=0}^{n}(-1)^k\\binom{n}{k}(n-k)^m.\\]\n\nThis equals \\(n!\\) times the number of partitions of the domain into \\(n\\) nonempty blocks.\n\nDivisibility and sieving\n\nIn number theory, take \\(A_i\\) to be the integers from \\(1\\) to \\(N\\) divisible by a specified prime \\(p_i\\). For distinct primes,\n\n\\[\\left|\\bigcap_{i\\in I}A_i\\right| = \\left\\lfloor \\frac{N}{\\prod_{i\\in I}p_i} \\right\\rfloor.\\]\n\nThe complement formula counts integers divisible by none of these primes. This is an elementary model of a sieve: unwanted objects are excluded through alternating corrections for overlapping divisibility conditions.\n\nGraph coloring\n\nFor a finite simple graph \\(G=(V,E)\\), consider all assignments of \\(q\\) colors to its vertices. For each edge, define a bad event in which its endpoints receive the same color.\n\nIf a collection \\(F\\subseteq E\\) of these equalities is imposed, all vertices in each connected component of \\((V,F)\\) must have one common color. There are \\(q^{c(F)}\\) such assignments, where \\(c(F)\\) includes isolated vertices.\n\nInclusion–exclusion therefore gives\n\n\\[P_G(q)=\\sum_{F\\subseteq E}(-1)^{|F|}q^{c(F)}.\\]\n\nFor positive integers \\(q\\), this counts proper colorings. It also exhibits the count as a polynomial, called the chromatic polynomial, an important invariant in graph theory."
    },
    {
      "heading": "Algebraic interpretation",
      "text": "Inclusion–exclusion is a special case of Möbius inversion on the collection of subsets of a finite set, ordered by inclusion.\n\nIf functions \\(f\\) and \\(g\\) satisfy\n\n\\[g(S)=\\sum_{T\\subseteq S}f(T),\\]\n\nthen\n\n\\[f(S)=\\sum_{T\\subseteq S}(-1)^{|S|-|T|}g(T).\\]\n\nThe coefficient \\((-1)^{|S|-|T|}\\) is the Möbius function of the subset order. This viewpoint places inclusion–exclusion within a broader theory of inverting cumulative sums on partially ordered sets."
    },
    {
      "heading": "Computational and infinite-family limitations",
      "text": "For \\(n\\) sets, the full formula contains \\(2^n-1\\) nonempty intersection terms. Direct evaluation therefore has exponential cost in the number of sets, even when each intersection count is readily available. Symmetry, empty intersections, or shared structure can reduce the work substantially.\n\nThe identity also requires higher-order overlap information. Individual set sizes and pairwise intersections generally do not determine a union when three or more sets are involved. Numerically, subtraction of large, nearly equal terms can cause loss of precision.\n\nThe finite identity should not be extended to an infinite alternating sum without checking convergence. For countably many events, a reliable formulation is\n\n\\[\\Pr\\left(\\bigcup_{i=1}^{\\infty}A_i\\right) = \\lim_{n\\to\\infty} \\Pr\\left(\\bigcup_{i=1}^{n}A_i\\right).\\]\n\nEach finite-union probability can be evaluated by inclusion–exclusion. Reorganizing the resulting limit into a series grouped by intersection order requires additional justification, such as suitable absolute-convergence conditions."
    }
  ],
  "links": [
    "combinatorics",
    "probability",
    "cardinality",
    "subset",
    "binomial-coefficient",
    "binomial-theorem",
    "indicator-function",
    "probability-space",
    "statistical-independence",
    "measure-theory",
    "bonferroni-correction",
    "permutation",
    "factorial",
    "e-mathematical-constant",
    "surjective-function",
    "set-partition",
    "number-theory",
    "polynomial",
    "graph-theory",
    "partial-order"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}