Counting Permutation Patterns with Multidimensional Trees
Abstract
We consider the well-studied pattern-counting problem: given a permutation and an integer , count the number of order-isomorphic occurrences of every pattern in .
Our first result is an -time algorithm for and . The proof relies heavily on a new family of graphs that we introduce, called pattern-trees. Every such tree corresponds to an integer linear combination of permutations in , and is associated with linear extensions of partially ordered sets. We design an evaluation algorithm for these combinations, and apply it to a family of linearly-independent trees. For , we show a barrier: the subspace spanned by trees in the previous family has dimension exactly , one less than required.
Our second result is an -time algorithm for . This algorithm extends the framework of pattern-trees by speeding-up their evaluation in certain cases. A key component of the proof is the introduction of pair-rectangle-trees, a data structure for dominance counting.
Keywords and phrases:
Pattern counting, patterns, permutationsCategory:
Track A: Algorithms, Complexity and GamesCopyright and License:
![[Uncaptioned image]](x1.png)
2012 ACM Subject Classification:
Mathematics of computing Permutations and combinations ; Mathematics of computing Combinatorial algorithms ; Theory of computation Pattern matchingSupplementary Material:
Software: https://github.com/perm-patterns-icalp2025/pattern-counting [5]archived at

Editors:
Keren Censor-Hillel, Fabrizio Grandoni, Joël Ouaknine, and Gabriele PuppisSeries and Publisher:

1 Introduction
A permutation occurs in a permutation if there exist points in that are order-isomorphic to . By way of example, in ,111 Throughout this paper, permutations are written in one-line notation. If they are short, we omit the parenthesis. the overlined points form an occurrence of . The number of occurrences of a permutation (a pattern) within a larger permutation has been the basis of many interesting questions, both combinatorial and algorithmic.
In a classical result, MacMahon [24] proved that the number of permutations that avoid the pattern (i.e., ) is counted by the Catalan numbers. Another classical result is the well-known Erdős-Szekeres theorem [16], which states that any permutation of size cannot simultaneously avoid both and . These early results gave rise to an entire field of study regarding pattern avoidance, c.f. [26, 23, 27]. One particularly noteworthy result is Marcus and Tardos’ resolution of the Stanley-Wilf conjecture [25]: for any fixed pattern , the growth rate of the number of permutations avoiding is bounded by , where is a constant depending only on .
Pattern avoidance can also be cast as an algorithmic problem. The permutation pattern matching problem is the task of determining, given a pattern and a permutation , whether avoids . What is the computational complexity of this task? Trivial enumeration over all -tuples of points yields an -time algorithm. This bound has been improved upon by a long line of works: Albert et al. [2] lowered the bound to , Ahal and Rabinovich [1] to , and finally Guillemot and Marx [21] established the fixed-parameter tractability of the problem, i.e., whenever is fixed, the problem can be solved in time linear in (see also [19] for an improvement on this result). In stark contrast, when the pattern is not fixed (i.e., when ), permutation pattern matching is known to be NP-complete, as shown by Bose, Buss and Lubiw [8].
A closely related algorithmic question is the counting version of permutation pattern matching. The permutation pattern-counting problem is the task of counting, given a pattern and permutation , the number of occurrences . Once again, there is a straightforward -time algorithm – how far is it from optimal? Albert et al. lowered the bound to [2]222 Their algorithm also works for the counting version. and the current best known bound is , due to Berendsohn et al. [7]. Berendsohn et al. also showed a barrier: assuming the exponential time hypothesis, there is no algorithm for pattern-counting with running time , for any function .
Another intriguing line of work focuses on the pattern-counting problem, for constant small . As the number of patterns is fixed in this regime, one can equivalently, up to a constant multiplicative factor, compute the entire -dimensional vector of all occurrences, . This vector, which characterises the local structure of a permutation over size- pointsets, is known as the -profile. The -profile has also featured in works aiming to understand the local structure of permutations, c.f. [6, 17, 11].
Even-Zohar and Leng [18] designed a class of algorithms with which they compute the -profile in -time,333 As usual, the notation hides poly-logarithmic factors. and the -profile in -time. Improving on their result for , Dudek and Gawrychowski [15] gave a bidirectional reduction between the task of computing the -profile, and that of counting -cycles in a sparse graph. The best known algorithm for the latter problem has running time [28], where [14] is the exponent of matrix multiplication. Consequently, Dudek and Gawrychowski obtain an -time algorithm for the -profile.444If one assumes the conjectured lower-bounds on the -cycle counting problem in sparse graphs, due to [12], then the reduction of Dudek and Gawrychowski [15] also implies a lower-bound on the -profile problem – stating that the profile cannot be computed in time , for any constant .
Our paper continues this line of work: we design algorithms computing the , and -profiles, and highlight a barrier in the way of computing the -profile.
1.1 Our Contribution
We introduce pattern-trees: a family of graphs that generalise the corner-trees of Even-Zohar and Leng [18]. Pattern-trees are rooted labeled trees, in which every vertex is associated with a set of point variables, along with constraints that fix their relative ordering in the plane, and every edge is labeled by a list of constraints over the ordering of points associated with its incident vertices.
Using an algorithm derived from pattern-trees, we obtain our first result.
Theorem 1.1.
For every , the -profile of an -element permutation can be computed in time and space.
Our proof of Theorem 1.1 relies on embeddings of trees into permutations. Consider the number of distinct embeddings of the points of a pattern-tree into the points in the plane associated with a permutation , in which the embedding satisfies all constraints defined by the tree (as demonstrated in Figure 1). We show that this quantity can be expressed as a fixed integer linear combination of permutation pattern-counts, irrespective of . Interpreted as a formal sum of patterns, this is simply a vector in , where is the number of point variables in the tree. These vectors are associated with pairwise compositions of linear extensions of partially ordered sets, whose Hasse diagrams can be partitioned in a particular way.
The subspaces spanned by the vectors of trees, over the rationals, are central to our proof. It is not hard to show that when the subspace of a set of trees is full-dimensional, one can derive from those trees an algorithm for the -profile. To this end, we design an evaluation algorithm: given a pattern-tree and an input permutation 555 Throughout this paper we operate on -element permutations as input. Such inputs are assumed to be presented to the algorithm sparsely, e.g., as a length- vector representing the permutation in one-line notation., the algorithm computes the number of occurrences of in , denoted . The complexity of this algorithm depends on properties of the tree. In our proof of Theorem 1.1, we construct a family of trees evaluable in -time, which are of full dimension for .
Compared to previous results, Theorem 1.1 offers an improvement whenever . The best known bound for the -profile problem is , due to Berendsohn et al. [7]. Their approach relies on formulating a binary CSP, and bounding its tree-width. It is well known that binary CSPs can be solved in time [13, 20], where is the domain size, and is the tree-width of the constraint graph. In the algorithm of [7], the tree-width is bounded by , where the -term is greater than one. Therefore, their algorithm has at least cubic running time when .
The relationship between properties of pattern-trees and the dimensions of the subspaces spanned by them is still far from understood (see Section 5). Corner-trees, which are exactly the pattern-trees whose evaluation is quasi-linear, were shown in [18] to have full rank for , and rank only , restricted to . Intriguingly, we show that the family of pattern-trees with which our proof of Theorem 1.1 is obtained, whose evaluation complexity is quadratic, have full rank for , and rank only restricted to . We observe several striking resemblances between the two vectors spanning the orthogonal complements, for and respectively, in terms of their symmetries. In fact, we extend a characterisation of [15] regarding the symmetries for to the case of (see Section 3.4).
Our second result is a sub-quadratic algorithm for the -profile.
Theorem 1.2.
The -profile of an -element permutation can be computed in time .
The proof of Theorem 1.2 is obtained by speeding-up the evaluation algorithm of pattern-trees. The original algorithm for pattern-trees has an integral exponent in its complexity, which is determined by properties of the tree. We show that trees with certain topological properties, i.e., containing a particular set of “gadgets”, can be evaluated faster. The family of trees constituting all corner-trees, and their augmentation by our gadgets, span a full-dimensional subspace over . This allows us to break the quadratic barrier for the -profile.
One of the key ingredients, both in the original evaluation algorithm and in its extended version, is a data structure known as a multidimensional segment-tree, or rectangle-tree [22, 9].666A -dimensional version of this data structure features in both [18] and [15]. A -dimensional rectangle-tree holds (possibly weighted) points in , and answers sum-queries over rectangles (i.e., Cartesian products of segments) in poly-logarithmic time.
The gadgets appearing in the proof of Theorem 1.2 are sub-structures related to the patterns and . For the former, we extend an algorithm of [18] into a weighted variant, and provide an evaluation algorithm of complexity . We then further extend this into an algorithm for the latter gadget, of complexity . The latter proof is involved, and requires the introduction of a new data structure, which we call a pair-rectangle-tree. A pair-rectangle-tree is an extension of rectangle-trees that can facilitate more complex queries, in particular, regarding the dominance counting of a set of points in a rectangle. These queries are also more general than those supported by the -dimensional dominance counting data-structures due to [22, 10]. We remark that the original pattern-tree evaluation algorithm can only compute equivalent gadgets in quadratic time. That is, the evaluation algorithm is not always optimal.
1.2 Paper Organization
In Section 3 we introduce pattern-trees. Our construction for can be found in Section 3.3, and the case is dealt with in Section 3.4. A straightforward application of pattern-trees for general is given in Section 3.5. Section 4 revolves around our construction of an -time algorithm for the -profile. The augmentation of the pattern-trees evaluation algorithm can be found in Section 4.2, and the particular gadgets used in the -profile are obtained in Section 4.3 and Section 4.4. The data structure we introduce for dominance counting in rectangles, pair-rectangle-tree, is given in Section 4.5. Finally, in Section 5 we discuss open questions and possible extensions of this work.
The proofs of Theorem 1.1 and Theorem 1.2 are accompanied by computer-assisted computations. All such computations are available in [5].
2 Preliminaries
2.1 Permutations
A permutation over elements is a bijection from to itself, where . Throughout this paper, we express permutations using one-line notation, and if the permutation range is sufficiently small, we omit the parentheses. For instance, is the identity permutation over elements. Associated with any permutation is a set of points in the plane, , which we refer to as the points of . In the other direction, any set of points in the plane defines a permutation , provided that no two points lie on an axis-parallel line. Given such a set , we use the notation to indicate that the points are order-isomorphic to .
An occurrence of a pattern in a permutation is a -tuple such that the set of points is order-isomorphic to . That is, if and only if for all . The number of occurrences of in is denoted by .
The dihedral group is the symmetry group of the regular -gon. The group naturally acts on the symmetric group , by acting on . Formally, for any element and permutation , we have that is the permutation for which . Our algorithms usually receive permutations as input, and compute some combination of pattern-counts. To this end, it is sometimes helpful to first act on the input with an element (as a preprocessing step), and only then invoke the algorithm as usual. In this way, if an algorithm computes the count , then after the action we obtain .
Our main focus in this paper is the computation of for all , where is given as input and is fixed. This collection of counts is defined as follows.
Definition 2.1.
The -profile of a permutation is the vector .
2.2 Partially Ordered Sets
A partially ordered set (poset) over a ground set is a partial arrangement of the elements in according to the order relation . If is not reflexive, we say that is strict. A partial order is said to be an extension of if implies for all . If an extension is a total order, it is called a linear extension of . As usual, the set of all linear extensions of a poset is denoted by .
2.3 Computational Model
Throughout this paper we disregard all -factors, so our results hold for any choice of standard computational model (say, word-RAM). The notation (adding the tilde) is used to hide poly-logarithmic factors. The algorithms presented in this paper operate on -element permutations as input, and we remark that such inputs are assumed to be presented to the algorithm sparsely, e.g., as a length- vector representing the permutation in one-line notation.
2.4 Rectangle-Trees
Our algorithms for efficiently computing profiles rely heavily on a simple and powerful data structure, which we refer to as a rectangle-tree777 A rectangle is a Cartesian product of segments, i.e., invervals of the form . or a multidimensional segment-tree. Concretely, we require the following folklore fact.
Proposition 2.2 ([9, 22], see also [15]).
For any fixed dimension , there exists a deterministic data structure that supports each of the following actions in time:
-
1.
Initialisation: Given , construct an empty tree over .
-
2.
Insertion: Given and , add weight to point .
-
3.
Query: Given a rectangle , the query returns the sum of weights over all points in .
One illustration of the application of rectangle-trees to pattern-counting is given by the simple (and again, folklore) case of monotone pattern-counting.
Proposition 2.3.
Let be a fixed integer and let be an input permutation. The pattern-counts and can be computed in time.
Proof.
See the full version [4].
Remark 2.4.
It is also possible to count monotone patterns using -dimensional segment-trees, somewhat more efficiently. However, the difference is only in logarithmic factors. The multidimensional structure highlighted above will serve us in more complicated cases.
3 Pattern-Trees
In this section we introduce a family of graphs, called pattern-trees. Using pattern-trees we derive algorithms for computing the -profile of a permutation. Our main result for this section (see Section 3.3) is a quadratic-time algorithm for the -profile of a permutation, for every : See 1.1
In Section 3.4 we consider the subspaces spanned by the same family of pattern-trees, restricted to . We show that this subspace is of dimension , one less than required. In Section 3.5 we consider the case of general (constant) , and show a straightforward application of pattern-trees yielding an -time algorithm for the -profile.
Before we present pattern-trees, let us begin by recalling corner-trees.
3.1 Warmup: Corner-Trees
One of the main components in the work of [18] is the introduction of corner-trees. Corner-trees are a family of rooted edge-labeled trees. Every corner-tree of vertices is associated with a particular vector in ; i.e., a formal integer linear combination of permutations, each of size at most . Furthermore, there exists an efficient evaluation algorithm for corner-trees: given any input permutation and corner-tree , the integer sum of permutation pattern-counts in , called the vector of , can be computed in time . We refer to this operation as evaluating the vector of over .
Definition 3.1 (corner-tree [18]).
A corner-tree888For convenience, we consider corner-trees to be edge-labeled, rather than vertex-labeled as in [18]. is a rooted999Hereafter, whenever we consider rooted trees, we orient their edges away from the root. edge-labeled tree, with edge labels in the set .
An occurrence of a corner-tree in a permutation is a map , in which the image agrees with the edge-labels of the tree. That is, for every edge , is to the left of if the edge is labeled NW or SW, and to its right otherwise. Similar rules apply for their vertical ordering. As in [18], the number of occurrences of a corner-tree in a permutation is denoted by .
The vector of a corner-tree is a formal sum of permutation patterns with integer coefficients, representing the number of occurrences of the tree in any input permutation. For instance, the vector of is . Clearly, the vector of a corner-tree over vertices may involve patterns of size at most , as the tree conditions on the relative ordering of at most points (smaller patterns may appear as well, since occurrences are not necessarily injective).
Theorem 1.1 of [18] presents an algorithm for evaluating the vector of a corner-tree over an input permutation . For expositionary purposes, under Section 3.1 we sketch a simplified version of their algorithm, phrased in terms of rectangle-trees.
Proposition 3.2 (Theorem 1.1 of [18]).
The vector of any corner-tree with a constant number of vertices can be evaluated over an input permutation in time .
Proof Sketch.
Let be a corner-tree and let be a permutation. To start, construct a -dimensional rectangle-tree (see Section 2.4), and insert the points with weight , in time . Associate this tree with the leaves of . Next, traverse the vertices of in post-order. At every internal vertex , construct a new (empty) rectangle-tree , and associate it with . Then, iterate over every point in , and at each point perform one rectangle query to the rectangle-tree associated with each of ’s children, querying the rectangle corresponding to the edge label in written on the parent-child edge. For example, if is labeled SW, the iteration over a point queries the rectangle . Store the product of all answers to these queries in , at the position of the current permutation point. It can be shown that the sum of all values at the root’s tree (i.e., a full rectangle query) is the number of occurrences, .
3.2 Pattern-Trees
We introduce pattern-trees: a family of graphs that generalise the corner-trees of [18]. In pattern-trees, every vertex is labeled by a permutation, and every edge is labeled by a list of constraints. The permutations written on the vertices fix the exact ordering of the points corresponding to them, and the edge-constraints are similarly imposed over the points corresponding to the two incident vertices. As with corner-trees, pattern-trees serve two purposes: firstly, every pattern-tree is associated with a set of constraints over permutation points, the number of satisfying assignments to which can be expressed as a formal integer linear combination of patterns (that is, a vector). Secondly, we present an algorithm for evaluating this vector over an input permutation. This allows us to efficiently compute certain pattern combinations not spanned by corner-trees.
Definition 3.3 (pattern-tree).
A pattern-tree is a rooted edge- and vertex-labeled tree, where:
-
1.
Every vertex is:
-
Labeled by a permutation , for some integer .
-
Associated with two sets of fresh variables,
where we denote for every , and .
-
-
2.
Every edge is labeled by:
-
Two strict posets, and .
-
A set of equalities between the points of and those of .
-
The size of a vertex is the size of the permutation with which it is labeled. The maximum size of a pattern-tree, denoted , is the maximum over all vertex sizes. The total size, denoted , is the sum over all vertex sizes. Under this notation, a corner-tree is a pattern-tree of maximum size one. Lastly, is the set of all points in the tree.
Pattern-Tree Constraints.
Any pattern-tree defines constraints over points :
-
1.
Every vertex labeled by contributes the following inequalities,101010These vertex-constraints enforce the pattern over the points .
-
2.
Every edge contributes the inequalities in and , and the equalities in .
Hereafter, we partition into two parts: its equalities, which define an equivalence relation over the points , and its inequalities, which define strict posets,
Given an equivalence relation , the posets and are the strict posets obtained from and by replacing every coordinate variable corresponding to a point by a single variable corresponding to the equivalence class of in .
Example 3.4.
Applying yields the posets:
Pattern-Tree Occurrences.
As with corner-trees, we define pattern-tree occurrences.
Definition 3.5.
An occurrence of a pattern-tree in a permutation is a map whose image conforms to the constraints .
An illustration of a pattern-tree occurrence is shown in Figure 3. Note that, as with corner-trees, occurrence maps need not be injective. We remark that some pattern-trees may have no occurrences, in any permutation . For example, is infeasible.
Pattern-Tree Vectors.
As with corner-trees, one can associate a vector with every pattern-tree , which is a formal integer linear combination of pattern-counts representing the number of occurrences of in any input permutation .
Lemma 3.6.
Let be a pattern-tree. The number of occurrences of in an input permutation is given by the following sum of pattern-counts, each of size at most :
Proof.
See the full version [4].
Evaluating a Pattern-Tree.
It remains to construct an evaluation algorithm for the vector of a pattern-tree. To present our algorithm, we require some notation.
-
1.
Points: To every set of points , where is a permutation and , we associate a -dimensional point,
-
2.
Rectangles: To every combination of an edge in a pattern-tree , where and are of sizes and respectively, and set of points , we associate a -dimensional rectangle,
The -th segment of contains the coordinates that can take under the constraints of , when is assigned . Namely, the intersection of the following segments:
The -segments are similarly defined.
Observe that the rectangle is the set of permissible locations for the points , subject to the edge-constraints on the edge , when the points are mapped to . That is, it enforces both the equalities (left) and inequalities (centre, right) written on the edge .
The evaluation algorithm now follows.
Input: A pattern-tree , and a permutation .
-
1.
Traverse the vertices of in post-order. For every vertex labeled by :
-
(a)
Construct a new (empty) rectangle-tree of dimension .
-
(b)
Iterate over all sets . If , then:
-
i.
For every child of , issue the query .
-
ii.
Add the weight (or , if is a leaf) to point in .
-
i.
-
(a)
-
2.
Return the answer to the query , where is the root of , and .
Theorem 3.7.
Let be a pattern-tree of constant total size, and let be a permutation. The vector of can be evaluated over in time, where is the maximum size of .111111 The space-complexity is also , since at every vertex of size , we insert points to a rectangle-tree.
Proof.
The running time of Algorithm 1 is , since every operation takes time (recall that ), except step (1b.), which we perform in time , by trivial enumeration. It remains to prove its correctness. We do so, by induction on the height of the tree.
Let be a vertex, let be its permutation label, and let be the sub-tree rooted at . We claim that for every , the weight of in is the number of occurrences in which the points are mapped to . That is, for every , it holds that .
In the base-case, is a leaf, and Algorithm 1 simply enumerates over all sets of cardinality , adding weight whenever . So the claim holds. For the inductive step, let be an internal vertex. For every child of , by the induction hypothesis, the query counts the number of occurrences in which there exists a point such that , for every . That is, the number of occurrences of the tree in which we add the as the root to the tree , where the occurrence maps to for every . These occurrences are independent for every child of , therefore picking any combination of them yields a new occurrence of in , the total number of which is indeed the product .
The proof now follows, as in the rectangle-tree corresponding to the root of , every point has weight which is the number of occurrences of in in which is mapped to . Therefore, the sum of all points in yields the total number of occurrences.
Remark 3.8.
The algorithm presented in Theorem 3.7 is not necessarily the most efficient way to compute the vector of a pattern-tree, for several reasons. Firstly, many trees may correspond to the same vector, and these trees need not have the same maximum size. For example, both
correspond to the vector . Secondly, as we will see in Section 4, there exist vectors for which bespoke efficient algorithms can be constructed, whose running time is strictly smaller than the maximum size of any pattern-tree with the same vector.
3.3 Algorithm for the -Profile, for
The corner-trees of [18] are very efficiently computable. However, asymptotically, there are quite few of them: the number of rooted unlabeled trees over vertices is only exponential in (see, e.g., [23] for a more accurate estimate), and clearly so is the number of corner-tree edge labels. Therefore, as , even if asymptotically almost all corner-trees vectors were linearly independent over , they would nevertheless contribute only a negligible proportion with respect to the full dimension, .
In contrast, it is not hard to see that pattern-trees are fully expressive: for every pattern , there exists a pattern-tree with , whose vector is precisely that pattern (in fact, suffices, see Section 3.5). To design efficient algorithms for the -profile, we are interested in finding families of pattern-trees of least maximum size, whose corresponding vectors are linearly independent.
In [18], corner-trees (i.e., pattern-tree of maximum size ) over vertices were shown to have full rank over for , and in the cases and , the subspaces spanned by them, restricted to and , were found to be of dimensions only and , respectively. Here, we show that for , pattern-trees of maximum size suffice.
Proof of Theorem 1.1..
Let . By enumeration (see Appendix A), there exists a family of pattern-trees of maximum size at most and total size at most , whose vectors are linearly independent over . Let be the matrix whose rows are these vectors, and let be its inverse. may be computed ahead of time, as can its inverse, for example using Bareiss’ algorithm [3]. Using Theorem 3.7, evaluate every row of over in time . This yields a vector , and the -profiles of , for , are obtained by computing .
This approach is not sufficient to compute the -profile in time, as discussed in Section 3.4. See Section 3.5 for an application of pattern-trees to general fixed .
3.4 The Case
Do pattern-trees over at most points, and with , have full dimension for ? Using a computer program, we exhaustively enumerate all pattern-trees with the following properties,121212 See Appendix A for a description of the enumeration process. For , this yields a matrix with columns, and rows. We remark that we explicitly do not consider pattern-trees over more than points, and trees whose edges are labeled by equalities. Whether this is without loss of generality, i.e., could their inclusion increase the rank, is unknown to us.
-
1.
Every tree has points, and maximum size .
-
2.
No edge is labeled with an equality.
In [18] it was shown that pattern-trees with vertices and maximum size (corner-trees) span a subspace of dimension only , when restricted to . Our pattern-trees extend this result: the subspace spanned by the above family of pattern-trees, with points and maximum size , is of dimension exactly , when restricted to . The two vectors spanning the orthogonal complements of the subspaces for and , and respectively, bear striking resemblance. See the full version [4] for the details.
3.5 Algorithm for the -Profile
We end this section by considering the problem of computing the -profile via pattern-trees, for arbitrary (fixed) . In the following proposition, we show that families of pattern-trees of maximal size suffice for computing the -profile, through Algorithm 1. See Section 5 for a discussion on the relationship between and .
Proposition 3.9.
Let be an input permutation, and let be a fixed integer. The -profile of can be computed in time.
Proof.
See the full version [4].
4 Algorithm for the -Profile
In Section 3, we recalled that pattern-trees of maximum size (i.e., corner-trees) have full rational rank for [18], and proved that trees of maximal size at most have full rank for (see Theorem 1.1). Therefore, up to , the -profile of an -element permutation can be computed in time, and up to , it is computable in time. This naturally raises the question: is there a sub-quadratic time algorithm for these cases, where ? We prove the following.
See 1.2
We remark that the case has been extensively studied in [15] and [18]. There, they construct sub-quadratic algorithms of complexities and , respectively.
4.1 Marked and Weighted Patterns
For the proof of Theorem 1.2, we introduce the following notation.
Marked Patterns.
A marked pattern is a pattern associated with an index . We say that a marked pattern occurs at index in , if there exists an occurrence of in , in which the -th -coordinate is . When the marked pattern is short, we underline the -th index to indicate that marked index. For instance, occurs in at index .
The marked pattern-count is a -dimensional rectangle-tree containing the points , in which the weight of every point is the number of marked pattern occurrences at position . For example, the tree appearing in Proposition 2.3 is precisely the marked pattern-count .
Weighted Pattern-Counts.
Let and let be weight functions, where is a fixed integer. The weighted pattern-count of in , denoted , is the sum of over all occurrences of in . In other words, we count occurrences where every point has weight depending on its position, rather than as usual.
The two concepts of marked patterns and weighted patterns can be combined in a straightforward way: the weighted marked pattern-count is once again defined as a -dimensional rectangle-tree, as with marked pattern-counts, but where now the number of occurrences for each point is appropriately weighted.
4.2 An Improvement to the Bottom-Up Algorithm
Recall that Algorithm 1 has time complexity , where is an integer. As we seek sub-quadratic algorithms, and since trees of (i.e., corner-trees) do not have full rank for , we take an alternative approach. We extend the family of pattern-trees of maximum size by allowing them to contain “gadgets” with specific patterns of sizes and , defined below. To evaluate such trees efficiently, we show a variation of Algorithm 1 that handles these gadgets as special cases.
Let be vertex of a pattern-tree , labeled by some permutation , such that (see Figure 4):
-
1.
The incoming edge to (if any) conditions on a single point of , say .
-
2.
Each outgoing edge of (if any) is labeled by a single equality to a point of .
Suppose that, for the permutation and index , and given a set of weight functions ,131313 We assume that for every weight function , the value can be computed in -time. we are able to construct a -dimensional rectangle-tree representing the weighted marked pattern-count, marked at . Then, we claim that one can modify Algorithm 1 by replacing the rectangle-tree associated with , with the weighted marked pattern-count of , marked at , for a particular choice of weight functions. Concretely, we make the following modifications in Algorithm 1:
Traversing .
Instead of the routine operation of Algorithm 1, when is visited we compute a weighted -marked pattern-count , abbreviated as , with the following weights: for every point , define a weight function by
where for an edge labeled , we define as the rectangle in which the -th -segment is and all other segments are unconstrained (if is not constrained by any outgoing edges, set its weight function to ). By the invariant of Algorithm 1, the query counts the number of occurrences of in such that . Therefore, the resulting tree contains, at every point , the number of occurrences of in such that .
Querying .
In Algorithm 1 we query the rectangle-trees of vertices in two scenarios:
-
1.
If is an internal vertex: In the original formulation of Algorithm 1, when the parent of is visited, we issue queries of the form , for pointsets . As the edge only constrains , the rectangles are degenerate, i.e., all of their segments are complete, except the two segments corresponding to . These queries can be answered by , where is the -dimensional projection of onto those two segments.
-
2.
If is the root: The final step of the algorithm performs the full rectangle query , which counts the occurrences of in all of . This can be answered by the full rectangle query .
As for the correctness of this modification to Algorithm 1, it remains to show that the new queries return the same values as the original ones. Let be some rectangle query to . The value of is the number of occurrences of in , constrained to the coordinates allowed by . Since in both cases, all segments in are complete except possibly those corresponding to , this counts the occurrences of in constrained only to , for a -dimensional projection of to the corresponding segments. By definition of a weighted marked pattern-count, this is exactly the value of .
In the remainder of this section, we design algorithms computing the pattern-counts and in sub-quadratic time141414The algorithm can be extended to the weighted count , but the unweighted count is sufficient for our purposes.. Consequently, we can insert vertices labeled and into pattern-trees of maximum size and with at most points, with the aforementioned edge constraints (see in Figure 4). Using the above modification to Algorithm 1, the overall time complexity for the evaluation of such trees remains sub-quadratic.
4.3 Computing in time
Theorem 1.2 of [18] describes an algorithm for counting . We require a slight alteration of their algorithm, and in particular, a weighted variant.
Lemma 4.1 (weighted version of Theorem 1.2 in [18]).
Given an input permutation and weight functions , the count can be computed in time.
Proof.
See the full version [4].
4.4 Computing in time
Lemma 4.2.
Let be a permutation. Then, can be computed in time.
Proof.
See the full version [4].
4.5 Pair-Rectangle-Trees
The evaluation of the weighted marked pattern-count relies on a data structure that can efficiently count ascending and descending pairs in a given rectangle.
Theorem 4.3.
(pair-rectangle-tree) There exists a data structure with the following properties:
-
1.
Preprocessing: Given an input permutation , the tree is initialised in time .
-
2.
Query: Given any rectilinear rectangle , return the number of descending (resp. ascending) pairs of permutations points in , in time .
where is a parameter that can be chosen arbitrarily.
Proof.
See the full version [4].
4.6 Algorithm for the -Profile
Proof of Theorem 1.2.
The proof proceeds along the same lines as Theorem 1.1, for a different family of pattern-trees. Let . Extend the pattern-trees of maximum size and over no more than points, by allowing the new vertices described in Section 4.2. By computer enumeration, there exists a family of linearly-independent vectors over , obtained from the vectors trees, along with their orbit under the action of on the symmetric group (see Section 2). The proof now follows, similarly to Theorem 1.1, and we remark that the evaluation of each pattern-tree over takes at most time, as that is the maximum amount of time spent handling any single vertex.
5 Discussion
Some immediate extensions of this work, such as the application of our methods to the and -profile, or the use of pattern-trees with maximum size , are computationally difficult and likely require further analysis or a different approach (say, algebraic). Several interesting open questions remain:
-
1.
Maximum size versus rank. For a given integer , denote by the largest integer such that the subspace spanned by the vectors of pattern-trees over at most points, of maximum size and with no equalities, is of full dimension, . The results of [18] imply that . In Section 3.3 and Section 3.4 we prove , and in Section 3.5 we show that , for every . What is the behavior of ? For example, do we have , as attained by the technique of [7]?
-
2.
A fine-grained variant of . For integers and , let be the number of linearly independent vectors in generated by Algorithm 1 when applied to trees of maximum size over permutation points with no equalities. What is the general behavior of ? This generalises a question of [18] about corner-trees. The following values are presently known.
Table 1: Bold values in this table are computed in this paper (new). 1 2 6 23 6 -
3.
Complexity of -profile, for . Can the time complexity for finding the -profiles be improved further, perhaps by utilising techniques along the lines of Section 4? In particular, we ask whether the -profile can be computed in sub-quadratic time.
-
4.
Study of the -profile. [15] shows the equivalence between the computation of the -profile and counting -cycles in sparse graphs. In Section 3.4 we show that many of the observations of [15] can be extended to . In fact, we conjecture that there exists an analogous hardness result for , and we refer the reader to Section 3.4 where the details are discussed.
References
- [1] Shlomo Ahal and Yuri Rabinovich. On complexity of the subpattern problem. SIAM Journal on Discrete Mathematics, 22(2):629–649, 2008. doi:10.1137/S0895480104444776.
- [2] Michael H Albert, Robert EL Aldred, Mike D Atkinson, and Derek A Holton. Algorithms for pattern involvement in permutations. In Algorithms and Computation: 12th International Symposium, ISAAC 2001 Christchurch, New Zealand, December 19–21, 2001 Proceedings 12, pages 355–367. Springer, 2001. doi:10.1007/3-540-45678-3_31.
- [3] Erwin H Bareiss. Sylvester’s identity and multistep integer-preserving gaussian elimination. Mathematics of computation, 22(103):565–578, 1968.
- [4] Gal Beniamini and Nir Lavee. Counting permutation patterns with multidimensional trees, 2024. doi:10.48550/arXiv.2407.04971.
- [5] Gal Beniamini and Nir Lavee. Computer-assisted computations for Theorems 1.1 and 1.2. https://github.com/perm-patterns-icalp2025/pattern-counting, 2025.
- [6] Gal Beniamini, Nir Lavee, and Nati Linial. How balanced can permutations be? Combinatorica, 45(1):1–31, 2025.
- [7] Benjamin Aram Berendsohn, László Kozma, and Dániel Marx. Finding and counting permutations via csps. Algorithmica, 83:2552–2577, 2021. doi:10.1007/S00453-021-00812-Z.
- [8] Prosenjit Bose, Jonathan F Buss, and Anna Lubiw. Pattern matching for permutations. Information Processing Letters, 65(5):277–283, 1998. doi:10.1016/S0020-0190(97)00209-3.
- [9] Bernard Chazelle. A functional approach to data structures and its use in multidimensional searching. SIAM Journal on Computing, 17(3):427–462, 1988. doi:10.1137/0217026.
- [10] Bernard Chazelle and Herbert Edelsbrunner. Linear space data structures for two types of range search. Discrete & Computational Geometry, 2:113–126, 1987. doi:10.1007/BF02187875.
- [11] Joshua Cooper and Andrew Petrarca. Symmetric and asymptotically symmetric permutations. arXiv preprint arXiv:0801.4181, 2008.
- [12] Søren Dahlgaard, Mathias Bæk Tejs Knudsen, and Morten Stöckel. Finding even cycles faster via capped k-walks. In Proceedings of the 49th Annual ACM SIGACT Symposium on Theory of Computing, pages 112–120, 2017. doi:10.1145/3055399.3055459.
- [13] Rina Dechter and Judea Pearl. Tree clustering for constraint networks. Artificial Intelligence, 38(3):353–366, 1989. doi:10.1016/0004-3702(89)90037-4.
- [14] Ran Duan, Hongxun Wu, and Renfei Zhou. Faster matrix multiplication via asymmetric hashing. In 2023 IEEE 64th Annual Symposium on Foundations of Computer Science (FOCS), pages 2129–2138. IEEE, 2023. doi:10.1109/FOCS57990.2023.00130.
- [15] Bartłomiej Dudek and Paweł Gawrychowski. Counting 4-patterns in permutations is equivalent to counting 4-cycles in graphs. In 31st International Symposium on Algorithms and Computation (ISAAC 2020). Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2020.
- [16] Paul Erdös and George Szekeres. A combinatorial problem in geometry. Compositio mathematica, 2:463–470, 1935.
- [17] Chaim Even-Zohar. Patterns in random permutations. Combinatorica, 40(6):775–804, 2020. doi:10.1007/S00493-020-4212-Z.
- [18] Chaim Even-Zohar and Calvin Leng. Counting small permutation patterns. In Proceedings of the 2021 ACM-SIAM Symposium on Discrete Algorithms (SODA), pages 2288–2302. SIAM, 2021. doi:10.1137/1.9781611976465.136.
- [19] Jacob Fox. Stanley-wilf limits are typically exponential. arXiv preprint arXiv:1310.8378, 2013. arXiv:1310.8378.
- [20] Eugene C. Freuder. Complexity of k-tree structured constraint satisfaction problems. In Proceedings of the Eighth National Conference on Artificial Intelligence - Volume 1, AAAI’90, pages 4–9. AAAI Press, 1990. URL: http://www.aaai.org/Library/AAAI/1990/aaai90-001.php.
- [21] Sylvain Guillemot and Dániel Marx. Finding small patterns in permutations in linear time. In Proceedings of the twenty-fifth annual ACM-SIAM symposium on Discrete algorithms, pages 82–101. SIAM, 2014. doi:10.1137/1.9781611973402.7.
- [22] Joseph JáJá, Christian W Mortensen, and Qingmin Shi. Space-efficient and fast algorithms for multidimensional dominance reporting and counting. In Algorithms and Computation: 15th International Symposium, ISAAC 2004, Hong Kong, China, December 20-22, 2004. Proceedings 15, pages 558–568. Springer, 2005.
- [23] Donald E Knuth. The Art of Computer Programming: Fundamental Algorithms, Volume 1. Addison-Wesley Professional, 1997.
- [24] Percy A MacMahon. Combinatory analysis, volumes I and II, volume 137. American Mathematical Society, 1915.
- [25] Adam Marcus and Gábor Tardos. Excluded permutation matrices and the Stanley–Wilf conjecture. Journal of Combinatorial Theory, Series A, 107(1):153–160, 2004. doi:10.1016/J.JCTA.2004.04.002.
- [26] Vaughan R Pratt. Computing permutations with double-ended queues, parallel stacks and parallel queues. In Proceedings of the fifth annual ACM symposium on Theory of computing, pages 268–277, 1973. doi:10.1145/800125.804058.
- [27] Rodica Simion and Frank W Schmidt. Restricted permutations. European Journal of Combinatorics, 6(4):383–406, 1985. doi:10.1016/S0195-6698(85)80052-4.
- [28] Virginia Vassilevska Williams, Joshua R Wang, Ryan Williams, and Huacheng Yu. Finding four-node subgraphs in triangle time. In Proceedings of the twenty-sixth annual ACM-SIAM symposium on discrete algorithms, pages 1671–1680. SIAM, 2014.
Appendix A Enumeration of Pattern-Tree Vectors
Let and be two positive integers, where . Consider the following enumeration process, which computes the matrix whose rows are the vectors of all pattern-trees of maximum size , with exactly points, restricted to .
For every ordered partition with no part larger than , and for every vertex-labeled tree ,151515Here is the set of all vertex-labeled trees over vertices. let be the tree in which vertex has size , and is assigned point variables
Think of as a “template” for a pattern-tree, where the topology, the sizes of vertices, and the names of their variables have been determined, but the edge-constraints have not. Next, iterate over all pairs of permutations, , and over all trees .
Any such combination maps to a pattern-tree in the above family. For every and such that and are associated with the same vertex , write the constraint in the vertex if , and write otherwise. Do likewise for the constraints and , and repeat the same operation for every pair , such that the points and are associated with adjacent vertices in – in this case, we write the inequality on the edge. Observe that the ordering of points in each vertex is fully determined, i.e., defines a permutation.
Therefore, for any combination of , and , we obtain a pattern-tree for which is a linear extension of the -poset, and is a linear extension of the -poset. In this case, we add to the vector of , at the index of (see Lemma 3.6). Once the process is completed, we obtain a matrix with columns, and no more than rows. The row-space of this matrix over the rationals is the subspace spanned by the above family of trees, restricted to .
We remark that if for every this process produces a matrix of full rank, then by induction, the vectors of the union of all trees in these families spans the entire subspace, for (no tree over points has a component in ).
An implementation can be found in the script accompanying Theorem 1.1 and Theorem 1.2 [5].