Permuta

Patterns in permutations

How a permutation contains a pattern, and how Permuta, a Python library for permutations and their patterns, works with them: pattern containment and avoidance, and how it builds a permutation class one length at a time.

Containment and avoidance

Two sequences of distinct numbers are order-isomorphic when their entries are in the same relative order: the smallest entry of one is where the smallest of the other is, the second smallest where the second smallest is, and so on. A permutation \(\sigma\) contains a pattern \(p\) when some subsequence of \(\sigma\) is order-isomorphic to \(p\), and that subsequence is an occurrence of \(p\) in \(\sigma\). Otherwise \(\sigma\) avoids \(p\). In the plot of \(\sigma\) an occurrence of \(p\) is a choice of points that “look like the plot of \(p\).”

A class, one length at a time

A set of permutations is a permutation class if it is closed downwards, in the sense that if \(\sigma\) is in the set, then every pattern that appears in \(\sigma\), is also in the set. \(\operatorname{Av}(B)\), the set of all permutations that avoid all the patterns in \(B\), forms a permutation class, and \(B\) is called its basis. Every permutation class can be given as \(\operatorname{Av}(B)\) for some \(B\). Removing the last entry of a member leaves a member one shorter, so each member of length \(n + 1\) is a member of length \(n\) with a new last entry. Permuta's Av builds the class this way, a length at a time: a member of length \(n\) has \(n + 1\) heights for a new last entry, below all its entries, between two of them or above all, and each new permutation is kept unless it contains a pattern of \(B\).

The clever part is that Permuta never looks for a pattern while it does this. It keeps each member's spots, the heights where a new last entry makes a member. A pattern of \(B\) that a new last entry creates uses the new entry and at most \(m - 1\) others, where \(m\) is the length of the longest pattern in \(B\), so it leaves out one of the member's last \(m\) entries. The member without that entry, one shorter, then lacks the height among its spots. So a member's spots are the heights that each member without one of its last \(m\) entries allows. All that is left to check is whether a new permutation is itself a pattern of \(B\).