Finding and counting permutations via CSPs

Permutation patterns and pattern avoidance have been intensively studied in combinatorics and computer science, going back at least to the seminal work of Knuth on stack-sorting (1968). Perhaps the most natural algorithmic question in this area is deciding whether a given permutation of length $n$ contains a given pattern of length $k$. In this work we give two new algorithms for this well-studied problem, one whose running time is $n^{k/4 + o(k)}$, and a polynomial-space algorithm whose running time is the better of $O(1.6181^n)$ and $O(n^{k/2 + 1})$. These results improve the earlier best bounds of $n^{0.47k + o(k)}$ and $O(1.79^n)$ due to Ahal and Rabinovich (2000) resp. Bruner and Lackner (2012) and are the fastest algorithms for the problem when $k \in \Omega(\log{n})$. We show that both our new algorithms and the previous exponential-time algorithms in the literature can be viewed through the unifying lens of constraint-satisfaction. Our algorithms can also count, within the same running time, the number of occurrences of a pattern. We show that this result is close to optimal: solving the counting problem in time $f(k) \cdot n^{o(k/\log{k})}$ would contradict the exponential-time hypothesis (ETH). For some special classes of patterns we obtain improved running times. We further prove that $3$-increasing and $3$-decreasing permutations can, in some sense, embed arbitrary permutations of almost linear length, which indicates that an algorithm with sub-exponential running time is unlikely, even for patterns from these restricted classes.

Perhaps the most important enumerative result related to permutation patterns is the theorem of Marcus and Tardos [43] from 2004, stating that the number of length-n permutations that avoid a fixed pattern π is bounded by c(π) n , where c(π) is a quantity independent of n. (This was conjectured by Stanley and Wilf in the late 1980s.) A fundamental algorithmic problem in this context is Permutation Pattern Matching (PPM): Given a length-n permutation τ ("text") and a length-k permutation π ("pattern"), decide whether τ contains π.
Solving PPM is a bottleneck in experimental work on permutation patterns [4]. The problem and its variants also arise in practical applications, e.g. in computational biology [39, § 2.4] and time-series analysis [38,10,48]. Unfortunately PPM is, in general, NP-complete, as shown by Bose, Buss, and Lubiw [15] in 1998. For small (e.g. constant-sized) patterns, the problem is solvable in polynomial (in fact, linear) time, as shown by Guillemot and Marx [33] in 2013. Their algorithm has running time n · 2 O(k 2 log k) , establishing the fixed-parameter tractability of the PPM problem in terms of the pattern length. The algorithm builds upon the Marcus-Tardos proof of the Stanley-Wilf conjecture and introduces a novel decomposition of permutations. Subsequently, Fox [30] refined the Marcus-Tardos result, thereby removing a factor log k from the exponent of the Guillemot-Marx bound. (Due to the large constants involved, it is however, not clear whether the algorithm can be efficient in practice.) For longer patterns, e.g. for k ∈ Ω(log n), the complexity of the PPM problem is less understood. An obvious algorithm with running time O(n k+1 ) is to enumerate all n Our first result is based on the observation that PPM can be formulated as a constraint satisfaction problem (CSP) with binary constraints. In this view, the path-based dynamic programming of previous works has a natural interpretation not observed earlier: it amounts to solving the CSP instance by Seidel's invasion algorithm, a popular heuristic [52], [57, § 9.3].
It is well-known that binary CSP instances can be solved in time O(n t+1 ), where n is the domain size, and t is the treewidth of the constraint graph [32,24]. In our reduction, the domain size is the length n of the text τ , and the constraint graph is the incidence graph of the pattern π; we thus obtain a running time of O(n tw(π)+1 ), improving upon the earlier O(n 2·tw(π)+1 ). Second, making use of a bound known for low-degree graphs [28], we prove that the treewidth of the incidence graph of π is at most k/3 + o(k). The final improvement from k/3 to k/4 is achieved via a technique inspired by recent work of Cygan, Kowalik, and Socała [23] on the k-OPT heuristic for the traveling salesman problem (TSP).
In summary, we obtain the following result, proved in § 3.

Theorem 1. Permutation Pattern Matching can be solved in time n k/4+o(k) .
Expressed in terms of n only, none of the mentioned running times improve, in the worst case, upon the trivial 2 n ; consider the case of a pattern of length k ≥ n/ log n. The first improvement in this parameter range was obtained by Bruner and Lackner [18]; their algorithm runs in time O(1.79 n ).
The algorithm of Bruner and Lackner works by decomposing both the text and the pattern into alternating runs (consecutive sequences of increasing or decreasing elements), and using this decomposition to restrict the space of admissible matchings. The exponent in the running time is, in fact, the number of runs of T , which can be as large as n. The approach is compelling and intuitive, the details, however, are intricate (the description of the algorithm and its analysis in [18] take over 24 pages).
Our second result improves this running time to O(1.618 n ), with an exceedingly simple approach. A different analysis of our algorithm yields the bound O(n k/2+1 ), i.e. slightly above the Ahal-Rabinovich bound [1], but with polynomial space. The latter bound also matches an earlier result of Guillemot and Marx [33, § 7], obtained via involved techniques.

Theorem 2. Permutation Pattern Matching can be solved using polynomial space, in time
At the heart of this algorithm is the following observation: if all even-index entries of the pattern π are matched to entries of the text τ , then verifying whether the remaining odd-index entries of π can be correctly matched takes only a linear-time sweep through both π and τ . This algorithm can be explained very simply in the CSP framework: after substituting a value to every even-index variable, the graph of the remaining constraints is a union of paths, and hence can be handled very easily.
Counting patterns. We also consider the closely related problem of counting the number of occurrences of π in τ , i.e. finding the number of subsequences of τ that have the same ordering as π. Easy modifications of our algorithms solve this problem within the bounds of Theorems 1 and 2. Note that the FPT algorithm of Guillemot and Marx [33] cannot be adapted for the counting version. In fact, we argue ( § 5) that a running time of the form n O(k) is almost best possible and a significant improvement in running time for the counting problem is unlikely.

Theorem 4.
Assuming the exponential-time hypothesis (ETH), there is no algorithm that counts the number of occurrences of π in τ in time f (k) · n o(k/ log k) , for any function f . Special patterns. It is possible that PPM is easier if the pattern π comes from some restricted family of permutations, e.g. if it avoids some smaller fixed pattern σ. Several such examples have been studied in the literature, and recently Jelínek and Kyncl [37] obtained the following characterization: PPM is polynomial-time solvable for σ-avoiding patterns π, if σ is one of (1), (1,2), (1,3,2), (2,1,3) or their reverses, and NP-complete for all other σ. (All tractable cases are such that π is a separable permutation [15,36,59,4].) In particular, Jelínek and Kyncl show that PPM is NP-complete even if π avoids (1, 2, 3) or (3, 2, 1), but polynomial-time solvable for any proper subclass of these families. For (1, 2, 3)-avoiding and (3, 2, 1)-avoiding patterns, it is known however, that PPM can be solved in time n O( √ k) , i.e. faster than the general case (Guillemot and Vialette [34]). These results motivate the following general and natural question.

Question. What makes a permutation pattern easier to find than others?
A permutation is t-monotone, if it can be obtained by interleaving t monotone sequences. When all t sequences are increasing (resp. decreasing), we call the resulting permutation t-increasing (resp. t-decreasing). It is well-known that t-increasing (resp. t-decreasing) permutations are exactly those that avoid (t + 1, . . . , 1), resp. (1, . . . , t + 1), see e.g. [7].
We prove that if π is 2-monotone, then the running time of the algorithm of Theorem 1 is n O( √ k) . This result follows from bounding the treewidth of the incidence graph of π, by observing that this graph is almost planar. For 2-increasing or 2-decreasing patterns we thus match the bound of Guillemot and Vialette by a significantly simpler argument. (In these special cases the incidence graph is, in fact, planar.) Jordan-permutations are a natural family of geometrically-defined permutations with applications in computational geometry [50]. They were studied by Hoffmann, Mehlhorn, Rosenstiehl, and Tarjan [35], who showed that they can be sorted with a linear number of comparisons (see also [2] for related enumerative results). A Jordan permutation is generated by the intersection-pattern of two simple curves in the plane: label the intersection points between the curves in increasing order along the first curve, and read out the labels along the second curve; the obtained sequence is a Jordan-permutation ( Figure 1). As the incidence graph of the pattern π is planar whenever π is a Jordan-permutation, in this case too an n O( √ k) bound on the running time follows.
We show that both 2-monotone (and even 2-increasing or 2-decreasing) and Jordanpermutations of length O(k) may contain grids of size √ k × √ k in their incidence-graphs, both statements of Theorem 5 are therefore tight, via known lower bounds on the treewidth of grids [11].
In light of these results, one may try to obtain further treewidth-bounds for families of patterns, in order to solve PPM in sub-exponential time. In this direction we show a (somewhat surprising) negative result.

Theorem 6.
There are 3-increasing permutations of length k whose incidence graph has treewidth Ω(k/ log k).
The same bound applies, by symmetry, to 3-decreasing permutations. The result is obtained by embedding the incidence graph of an arbitrary permutation of length O(k/ log k) as a minor of the incidence graph of a 3-increasing permutation of length k.
Theorems 5 and 6 (proved in § 4) lead to an almost complete characterization of the treewidth of σ-avoiding patterns. By the Erdős-Szekeres theorem [27] every k-permutation contains a monotone pattern of length √ k . Thus, for all permutations σ of length at least 10, the class of σ-avoiding permutations contains all 3-increasing or all 3-decreasing permutations, hence by Theorem 6 there exist σ-avoiding patterns π with tw(π) ∈ Ω(k/ log k). Addressing a few additional small cases by similar arguments (details given in the forthcoming thesis of the first author), the threshold 10 can be further reduced. We remark that no algorithm is known to solve PPM in time n o(tw(π)) ; see the discussion in [1,37].
With a weaker bound we obtain a full characterisation that strengthens the dichotomyresult of Jelínek and Kyncl: in the worst case, the only σ-avoiding patterns π for which tw(π) ∈ o( √ k) are those for which PPM is known to be polynomial-time solvable (Appendix A.4).
Further related work. The complexity of the PPM problem has also been studied under the stronger restriction that the text τ is pattern-avoiding. The problem is polynomialtime solvable if τ is monotone [21] or 2-monotone [19,34,6,4,45], but NP-hard if τ is 3-monotone [37]. A broader characterization is missing.
Only classical patterns are considered in this paper; variants in the literature include vincular, bivincular, consecutive, and mesh patterns; we refer to [17] for a survey of related computational questions.
Newman et al. [46] study pattern matching in a property-testing framework (aiming to distinguish pattern-avoiding sequences from those that contain many copies of the pattern). In this setting, the focus is on the query complexity of different approaches, and sampling techniques are often used; see also [9,31].
A different line of work investigates whether standard algorithmic problems on permutations (e.g. sorting, selection) become easier if the input can be assumed to be patternavoiding [8,20].
We define four neighbors of a point (x, y) ∈ S σ as follows.
N R ((x, y)) = (x + 1, σ(x + 1)), N U ((x, y)) = (σ −1 (y + 1), y + 1), The superscripts R, L, U , D are meant to evoke the directions right, left, up, down, when plotting S σ in the plane. Some neighbors of a point may coincide. When some index is out of bounds, we let the offending neighbor be a "virtual point" as follows: N R (n, i) = N U (i, n) = (∞, ∞), and N L (1, i) = N D (i, 1) = (0, 0), for all i ∈ [n]. The virtual points are not contained in S σ , we only define them to simplify some of the statements.
The incidence graph of a permutation σ is In words, each point is connected to its (at most) four neighbors: its successor and predecessor by index, and its successor and predecessor by value. It is easy to see that G σ is a union of two Hamiltonian paths on the same set of vertices and that this is an exact characterization of permutation incidence-graphs. (See Figure 1 for an illustration.) Consider a function f : S π → S τ . We say that f is a valid embedding of S π into S τ if for all p ∈ S π the following hold: (1) whenever the corresponding neighbor N α (p) is also in S π , i.e. not a virtual point. In words, valid embeddings preserve the relative positions of neighbors in the incidence graph.

Lemma 7 (Proof in Appendix A.1). Permutation τ contains permutation π if and only if there exists a valid embedding
For sets A ⊆ B ⊆ S π and functions g : In this case, we also say that f is the extension of g to B. Restrictions of valid embeddings will be called partial embeddings. We observe that if f : B → S τ is a partial embedding, then it satisfies conditions (1) and (2) with respect to all edges in the induced graph G π [B], i.e. the corresponding inequality holds whenever p, N α (p) ∈ B.

Pattern matching as constraint satisfaction
Readers familiar with the terminology of CSPs should immediately recognize that the definition of valid embedding and Lemma 7 allow us to formulate PPM as a CSP instance with binary constraints. Then known techniques can be applied to solve the problem. A (somewhat different) connection of PPM to CSPs was previously observed by Guillemot and Marx [33]. We first review briefly the CSP problem, referring to [57,52,22] for more background.
A solution of the CSP instance is a function f : V → D (i.e. an assignment of admissible values to the variables), such that for each constraint The reduction from PPM to CSP is straightforward. Given a PPM instance with text τ and pattern π, of lengths n and k respectively, let V = {x 1 , . . . , x k }, and D = {1, . . . , n}. The fact that variable x i takes value j signifies that π(i) is matched (embedded) to τ (j). For the embedding to be valid, by Lemma 7, the relative ordering of entries must be respected, in accordance with conditions (1) and (2). These conditions can readily be described by binary relations for all pairs of variables whose corresponding entries are neighbors in the incidence graph G π .
More precisely, for p, N α (p) ∈ S π , for α ∈ {R, L, U, D}, we add constraints of the form x and R contains those pairs (a, b) ∈ [n] 2 , for which the relative position of (a, τ (a)) and (b, τ (b)) matches the relative position of p and N α (p).
The constraint graph of the binary CSP instance (also known as primal graph or Gaifman graph) is a graph whose vertices are the variables V and whose edges connect all pairs of variables that occur together in a constraint. Observe that for instances obtained via our reduction, the constraint graph is exactly the incidence graph G π . We make use of the following well-known result.

Lemma 8 ([32, 24]). A binary CSP instance
where t is the treewidth of the constraint graph.
As discussed in § 2, the incidence graph G π consists of two Hamiltonian-paths. Accordingly, its vertices have degree at most 4, and the following structural result is applicable.
Lemma 9 ( [28,29]). If G is an order-k graph with vertices of degree at most 4, then the pathwidth (and consequently, the treewidth) of G is at most k/3 + o(k). A corresponding tree-(path-)decomposition can be found in polynomial time.
Algorithms. Our first algorithm amounts to reducing the PPM instance to a binary CSP instance, and using the algorithm of Lemma 8 with a tree-decomposition obtained via Lemma 9. To reach the bound given in Theorem 1, it remains to improve the k/3 term in the exponent to k/4. We achieve this with a recent technique of Cygan et al. [23], developed in the context of the k-OPT heuristic for TSP.
In our setting, the technique works as follows. We split [n] into n 1/4 contiguous intervals of equal widths, n 3/4 each. (For simplicity, we ignore issues of rounding and divisibility.) The intervals induce vertical strips in the text τ . For each pattern-index i ∈ [k] we guess the vertical strip of τ into which i is mapped in the sought-for embedding of π into τ . It is sufficient to do this for a subset of the entries in π, namely those that become the leftmost in their respective strips in τ . Let X ⊆ [k] be the set of indices of such entries in π.
Guessing X and the strips of τ into which entries of X are mapped increases the running time by a factor of X⊆[k] |X|/4 . Assuming that we guessed correctly, the problem simplifies. First, each pattern-entry can now be embedded into at most n 3/4 possible locations, hence the domain of each variable will be of size at most n 3/4 . Second, the horizontal constraints that go across strip-boundaries can now be removed as they are implicitly enforced by the distribution of entries into strips (the L-constraint of every X-entry is removed). We have thus reduced the number of edges in the constraint-graph by |X| − 1 and can use a stronger upper bound of (k − |X|)/3 + o(k) on the treewidth (see e.g. [28,23]). The overall running time becomes We remark that our use of this technique is essentially the same as in Cygan et al. [23], but the CSP-formalism makes its application more transparent. We suspect that further classes of CSPs could be handled with a similar approach.
The even-odd method. The algorithm for Theorem 2 can be obtained as follows. Let (Q E , Q O ) be the partition of S π into points with even and odd indices. Formally, Construct the CSP instance corresponding to the problem as above. A solution is now found by trying first every possible combination of values for the variables representing Q E . Clearly, there are n |Q E | = n k/2 possible combinations. If the value of a variable x i is fixed to a ∈ [n], then x i is removed from the problem and every neighbor of x i is restricted by a new unary constraint in an appropriate way, i.e. if there is a constraint (( How does the constraint graph look like if we remove every variable (and its incident edges) corresponding to Q E ? It is easy to see that this destroys every constraint corresponding to L-R neighbors and all the remaining binary constraints represent U-D neighbors. As these constraints form a Hamiltonian path, the remaining constraint graph consists of a union of disjoint paths. Such graphs have treewidth 1, hence the resulting CSP instance can be solved efficiently using Lemma 8, resulting in the running time O(n k/2+2 ). A more careful argument improves this bound to O(n k/2+1 ); we describe the details in Appendix A.2.
We can refine the analysis, noting that when we are assigning values a 2 < a 4 < a 6 < . . . to the variables x 2 , x 4 , x 6 , . . . representing Q E , then we need to consider only increasing sequences where there is a gap of at least one between each successive entry (e.g. a 4 > a 2 + 1) to allow a value for the odd-indexed variables. The number of such subsequences is n− k/2 k/2 : consider a sequence with a minimum required gap of one between consecutive entries, then distribute the remaining total gap of n − k among the k/2 + 1 slots. As max k n−k k = O(1.6181 n ), see e.g. [55,54], we obtain an upper bound of this form (independent of k) on the running time of the algorithm.
Counting solutions. The algorithms described above can be made to work for the counting version of the problem. This has to be contrasted with the FPT algorithm of Guillemot and Marx [33], which cannot be adapted for the counting version: a crucial step in that algorithm is to say that if the text is sufficiently complicated, then it contains every pattern of length k, hence we can stop. Indeed, as we show in § 5, we cannot expect an FPT algorithm for the counting problem.
To solve the counting problem, we modify the dynamic programming algorithm behind Lemma 8 in a straightforward way. Even if not stated in exactly the following form, results of this type are implicitly used in the counting literature. It is not difficult to see that by replacing the use of Lemma 8 with Lemma 10 in the algorithms of Theorems 1 and 2, the counting algorithms stated in Theorem 3 follow.

Special patterns
In this section we prove Theorems 5 and 6. We define a k-track graph G = (V, E) to be the union of two Hamiltonian paths H 1 and H 2 , where V can be partitioned into sequences S 1 , S 2 , . . . , S k , the tracks of G, such that both H 1 and H 2 visit the vertices of S i in the given order, for all i ∈ [k]. Observe that k-track graphs are exactly the incidence graphs of permutations that are either k-increasing or k-decreasing.
2-monotone patterns. We prove Theorem 5(i). As a special case, we first look at patterns that are 2-increasing. Let G be a 2-track graph. Arrange the vertices of the two tracks on a line , the first track in reverse order, followed by the second track in sorted order. Any Hamiltonian path that respects the order of the two tracks can be drawn (without crossings) on one side of . This means that the two Hamiltonian paths of G can be drawn on different sides of , and therefore G is planar. See Figure 2 (left) for an example. The treewidth of a k-vertex planar graph is known to be O( √ k) [11,25]. A corresponding path-decomposition can be obtained by a recursive use of planar separators. For the case of a pattern π that consists of an increasing and a decreasing subsequence (i.e. 2-monotone patterns), we show that the straight-line drawing of G π (with points S π as vertices) has at most one intersection. An O( √ k) bound on the treewidth follows via known results [26]. Divide S π by one horizontal and one vertical line, such that each of the resulting four sectors contains a monotone sequence. More precisely, the top left and bottom right sectors contain decreasing subsequences, and the other two sectors contain increasing subsequences. Let e = {u, v} be an edge of the horizontal Hamiltonian path such that u.x = v.x − 1, and let f = {s, t} be an edge that intersects e, such that s.x < t.
Thus (s, u, v, t) must form the pattern (3, 1, 4, 2), and therefore s and t belong to the decreasing and u and v to the increasing subsequence. It is easy to see now that s, u, v, t must be in pairwise distinct sectors, and u (s, t, v) is the unique rightmost (bottommost, topmost, leftmost) point of the bottom left (top left, top right, bottom right) sector, and due to the monotonicity of all four sectors no more intersections can happen; see Figure 2 (right). This concludes the proof of Theorem 5(i).
We show that the previous result is tight, by constructing a 2-track graph G = (V, E) with n = 2k 2 vertices, for some even k, that contains a k × 2k grid graph.
Let x 1 , x 2 , . . . , x k 2 and y 1 , y 2 , . . . , y k 2 be the two tracks of G. We obtain G as the union of the following two Hamiltonian paths: The two Hamiltonian paths respect the order of the two tracks. We now relabel the vertices to show the contained grid. For i ∈ [k], j ∈ [2k], let z i,j = x j/2 k+i if j is odd, and z i,j = y (j/2−1)k+i if j is even. It is easy to see that z i,j is adjacent to z i+1,j and z i,j+1 for i ∈ [k − 1] and j ∈ [2k − 1]. For an illustration of the obtained permutation and the contained grid graph, see Figure 3. The case of Jordan patterns (Theorem 5(ii)) is immediate, as the incidence graph of Jordan permutations is by definition planar. We defer the details to Appendix A.3.
3-monotone patterns. We now prove Theorem 6. We first need some definitions and observations. For a set Π of length-n permutations, define the graph G H (Π) = ([n], E), where E is the union of the Hamiltonian paths corresponding to all π ∈ Π. For an arbitrary length-n permutation π, the graph G π is isomorphic to G H ({id n , π}), where id n is the length-n identity permutation.
Permutation π is a split of a permutation π if π arises from π by moving a subsequence of π to the front. For example, (1, 3, 5, 2, 4) is a split of id 5 , obtained by moving (1,3,5) to the front. We call a permutation split permutation if it is a split of the identity permutation.
To prove Theorem 6, we show that the graph G H (Π) can be embedded (as a minor) in the incidence graph G of some permutation of length at most 2mn. We further show that G is a 3-track graph (and thus, its underlying permutation can be assumed 3-increasing). The lower bound on the treewidth of G then follows by (i) choosing π to be a permutation whose incidence graph has treewidth Ω(n), (ii) the fact that G π is a subgraph of G H (Π), and thus, a minor of G, and (iii) the observation that the treewidth of a graph is not less than the treewidth of its minor.
We first define the vertex sets corresponding to the three tracks of G. Let be the vertex set of G, and observe that |V | = mn + (m − 1)n ≤ 2mn. Figure 6 (left) shows the vertices in an arrangement useful for the rest of the construction.
To later show that G is a 3-track graph, we fix a total order ≺ on each track, namely, the lexicographic order of the vertex-indices, i.e. x i,j ≺ x i ,j if and only if i < i or (i = i ) ∧ (j < j ), and analogously for V y and V z . Before proceeding, we define the following functions: Note that s x is just the successor with respect to the total order ≺ on V x , and that s c is a bijection. Figure 6 (middle) illustrates the two functions. Now we define the two Hamiltonian paths whose union is G. The first path P 1 goes as follows: start at x 1,1 , then, from every x i,j with i < m, go to s c (x i,j ), and then to s x (x i,j ). For x m,j with j < n, go directly to s x (x m,j ). Path P 1 contains all vertices of V x in the correct order. The same holds for V y and V z , by the definition of s c .
The second path P 2 also starts at x 1,1 , but first goes along V x until it reaches x 2,1 , i.e. the first part of P 2 is x 1,1 , x 1,2 , . . . , x 1,n , x 2,1 . Then, from every x i,j with i ≥ 2, it first moves to s −1 c (x i,j ) and then to s x (x i,j ). Again, P 2 contains all vertices of V x in the correct order. As s −1 c (x i,j ) is either y i−1,j or z i−1,j , this is also true for V y and V z .
To obtain G H (Π) = ([n], E), color the vertices of the graph with n colors, where color k induces a path C k of length m in G. We then prove that for each {k 1 , k 2 } ∈ E, the graph G contains adjacent vertices of the colors k 1 and k 2 . Then, by contracting C k for k ∈ [n], we obtain a supergraph of G H (Π). See Figure 6 (right) for an illustration.
For k ∈ [n], define the path C k = (x 1,k , s c (x 1,k ), s 2 c (x 1,k ), . . . , s 2m−2 c (x 1,k )). As s c is a bijection, these paths are disjoint. Note that for each . We claim that the color of x i,j is π i (j). This is because: Now let k 1 and k 2 be adjacent in G H (Π). Then, there exist i, j such that π i (j) = k 1 and π i (j + 1) = k 2 and, as discussed above, . This concludes the proof.
The construction can be extended to embed the union of k arbitrary Hamiltonian paths on n vertices as a minor of a 3-track graph with O(kn log n) vertices. As every order-n graph of maximum degree d is edge-colorable with d + 1 colors (by Vizing's theorem), such graphs are in the union of at most d + 1 Hamiltonian paths, can thus be embedded in 3-track graphs of order O(dn log n).

Hardness result
In this section we prove Theorem 4. The hardness proof proceeds in two steps. First, we reduce the partitioned subgraph isomorphism (PSI) problem to the partitioned permutation pattern matching (PPPM) problem. Then, we reduce from the more difficult, counting variant of PPPM to the regular counting PPM (the subject of Theorem 4), using a (by now standard) technique based on inclusion-exclusion.
PSI to PPPM. The input to the PSI problem (introduced in [44]) consists of a graph G, a graph H, and a coloring φ of V (G) with colors V (H). The task is to decide whether there is a mapping g : and φ(g(u)) = u for all u ∈ V (H). In words, we look for a subgraph of G that is isomorphic to H, with the restriction that each vertex of H can only correspond to a vertex of G from a prescribed set, moreover, these sets are disjoint. Let n denote the number of vertices of G, and let k denote the number of edges of H. It is known [44,Corr. 6.3], that PSI cannot be solved in time f (k) · n o(k/ log k) , unless ETH fails, moreover, this holds even if |E(H)| = |V (H)| (see e.g. [16]).
The input to the PPPM problem (introduced in [33]) consists of permutations τ and π of lengths n and k respectively, and a coloring φ : [n] → [k] of the entries of τ . The task is to decide whether there is an embedding g : [k] → [n] of π into τ in the sense of the standard PPM problem, with the additional restriction that φ(g(i)) = i, for all i ∈ [k].
Guillemot and Marx show [33, Thm. 6.1], through a reduction from partitioned clique, that PPPM is W [1]-hard. Due to the density of a clique, the same reduction would, at best, yield a lower bound with exponent √ k. We strengthen (and somewhat simplify) this reduction, to show that PPPM is at least hard as PSI, obtaining the following.

Lemma 11 (Proof in Appendix A.6). PPPM cannot be solved in time f (k) · n o(k/ log k) , unless ETH fails.
#PPPM to #PPM. The counting variant of PPPM (denoted #PPPM) is clearly at least as hard as PPPM. We now show that the counting variant of PPM (denoted #PPM) is at least as hard as #PPPM, thereby proving Theorem 4.
We use oracle-calls to #PPM for all subsets X ⊆ [k], to count the number of embeddings of π into τ using entries of τ with colors from the set X, but ignoring colors for the purpose of the embedding. (We can achieve this by deleting the entries of τ with color in [k] \ X before each oracle-call.) Then, using the inclusion-exclusion formula, we obtain the number C of embeddings that use all colors in [k] as follows: where C X denotes the number of embeddings that use colors from the set X (obtained by oracle calls). Since π is of length k, the quantity C counts exactly the number of embeddings that use each color once.
It remains to show that embeddings that use every color in [k] are such that π i is matched to an entry of τ of color i, for all i ∈ [k], i.e. the colors are not permuted. This is indeed the case for the hard instance constructed in the proof of Lemma 11. Towards this claim (referring to Appendix A.6) observe that all points that are unique in their respective pattern-cell (i, j) can only be matched to a point in the corresponding text-cell (i, j), which is of the correct color. In each diagonal cell (i, i), for i > 0, there is a matched point, and the pattern has two bracketing points in decreasing order in pattern-cell (i, 0), and two bracketing points in increasing order in pattern-cell (0, i). By construction, the only two points in the correct order are the nearest bracketing points in text-cell (i, 0), resp. (0, i), which are indeed of the correct color.
The number of oracle calls and additional overhead amounts to a factor 2 k in the running time, absorbed in the quantity f (k) · n o(k/ log k) . This concludes the proof.
In the other direction, let f : S π → S τ be a valid embedding. Define i j = f (p j ).x, for all j ∈ [k]. Since f (N L (p j )).x < f (p j ).x < f (N R (p j )).x for all j, we have i 1 ≤ · · · ≤ i k . Let j , j ∈ [k], such that j < j .
Then (N U (p j )) . . . )).y = τ (i j ) where the N U (·) operator, and the second property of a valid embedding are applied j − j times.

A.2 The even-odd method
In this section we describe the algorithm of Theorem 2 in a self-contained manner, not using the formalism of CSPs.
Let (Q E , Q O ) be the partition of S π into points with even and odd indices. Formally, Suppose τ contains π. Then, by Lemma 7, there exists a valid embedding f : S π → S τ . We start by guessing a partial embedding g 0 : Q E → S τ . (For example, g 0 = f | Q E is such a partial embedding.) We then extend g 0 step-by-step, adding points to its domain, until it becomes a valid embedding S π → S τ .
Let p 1 , . . . , p k/2 be the elements of Q O in increasing order of value, i.e. 1 ≤ p 1 .y < · · · < p k/2 .y ≤ n, and let P 0 = ∅ and P i = P i−1 ∪ {p i }, for 1 ≤ i ≤ k/2 . For all i, we maintain the invariant that g i is a restriction of some valid embedding to Q E ∪ P i . By our choice of g 0 , this is true initially for i = 0.
In the i-th step (for i = 0, . . . , k/2 − 1), we extend g i to g i+1 by mapping the next point p i+1 onto a suitable point in S τ . For g i+1 to be a restriction of a valid embedding, it must satisfy conditions (1) and (2) on the relative position of neighbors. Observe that all, except possibly one, of the neighbors of p i+1 are already embedded by g i . This is because N L (p i+1 ) and N R (p i+1 ) have even index, are thus in Q E , unless they are virtual points and thus implicitly embedded. The point N D (p i+1 ) is either an even-index point, and thus in Q E , or the virtual point (0, 0) and thus implicitly embedded, or an odd-index point, in which case, by our ordering, it must be p i , and thus, contained in P i . The only neighbor of p i+1 possibly not embedded is N U (p i+1 ).
If we map p i+1 to a point q ∈ S τ , we have to observe the constraints g i (N L (p i+1 )).x < q.x < g i (N R (p i+1 )).x, and g i (N D (p i+1 )).y < q.y. If N U (p i+1 ) is also in the domain of g i , then we have the additional constraint q.y < g i (N U (p i+1 )).y.
These constraints determine an (open) axis-parallel box, possibly extending upwards infinitely (in case only three of the four neighbors of p i+1 are embedded so far). Assuming g i is a restriction of a valid embedding f , the point f (p i+1 ) must satisfy all constraints, it is thus contained in this box. We extend g i to obtain g i+1 by mapping p i+1 to a point q ∈ S τ in the constraint-box, and if there are multiple such points, we pick the one that is lowest, i.e. the one with smallest value q.y. The crucial observation is that if g i is a partial embedding, then g i+1 is also a partial embedding, and the correctness of the procedure follows by induction.
Indeed, some valid embedding f : S π → S τ must be the extension of ). The only conditions of a valid embedding that may become violated are those involving ).x and f (N D (p i+1 )).y < f (p i+1 ).y hold by our choice of q.
Assuming that our initial guess g 0 was correct, we succeed in constructing a valid embedding that certifies the fact that τ contains π. We remark that guessing g 0 should be understood as trying all possible embeddings of Q E . If our choice of g 0 is incorrect, i.e. not a partial embedding, then we reach a situation where we cannot extend g i , and we abandon the choice of g 0 . If extending g 0 to a valid embedding fails for all initial choices, we conclude that τ does not contain π. The resulting algorithm is described in Figure 5.
The space requirement is linear in the input size; apart from minor bookkeeping, only a single embedding must be stored at all times. To analyse the running time, observe first, that g 0 must map points in Q E to points in S τ , preserving their left-to-right order (by index), and their bottom-to-top order (by value). The first condition can be enforced directly, by considering only subsequences of τ . This amounts to n k/2 choices for g 0 in the outer loop. The second condition can be verified in a linear time traversal of G π (this is the second line of the algorithm in Figure 5).
All remaining steps can be performed using straightforward data structuring: we need to traverse to neighbors in the incidence graph, to go from x to g i (x) and back, and to answer rectangle-minimum queries; all can be achieved in constant time, with a polynomial time preprocessing. We can in fact do away with rectangle queries, since candidate points of τ are considered in increasing order of value-the inner loop thus consists of a single sweep through both π and τ , which can be implemented in O(n) time. By a standard bound on the binomial coefficient, the claimed running time of O(n k/2+1 ) follows.
Polynomial-space algorithm for PPM: for all g 0 : Q E → S t do if g 0 not valid, then next g 0 for i ← 0 to k/2 − 1 do let q ∈ S τ with minimum q.y such that: if no such q, then next g 0 extend g i to g i+1 by mapping p i+1 → q return g k/2 return "τ avoids π" The efficient enumeration of initial embeddings with the required property can be achieved with standard techniques, see e.g. [47]. Finally, we remark that instead of trying all embeddings g 0 , in practice it may be preferable to build such an embedding incrementally, using backtracking. This allows the process to "fail early" if a certain embedding can not be extended to any partial embedding of Q E . The order in which points of Q E are considered in the backtracking process can affect the performance significantly, see [41,42] for consideration of similar issues. Alternatively, a modification of the algorithm of Theorem 1 may also be used to enumerate all valid initial g 0 .

A.3 Jordan patterns
The proof of Theorem 5(ii) is immediate: the incidence graph of Jordan permutations is by definition planar. To see this, recall that a Jordan permutation is defined by the intersection pattern of two curves. We view the curves as the planar embedding of G π . The portions of the curves between intersection points correspond to edges (we trim away the loose ends of both curves), and the curves connect the points in the order of their index, resp. value. This turns out to be an exact characterization: G π is planar if and only if π is a Jordan permutation.
We remark that for the "only if" direction to hold, touching points between the two curves must also be allowed. Consider any noncrossing embedding of G π , and construct the two curves as the Hamiltonian paths of G π that connect the vertices by increasing index, resp. value. Whenever the two curves overlap over an edge of G π , bend the corresponding part of one of the curves, such as to create two intersection points at the two endpoints of the edge (one of the two intersection points may need to be a touching point).
The tightness of the result follows by the example of § 4. As 2-track graphs are planar, the given 2-increasing permutation (whose incidence-graph contains a large grid) is also a Jordan-permutation.
A.5 Illustration for the proof of Theorem 6 (3-increasing permutation) x 2,2 y 2,2 x 2,3 z 2,4 x 2,4 z 2,5 x 2,5 y 2,3 x 3,1 z 3,5 x 3,2 y 3,1 x 3,3 y 3,2 x 3,4 y 3,3 x 3,5 y 3,4  (u 1 , . . . , u n ) of the vertices of G, in which vertices in V i appear before vertices in V j for all i < j. Assume a preprocessing of G, where all edges with endpoints in the same class V i are deleted, for all i ∈ [k], as these cannot be part of a subgraph of the required form.
A PPPM instance is created, consisting of a text permutation τ of length O(n 2 ), a pattern π of length O(k), and a coloring of τ with |π| colors. Both τ and π are based on a tilted grid permutation. Intuitively, τ and π represent the adjacency matrices of G and H, as well as the coloring φ of V (G), with minor additional gadgets that enforce that valid embeddings of π into τ correspond exactly to selections of valid H-isomorphic subgraphs in G.
We first describe the construction of π. Consider (conceptually) a grid of size (k + 1) × (k + 1), with cells indexed from (0, 0) (top left) to (k, k) (bottom right). In the first row, excluding cell (0, 0), place points forming the permutation (2, 1, 4, 3, 6, 5, . . . , 2k, 2k − 1), with two consecutive points in each cell, left to right. In the first column, excluding cell (0, 0), place points forming the permutation (2k − 1, 2k, 2k − 3, 2k − 2, 2k − 5, 2k − 4, . . . , 1, 2), with two consecutive points in each cell, top to bottom. Place one point in cell (0, 0) below all other points in its row, and to the left of all other points in its column. Place one point in each grid cell (i, j), for i, j ∈ [k] such that i = j or {v i , v j } ∈ E(H). The placement of a point in cell (i, j) is such that it falls horizontally between the two points in cell (i, 0) and vertically between the two points in cell (0, j). Furthermore the points in cells (i, j) for i, j ∈ [k] are in each row increasing by y-coordinate left-to-right, and are in each column increasing by x-coordinate top-to-bottom. Let π denote the permutation that has the same ordering as the constructed point set. It is easy to verify that π is of length 7k + 1. See Figure 7 for an example.
We next describe the construction of τ . Consider again a grid of size (k + 1) × (k + 1), with cells indexed from (0, 0) (top left) to (k, k) (bottom right). In the first row, excluding cell (0, 0), place points forming the permutation (2, 1, 4, 3, 6, 5, . . . , 2n, 2n − 1), with 2|V i | consecutive points in cell (i, 0), left to right. In the first column, excluding cell (0, 0), place points forming the permutation (2n − 1, 2n, 2n − 3, 2n − 2, 2n − 5, 2n − 4, . . . , 1, 2), with 2|V i | consecutive points in cell (0, i), top to bottom. Place one point in cell (0, 0) below all other points in its row, and to the left of all other points in its column. Place additional points, horizontally between the (2i)-th and (2i + 1)-th points of the first row and vertically between the (2j)-th and (2j + 1)-th points of the first column, for i, j ∈ [n] such that i = j, or {u i , u j } ∈ E(G). The placement of the points in cells ( , h) for , h ∈ [k] is such that they are in each row increasing by y-coordinate left-to-right, and they are in each column increasing by x-coordinate top-to-bottom. Let τ denote the permutation that has the same ordering as the constructed point set. It is easy to verify that τ is of length 5n + 2m + 1 ∈ O(n 2 ). This concludes the construction of the PPPM instance.
Suppose there is a subgraph of G that is isomorphic to H, with an embedding of the vertices that respects the coloring of V (G). Let u i1 , . . . , u i k ∈ V (G) be the vertices matched, respectively, to v 1 , . . . , v k ∈ V (H). Observe that u ij ∈ V j for all j ∈ [k]. We argue that there is a valid embedding of π into τ that respects the coloring of τ . Indeed, match the (2j)th and (2j + 1)th points in the first row of the pattern-cell to the (2i j )th and (2i j + 1)th points in the first row of the text-cell. Assuming the canonical ordering of vertices, the coloring of these points is in accordance with the matching. Similarly, match the (2j)th and (2j + 1)th points in the first column of the pattern-cell to the (2i j )th and (2i j + 1)th points in the first column of the text-cell. Assuming the canonical ordering of vertices, the coloring of these points is in accordance with the matching. If there is a point in the pattern-cell ( , h) for , h ∈ [k], then {v , v h } ∈ E(H), and then, by assumption, {u i , u i h } ∈ E(G). Consequently, there is a unique point in the text-cell ( , h) that has the correct color, and that is horizontally between the matched points in cell ( , 0) and vertically between the matched points in cell (0, h). Finally, match the unique point in the pattern-cell (0, 0) to the unique point in the text-cell (0, 0). The fact that the text-points used in the embedding have the correct relative positions is easy to verify.
Conversely, suppose there is a valid, color-respecting embedding of π into τ . Then, pairs of points in the pattern-cells (1, 0), . . . , (k, 0) and (0, 1), . . . , (0, k) must be matched to consecutive points in the corresponding text-cells of colors a ( * , * ) and b ( * , * ) (since only such pairs are in the correct relative position). These pairs of points determine a selection of vertices u i1 , . . . , u i k from the classes V 1 , . . . , V k , respectively. The existence of a point in a pattern-cell ( , h) for , h ∈ [k] means that {v , v h } ∈ E(H). Such a point can only be matched to a point in the text-cell ( , h), and since the embedding has the valid ordering, this point must be bracketed by the pairs of points in the first row and first column that correspond to the vertices u i , resp. u i h . The existence of such a point in the text-pattern means that indeed, the corresponding edge {u i , u i h } exists in G.