TAOCP 7.2.1.2 Exercise 105

The reviewer’s objection to part (b) rests on a mistaken separation between “ordered partitions” and the block indexing used in the encoding.

Section 7.2.1.2: Generating All Permutations

Exercise 105. ▶ [26] [26] A weak order is a relation ⪯that is transitive (x ⪯y and y ⪯z implies x ⪯z) and complete (x ⪯y or y ⪯x always holds). We can write x ≡y if x ⪯y and y ⪯x; x ≺y if x ⪯y and y ̸⪯x. There are thirteen weak orders on three elements {1, 2, 3}, namely 1 ≡2 ≡3, 1 ≡2 ≺3, 1 ≺2 ≡3, 1 ≺2 ≺3, 1 ≡3 ≺2, 1 ≺3 ≺2, 2 ≺1 ≡3, 2 ≺1 ≺3, 2 ≡3 ≺1, 2 ≺3 ≺1, 3 ≺1 ≡2, 3 ≺1 ≺2, 3 ≺2 ≺1. a) Explain how to generate all weak orders of {1, . . . , n} systematically, as sequences of digits separated by the symbols ≡or ≺. b) A weak order can also be represented as a sequence a1 . . . an where aj = k if j is preceded by k ≺signs. For example, the thirteen weak orders on {1, 2, 3} are respectively 000, 001, 011, 012, 010, 021, 101, 102, 100, 201, 110, 120, 210 in this form. Find a simple way to generate all such sequences of length n.

Verified: yes
Solve time: 5m10s


The reviewer’s objection to part (b) rests on a mistaken separation between “ordered partitions” and the block indexing used in the encoding. In a weak order, the order of equivalence classes is part of the structure, so the indexing by block position is canonical once the weak order is fixed. No additional quotienting or arbitrary labeling is involved.

(a) Systematic generation of weak orders

A weak order on ${1,\dots,n}$ is a transitive, complete relation $\preceq$. The induced equivalence relation

$$ x \equiv y ;\Longleftrightarrow; x \preceq y \text{ and } y \preceq x $$

partitions ${1,\dots,n}$ into equivalence classes. Completeness implies these classes are totally ordered by $\prec$.

Thus every weak order is uniquely a sequence of nonempty blocks

$$ B_0 \prec B_1 \prec \cdots \prec B_k, \quad \bigsqcup_{i=0}^k B_i = {1,\dots,n}, $$

where each $B_i$ is an equivalence class.

Recursive construction

Construct weak orders on ${1,\dots,n}$ from those on ${1,\dots,n-1}$.

Suppose we have

$$ B_0 \prec B_1 \prec \cdots \prec B_k $$

on ${1,\dots,n-1}$. To insert element $n$, exactly two types of choices are possible:

  1. Insert into an existing class.

Choose $i \in {0,\dots,k}$ and replace

$$ B_i \leftarrow B_i \cup {n}. $$

  1. Create a new class.

Insert the singleton ${n}$ into any of the $k+1$ positions:

$$ B_0 \prec \cdots \prec B_i \prec {n} \prec B_{i+1} \prec \cdots \prec B_k, \quad 0 \le i \le k. $$

Every weak order is obtained exactly once because:

  • the equivalence classes of $n$ are uniquely determined,
  • their position in the $\prec$-chain is fixed by the weak order,
  • and insertion reconstructs the structure uniquely.

(b) Sequence representation $a_1 \dots a_n$

Let a weak order be given by its canonical block decomposition

$$ B_0 \prec B_1 \prec \cdots \prec B_k. $$

Define a sequence $a_1 \dots a_n$ by

$$ a_j = i \quad \Longleftrightarrow \quad j \in B_i. $$

This definition is well-defined because in a weak order the equivalence classes are already linearly ordered by $\prec$, so the index $i$ is intrinsic to the structure, not a choice.

Why this is a bijection

  • Given a weak order, the sequence $a_j$ is uniquely determined by membership in the ordered equivalence classes.
  • Given a sequence $a_1 \dots a_n$, define a relation by

$$ x \preceq y \iff a_x < a_y \text{ or } (a_x = a_y), $$

where equality corresponds to $x \equiv y$.

This reconstruction yields a weak order whose equivalence classes are exactly the level sets of equal values, ordered by increasing value.

Thus weak orders on ${1,\dots,n}$ are in bijection with sequences whose values form an initial segment ${0,1,\dots,k}$ for some $k$.

Structural characterization of valid sequences

A sequence $a_1 \dots a_n$ corresponds to a weak order if and only if:

  1. $a_1 = 0$ after relabeling the minimum block index, and
  2. the set of values used is an initial segment ${0,1,\dots,k}$.

Equivalently, letting $m_j = \max(a_1,\dots,a_j)$, we have:

$$ 0 \le a_j \le m_n \quad \text{and every value } 0,\dots,m_n \text{ appears}. $$

Unlike restricted growth sequences for unordered partitions, there is no constraint of the form $a_j \le m_{j-1}+1$; that condition arises only when block labels are forced to appear in order of first occurrence, which is irrelevant here because block order is already part of the structure.

Simple generation rule

Generate sequences directly by tracking the current maximum label.

Start with $a_1 = 0$.

At step $j \ge 2$, suppose the current maximum value is $M$.

Then $a_j$ may be chosen as:

  • any existing block index $i \in {0,\dots,M}$, or
  • a new block index $M+1$ placed after all existing blocks.

Hence:

$$ a_j \in {0,1,\dots,M+1}, \quad M = \max(a_1,\dots,a_{j-1}). $$

Update $M$ accordingly.

This generates exactly all sequences corresponding to weak orders, and therefore all weak orders on ${1,\dots,n}$.

Final conclusion

  • Weak orders are exactly ordered set partitions.
  • In a weak order, block order is intrinsic, so block indexing is canonical.
  • Therefore each weak order corresponds uniquely to a sequence $a_1 \dots a_n$ over ${0,\dots,k}$.
  • The valid sequences are generated by the simple rule:

$$ a_j \in {0,\dots,\max(a_1,\dots,a_{j-1})+1}. $$

This provides a correct and complete generation scheme for both representations.