AlgoViz

Permutations

medium 24 min

Generating every ordering of a list is the backtracking pattern where order matters.

Where it shows up

The same pattern appears across everyday software.

  • Brute-force route planning

    A courier with a handful of stops can try every visiting order and keep the shortest. Five stops is 120 orders, which is instant; fifteen is over a trillion, which is why real route planners switch to dynamic programming over subsets (about 2ⁿ · n² work) or heuristics.

  • Test ordering and flaky tests

    Tests that pass alone but fail together usually depend on run order. Test runners can shuffle the order, and for a small suspect group a script can try every ordering to find the pair that interferes.

  • Scheduling and seating

    Assigning a few speakers to time slots, or guests to seats with constraints such as who must not sit together, is a permutation search with pruning: place one person at a time and back out as soon as a rule is broken.

  • Anagrams and word games

    Word game helpers generate arrangements of the letters on a rack and keep the ones in a dictionary. Repeated letters are the Permutations II case: the letters of a word with repeats have n! divided by each letter count's factorial distinct arrangements, not n!.

  • Lexicographic iteration in libraries

    C++ provides std::next_permutation for stepping through arrangements in sorted order, duplicates included, and Python provides itertools.permutations, which yields tuples lazily so the n! results never have to sit in memory at once.