Generating Permutations by Choosing the Next Item
To enumerate input permutations, choose each possible first item and recursively enumerate arrangements of what remains.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Show the smallest full case
For A, B, and C, the branch starting with A gives ABC and ACB. Repeating for B and C yields six arrangements. Backtracking can preserve the remaining items and append each choice in turn.
Check duplicate symbols
For A, A, and B, choosing input positions produces six paths but only three distinct text outputs. If unique outputs are wanted, choose among available symbols and track counts. Frequency maps can store those counts across levels.[1]
Track which value becomes unavailable after each choice
For three distinct tokens, fixing B first leaves A and C in any order. Once choosing C next, only A can finish that branch. Backtracking restores C so the sibling branch can choose A second. See backtracking and choice points.
Prefix B
Prefix BA → BAC
Prefix BC → BCA
Other first choices A and C each yield two further outputs
Total 3! = 6 orderings
Handle repeated values when output is value-based
Tokens [A, A, B] have six position-based permutations but only three distinct value sequences. A count map can choose each available value once per level and decrement its remaining count. Test that every emitted result preserves the original multiplicities.