Skip to the Main Content

Note:These pages make extensive use of the latest XHTML and CSS Standards. They ought to look great in any standards-compliant modern browser. Unfortunately, they will probably look horrible in older browsers, like Netscape 4.x and IE 4.x. Moreover, many posts use MathML, which is, currently only supported in Mozilla. My best suggestion (and you will thank me when surfing an ever-increasing number of sites on the web which have been crafted to use the new standards) is to upgrade to the latest version of your browser. If that's not possible, consider moving to the Standards-compliant and open-source Mozilla browser.

June 22, 2025

Counting with Categories (Part 1)

Posted by John Baez

These are some lecture notes for a 412\frac{1}{2}-hour minicourse I’m teaching at the Summer School on Algebra at the Zografou campus of the National Technical University of Athens. To save time, I am omitting the pictures I’ll draw on the whiteboard, along with various jokes and profoundly insightful remarks. This is just the structure of the talk, with all the notation and calculations.

Long-time readers of the nn-Category Café may find little new in this post. I’ve been meaning to write a sprawling book on combinatorics using categories, but here I’m trying to explain the use of species and illustrate them with a nontrivial example in less than 1.5 hours. That leaves three hours to go deeper.

Part 2 is here, and Part 3 is here.

Species and their generating functions

Combinatorics, or at least part of it, is the art of counting. For example: how many derangements does a set with nn elements have? A derangement is a bijection

f:SS f: S \to S

with no fixed points, i.e. no points xx with f(x)=xf(x) = x. There is just one derangement of S={0,1}S = \{0,1\}, namely the function ff with

f(0)=1,f(1)=0 f(0) = 1, \qquad f(1) = 0

There are 2 derangements of S={0,1,2}S = \{0,1,2\} — can you see what they are? How many derangements are there of a 5-element set? We’ll see the answer soon. This is a typical sort of combinatorics question.

But what does counting have to do with category theory? The category of finite sets, FinSet\mathsf{FinSet}, has

  • finite sets as objects
  • functions between these as morphisms

What we count, ultimately, are finite sets. Any object SFinSetS \in \mathsf{FinSet} has a cardinality |S|,{|S|}\in \mathbb{N}, so counting a finite set simplifies it to natural number, and the key feature of this process is that

ST|S|=|T| S \cong T \iff {|S|}= {|T|}

We’ll count structures on finite sets. A ‘species’ is roughly a type of structure we can put on finite sets.

Example 1. There is a species of derangements, called DD. A DD-structure on a finite set SS is simply a derangement f:SS.f: S \to S. We write D(S)D(S) for the set of all derangements of S.S. We would like to know |D(S)| {|D(S)|} for all SFinSet.S \in \mathsf{FinSet}.

Example 2. There is a species of permutations, called PP. A PP-structure on SFinSetS \in \mathsf{FinSet} is a bijection f:SSf: S \to S. We know

|P(S)|=|S|! {|P(S)|}= {|S|}!

We often use nn interchangeably for a natural number and a standard finite set with that many elements:

n={0,1,,n1}n = \{0,1, \dots, n-1 \}

With this notation

|P(n)|=n! {|P(n)|}= n!

But what exactly is a species?

Definition. Let E\mathsf{E} be the category where

  • an object is a finite set
  • a morphism is a bijection between finite sets

Definition. A species is a functor F:ESet.F: \mathsf{E} \to \mathsf{Set}. A tame species is a functor F:EFinSet.F: \mathsf{E} \to \mathsf{FinSet}.

Any tame species has a generating function, which is actually a formal power series F^[[x]],\widehat{F} \in \mathbb{R}[[x]], given by

F^= n0|F(n)|n!x n \displaystyle{ \widehat{F} = \sum_{n \ge 0} \frac{ {|F(n)|}}{n!} x^n }

Example 3. We can compute the generating function of the species of permutations:

P^= n0n!n!x n=11x \displaystyle{ \widehat{P} = \sum_{n \ge 0} \frac{n!}{n!} x^n = \frac{1}{1 - x} }

Example 4. Given two species FF and GG there is a species FGF \cdot G defined as follows. To put an FGF \cdot G-structure on a finite set SS is to choose a subset TST \subseteq S and put an FF-structure on TT and a GG-structure on STS - T. We thus have

FG^= n0|(FG)(n)|n!x n \widehat{F \cdot G} = \sum_{n \ge 0} \frac{ {|(F \cdot G)(n)|}}{n!} x^n

= n0 0kn(nk)|F(k)||G(nk)|x nn! = \sum_{n \ge 0} \sum_{0 \le k \le n} \binom{n}{k} {|F(k)|} {|G(n-k)|} \frac{x^n}{n!}

= n0 k0|F(k)|k!|G(nk)|(nk)!x n = \sum_{n \ge 0} \sum_{k \ge 0} \frac{ {|F(k)|}}{k!} \frac{|G(n-k)|}{(n-k)!} x^n

= k0 0|F(k)|k!|G(|!x kx = \sum_{k \ge 0} \sum_{\ell \ge 0} \frac{ {|F(k)|}}{k!} \frac{ {|G(\ell|}}{\ell!} x^k x^\ell

=F^G^ = \widehat{F} \widehat{G}

Example 5. There is a species Exp\text{Exp} such that every finite set has a unique Exp\text{Exp}-structure! We thus have

|Exp(S)|=1 {|\text{Exp}(S)|}= 1

for all SFinSetS \in \mathsf{FinSet}, so

Exp^= n01n!x n=expx \widehat{Exp} = \sum_{n \ge 0} \frac{1}{n!} x^n = \exp x

That’s why we call this boring structure an Exp\text{Exp}-structure. I like to call Exp\text{Exp} being a finite set. Every finite set has the structure of being a finite set in exactly one way.

Example 6. Recall that a DD-structure on a finite set is a derangement of that finite set. To choose a permutation f:SSf: S \to S of a finite set SS is the same as to choose a subset TST \subset S, which will be the set of fixed points of ff, and to choose a derangement of STS - T. Thus by Example 4 we have

PExpD P \cong \text{Exp} \cdot D

and also

|P|=|Exp||D| {|P|}= {|\text{Exp}|}\cdot {|D|}

so by Example 3 and Example 5 we have

11x=exp(x)|D| \frac{1}{1 - x} = \exp(x) {|D|}

so

|D|=e x1x {|D|}= \frac{e^{-x}}{1 - x}

or

n0|D(n)|n!x n=e x1x \sum_{n \ge 0} \frac{ {|D(n)|}}{n!} x^n = \frac{e^{-x}}{1 - x}

=(1x+x 22!x 33!+x 44!)(1+x+x 2+x 3+x 4+) = \left(1 - x + \frac{x^2}{2!} - \frac{x^3}{3!} + \frac{x^4}{4!} - \cdots \right)(1 + x + x^2 + x^3 + x^4 + \cdots )

From this it’s easy to work out |D(n)| {|D(n)|}. I’ll do the example of n=5n = 5. If you think about the coefficient of x 5x^5 in the above product, you’ll see it’s

11+12!13!+14!15! 1 - 1 + \frac{1}{2!} - \frac{1}{3!} + \frac{1}{4!} - \frac{1}{5!}

So we must have

|D(5)|5!=12!13!+14!15! \frac{ {|D(5)|}}{5!} = \frac{1}{2!} - \frac{1}{3!} + \frac{1}{4!} - \frac{1}{5!}

or

|D(5)| = 5!(12!13!+14!15!) = 34545+51 = 6020+51 = 44 \begin{array}{ccl} {|D(5)|}&=& 5! \left( \frac{1}{2!} - \frac{1}{3!} + \frac{1}{4!} - \frac{1}{5!} \right) \\ &=& 3 \cdot 4 \cdot 5 - 4 \cdot 5 + 5 - 1 \\ &=& 60 - 20 + 5 - 1 \\ &=& 44 \end{array}

In general we see

|D(n)|=n!(10!11!+12!13!+14!+(1) nn!) {|D(n)|}= n! \left(\frac{1}{0!} - \frac{1}{1!} + \frac{1}{2!} - \frac{1}{3!} + \frac{1}{4!} - \cdots + \frac{(-1)^{n} }{n!} \right)

But we can go further with this… and I’ll ask you to go further in this homework:

Exercise 1. Do the problems here:

The category of species

You’ll notice that above I said there was an isomorphism

PExpDP \cong \text{Exp} \cdot D

without ever defining an isomorphism of species! So let’s do that. In fact there’s a category of species.

I said a species is a functor

F:ESet F : \mathsf{E} \to \mathsf{Set}

where E\mathsf{E} is the category of finite sets and bijections. But what’s a morphism between species?

It’s easy to guess if you know what goes between functors: it’s a natural transformation.

Definition. For any categories C\mathsf{C} and D\mathsf{D}, let the functor category D C\mathsf{D}^\mathsf{C} be the category where

  • an object is a functor F:CDF: \mathsf{C} \to \mathsf{D}
  • morphism α:FG\alpha : F \Rightarrow G is a natural transformation.

(We write this with double arrows since a functor was already a kind of arrow, and now we’re talking about an arrow between arrows.)

Definition. The category of species is Set E\mathsf{Set}^\mathsf{E}. The category of tame species is FinSet E\mathsf{FinSet}^\mathsf{E}.

Two tame species can have the same generating function but not be isomorphic! You can check this in these exercises:

Exercise 2. In Example 2 we began defining the species of permutations, PP. We said that for any object SES \in \mathsf{E}, P(S)P(S) is the set of permutations of SS. But to make PP into a functor we also need to say what it does on morphisms of mathE\math{E}. That is, given a bijection f:SSf: S \to S' and a permutation gF(S)g \in F(S) we need a way to get a permutation F(f)(g):SSF(f)(g): S' \to S'. Figure out the details and then show that P:ESetP : \mathsf{E} \to \mathsf{Set} obeys the definition of a functor:

P(fg)=P(f)P(g) P(f \circ g) = P(f) \circ P(g)

for any composable pair of bijections g:SS,f:SSg: S \to S', f: S' \to S'', and

P(1 S)=1 P(S) P(1_S) = 1_{P(S)}

Exercise 3. Show that there is a species LL, the species of linear orderings, such that an LL-structure on SFinSetS \in \mathsf{FinSet} is a linear ordering on SS. In other words: first let L(S)L(S) be the set of linear orderings. Then for any bijection f:SSf: S \to S' define a map L(f):L(S)L(S)L(f): L(S) \to L(S') that sends linear orderings of SS into linear orderings of SS'. Then show that L:ESetL: \mathsf{E} \to \mathsf{Set} obeys the definition of a functor.

Exercise 4. Now show there is no natural isomorphism α:PL\alpha : P \to L. However we have

|P(S)|=|L(S)|=|S|! {|P(S)|}= {|L(S)|}= {|S|}!

for any finite set SS, so PP and LL have the same generating function:

P^=L^ \widehat{P} = \widehat{L}

Thus, you’ve found nonisomorphic tame species with the same generating function!

This may make you sad, because you might hope that you could tell whether two species were isomorphic just by looking at their generating function. But if that were true, species would contain no more information than formal power series. In fact they contain more! Species are like an improved version of formal power series. And there’s not just a set of them, there’s a category of them.

Posted at June 22, 2025 2:59 PM UTC

TrackBack URL for this Entry:   https://golem.ph.utexas.edu/cgi-bin/MT-3.0/dxy-tb.fcgi/3601

3 Comments & 2 Trackbacks

Read the post Counting with Categories (Part 2)
Weblog: The n-Category Café
Excerpt: Notes for a second lecture on combinatorial species.
Tracked: June 23, 2025 10:51 PM
Read the post Counting with Categories (Part 3)
Weblog: The n-Category Café
Excerpt: First lecture in a 4.5-hour minicourse on combinatorics with species.
Tracked: June 24, 2025 10:44 PM

Re: Counting with Categories (Part 1)

I remember doing this homework assignment two decades ago! I hope that you write that book.

Typo: In discussing the product of two species, at one point you mention XX, but there is no XX there; you mean TT.

Formatting (itex) issue: Vertical bars are rendered as operators when they should usually be delimiters (or at least ordinary characters), creating extra space in most contexts. A quick way to fix this is to always put curly braces around them: {|X|} = {|Y|} = {|Z|} produces |X|=|Y|=|Z|{|X|} = {|Y|} = {|Z|} (whereas |X| = |Y| = |Z| produces |X|=|Y|=|Z||X| = |Y| = |Z| with extra space except at the endpoints).

And you probably anticipated this as soon as you saw that it was me making a comment on this post, but the formula for |D(n)|{\lvert{D(n)}\rvert} is missing the first two terms, 10!\frac{1}{0!} and 11!-\frac{1}{1!}. Sure, they cancel, but not for n=0n=0, where 10!\frac{1}{0!} is both the first and last term and 11!-\frac{1}{1!} doesn’t appear. Then you need 10!\frac{1}{0!} to get the correct answer, |D(0)|=1{\lvert{D(0)}\rvert} = 1. (The unique function from the empty set to itself is a bijection with no fixed points.)

Posted by: Toby Bartels on June 27, 2025 9:15 AM | Permalink

Re: Counting with Categories (Part 1)

Thanks! I’ve been annoyed by the spacing around |\vert for years, but never enough to figure out how to fix it. I’m not sure I’ll always have the energy to do it, but I tried it out and yes, it works.

Posted by: John Baez on June 27, 2025 9:44 AM | Permalink

Re: Counting with Categories (Part 1)

My flight to Athens was delayed and I wound up taking a taxi to my hotel at midnight. Out of the blue, after no conversation, the driver asked me if I was a mathematician or philosopher. When I admitted to the former, he tapped his temple and said “they’ve got it going on there, no?” His smile was impish yet benign.

When I arrived, I was so exhausted I couldn’t figure out how to turn on the shower and simply went to sleep. The next day I sheepishly asked the guy at the front desk to show me how.

Later that day my key card didn’t work, so I went to the same guy and said “I think my key card has become demagnetized.”

He said “Are you an engineer?”

If this was an insult it was delivered in the most dead-pan manner possible, so I decided to take it as a serious question and said “No, a mathematician”. He replied “So was my father”, and something let me know he was not making fun of me.

Based on this insignificant sample I conclude that the Greeks, or at least the taxi drivers and hotel desk clerks among them, enjoy guessing people’s occupations from their looks, and are not bad at it. Also, they are less anti-intellectual than Americans: they do not say “oh, I always hated mathematics” when they hear what I do.

Posted by: John Baez on June 30, 2025 2:50 PM | Permalink