341 lines
16 KiB
HTML
341 lines
16 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html lang='en'><head><meta charset='utf-8' /><meta name='pinterest' content='nopin' /><link href='../../../../static/css/style.css' rel='stylesheet' type='text/css' /><link href='../../../../static/css/print.css' rel='stylesheet' type='text/css' media='print' /><title>What the Hell are Permutation Patterns? / Steve Losh</title></head><body><header><a id='logo' href='https://stevelosh.com/'>Steve Losh</a><nav><a href='../../../index.html'>Blog</a> - <a href='https://stevelosh.com/projects/'>Projects</a> - <a href='https://stevelosh.com/photography/'>Photography</a> - <a href='https://stevelosh.com/links/'>Links</a> - <a href='https://stevelosh.com/rss.xml'>Feed</a></nav></header><hr class='main-separator' /><main id='page-blog-entry'><article><script type='text/javascript' async
|
||
|
src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script><h1><a href='index.html'>What the Hell are Permutation Patterns?</a></h1><p class='date'>Posted on December 10th, 2015.</p><p>I'm currently in the Mathematical Programming class at Reykjavík University and
|
||
|
we're working with permutations and patterns. They're really simple to
|
||
|
understand once they're explained to you, but after searching around online
|
||
|
I haven't found a nice explanation for humans, so here you go.</p>
|
||
|
|
||
|
<p>None of this is new research, I just want to summarize things so other people
|
||
|
can understand it without having to shovel through the internet trying to piece
|
||
|
together a bunch of terse definitions. All of this is covered in more detail on
|
||
|
Wikipedia, as well as in <a href="http://www.amazon.com/dp/1439850518/?tag=stelos-20">Combinatorics of Permutations</a> by Miklós Bóna
|
||
|
(and many other places).</p>
|
||
|
|
||
|
<ol class="table-of-contents"><li><a href="index.html#s1-permutations-non-mathy">Permutations (Non-Mathy)</a><ol><li><a href="index.html#s2-basics">Basics</a></li><li><a href="index.html#s3-restricting-slots">Restricting Slots</a></li></ol></li><li><a href="index.html#s4-permutations-mathy">Permutations (Mathy)</a><ol><li><a href="index.html#s5-standard-classical-permutations">Standard/Classical Permutations</a></li><li><a href="index.html#s6-subwords-subsequences">Subwords/Subsequences</a></li></ol></li><li><a href="index.html#s7-permutation-patterns">Permutation Patterns</a></li><li><a href="index.html#s8-so-what">So What?</a></li><li><a href="index.html#s9-further-information">Further Information</a></li></ol>
|
||
|
|
||
|
<h2 id="s1-permutations-non-mathy"><a href="index.html#s1-permutations-non-mathy">Permutations (Non-Mathy)</a></h2>
|
||
|
|
||
|
<p>If you've used permutations in a programming language you can probably skip this
|
||
|
section.</p>
|
||
|
|
||
|
<p>If you're interested in programming and/or math you've probably heard the term
|
||
|
"permutations" before. It's typically explained to be something like "different
|
||
|
ways to order a collection of objects".</p>
|
||
|
|
||
|
<p>The key word here is "order" — permutations are all about ordering.</p>
|
||
|
|
||
|
<h3 id="s2-basics"><a href="index.html#s2-basics">Basics</a></h3>
|
||
|
|
||
|
<p>Let's look at an example. If you have a list of three cats:</p>
|
||
|
|
||
|
<ul>
|
||
|
<li>Scruffy</li>
|
||
|
<li>Boots</li>
|
||
|
<li>Patches</li>
|
||
|
</ul>
|
||
|
|
||
|
<p>How many different ways can you list them out? Order matters, and you're also
|
||
|
not allowed to repeat a cat (no cloning here).</p>
|
||
|
|
||
|
<pre><code>1 2 3 4 5 6
|
||
|
Scruffy Scruffy Patches Patches Boots Boots
|
||
|
Boots Patches Scruffy Boots Scruffy Patches
|
||
|
Patches Boots Boots Scruffy Patches Scruffy
|
||
|
</code></pre>
|
||
|
|
||
|
<p>So there are six permutations. Simple enough. Let's think about the edge
|
||
|
cases. How many ways can you permute one cat? Just one:</p>
|
||
|
|
||
|
<pre><code>1
|
||
|
Scruffy
|
||
|
</code></pre>
|
||
|
|
||
|
<p>Something a bit tougher: how many ways can you permute <em>zero</em> cats? This seems
|
||
|
a bit weird, but if you phrase it as "how many ways can you order a list of zero
|
||
|
objects" you can convince yourself the answer is one as well (the single
|
||
|
permutation is "the empty list").</p>
|
||
|
|
||
|
<p>We don't want to have to count out the number of permutations by hand every
|
||
|
time, so it would be nice to have a formula. If we have a list of <em>n</em> things,
|
||
|
how many different permutations can we come up with?</p>
|
||
|
|
||
|
<p>We can start by filling the first slot with any item we want, so we have \(n\)
|
||
|
options. Then for each of those choices we pick something to go in the next
|
||
|
slot from the \(n - 1\) things that are left. We can keep going all the way
|
||
|
down until we're at the last slot, by which point we've only got one thing left,
|
||
|
so we get:</p>
|
||
|
|
||
|
<div>$$
|
||
|
(n)(n - 1)(n - 2)...(1)
|
||
|
$$</div>
|
||
|
|
||
|
<p>Which is just the <a href="https://en.wikipedia.org/wiki/Factorial">factorial</a> of \(n\). For our three cats we have:</p>
|
||
|
|
||
|
<div>$$
|
||
|
(3)(3 - 1)(3 - 2) = 3 \cdot 2 \cdot 1 = 6
|
||
|
$$</div>
|
||
|
|
||
|
<p>So now we know that:</p>
|
||
|
|
||
|
<div>$$
|
||
|
\operatorname{number-of-permutations}(\mathit{items}) =
|
||
|
\mathit{items}!
|
||
|
$$</div>
|
||
|
|
||
|
<h3 id="s3-restricting-slots"><a href="index.html#s3-restricting-slots">Restricting Slots</a></h3>
|
||
|
|
||
|
<p>Things start to get more interesting when we start to restrict the number of
|
||
|
slots. For example, we can ask "how many different lists of three items can we
|
||
|
take from a group of five items?"</p>
|
||
|
|
||
|
<p>I'm going to stop using cat names now because it's getting painful to type, so
|
||
|
let's just use letters. Our five items will be:</p>
|
||
|
|
||
|
<pre><code>a, b, c, d, e
|
||
|
</code></pre>
|
||
|
|
||
|
<p>How many different three-length lists can we produce?</p>
|
||
|
|
||
|
<pre><code>(a, b, c) (a, b, d) (a, b, e) (a, c, b) (a, c, d) (a, c, e)
|
||
|
(a, d, b) (a, d, c) (a, d, e) (a, e, b) (a, e, c) (a, e, d)
|
||
|
|
||
|
(b, a, c) (b, a, d) (b, a, e) (b, c, a) (b, c, d) (b, c, e)
|
||
|
(b, d, a) (b, d, c) (b, d, e) (b, e, a) (b, e, c) (b, e, d)
|
||
|
|
||
|
(c, a, b) (c, a, d) (c, a, e) (c, b, a) (c, b, d) (c, b, e)
|
||
|
(c, d, a) (c, d, b) (c, d, e) (c, e, a) (c, e, b) (c, e, d)
|
||
|
|
||
|
(d, a, b) (d, a, c) (d, a, e) (d, b, a) (d, b, c) (d, b, e)
|
||
|
(d, c, a) (d, c, b) (d, c, e) (d, e, a) (d, e, b) (d, e, c)
|
||
|
|
||
|
(e, a, b) (e, a, c) (e, a, d) (e, b, a) (e, b, c) (e, b, d)
|
||
|
(e, c, a) (e, c, b) (e, c, d) (e, d, a) (e, d, b) (e, d, c)
|
||
|
</code></pre>
|
||
|
|
||
|
<p>You can see how working with numerical formulas would be a lot nicer than
|
||
|
listing all those out by hand and counting them. The formula to tell us the
|
||
|
number is:</p>
|
||
|
|
||
|
<div>$$
|
||
|
\operatorname{number-of-permutations}(\mathit{items}, \mathit{slots}) =
|
||
|
\frac
|
||
|
{\mathit{items}!}
|
||
|
{(\mathit{items} - \mathit{slots})!}
|
||
|
$$</div>
|
||
|
|
||
|
<p>So in this example we have:</p>
|
||
|
|
||
|
<div>$$
|
||
|
\frac
|
||
|
{\mathit{items}!}
|
||
|
{(\mathit{items} - \mathit{slots})!}
|
||
|
=
|
||
|
\frac{5!}{(5 - 3)!} =
|
||
|
\frac{5!}{2!} =
|
||
|
\frac{5 \cdot 4 \cdot 3 \cdot 2 \cdot 1}{2 \cdot 1} =
|
||
|
\frac{120}{2} =
|
||
|
60
|
||
|
$$</div>
|
||
|
|
||
|
<p>Which is correct (feel free to count them by hand if you're skeptical).</p>
|
||
|
|
||
|
<p>This formula continues to work when the number slots is equal to the number of
|
||
|
elements (like in our cat example). If \(n = slots = items\) then:</p>
|
||
|
|
||
|
<div>$$
|
||
|
\frac
|
||
|
{\mathit{items}!}
|
||
|
{(\mathit{items} - \mathit{slots})!}
|
||
|
=
|
||
|
\frac
|
||
|
{\mathit{n}!}
|
||
|
{(\mathit{n} - \mathit{n})!}
|
||
|
=
|
||
|
\frac
|
||
|
{\mathit{n}!}
|
||
|
{0!}
|
||
|
=
|
||
|
\frac
|
||
|
{\mathit{n}!}
|
||
|
{1}
|
||
|
=
|
||
|
\mathit{n}!
|
||
|
$$</div>
|
||
|
|
||
|
<p>Remember that \(0!\) is 1, not 0 as you might first expect. Feel free to plug
|
||
|
in the edge cases we talked about before (zero- and one-length permutations) and
|
||
|
make sure they work.</p>
|
||
|
|
||
|
<h2 id="s4-permutations-mathy"><a href="index.html#s4-permutations-mathy">Permutations (Mathy)</a></h2>
|
||
|
|
||
|
<p>That was a pretty standard introduction to permutations, and it's about as far
|
||
|
as most basic programming/math books go at first (they'll also talk about
|
||
|
"combinations", which are something else that we don't care about right now).
|
||
|
But if we want to start looking at permutations more closely we need to add
|
||
|
a few additional rules.</p>
|
||
|
|
||
|
<p>So far we've been talking about permutations of arbitrary objects, like cats,
|
||
|
letters, or playing cards. From here on out we're going to restrict ourselves
|
||
|
a bit more: we'll only consider lists of positive integers. This is important
|
||
|
because integers can be compared. We can't say that Scruffy is "less than" or
|
||
|
"greater than" Boots, but we <em>can</em> say that 15 is less than 16.</p>
|
||
|
|
||
|
<p>Notice that we're not including the number zero. When we say "show me all
|
||
|
standard permutations of length 3" we mean:</p>
|
||
|
|
||
|
<pre><code>1 2 3
|
||
|
1 3 2
|
||
|
2 1 3
|
||
|
2 3 1
|
||
|
3 1 1
|
||
|
3 2 1
|
||
|
</code></pre>
|
||
|
|
||
|
<p>Sorry programmers, permutation people like to count from one.</p>
|
||
|
|
||
|
<p>You'll notice that I wrote out the permutations above each on their own line,
|
||
|
with the numbers just listed out. This is called "one-line notation".
|
||
|
Sometimes people drop the spaces in between the numbers (e.g. <code>1 2 3</code> becomes
|
||
|
<code>123</code>) if all the numbers are single digits and they feel like being annoying.</p>
|
||
|
|
||
|
<p>There's also a "two-line notation" that we won't use here, check out the
|
||
|
Wikipedia page at the end of the post for more.</p>
|
||
|
|
||
|
<h3 id="s5-standard-classical-permutations"><a href="index.html#s5-standard-classical-permutations">Standard/Classical Permutations</a></h3>
|
||
|
|
||
|
<p>Another term we'll use is "standard permutations" or "classical patterns" (don't
|
||
|
worry about the word "patterns" too much yet). This just means they contain all
|
||
|
the numbers 1 to \(n\) with no missing numbers. So <code>3 2 4 1</code> is a standard
|
||
|
permutation (of length 4), but <code>1 902 23232 5</code> is not.</p>
|
||
|
|
||
|
<h3 id="s6-subwords-subsequences"><a href="index.html#s6-subwords-subsequences">Subwords/Subsequences</a></h3>
|
||
|
|
||
|
<p>One more bit of terminology before we get to the meat of the post. A "subword"
|
||
|
of a permutation is all the different lists we can get by dropping none, some,
|
||
|
or all of the items, <em>without changing the order</em>. For example, the sublists of
|
||
|
<code>9 3 1 4</code> are:</p>
|
||
|
|
||
|
<pre><code>dropping no items
|
||
|
(9 3 1 4)
|
||
|
|
||
|
dropping one item
|
||
|
( 3 1 4)
|
||
|
(9 1 4)
|
||
|
(9 3 4)
|
||
|
(9 3 1 )
|
||
|
|
||
|
dropping two items
|
||
|
( 1 4)
|
||
|
(9 4)
|
||
|
(9 3 )
|
||
|
( 3 4)
|
||
|
(9 1 )
|
||
|
( 3 1 )
|
||
|
|
||
|
dropping three items
|
||
|
(9 )
|
||
|
( 3 )
|
||
|
( 1 )
|
||
|
( 4)
|
||
|
|
||
|
dropping four items
|
||
|
( )
|
||
|
</code></pre>
|
||
|
|
||
|
<p>You will also hear these called "subsequences", but this is way too confusing
|
||
|
because <code>subsequence</code> in a programming language almost always means
|
||
|
a <em>consecutive</em> portion of a list, so I'm going to stick with "subwords".</p>
|
||
|
|
||
|
<h2 id="s7-permutation-patterns"><a href="index.html#s7-permutation-patterns">Permutation Patterns</a></h2>
|
||
|
|
||
|
<p>Hold onto your hats, things are about to get intense.</p>
|
||
|
|
||
|
<p>We say that one permutation X <strong>contains</strong> another permutation Y if one of the
|
||
|
subwords of X has <strong>the same length and relative order</strong> as Y. I know that's
|
||
|
confusing and not at all clear, so let's take an example.</p>
|
||
|
|
||
|
<p>Let X be the permutation <code>1 4 3 2</code>. Let Y be <code>1 2</code>. Does X contain Y?</p>
|
||
|
|
||
|
<p>If we look at Y's elements, we see that it starts with the lowest element and
|
||
|
then goes to the highest element. Is there some subword of X, of length 2, that
|
||
|
does the same thing? Sure: <code>1 4</code> is a subword of X, has length 2, and starts at
|
||
|
the lowest element then goes to the highest.</p>
|
||
|
|
||
|
<p>Note that our subword has a <code>4</code> in it, but Y only has <code>1</code> and <code>2</code>. This is the
|
||
|
most confusing part about permutation patterns: we don't care about the actual
|
||
|
numbers themselves — we only care about the relationships between the numbers.</p>
|
||
|
|
||
|
<p>Another example. Let X be <code>1 4 3 2</code>. Let Y be <code>2 1</code>. Does X contain Y? Try
|
||
|
this one on your own before reading on.</p>
|
||
|
|
||
|
<p>Y is still two elements long, but now it starts at the highest element and goes
|
||
|
the lowest. The subword we used in the last example (<code>1 4</code>) doesn't work here,
|
||
|
because it starts low and goes high (the opposite of what we want). But <code>4 3</code>
|
||
|
is another subword of X, and that <em>does</em> match, so X does contain Y. Note that
|
||
|
<code>4 2</code> would also fit here (remember: subwords don't have to be consecutive!).</p>
|
||
|
|
||
|
<p>Let's try something more interesting. Let X still be <code>1 4 3 2</code>, but let's make
|
||
|
Y <code>1 2 3</code>. Does X contain Y?</p>
|
||
|
|
||
|
<p>This one is a little trickier. The order of Y is that it starts at the lowest
|
||
|
element, then it goes to the middle one, then finally it goes to the highest
|
||
|
element. Let's look at the subwords of X that are length 3:</p>
|
||
|
|
||
|
<pre><code>1 4 3
|
||
|
1 4 2
|
||
|
1 3 2
|
||
|
4 3 2
|
||
|
</code></pre>
|
||
|
|
||
|
<p>Do any of those have the same relative ordering as Y? No! None of them start
|
||
|
at the lowest, go to the middle, then go to the highest. So this time X does
|
||
|
not contain Y, so we say that X <strong>avoids</strong> Y.</p>
|
||
|
|
||
|
<h2 id="s8-so-what"><a href="index.html#s8-so-what">So What?</a></h2>
|
||
|
|
||
|
<p>So now that we know what "X contains Y" and "X avoids Y" mean, what can we use
|
||
|
this stuff for? I'm not an expert, but I can give you a few examples to whet
|
||
|
your apetite.</p>
|
||
|
|
||
|
<p>Consider a permutation X that avoids <code>2 1</code>. What might X look like?</p>
|
||
|
|
||
|
<p>Well the empty permutation and any permutation with only one element certainly
|
||
|
avoid <code>2 1</code> (they don't have any subwords of the correct length at all). What
|
||
|
about longer ones?</p>
|
||
|
|
||
|
<p>If X avoids <code>2 1</code>, then for any two elements in it, the leftmost element must be
|
||
|
smaller than the rightmost one (otherwise it would <em>contain</em> <code>2 1</code>). This means
|
||
|
that all the elements must be getting bigger as you go to the right, or to put
|
||
|
it another way: the permutation must be in sorted order!</p>
|
||
|
|
||
|
<p>Similarly, if X avoids <code>1 2</code> then it must be in <em>reverse</em> sorted order. Poke at
|
||
|
this for a minute and convince yourself it must be true. Make sure to consider
|
||
|
edge cases like the empty permutation and single-element permutations.</p>
|
||
|
|
||
|
<p>Another example comes from Knuth: if a permutation contains <code>2 3 1</code> it cannot be
|
||
|
sorted by a stack in a single pass. Remember: the numbers in the matching
|
||
|
subword don't have to be consecutive, and they don't have to match the exact
|
||
|
numerals, only the relative order matters. <code>66 1 99 2 33</code> contains <code>2 3 1</code>
|
||
|
because the subword <code>66 99 33</code> has the same relative order (and so <code>66 1 99
|
||
|
2 33</code> cannot be sorted by a stack in a single pass). This is probably not
|
||
|
intuitively obvious, so for further explanation check out Wikipedia, or <a href="http://www.dmtcs.org/pdfpapers/dmAR0153.pdf">this
|
||
|
paper</a> coincidentally by my professor, or even Knuth's <a href="http://www.amazon.com/dp/0201896834/?tag=stelos-20">book</a>
|
||
|
itself.</p>
|
||
|
|
||
|
<h2 id="s9-further-information"><a href="index.html#s9-further-information">Further Information</a></h2>
|
||
|
|
||
|
<p>This was a really quick introduction. I glossed over a bunch of things (like
|
||
|
defining a permutation as a mapping of elements of a set to themselves, the
|
||
|
identity permutation, etc). If you want to dive in more, you can check out
|
||
|
Wikipedia:</p>
|
||
|
|
||
|
<ul>
|
||
|
<li><a href="https://en.wikipedia.org/wiki/Permutation">Permutations</a></li>
|
||
|
<li><a href="https://en.wikipedia.org/wiki/Permutation_pattern">Permutation Patterns</a></li>
|
||
|
</ul>
|
||
|
|
||
|
<p>Or go all-in and grab a copy of <a href="http://www.amazon.com/dp/1439850518/?tag=stelos-20">Combinatorics of Permutations</a>.</p>
|
||
|
|
||
|
<p>I'm also planning on doing another post like this about mesh patterns, which are
|
||
|
a slightly more general/powerful version of these basic patterns.</p>
|
||
|
</article></main><hr class='main-separator' /><footer><nav><a href='https://github.com/sjl/'>GitHub</a> ・ <a href='https://twitter.com/stevelosh/'>Twitter</a> ・ <a href='https://instagram.com/thirtytwobirds/'>Instagram</a> ・ <a href='https://hg.stevelosh.com/.plan/'>.plan</a></nav></footer></body></html>
|