Advanced maths

Set partition rank and unrank calculator with restricted growth strings

Set-partition ranking assigns each unlabeled-block partition a unique position in a declared lexicographic order.

Your numbers

Canonical zero-based block labels.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

Set-partition ranking assigns each unlabeled-block partition a unique position in a declared lexicographic order. The calculator uses restricted-growth strings to avoid counting arbitrary block relabelings. Exact dynamic-programmed completion counts support both ranking and reconstruction, with an optional fixed block count.

THE FORMULACompletion count F(r,b)=b F(r−1,b)+F(r−1,b+1).

A worked example

For three labeled elements, the unrestricted strings in order are 000,001,010,011,012. String 010 represents blocks {0,2} and {1}, so its zero-based rank is two. Unranking two returns the same canonical string, while the total family size is Bell number five.

Good to know

One to 40 elements, exact integer ranks up to 100 digits and optional block count one through n. Strings begin at zero and cannot introduce a label beyond one plus the preceding maximum. Order is on strings, not arbitrary human-written block names.

Canonical block labels remove redundant names

A restricted-growth string lists the block containing each labeled element, with new block labels introduced in order of first appearance. The first label is zero and every later label is at most one above the preceding maximum. This canonicalization makes one unlabeled-block partition correspond to one string rather than many arbitrary block renamings.

The labels identify block membership, not numerical values assigned to the underlying elements. For example, 010 places the first and third elements together. Renaming those two blocks as seven and four would describe the same mathematical partition, but it must be converted to the canonical zero,one labels before ranking in this family.

Five restricted-growth strings index the partitions of three labels000rank 0001rank 1010rank 2011rank 3012rank 4Rank 2 → 010 → blocks {0,2}, {1}No duplicate block relabelings
For three labeled elements, the unrestricted strings in order are 000,001,010,011,012. String 010 represents blocks {0,2} and {1}, so its zero-based rank is two. Unranking two returns the same canonical string, while the total family size is Bell number five.

The Stirling set partition count calculator covers the nearby task. Stirling counts do not index or recover a particular unlabeled-block partition.

Completion counts support both directions

At each position, the number of valid completions determines how many earlier strings are skipped by selecting a larger label. Existing blocks contribute one branch each, while introducing a new block changes the used-block count. The dynamic-program recurrence stores those suffix counts as exact integers, avoiding explicit enumeration of a rapidly growing Bell family.

Unranking reverses the same skip process by subtracting whole completion blocks until it reaches the branch containing the requested rank. Zero-based bounds are checked against the total count before reconstruction. A rank equal to the family size is invalid, even if a one-based listing would display that number for its final row.

Fixed block count changes the ranked family

Selecting an exact block count restricts the ranked strings to that Stirling family, so the same string can have a different rank than in the unrestricted Bell family. The completion recursion rejects branches that cannot reach the requested count. The selector therefore changes the combinatorial set, not only an informational output field.

For three elements, every unrestricted string can be written out and compared by hand. Independent tests enumerate small fixed-block families and perform rank-unrank round trips at boundary and random ranks. The browser cap limits element count and rank input size without replacing exact counts by floating-point approximations or introducing a separate duplicate counting-only page.

Use a canonical string to remove arbitrary block names

A set partition groups labeled elements into nonempty unlabeled blocks. A restricted-growth string records the block of each element, assigning label zero to the first block and each newly encountered block the next unused label. The first entry is zero and every later entry is at most one more than the previous maximum.

For elements 0,1,2, string 0,1,0 represents blocks {0,2} and {1}. Calling the same blocks A and B, or swapping their written order, does not create another set partition. Canonical strings make ranking unambiguous by removing those arbitrary block names before lexicographic comparison.

List every rank for three elements

The unrestricted strings in lexicographic order are 000,001,010,011,012, with zero-based ranks 0 through 4. String 010 therefore has rank two. Rank zero is a valid first result, not an absent result. Unranking two returns the canonical string 010 and its actual block count two.

If the block selector is fixed at two, the family becomes 001,010,011, so the same string 010 has rank one instead. Ranks depend on the selected family as well as the order convention. An unrestricted rank cannot be reused unchanged after constraining the block count, even though the partition itself is still allowed.

Count completions instead of enumerating the whole family

At a prefix with b existing blocks, the next element can join any of those b blocks or start one new block. Dynamic programming counts how many valid completions follow each candidate next digit, also respecting a requested final block count. Ranking adds the sizes of lexicographically earlier branches; unranking subtracts branch sizes until locating the branch containing the desired rank.

This avoids writing out every partition, which would be impractical by n=40. Counts and ranks use exact integers rather than floating-point numbers. In the unrestricted family the count is the Bell number; with exactly k blocks it is the Stirling number of the second kind. Those are family counts, not the rank of a particular string.

Keep indexing and validity checks with the result

An unrank input must lie from zero through family_count−1. A string with a skipped new label, such as 0,2,0, is invalid even if renaming its blocks could turn it into a valid partition: the input contract requires the canonical labeling already. The number of entries must equal the declared number of elements.

These are set partitions of labeled elements, not integer partitions of a sum. Their order is lexicographic on full canonical strings, which can differ from the order used by another combinatorics library. Record n, the optional block constraint, zero-based indexing and the string convention when storing a rank for later reproduction.

A few questions, answered

Are ranks zero based?

Yes. Valid ranks run from zero through family size minus one.

Why canonical restricted-growth labels?

They prevent arbitrary renaming of the same unlabeled blocks from creating duplicates.

Why does a partition’s rank change when I select a block count?

The ranked family changes. Lexicographic positions are recomputed among only partitions with that exact number of blocks.

Is this an integer-partition rank?

No. It ranks partitions of labeled set elements into unlabeled blocks, represented by restricted-growth strings.

Find your calculator

Start typing to explore the library.