# TAOCP 7.2.1.5 Generating All Set Partitions

> Study guide to TAOCP 7.2.1.5 Generating All Set Partitions. Generating all partitions of a set: restricted growth strings and Algorithm H, Gray codes for…

Updated October 20, 2026.

## Summary {#summary}

TAOCP 7.2.1.5 Generating All Set Partitions: Generating all partitions of a set: restricted growth strings and Algorithm H, Gray codes for set partitions, Bell numbers and Peirce's triangle, the asymptotics of Bell numbers, Stirling numbers of the second kind, and more. Budget about 6 hours; do exercises 2, 4 and 6.

Generating all partitions of a set: restricted growth strings and Algorithm H, Gray codes for set partitions, Bell numbers and Peirce's triangle, the asymptotics of Bell numbers, Stirling numbers of the second kind, and partitions of a multiset (Algorithm M), with applications to rhyme schemes and alphametics.

## Key facts {#key-facts}

- Week 131 of 145 on the cracked.guide path, in [TAOCP Vol 4A: Combinatorial Algorithms, Part 1](https://cracked.guide/taocp/volume-4a).
- Time budget: about 6 hours.
- 82 exercises in the section, rated 14 to HM46; the guide picks 8.
- Picked exercises with Knuth's triangle for recommended: 2, 4, 6, 11, 26, 65, 69 and 38.
- Before you start: 7.2.1.4 Generating All Partitions.

## Week 131 goals {#objectives}

By the end of week 131 you can:

- Use restricted growth strings to list the partitions of a set.
- Compute Bell numbers with Peirce's triangle.
- Compute Stirling numbers of the second kind for small cases.

## This week's steps {#steps}

- Read §7.2.1.5 Generating All Set Partitions: Implement generation via restricted growth strings and verify counts match Bell numbers.
- Work the exercises (8 picked): Start with Ex 2, Ex 4, Ex 6; log what you solve below.
- Write down one thing you'd explain to someone: A few sentences in your own words in Your notes: the idea, why it works, an example. 30 words ticks this.

## Exercises {#exercises}

Section 7.2.1.5 has 82 exercises, rated 14 to HM46, and Knuth marks 19 of them with the triangle for recommended. Algorithms and Gray codes 1 to 25, Peirce's triangle and combinatorics 26 to 44, asymptotics 45 to 68, multiset partitions 69 to 82.

Worth your time:

- 2 (rated 22, recommended): Keep linked lists of each block up to date as Algorithm H runs.
- 4 (rated 21, recommended): Classify the SGB five-letter words by their letter-repetition pattern. Restricted growth strings in practice.
- 6 (rated 25, recommended): Generate set partitions with given numbers of blocks of each size.
- 11 (rated 28, recommended): Count how many set partitions of the digit positions in SEND+MORE=MONEY give pure alphametics.
- 26 (rated M22, recommended): Interpret Peirce's triangle as counting paths.
- 65 (rated HM22, recommended): Find the variance of the number of blocks of size k in a random set partition.
- 69 (rated 22, recommended): Restrict the multiset partition generator to at most r parts.
- 38 (rated M30, recommended): Study sequences of cyclic shifts whose product is the identity. The stretch.

## Why it matters {#why}

Set partitions show up in clustering, test case generation and some scheduling problems.

## Before you start {#before}

- 7.2.1.4 Generating All Partitions

## How to approach it {#approach}

Implement generation via restricted growth strings and verify counts match Bell numbers.

## Links {#links}

- [Knuth's TAOCP page (editions, errata, MIX/MMIX)](https://www-cs-faculty.stanford.edu/~knuth/taocp.html)
- [Vol 4A errata (plain text, from Knuth)](https://www-cs-faculty.stanford.edu/~knuth/err4a.textxt)
- [Wikipedia: Partition of a set](https://en.wikipedia.org/wiki/Partition_of_a_set)
- [Wikipedia: Bell number](https://en.wikipedia.org/wiki/Bell_number)

---

Source: https://cracked.guide/guide/knuth-w113-0
cracked.guide: A free, dated 145-week path to getting genuinely cracked at programming.
