The Complexity of Pattern Matching for $321$-Avoiding and Skew-Merged Permutations

The Permutation Pattern Matching problem, asking whether a pattern permutation $\pi$ is contained in a permutation $\tau$, is known to be NP-complete. In this paper we present two polynomial time algorithms for special cases. The first algorithm is applicable if both $\pi$ and $\tau$ are $321$-avoiding; the second is applicable if $\pi$ and $\tau$ are skew-merged. Both algorithms have a runtime of $O(kn)$, where $k$ is the length of $\pi$ and $n$ the length of $\tau$.


Introduction
In this paper, a permutation is a bijective function from [n] to itself, where n is a positive integer and [n] = {1, 2, . . . , n}. Therefore, a permutation, π : [n] → [n] is the set of ordered pairs (i, π(i)). We occasionally write specific permutations in the usual one line notation, e.g., 321 represents the permutation of [3] equal to {(1, 3), (2,2), (3,1)}. The size of π is just the cardinality of this set, and we denote the elements, also called points, of a permutation by variables such as x and y. We adopt the usual conventions with respect to order of such points, i.e., (i, π(i)) lies to the left of (j, π(j)) if i < j and above (k, π(k)) if π(i) > π(k), with corresponding definitions for 'to the right of' and 'below'. Given an element x in a permutation π, we define (wherever possible): x the element immediately to its left, x the element immediately to its right, x the element immediately above it, and x the element immediately below it.

C PERMUTATION PATTERN MATCHING (C-PPM)
Input: A text permutation τ of size n and a pattern π of size k, both belonging to a fixed proper permutation class C. Question: Does τ contain π?
Clearly, for a fixed C, polynomial time algorithms for C-PATTERN PPM apply to C-PPM as well. Consequently, the separable case, i.e., Av(3142, 2413)-PPM, can be solved in O(kn 4 ) time [11]. Note that if the pattern avoids 132, 231, 213 or 312 then it is automatically separable and thus the C-PATTERN PPM problem for all four classes Av(132), Av(231), Av(213) or Av(312) can be solved in polynomial time. Most relevant to our work is a result by Guillemot and Vialette [10] that establishes an O(k 2 n 6 )-time algorithm for Av(321)-PPM. In Sections 2 and 3, we improve their approach to give the following. In Section 4 we show how to adapt this approach to the class of skew-merged permutations, which are those permutations whose elements can be partitioned into an increasing subsequence and a decreasing subsequence. Skew-merged permutations can also be characterised as those permutations that avoid both 3412 and 2143 [14]. Theorem 1.2. Given skew-merged permutations τ of size n and π of size k, there is an O(kn)-time algorithm which determines whether τ contains π.
The following elementary observation will be used repeatedly. Lemma 1.3. Let π and τ be permutations and f : π → τ . Then f is an embedding of π into τ if and only if for every element x of π: • if x = ⊥ then f (x) lies strictly to the right of f (x ) and • if x = ⊥ then f (x) lies strictly above f (x ).

Proof:
Suppose that x and y are points of π and that, without loss of generality, y lies strictly to the left of x. Then y occurs in the sequence x , x , x , . . . . So, by inductive use of the first property, f (x) lies strictly to the right of f (y). Similarly, inductive use of the second property establishes that the vertical relationship between f (x) and f (y) is the same as that between x and y, and the result follows. The other direction follows directly from the definition of embeddings given on page 2: x is an element strictly to the left of x and thus f (x) lies strictly to the right of f (x ) for an embedding f . In the same way, x is an element strictly below x and thus f (x) lies strictly above f (x ).

The Lattice of Rigid Embeddings of 321-Avoiding Permutations
It is easy to see that the elements of any 321-avoiding permutation π can be partitioned into two increasing subsequences. This partition is in general not unique but in any such partition, one of these subsequences will contain all those elements which participate as the '2' in a copy of 21-called the upper elements of π and denoted U π -and the other will contain all those elements which participate as the '1' in a copy of 21-called the lower elements of π and denoted L π . Elements that are neither upper nor lower elements, i.e., those that are not involved in a copy of 21, can be part of either of the two subsequences. Let us formalise these definitions: An element x of π is an upper element if there is some embedding of upper lower fluid rigid Fig. 1: The decomposition of the 321-avoiding permutation π = 3 1 2 4 5 9 6 7 10 8 11 13 12 into rigid and fluid elements. 21 = {(1, 2), (2, 1)} into π such that x is the image of (1, 2) and a lower element if there is an embedding of 21 such that x is the image of (2, 1).
Following Albert, Atkinson, Brignall, Ruškuc, Smith, and West [3], elements which are either upper or lower elements of π are referred to as rigid elements, and π is called a rigid permutation if all of its elements are rigid (i.e., if π = U π ∪ L π ). The remaining elements will be called fluid elements. For an example of a 321-avoiding permutation and its decomposition into rigid and fluid elements, see Figure 1.
Note that it can be determined in linear time which elements are upper, lower and fluid in a permutation. For this purpose one simply needs to scan the permutation from left to right and record the largest element encountered so far, denoted by ℓ, and the smallest element yet to come at the right, denoted by s. When we read an element x, three cases can occur: • x > s: In this case x s forms a 21-pattern and thus x is an upper element. • x < ℓ: In this case ℓ x forms a 21-pattern and thus x is a lower element • x ≤ s and x ≥ ℓ (which implies that x = s and s > ℓ): In this case x does not occur in a 21-pattern and is thus a fluid element. The existence of fluid elements in a pattern will be the source of some difficulty in solving the Av(321)-PPM problem, and will be addressed in the next section. For the remainder of this section we consider a rigid pattern π of size k and a 321-avoiding text τ of size n. Since an embedding preserves relative locations of points, the image of any rigid element must be rigid. More precisely, we have the following: Observation 2.1. Let π be a rigid pattern and τ be an arbitrary 321-avoiding permutation. If there exists an embedding of π into τ , then it must map upper (resp., lower) elements of π to upper (resp., lower) elements of τ and the fluid elements of τ will never occur in an embedding.
In order to look for such embeddings we must widen our search space. A map f : π → τ is called a rigid mapping if f maps upper (resp., lower) elements of π to upper (resp., lower) elements of τ . As noted above, because π is rigid, every embedding of π into τ is a rigid mapping, but the converse is far from true since, among other reasons, rigid mappings need not be injective.
Given two points, x and y, in U π , we say x ≤ y if y lies above and to the right of x. This is a linear order on U π , and we have similar linear orders (all denoted ≤) on L π , U τ and L τ . This makes the set of all rigid mappings of π into τ into a partially ordered set using point-wise comparison; that is, given rigid mappings f, g : π → τ , we write f ≤ g if f (x) ≤ g(x) for all elements x of π. In fact, it is easy to see that this partially ordered set is a distributive lattice; given two rigid mappings f, g : π → τ their meet and join can be defined, respectively, by It is notable that these observations also hold for embeddings. That is, the set of embeddings from π into τ is a sublattice of the lattice of rigid mappings: Theorem 2]] Given a rigid pattern π and a 321-avoiding text τ , the set of embeddings of π into τ forms a distributive lattice under the operations of meet and join defined above.
It follows from Theorem 2.2 that if π is contained in τ then there is a minimum embedding of π into τ which we denote by e min .
Given an element x of some 321-avoiding permutation σ we define x U to be the rightmost element of U σ that is to the left of x. Of course, we have corresponding notations such as x L , x U and so on. In all cases, if no such element exists we get ⊥ as usual. We also define the type of x, T (x) to be U if x ∈ U σ and L if x ∈ L σ . The following result forms the core of our algorithm for determining whether there is an embedding of π into τ , at least for the case where π is rigid. It will allow us to turn an arbitrary rigid mapping into an embedding, if possible.

Proof:
We first establish that f (x ) T (x) ≤ e(x). Since x lies strictly to the right of x (and e is an embedding), e(x) lies strictly to the right of e(x ) and is of the same type as x, so, it does not lie to the left of e(x ) T (x) . Consequently, e(x ) T (x) ≤ e(x). But f (x ) ≤ e(x ) and so f (x ) T (x) ≤ e(x ) T (x) ≤ e(x). The arguments for the other case are exactly the same.
Applying the proposition above in the case where f = e, we see that for any embedding, e, from a rigid π into τ , and any x ∈ π: .
Now suppose that f is any rigid mapping from π to τ . We say that x is a problem if it violates the above condition, i.e., x is a problem if Intuitively , x is a problem if f (x) is too low compared with f (x ) or too far left compared with f (x ).
We let P (f ) be the set of problems for f , for which the following holds: Proof: If f is an embedding, it follows from Proposition 2.3 that no element x ∈ π fulfills condition (1).
Thus P (f ) is empty. For the other direction, assume that f is not an embedding. From Lemma 1.3 we know that there exists Moreover we know that f preserves types and thus f (x) and f (x ) T (x) have the same type. We conclude that We now describe an algorithm, displayed as Algorithm 1. Given as input a rigid permutation π and a 321-avoiding permutation τ , it returns the minimum embedding e min of π into τ when it exists, and fails otherwise. The algorithm constructs and updates a rigid mapping f , ensuring that f ≤ e min at all times (if an embedding exists). Let f 0 be the map that sends all the elements of U π to the least element of U τ and all elements of L π to the least element of L τ . Algorithm 1 Find a minimum embedding of π into τ , or demonstrate that no embeddings exist. Initialise The correctness of this algorithm is easy to establish. Within the while loop, if f is everywhere defined, P (f ) is non-empty, and x is chosen for the update step, then the updated version of f is strictly greater than the original at x, and has the same value elsewhere. Since the set of rigid maps is finite, the loop can be executed a bounded number of times, and the algorithm halts. In the case where e min exists, we certainly have f 0 ≤ e min . So, by Proposition 2.3, it is always the case that f ≤ e min . Therefore, when the loop terminates, the algorithm returns an embedding that is less than or equal to e min , and hence must equal e min . Should e min not exist, then termination can only occur because f is not everywhere defined, and so the algorithm fails as required in this case.
We can further combine the correctness analysis with a run-time analysis to obtain the following.
Proposition 2.5. Given a rigid 321-avoiding permutation π of size k and a 321-avoiding permutation τ of size n there is an algorithm which determines an embedding of π into τ if one exists, and fails otherwise, whose run-time is O(kn).

Proof:
The algorithm in question is Algorithm 1, and it remains to show that we can achieve the bound claimed for the run-time. As noted, each execution of the loop increases the value of f (x) for at least one x (in the linear ordering, ≤, of either U τ or L τ ). Since there are at most n possible values any f (x) can take, and only k distinct x, the loop certainly executes not more than kn times. So, if we can establish that the computation in the loop can be carried out in constant time, the claim follows.
In an initialisation phase (not part of the algorithm proper) we can certainly compute tables of all the values x a b for x in both π and τ , a ∈ { , , , } and b either absent or equal to one of L or U . For π this can be done in O(k) time, and for τ in O(n) time, so this initialisation can be absorbed into the claimed run-time. This ensures that the "Update" operations in the loop can be carried out in constant time. We can maintain P (f ) as a queue, and separately maintain an array of boolean values that indicate whether or not x ∈ P (f ). To start the loop, we dequeue some x. The update operation ensures that x is no longer a problem, so we can set its value in the array to false. Moreover, the update operation only changes the value of f (x), and increases it. So it cannot "solve" any existing problem (other than that of x) and the only other way that it could change the problem set would be if f (x) moved to the right of f (x ) or above f (x ). Therefore, in the recompute phase we only need to check those two possibilities, and enqueue x and/or x (setting their boolean values in the array to true) if necessary. By making reference to the array, we can ensure that we never have duplicate elements in the queue -so every iteration of the loop really does result in a proper update.
Let us end this section by providing a simple example illustrating how the presented algorithm works.
Example 2.6. Let us consider the text permutation τ = 3 1 2 4 5 9 6 7 10 8 11 13 12 represented in Figure 1 and the pattern π = 2 1 4 5 3. Note that π is indeed rigid, whereas τ is not; we can however ignore the fluid elements when looking for an embedding of π into τ as explained above. The upper elements in π are 2, 4 and 5 and the lower elements are 1 and 3. We now describe a possible run of the algorithm (the order in which problems are resolved is not determined): 2. We resolve the problem x = 4 for which we have max{f (x ) U , f (x ) U } = 9 and update f such that f (4) = 9. In order to recompute P (f ), we only need to check x = x = 5. We cannot possibly have resolved the problem 5 at the same time, so it remains in P (f ) and we have P (f ) = {3, 5}.
3. We resolve the problem x = 5 for which we have max{f (x ) U , f (x ) U } = 10 and update f such that f (5) = 10. In order to recompute P (f ), we only need to check x = 3 (5 is not defined). We cannot possibly have resolved the problem 3 at the same time, so it remains in P (f ) and we have 4. We resolve the last problem x = 3 for which we have max{f (x ) L , f (x ) L } = 8 and update f such that f (3) = 8. In order to recompute P (f ), we only need to check x = 4 (3 is not defined). The element 4 is no longer a problem since it is large enough and thus P (f ) is empty.

Fluid Elements and the O(kn) Algorithm for 321-Avoiding Permutations
In this section we aim to complete the proof of Theorem 1.1 and to do so we must face the issue of fluid elements in the pattern π. Since a fluid element participates in no 21, each other element of π is either below and left of it, or above and right of it. This is represented most easily using another notational convention. Suppose that σ and θ are two permutations of size m and n respectively. Then σ ⊕ θ is the permutation whose points are: Informally, to form σ ⊕ θ we just place θ above and to the right of σ. Clearly ⊕ is associative, though of course not commutative. For any 321-avoiding permutation π there is a unique decomposition: where, for 1 ≤ i ≤ t, π i is either rigid or a singleton, and it is never the case that both π i and π i+1 are rigid. The singleton elements of this representation correspond precisely to the fluid elements of π.
For an example, consider again Figure 1 where the black squares correspond to the blocks π i of this representation.
Given π of size k we can easily compute this representation in O(k) time, simply by finding the fluid elements of π (which are those elements that are both left-to-right maxima and right-to-left minima). Henceforth, we assume that this representation is given.
In the algorithm to determine whether π embeds in τ we will construct, for each 1 ≤ i ≤ t at most two embeddings of π 1 ⊕ · · · ⊕ π i into τ in such a way that, if any embedding of π into τ exists, then at least one of the two partial embeddings can be extended to a full embedding.
So we first consider the following question: given an embedding, e i , of π 1 ⊕ · · · ⊕ π i into τ that extends to an (unknown) embedding, e, of π into τ , how can we construct a pair of embeddings of π 1 ⊕ · · · ⊕ π i ⊕ π i+1 into τ , at least one of which extends to an embedding of π into τ ?
We distinguish three cases for π i+1 . For this purpose, let T i denote the set of elements that lie above and to the right of the image of e i . Then, the image of e restricted to the elements corresponding to π i+1 is contained in T i . Let us first consider the case where π i+1 is rigid. Then the image of e on the elements corresponding to π i+1 must be greater than or equal to (point by point), the image of π i+1 under its minimum embedding into T i . Thus, if we choose the minimal embedding of π i+1 into T i , the resulting embedding e i+1 extends to an embedding of π into τ . Though T i is, strictly speaking, not a permutation all of its associated operators are the same as those of τ (except some are undefined, e.g., the leftmost element of T i has no left neighbour in T i but may well have one in τ ). So, in this case we can use Algorithm 1 in order to find the minimal embedding of π i+1 into T i and hereby obtain a single extension of e i with the required property.
A similarly easy case is where π i+1 is a singleton, i.e., a fluid element and T i begins with its least element (which is a fluid element as well). Then nothing can be lost by mapping π i+1 to that element.
The only remaining case is where π i+1 is a singleton and the first element of T i is not its minimum. Since every element of T i lies above its first element, or above and to the right of its minimum, we can extend e i in two ways -one sending π i+1 to the leftmost element of T i (which is an upper element) and one to its minimum (which is a lower element), and one of these must be extensible.
Now it seems that we might have a problem -given two partial embeddings of π 1 ⊕ · · · ⊕ π i might they not extend to three or four candidate embeddings of π 1 ⊕ · · · ⊕ π i ⊕ π i+1 ? Indeed this is the case, but only if π i+1 is a singleton. If it has four possible images, two belong to U τ and two to L τ . Since all further elements of π lie above and to the right of this fluid element, we only need to retain the embeddings where its image is the lesser of the two in each of these sets. Likewise, if it has three possible images (one of which might be fluid), at least one of them can be ignored. Another way to say this is that because π 1 ⊕ · · · ⊕ π i ⊕ π i+1 ends with its maximum element, so do its images under the embedding. Among three or more elements of a 321-avoiding permutation there are at least one and at most two elements that do not participate as the 2 in a 12 pattern. We need retain only those embeddings whose maximum is not such a 2, as otherwise they could be replaced by an embedding with a smaller maximum in forming a full extension.
Since the sum of the size of the rigid permutations in the representation of π is at most the total size of π, the parts of the algorithm where we construct minimal rigid embeddings still require at most O(kn) time in total. Dealing with singletons (fluid elements) clearly requires only constant time since we can find the next (to the right) fluid/upper/lower element in τ in constant time. Also, filtering out non-optimal extensions can be done in constant time since only the maximal elements of these extensions have to be compared and at most four extensions exist at the same time. We conclude that the total cost of the algorithm is still O(kn). If τ contains π the algorithm terminates successfully and returns one or possibly two embeddings.
What if no embedding exists? Then, following the plan above as if it did (beginning from an empty map, i.e., the case i = 0) we must at some point reach a failing case of Algorithm 1, or possibly encounter an empty T i . In either case, we fail since we have demonstrated that no embedding can be possible.
This completes the proof of Theorem 1.1. Again, let us provide an example demonstrating how the algorithm for arbitrary 321-avoiding patterns works. 2. The next block π 2 is the singleton element 3. T 1 , the set of elements that lie above and to the right of the image of e starts with a fluid element and thus we can set e(3) = 4.
3. We have the same situation for π 3 which consists of the singleton element 4 and set e(4) = 5.
4. The block π 4 is again a singleton element. However, T 3 does not start with its minimal element and thus two choices are possible for e(5): we can either send 5 to the leftmost upper element in T 3 or to the leftmost lower element. We store these two possibilities: e U (5) = 9 and e L (5) = 6.
5. The next block π 5 is rigid and we thus apply Algorithm 1 which is not detailed here. For the choice e U (5) = 9 it leads to e U (7) = 13 and e U (6) = 12 whereas for e L (5) = 6 it leads to e L (7) = 10 and e L (6) = 8. These two partial embeddings are rigid and thus comparable: e L ≤ e U and we can disregard e U . This is a good choice, since e U cannot be extended to an embedding of π into τ since the last element 8 cannot be mapped anywhere.
6. It remains to determine e(8). Since T 5 starts with its minimal element we can choose this one and set e(8) = 11.

Skew-Merged Permutations
The permutations avoiding 321 can be partitioned into two monotone increasing sequences. Of course the permutations avoiding 123 can be similarly partitioned (into decreasing sequences) and the results of the previous section apply to them as well. However, the class of skew-merged permutations, those that can be partitioned into an increasing and a decreasing sequence, requires further analysis, though as we shall see the analogue of Theorem 1.1 is also true in this context. Towards this goal, we first identify a set of rigid elements of a skew-merged permutation. In Figure 2 these are the elements lying in the corner regions. Specifically we say that an element of a skew-merged permutation is of type: NE if it participates as a 3 in a 213; NW if it participates as a 3 in a 312; SW if it participates as a 1 in a 132; SE if it participates as a 1 in a 231, and we call any other element of a skew-merged permutation central. We first verify that the illustration of a skew-merged permutation shown in Figure 2 is correct. This is a result due to Atkinson [4], and so we only sketch part of the proof to give its flavour. Figure 2. Moreover, the central elements form a monotone subsequence.

Proposition 4.1. The elements of a skew-merged permutation decompose by type as shown in
Proof: Recall that another characterisation of skew-merged permutations is the following: they are those permutations that do not contain either 3412 or 2143.
Let a skew-merged permutation π be given, and suppose that π = I ∪ D is a partition of π into a monotone increasing and monotone decreasing sequence. Consider first elements of type NE (all other types can be handled by parallel arguments due to symmetry). Since any such participates as a 3 in a 213, it must belong to I (otherwise, the elements participating as the 2 and 1 would both belong to I which is of course impossible). So the elements of type NE form a monotone increasing sequence.
Suppose that C is of type NE, with BAC an occurrence of 213 and a is of type SW with acb an occurrence of 132. Then a ∈ I for similar reasons to the preceding ones. If C preceded a (and hence was also smaller than it) then, BAcb would be an occurrence of 2143. So, all elements of type SW lie below and to the left of those of type NE. Now suppose that z is of type NW, with zxy an occurrence of 312. If C were to precede z we would have various cases: first if C lay below y then BAzy would be 2143, if C lay above y but below z then Czxy would be 3412, if C lay above z and B above y, then BCxy would be 3412, but if B lay below y then BAzy would be 2143. As all these cases lead to contradictions, C must follow z.
All other cases can be dealt with similarly. Finally, to see that the central elements form a monotone sequence observe that they must certainly avoid all of 132, 213, 231, and 312 lest some of them be noncentral. But, only monotone permutations (of either type) avoid these four permutations.
This decomposition can be computed in linear time: Given a skew-merged permutation of size n, there is an algorithm that computes its partition into types in O(n)-time.
Proof: Let θ be an arbitrary skew-merged permutation. Notice that the part of θ to the left of the leftmost element of type NE or SE avoids 231 and 213. Such permutations have a characteristic > shape since any element must not be intermediate in value between two to its right. We are interested in finding the maximum prefix of θ which has this characteristic shape, or what amounts to the same thing, the leftmost element of θ such that the prefix ending at that element involves 231 or 213.
This can be accomplished in linear time: we scan θ from left to right and determine for every position i whether it is an ascent (θ(i) < θ(i + 1)) or a descent (θ(i) > θ(i + 1)). At any moment we only store the last encountered ascent a and descent d. The element θ(i) plays the role of a 1 in a 231 pattern, if θ(i) < θ(a); it plays the role of a 3 in a 213, if θ(i) > θ(d). If either of the two conditions apply to position i, we have identified the leftmost element of type NE or SE. That is, we have found the boundary line between the centre region and the Eastern region of θ.
In a similar manner we can find all of the boundary lines: by scanning θ from right to left we find the boundary between West and centre, by scanning from bottom to top we find the boundary between South and centre and by scanning from top to bottom we find the boundary between North and centre. We can thus compute the partition of θ into types by scanning θ four times.
We will now describe an algorithm for skew-merged patterns and texts and provide the necessary theoretic background. This algorithm consists of two main parts: In the first part, the non central elements of the pattern π are embedded into τ using a similar approach as for rigid permutations and adapting Algorithm 1 which will deliver a minimal embedding of the non-central elements. In the second part, we will extend this minimal embedding to the central elements of π.
In this sense, the non-central elements of a skew-merged permutation correspond to the rigid elements of a 321-avoiding permutation. Since they are defined by the occurrence of certain patterns and since embeddings preserve such patterns it is immediately clear that if e : π → τ is an embedding of one skew-merged permutation into another, then e must preserve the type of all non-central elements.
In order to be able to speak of minimal embeddings in the context of skew-merged permutations, we need to introduce some new notation. For two non-central elements of the same type we write x ✁ y if x lies strictly further out from the center than y (x ✂ y will mean that either x ✁ y or x = y). The minimum with respect to this relation ✁ is denoted by outer and the maximum by inner. For two embeddings, e 1 and e 2 of the non-central elements of π into τ that preserve types we can define their meet by e 1 ∧ e 2 (x) = outer{e 1 (x), e 2 (x)} for all non-central x ∈ π. Then, just as in the 321-avoiding case, e 1 ∧ e 2 is also an embedding of the non-central elements of π into those of τ : Lemma 4.3. Let π be a skew-merged pattern with no central elements and τ be an arbitrary skew-merged permutation. Then the following holds: If e 1 and e 2 are embeddings of π into τ then their meet f := e 1 ∧e 2 as defined above is an embedding as well.
Proof: Let x = y be two elements in π and let us assume that x lies to the left of y in π. We need to show that f (x) lies to the left of f (y) in τ and that the horizontal relation between x and y is preserved as well. The key argument is that taking the minimum of the elements in the above sense automatically translates into taking their actual minimum or maximum (equivalently, the leftmost or rightmost element), depending on the type of element. In order to give a formal proof, we distinguish between three cases.
• If x and y are of the same type. We detail the case of SW elements here, as the other ones are analogous (one simply needs to replace "minimum" by "maximum" and/or "left of" by "right of" depending on the type). In this case, taking the minimum of the elements in the sense defined earlier is nothing else than taking their actual minimum, which again is the same as taking the left-most element. Since we have that f (x) ≤ e 1 (x) < e 1 (y) (and f (x) is to the left of e 1 (y)) as well as f (x) ≤ e 2 (x) < e 2 (y) (and f (x) is to the left of e 2 (y)), it follows that f (x) < outer(e 1 (y), e 2 (y)) = f (y) (and f (x) is to the left of f (y)).
• If x and y lie in opposite corners of the diagram. In this case the statement follows immediately from the fact that an embedding preserves types. Indeed, all SW elements are to the left of and smaller than NE ones and all NW elements are to the left of and larger than SE ones. Thus both the vertical as well as the horizontal relation between x and y is preserved.
• The remaining cases, where x and y are not of the same type, but are both elements in the south, north, east or west. We detail the case of two elements in the north, i.e., x is a NW element and y a NE one. The other cases can be dealt with analogously (by interchanging minimum with maximum or vertical with horizontal positions). Without loss of generality, we further assume that x < y. First, it is clear that f (x) lies strictly to the left of f (y) since types are preserved. Second, regarding the horizontal relation between x and y, let us note that taking the element that is furthest away from the centre translates into taking the maximum. Thus, we have that f (y) ≥ e 1 (y) > e 1 (x) as well as f (y) ≥ e 2 (y) > e 2 (x) which implies that f (y) > f (x).
The consideration of these cases completes the proof.
Observe the following: if either e 1 or e 2 was the restriction of an actual embedding, e, of π into τ to the non-central elements then we can extend the mapping e 1 ∧ e 2 to central elements using e there, and thereby obtain an embedding. So, among all embeddings of π into τ there is one whose effect on the non-central elements is the minimum of all the embeddings of the non-central elements of π into those of τ . We will see later on how such an extension to the central elements of π can be found.
This minimum embedding of the non-central elements can be found by modifying the definition of the problem set and the update rule of Algorithm 1. The only thing we need to do in order to reflect the new notion of minimum/maximum in this definition, is to redefine the notation introduced in the Introduction. Given a non-central element x in a skew-merged permutation π, we denote by (wherever possible): x oh the next non-central element further out from the center in horizontal direction, x ih the next non-central element towards the center (inwards) in horizontal direction, x iv the next non-central element towards the center (inwards) in vertical direction, and x ov the next non-central element further out from the center in vertical direction. For example, in the skew-merged pattern π depicted in Figure 3 and x = 7, we have: x oh = 1, x ih = ⊥, x iv = 6, and x ov = ⊥.
We also define the type of a non-central element x in a skew-merged permutation, T (x), to be the corner in which x lies, i.e., T (x) can be NW, SW, NE or SE. Moreover, we extend the notation introduced above as follows: For a ∈ {oh, ih, iv, ov} and b ∈ {N E, SE, SW, N W }, we define x a b to be the next non-central element in π according to direction a that is of type b. In other words, x a b is the first element in the sequence (x a , (x a ) a , . . . ) of type b. If there is no such element, i.e., no element in (x a , (x a ) a , . . . ) is of type b, then we set x a b = ⊥. For example, in the skew-merged pattern π depicted in Figure 3 and x = 5, we have x ov N W = 7, x ov N E = 6, whereas x oh SE = ⊥ and x iv SE = ⊥. With this new notation, one can see that an analogue of Proposition 2.3 holds for skew-merged permutations:

Proposition 4.4.
Suppose that e is an embedding of the non-central elements of π into τ , f is a mapping of the non-central elements of π into τ that preserves types, and, for all non-central x ∈ π, f (x) ✂ e(x). Then, for all non-central x ∈ π: The proof of this Proposition is analogous to the one of Proposition 2.3. We thus say that a non-central element x of a skew-merged permutation is a problem if: for a mapping f of the non-central elements of π into τ that preserves types. Moreover, when we resolve the problem x by updating the value of f (x) this is done analogously to the case of 321-avoiding permutations and we set f (x) = inner f (x oh ) ih T (x) , f (x ov ) iv T (x) . This finishes the description of the necessary modifications of Algorithm 1. As for Algorithm 1 we assume that x a and x a b for a ∈ {oh, ih, iv, ov} and b ∈ {N E, SE, SW, N W } is precomputed and thus can be found in constant time. Given the decomposition of π and τ into types, these precomputations can be done in linear time. Both the steps required for the update of f and the recomputation of the problem set P (f ) can be carried out in constant time.
To complete the proof of Theorem 1.2 we must show that, having found a minimum embedding of the non-central elements of π to those of τ , the existence of a full embedding can also be determined sufficiently quickly. We need to determine whether or not the central part of π can be embedded into the remainder of τ , i.e., the set of elements in τ which consists of central elements and all adjacent elements that have not yet been used in the minimum embedding. The central part of π is a monotone pattern of a certain size at most k, and the remaining part of τ is a skew-merged permutation of size at most n (whose endpoints we know).
In general, finding a longest increasing (or decreasing) subsequence of size k in a permutation of size n can be done in time O(n log log k) [7]. Thus, checking whether the central part can be embedded into π = 1 7 3 4 2 5 6 τ = 10 1 9 3 5 4 6 2 7 8 the remaining part of τ can be done within the O(nk) runtime bound of our algorithm. In the special case of skew-merged permutations, finding a longest increasing (resp., decreasing) subsequence can even be done in O(n) time.
To be more precise, O(n) time is only required for obtaining the partition into five types as represented in Figure 2 (which is already available in our case); the remaining steps require only constant time. Indeed, for longest increasing subsequences the following observations can be made (the case of decreasing subsequences can be treated analogously): The elements of type SW and NE will always contribute to a longest increasing subsequence. Moreover, such a subsequence also contains as many elements as possible from the centre, i.e., if the center is increasing then all elements contribute to a longest increasing subsequence and if the center is decreasing we can arbitrarily pick one centre element. Note that it is never advantageous to include elements of type NW or SE. This can be seen as follows: At most one NW or SE element can be part of an increasing subsequence. Thus, if the centre is non-empty, it is certainly not advantageous to include a NW or SE element. Let us assume that the centre is empty. An element of type NW occurs as a 3 in a 312 pattern. Among the elements playing the role of the 1 and the 2, at least one element (and possibly both of them) is of type SW or NE. Thus, including an element of type NW would force us to exclude one or two elements of type SW or NE. In other words, we cannot increase the size of an increasing subsequence by adding an element of type NW. A similar argument holds for elements of type SE. We conclude that for the size of the longest increasing subsequence we only have to add the number of elements of type SW and NE as well as the size of the longest increasing subsequence in the central part. Let us end this section by providing a simple example illustrating how this modified version of Algorithm 1 works. Example 4.5. Let us consider the text permutation τ = 10 1 9 3 5 4 6 2 7 8 and the pattern π = 1 7 3 4 2 5 6. Both permutations and their decomposition into types are shown in Figure 3. We start by describing a possible run of the algorithm (the order in which problems are resolved is not determined) finding the minimal embedding of the non-central elements of π into τ : 1. We start with the initial mapping f = f 0 that sends all non-central elements of one type in π to the minimal element of this type in τ (i.e., the element that is furthest out from the center). It is defined as follows: f (1) = 1, f (7) = 10, f (5) = f (6) = 8 and f (2) = 2. We compute the problem set Note added in proof. After a draft of this paper was posted on the arXiv, Jelínek and Kynčl [12] established that the Av(β)-PATTERN PPM problem is indeed NP-complete for every β / ∈ {1, 12, 21, 132, 213, 231, 312}.
They further showed that the Av(4321)-PPM problem is NP-complete, even when the pattern is restricted to be 321-avoiding.