TAOCP 7.2.1.2 Exercise 8

The reviewer’s diagnosis is correct: the previous proof failed because it tried to identify _submultisets_ with _Algorithm L states_, which are not unique.

Section 7.2.1.2: Generating All Permutations

Exercise 8. ▶ [21] x 8. [21] The variations of a multiset are the permutations of all its submultisets. For example, the variations of {1, 2, 2, 3} are ϵ, 1, 12, 122, 1223, 123, 1232, 13, 132, 1322, 2, 21, 212, 2123, 213, 2132, 22, 221, 2213, 223, 2231, 23, 231, 2312, 232, 2321, 3, 31, 312, 3122, 32, 321, 3212, 322, 3221. Show that simple changes to Algorithm L will generate all variations of a given multiset {a1, a2, . . . , an}.

Verified: yes
Solve time: 4m05s


The reviewer’s diagnosis is correct: the previous proof failed because it tried to identify submultisets with Algorithm L states, which are not unique. A correct solution must separate:

  1. selection of a submultiset (a canonical combinatorial object), and
  2. permutation of that selected multiset (handled by Algorithm L).

The fix is to restructure the method so that Algorithm L is used only on a fixed multiset, while a separate, well-defined enumeration generates all submultisets.

Correct Solution

Let the input multiset be given in nondecreasing form

$$ a_1 \le a_2 \le \cdots \le a_n. $$

Let the distinct values be

$$ b_1 < b_2 < \cdots < b_k $$

with multiplicities $m_1,\dots,m_k$, so that each $b_i$ occurs $m_i$ times.

A submultiset is uniquely determined by an integer vector

$$ c = (c_1,\dots,c_k), \quad 0 \le c_i \le m_i, $$

meaning that $c_i$ copies of $b_i$ are chosen.

Thus the problem reduces to generating all such vectors $c$, and for each $c$, generating all permutations of the corresponding multiset.

Step 1: Outer generation of submultisets

We generate all vectors

$$ (c_1,\dots,c_k), \quad 0 \le c_i \le m_i, $$

in lexicographic order.

This is achieved by a simple “bounded odometer” modification of Algorithm L style successor logic:

  • Start with $c_i := 0$ for all $i$.

  • Repeatedly attempt to increment the rightmost component $c_i < m_i$.

  • When such an $i$ is found:

  • set $c_i := c_i + 1$,

  • set $c_{i+1} = \cdots = c_k := 0$,

  • output the new submultiset.

  • If no such $i$ exists, terminate.

This generates each bounded vector exactly once, in lexicographic order.

Each vector $c$ corresponds to the canonical submultiset

$$ S(c) = {\underbrace{b_1,\dots,b_1}{c_1}, \dots, \underbrace{b_k,\dots,b_k}{c_k}}. $$

Step 2: Inner generation of permutations (Algorithm L)

For each fixed vector $c$, apply Algorithm L to the multiset $S(c)$.

Algorithm L is correct for generating all permutations of a fixed multiset, so it produces every permutation of $S(c)$ exactly once.

Step 3: Combined algorithm (variations)

The full algorithm is:

  1. Generate the first submultiset $c = (0,\dots,0)$.
  2. Repeat:
  • Form the multiset $S(c)$.
  • Apply Algorithm L to generate all permutations of $S(c)$, outputting each permutation as a variation.
  • Move to the next vector $c$ using the bounded lexicographic successor rule.
  1. Terminate when all $c_i = m_i$ have been exhausted.

Correctness Proof

Lemma 1 (Uniqueness of submultisets)

Each vector $c = (c_1,\dots,c_k)$ corresponds to exactly one submultiset $S(c)$, and every submultiset arises uniquely in this way.

Proof.

The multiplicities $c_i$ completely determine how many copies of each distinct element $b_i$ are present. Hence the representation is bijective. ∎

Lemma 2 (Exhaustion of submultisets)

The bounded lexicographic successor procedure generates every vector

$$ 0 \le c_i \le m_i $$

exactly once.

Proof.

This is the standard mixed-radix counting scheme. Each increment increases the vector lexicographically, and resetting trailing coordinates prevents repetition. The process enumerates all $\prod_{i=1}^k (m_i+1)$ vectors exactly once. ∎

Lemma 3 (Correctness of permutation phase)

For each fixed $c$, Algorithm L generates every permutation of $S(c)$ exactly once.

Proof.

This is precisely the correctness theorem of Algorithm L for multiset permutations. ∎

Lemma 4 (No duplication across different submultisets)

If $c \ne c'$, then no permutation produced from $S(c)$ equals a permutation produced from $S(c')$.

Proof.

Different vectors $c \ne c'$ define different multiplicity profiles, hence different underlying multisets. Therefore their permutation sets are disjoint. ∎

Final Theorem

Every variation of the multiset ${a_1,\dots,a_n}$ is generated exactly once.

Proof.

A variation is, by definition, a permutation of some submultiset of the input multiset.

  • By Lemma 2, every submultiset $S(c)$ is generated exactly once.
  • By Lemma 3, for each such submultiset, Algorithm L generates all permutations exactly once.
  • By Lemma 4, no permutation is generated from two different submultisets.

Therefore every variation appears exactly once in the output. ∎

Why this fixes the original error

  • We do not identify states of Algorithm L with submultisets.
  • We instead use a unique canonical representation (multiplicity vector $c$).
  • Algorithm L is applied only where it is valid: a fixed multiset.
  • The enumeration of submultisets is a standard bounded lexicographic traversal, not an ill-defined DFS on permutations.

This yields a correct and structurally sound modification of Algorithm L.