{
  "aliases": null,
  "backlinks": null,
  "body": "\n## Proof\n\nWe apply the [[inclusion–exclusion principle|0FF7726C7P]] to the permutations that fix a given point.\n\n1. Let $X$ be a set with $n$ elements. Choose a bijection $\\beta : X \\to [n]$, where $[n] = \\set{1, \\dots, n}$.\n2. The map $\\sigma \\mapsto \\beta \\sigma \\beta^{-1}$ is a bijection from the [[permutations|0BQMWCNS8S]] of $X$ to those of $[n]$. Its inverse is $\\tau \\mapsto \\beta^{-1} \\tau \\beta$.\n3. We have $\\beta \\sigma \\beta^{-1}(\\beta(x)) = \\beta(x)$ exactly when $\\sigma(x) = x$. So this bijection sends [[derangements|0TH61JR04G]] to derangements, in both directions.\n4. By the [[bijection principle|0GV2573FP4]], it suffices to count the derangements of $[n]$.\n5. If $n = 0$, the only permutation of $[0] = \\varnothing$ is the empty map. It has no fixed point, so $D_0 = 1$. The formula gives $0! \\cdot \\frac{1}{0!} = 1$. From now on let $n \\ge 1$.\n6. Let $\\Sigma$ be the set of permutations of $[n]$. By the [[number of permutations|0CEBCS0E0E]], $|\\Sigma| = n!$.\n7. For $i \\in [n]$, let $A_i = \\set{\\sigma \\in \\Sigma : \\sigma(i) = i}$. A permutation is a derangement exactly when it lies in no $A_i$.\n8. So the derangements form the complement of $\\bigcup_{i=1}^{n} A_i$ in $\\Sigma$. By the [[complement principle|03QC59M99B]], $D_n = n! - \\left| \\bigcup_{i=1}^{n} A_i \\right|$.\n9. Let $I \\subseteq [n]$ have $k$ elements, and let $A_I = \\bigcap_{i \\in I} A_i$, the permutations that fix every point of $I$. We claim $|A_I| = (n - k)!$.\n10. Let $\\sigma \\in A_I$ and $x \\notin I$. If $\\sigma(x) = i \\in I$, then $\\sigma(x) = \\sigma(i)$ with $x \\ne i$, against injectivity. So $\\sigma$ maps $[n] \\setminus I$ into itself.\n11. This restriction is injective. It is also surjective: if $y \\notin I$ and $y = \\sigma(x)$, then $x \\notin I$, since otherwise $y = \\sigma(x) = x \\in I$.\n12. So restriction is a map from $A_I$ to the permutations of $[n] \\setminus I$. Its inverse extends a permutation of $[n] \\setminus I$ by the identity on $I$.\n13. Hence $A_I$ is in bijection with the permutations of an $(n - k)$-element set. By the number of permutations, $|A_I| = (n - k)!$.\n14. By the [[number of $k$-element subsets|0BFMR2FQ8B]], there are $\\binom{n}{k}$ sets $I$ with $k$ elements. By the [[factorial formula|033KTKKRV6]], $\\binom{n}{k} (n - k)! = \\frac{n!}{k!}$.\n15. The inclusion–exclusion principle, with terms grouped by $k = |I|$, gives\n$$\\left| \\bigcup_{i=1}^{n} A_i \\right| = \\sum_{k=1}^{n} (-1)^{k+1} \\binom{n}{k} (n - k)! = \\sum_{k=1}^{n} (-1)^{k+1} \\frac{n!}{k!}.$$\n16. By step 8, and since $n! = (-1)^0 \\frac{n!}{0!}$ by the definition of the [[factorial|0WFCGVMRRC]],\n$$D_n = n! - \\sum_{k=1}^{n} (-1)^{k+1} \\frac{n!}{k!} = \\sum_{k=0}^{n} (-1)^k \\frac{n!}{k!} = n! \\sum_{k=0}^{n} \\frac{(-1)^k}{k!}.$$\n\n## Idea\n\nA derangement is a permutation that avoids every set $A_i$ of permutations fixing $i$. The permutations that fix $k$ given points are just the permutations of the other $n - k$ points, so inclusion–exclusion has easy terms. They simplify to $(-1)^k n!/k!$.\n",
  "commit": "4a3290d299c8b2cd6493fce2296920ba7f82d942",
  "front": {
    "kind": "proof",
    "msc": [
      "05A05"
    ],
    "name": "Proof of the formula for the number of derangements by inclusion–exclusion",
    "proves": "03RSJXF728",
    "sources": [
      {
        "locator": "Derivation by inclusion–exclusion principle",
        "url": "https://en.wikipedia.org/wiki/Derangement"
      }
    ],
    "tag": "67S4PPN54V",
    "uses": [
      "0TH61JR04G",
      "0BQMWCNS8S",
      "0FF7726C7P",
      "0CEBCS0E0E",
      "0BFMR2FQ8B",
      "033KTKKRV6",
      "03QC59M99B",
      "0WFCGVMRRC",
      "0GV2573FP4"
    ]
  },
  "kind": "proof",
  "link": "https://math2.tamnd.com/node/67S4PPN54V/",
  "links": [
    {
      "field": "proves",
      "target": "03RSJXF728"
    },
    {
      "field": "uses",
      "target": "0TH61JR04G"
    },
    {
      "field": "uses",
      "target": "0BQMWCNS8S"
    },
    {
      "field": "uses",
      "target": "0FF7726C7P"
    },
    {
      "field": "uses",
      "target": "0CEBCS0E0E"
    },
    {
      "field": "uses",
      "target": "0BFMR2FQ8B"
    },
    {
      "field": "uses",
      "target": "033KTKKRV6"
    },
    {
      "field": "uses",
      "target": "03QC59M99B"
    },
    {
      "field": "uses",
      "target": "0WFCGVMRRC"
    },
    {
      "field": "uses",
      "target": "0GV2573FP4"
    }
  ],
  "markdown": "https://math2.tamnd.com/node/67S4PPN54V.md",
  "msc": [
    "05A05"
  ],
  "name": "Proof of the formula for the number of derangements by inclusion–exclusion",
  "prov": null,
  "role": "",
  "source": "https://github.com/tamnd/math2/blob/main/nodes/67S/67S4PPN54V.md",
  "status": null,
  "tag": "67S4PPN54V",
  "uri": "m2://67S4PPN54V",
  "url": "https://math2.tamnd.com/wiki/proof-of-the-formula-for-the-number-of-derangements-by-inclusion-exclusion/",
  "writing": {
    "tag": "67S4PPN54V",
    "kind": "proof",
    "level": "W0",
    "next": [
      {
        "what": "an inventory row",
        "note": "the row of the inventory that the node accounts for",
        "have": 0,
        "want": 1
      }
    ]
  }
}
