Abstract 1 Introduction 2 The framework 3 Framework extension 4 Main results 5 Experimental implementation 6 Conclusion References Appendix A Transposition networks Appendix B Proof of main theorem

Doubly-Periodic String Comparison

Nikita Gaevoy Department of Mathematics and Computer Science, St. Petersburg State University, Russia
The Faculty of Data and Decision Sciences, Technion, Haifa, Israel
Boris Zolotov Department of Mathematics and Computer Science, St. Petersburg State University, Russia Alexander Tiskin Department of Mathematics and Computer Science, St. Petersburg State University, Russia
Abstract

The longest common subsequence (LCS) problem is a fundamental algorithmic problem. Given a pair of strings, the problem asks for the length of the longest string that is a subsequence in both input strings. Among the many relatives of this problem, there is its natural version where one or both of input strings have periodic structure. The case where only one of the input strings is periodic has been considered before; in this work, we develop an efficient algorithm for the more difficult case where both input strings are periodic. The algorithm is based on the existing algebraic framework for the LCS problem, developed by the third author; in particular, we extend this framework to dealing with affine (i.e. doubly-infinite periodic) permutations instead of finite ones. Given input strings that are a k-repeat of a period of length m and an -repeat of a period of length n, the resulting algorithm runs in time O(mn+nlognlogk), which is a substantial improvement over existing approaches. The algorithm has been implemented by the first author; by running his code, one can process pairs of periodic input strings with lengths far beyond the reach of all known alternative algorithms.

Keywords and phrases:
String Comparison, periodic Strings, Longest common Subsequence, affine Hecke Monoid, affine sticky Braids
Funding:
Boris Zolotov: Research is supported by „Native towns“, a social investment program of PJSC „Gazprom Neft“.
Copyright and License:
[Uncaptioned image] © Nikita Gaevoy, Boris Zolotov, and Alexander Tiskin; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Mathematics of computing Combinatorial algorithms
; Theory of computation Pattern matching
Editors:
Paola Bonizzoni and Veli Mäkinen

1 Introduction

The longest common subsequence (LCS) problem is a fundamental algorithmic problem. We consider strings a, b, of length m, n respectively, as input. Given a string, we will distinguish between its contiguous substrings, and not necessarily contiguous subsequences.

Definition 1.

The longest comon subsequence (LCS) score for a pair of strings, denoted lcs(a,b), is the length of the longest string that is a subsequence of both a and b. Given strings a, b, the LCS problem asks for their LCS score.

The classical dynamic programming algorithm for the LCS problem [21, 30] runs in time O(mn). The best known algorithms speed it up by a (model-dependent) polylogarithmic factor [20, 10, 6], and a SETH-conditional lower bound on the running time exponent has famously been proven [2, 4].

Among the many relatives of this problem, there is the natural problem of obtaining the LCS length for a pair of strings, one or both of which have periodic structure. The case where one of the input strings is periodic has been considered before [27]; in this work, we develop an efficient algorithm for the more difficult case where both input strings are periodic. The algorithm’s construction is based on the existing algebraic framework for the LCS problem, developed by the third author. As a new contribution, we extend this framework to dealing with affine (i.e. doubly-infinite periodic) permutations instead of finite ones, and then show how to apply the extended framework to doubly-periodic strings.

1.1 Existing work

To our knowledge, the doubly-periodic LCS problem has not been considered before as a separate algorithmic problem; however, more general as well as more specific problems have been considered. In particular, the problem includes as a special case the (singly)-periodic LCS problem, where only one of the input strings is periodic; the periodic structure of the other strings, if one exists, is ignored. Tiskin [27] gave an algorithm for this problem, called “wraparound combing”. Given a string a of length m, and string b, which is an -repeat of string b of length n, the algorithm runs in time O(mn). The algorithm’s construction is based on the framework that will be described in the next section. This algorithm was rediscovered independently by Bukh and Cox [8].

For the more general setting, a periodic string can be considered as a special case of a string compressed by a straight-line program (SLP). In particular, string ak, which is a k-repeat of string a of length m, can be represented by an SLP of length O(m+logk), and similarly for b. Therefore, any algorithm for LCS on SLP-compressed strings implies an algorithm for the doubly-periodic LCS problem as a special case. Unfortunately, this more general problem was proved to be NP-hard by Lifshits and Lohrey [17, 18]. Hermelin et al. [13] and Gawrychowski [12], again using Tiskin’s framework, gave algorithms for the still more general weighted alignment problem, which includes LCS as a special case, on a pair of SLP-compressed strings a, b of total compressed length r. The algorithms’ running time is parameterised by the strings’ total plain length R (which, in general, may be exponential in r). The algorithm of [13] runs in time O(Rlog(R/r)r), which is reduced in [12] to O(R(log(R/r))1/2r). For our problem, the worst-case running time is given by substituting R=mk+n, r=(m+logk)+(n+log). The algorithm proposed in this paper is a substantial improvement on this approach.

The remainder of this paper is organised as follows. In Section 2, we give a summary (without proofs) of Tiskin’s framework [25, 24, 26, 28] for the ordinary and singly-periodic LCS problem. In Section 3, we make explicit the algebraic definitions (affine permutations, affine sticky braids) behind the singly-periodic case, that were not stated explicitly before, and add a few more components to the framework (such as the concept of ΦΓ-decomposition) that are required by our results. In Section 4, we present the main results, including fast algorithms for sticky multiplication of affine permutations, and for computing the LCS of doubly-periodic strings.

2 The framework

2.1 Preliminaries and notation

Alongside integer numbers, we will be using half-integers extensively. We denote half-integers by hatted letters (ı^, ȷ^, etc.), and the set of all half-integers is denoted ^. Given an integer number i, i=i12 and i+=i+12 are the half-integers adjacent to it. Given a half-integer ı^, integers ı^+, ı^ are defined analogously.

We denote by m:n the set of half-integers strictly between integer numbers m, n, and we denote usual integer intervals by [m:n]. The space to either side of the colon may be left empty if the corresponding boundary is obvious from the context: for example, 0, ±, the dimension of the matrix, etc. A semicolon (e.g. [k:;m:n]) denotes the Cartesian product of the corresponding intervals.

We will be indexing string characters with half-integers, because this way the spaces between characters are indexed with integers, which will become convenient further. Given a string a, aı^ is its character at position ı^, and ai:j is its substring of length ji.

We will also be considering permutations of half-integers and the corresponding permutation matrices, indexed with half-integers. We specify a matrix element by Mı^;ȷ^, when it is indexed with half-integers, and by M[i;j], when it is indexed with integers. The numbers in the indices may be replaced with intervals if we want to specify a submatrix.

Various forms of string periodicity are very common in string combinatorics and algorithms. We define periodic strings as follows.

Definition 2.

A string of the form ak=aaa​” (k times) is called a k-repeat, or simply a repeat. More generally, a string of the form aka, where a is a prefix of a, is called periodic. A doubly infinite string a=aaaa is called an infinite repeat.

2.2 Semi-local LCS

Unless indicated otherwise, in this section we consider algorithmic problems that take strings a0:m, b0:n as input.

Although global comparison (full string against full string) and fully-local comparison (all substrings against all substrings) are the two most common approaches to comparing strings, another important type of string comparison lies “in between”.

Definition 3.

Given strings a, b, the semi-local LCS (SLCS) problem asks for the LCS scores as follows:

  • the whole a against every substring of b (string-substring LCS);

  • every prefix of a against every suffix of b (prefix-suffix LCS);

  • every suffix of a against every prefix of b (suffix-prefix LCS);

  • every substring of a against the whole b (substring-string LCS).

A standard approach in LCS algorithms is to represent common subsequences of two strings as paths in a certain grid graph with added diagonal edges.

Definition 4.

The LCS grid 𝖦a,b[0:m;0:n] for strings a, b is a directed acyclic graph defined on the set of nodes [0:m;0:n]. For all i[0:m], ı^0:m, j[0:n], ȷ^0:n, it contains:

  • the horizontal edge [i;ȷ^][i;ȷ^+];

  • the vertical edge [ı^;j][ı^+;j].

Also, whenever aı^ matches bȷ^, there is the diagonal edge [ı^;ȷ^][ı^+;ȷ^+] in the grid called a match edge, and the corresponding cell of the grid is called a match cell (otherwise the cell is called a mismatch cell).

The LCS of two strings a, b corresponds to the path from the top left corner to the bottom right corner of 𝖦a,b[0:m;0:n] containing the most diagonal edges. The string-substring LCS of a, bi:j corresponds to the path with the most diagonal edges from the top left to the bottom right corner of the subgrid enclosed by the substring bi:j, denoted 𝖦a,b[0:m;i:j]. Subgrids corresponding to the three other components of the semi-local LCS problem are defined analogously.

The analysis of boundary-to-boundary paths in an LCS grid can be simplified by padding string b with m wildcard characters (which is a character matching all other characters) on both sides. Let bpadm:m+n=?mb?m. Semi-local common subsequences then correspond only to paths connecting the top and the bottom boundaries of the m×(2m+n) padded LCS grid 𝖦a,bpad[0:m;m:m+n]. It is convenient to store scores of such paths in a specially defined matrix.

Definition 5.

The SLCS matrix for strings a, b is a matrix 𝖧a,b[m:n;0:m+n], defined by

𝖧a,b[i;j]={lcs(a,bpadi:j)=max𝗌𝖼𝗈𝗋𝖾([0;i][m;j])if ijjiif ij

where i[m:n], j[0:m+n]. In particular, we have 𝖧a,b[i;i]=0.

2.3 Permutations and unit-Monge matrices

Let I, J be finite intervals of half-integers.

Definition 6.

A subpermutation is a partial injective function P:IJ. We denote the domain of P by domP, and the range of P by ranP. Its degree is |domP|=|ranP|. A permutation is a subpermutation that is total and bijective.

Definition 7.

The symmetric group 𝕊n of degree n is the set of all permutations of degree n, where the group operation is given by standard composition.

Definition 8.

A permutation (respectively, subpermutation) matrix is a zero-one matrix PI;J, containing exactly one (respectively, at most one) nonzero element in every row and every column. A (sub)permutation P:IJ corresponds to a (sub)permutation matrix PI;J, where Pı^=ȷ^, whenever Pı^;ȷ^=1; such a pair ı^;ȷ^ will be called a nonzero of P. The order of a permutation matrix is the degree of the corresponding permutation.

 Remark 9.

We will be using deliberately an identical notation for a (sub)permutation and its corresponding (sub)permutation matrix; the actual meaning will always be clear from the context.

We define two fundamental operations on matrices: dominance summation and taking cross-differences. For the first operation, it will be convenient to always assume the argument matrix to be indexed by half-integers, and the resulting matrix by integers; for the second operation, the index types of the argument matrix and the resulting matrix are reversed.

Definition 10.

Let DI;J be a matrix. Its dominance-sum matrix (also sometimes called distribution matrix) DΣ[I;J] is defined by

DΣ[i;j]=Di:;:j

for all i[I], j[J]. Analogously, its reverse dominance-sum matrix DΣ[I;J] is defined by

DΣ[i;j]=D:i;j:.
Definition 11.

Let A[I;J], B[J;K], C[I;K] be matrices. Tropical matrix multiplication AB=C is defined by

C[i;k]=j[J](A[i;j]B[j;k])=minj[J](A[i;j]+B[j;k])

for all i[I], k[K].

Definition 12.

Let D, E, F be matrices. The implicit tropical multiplication, also called sticky multiplication, is defined by

DE=Fiff DΣEΣ=FΣ
Definition 13.

The sticky multiplication monoid of permutation matrices n is the set of all n×n permutation matrices, with the monoid operation given by sticky matrix multiplication.

At the centre of the framework is an efficient algorithm for sticky multiplication of permutation matrices, proposed by Tiskin [28]; a similar algorithm was proposed independently by Sakai [23].

Theorem 14.

Let PQ=R, where P, Q, R are (sub)permutation matrices with at most n nonzeros each. Given the nonzeros of P, Q, the nonzeros of R can be obtained in time O(nlogn).

2.4 Sticky braids

We now take a complementary view on the sticky multiplication monoid of permutation matrices, defining it by an algebraic formalism of generators and relations.

Definition 15.

The sticky braid monoid111Notation n that we are using for the sticky braid monoid has already been used in the previous section for the sticky multiplication monoid of permutation matrices. We will see shortly that this abuse of notation is justified by the isomorphism of these monoids. n of degree n, as a finitely presented monoid in Coxeter presentation, is defined by a formal identity element ι, n1 formal generators τ1, τ2, …, τn1, the idempotence relations

τi2=τi (1a)
the far commutativity relations
τiτj=τjτi ji2 (1b)
and the braid relations
τiτjτi=τjτiτj ji=1 (1c)

where i,j[1:n1]

Just as the elements of the classical braid group 𝔹n are represented visually by braids, an element of n can be represented by a sticky braid of degree n: a set of n vertically-monotone curves (called strands) connecting disjoint pairs between two sets of n points, each set drawn on a separate horizontal line. The strands’ crossings correspond to the monoid’s generators. Such a representation is not unique; two sticky braids are considered to be equivalent, if they represent the same element of n, i.e. they can be obtained from one another by applying the monoid’s relations.

Two sticky braids of the same degree can be composed in the same way as classical braids: we draw one braid above the other, identifying the bottom nodes of the top braid with the respective top nodes of the bottom braid, and then we join up each pair of strands that became incident.

From the general theory of Coxeter monoids, it is well-known that the elements of the sticky braid monoid n correspond bijectively to the elements of the symmetric group 𝕊n, and that any reduced word (minimal-length word in the generators) for an elements in one of these structures is also a reduced word for the corresponding element in the other [29] (see also [14]). For completeness, we now restate these facts in the language of sticky braids.

Definition 16.

In a sticky braid, a pair of strands is called unentangled, if they cross one another at most once (i.e. either once, or not at all), and entangled otherwise. A sticky braid is called reduced, if every pair of its strands is unentangled.

Note that the composition of two reduced sticky braids may not in general be reduced. The following two propositions on sticky braid reduction are a consequence of the general Coxeter theory (see e.g. [7]).

Proposition 17.

Every sticky braid has an equivalent reduced sticky braid.

A reduced sticky braid can be associated with a permutation by canonical projection π:n𝕊n: a strand connecting top node i and bottom node j in the braid corresponds to an argument-image pair ij.

Proposition 18.

All the reduced braids of a given degree having the same canonical projection are equivalent in n.

A surprising key theorem of the framework states that the two monoids we have defined are in fact isomorphic. This has been rediscovered independently by Pflueger [22].

Theorem 19.

For a fixed degree n, the sticky multiplication monoid of permutation matrices (Definition 13) is isomorphic to the sticky braid monoid defined by the Coxeter presentation (Definition 15). Hence, we are justified in retaining the notation n for both monoids.

The isomorphism in the above theorem is established by identifying formal generator τi with a transposition, i.e. the permutation of interval 0:n that transposes i with i+, and leaves the other elements unchanged.

2.5 Iterative combing

Given a (generally unreduced) sticky braid, an equivalent reduced one can be obtained by applying the following iterative combing procedure. We iterate over the braid’s transpositions from the top down (formally, along the word in the generators constituting the braid), removing those that create entanglements, while keeping the transpositions between pairs of strands that have not crossed previously. This procedure is an equivalence transformation on sticky braids, producing a reduced braid equivalent to the original one.

Iterative combing can also be used to perform sticky multiplication of permutations. Given permutations P, Q, we first represent them as reduced braids (P), (Q), picking arbitrary ones from the respective equivalence classes. We then compose these two braids and apply iterative combing to the composition (which may skip (P) and start at the boundary between the two braids, since (P) is already reduced). Finally, we take the canonical projection of the resulting braid as the product PQ. Reflecting the asymmetric nature of this procedure, we can think of sticky multiplication of permutations P, Q as reduction of the permutation Q relative to P. We denote222Pflueger [22] develops a particularly elegant theory and notation for this concept of relative reduction; however, the above definition and notation will suffice for our purposes. the result of such a reduction by PQ. Formally, we have PQ=P1(PQ), where the inversion and the first multiplication are in 𝕊n, and the second multiplication in n.

3 Framework extension

3.1 Affine permutations, matrices and sticky braids

The definitions of the previous sections have a natural extension, from finite objects to analogous objects with an infinite periodic structure. Generally, extensions of such type are well-known in algebra, where the infinite periodic analogues of structures such as 𝕊n or n are called affine (see e.g. [16, 19]); we borrow this term here.

Definition 20.

A skew-affine permutation of degree333We are deliberately using the same notation n for the period length of input string b, and for the degree of a generic affine permutation or braid, since they will turn out to be equal in our main results. n and skew s is a bijective function P:^^, such that P(ı^+n)=P(ı^)+n for all ı^^, and ı^0:nP(ı^)=ı^0:nı^+s. A skew-affine permutation with skew 0 is called affine.

Note that an affine permutation of degree n is also affine for any degree that is multiple of n.

Definition 21.

The affine symmetric group 𝕊~n of degree n is the set of all affine permutations of degree n, where the group operation is given by standard composition.

Pflueger [22] shows how the definitions of a permutation matrix (Definition 8), dominance-sums matrix (Definition 10), tropical multiplication (Definition 11), sticky multiplication (Definition 12) all extend naturally from the finite to the affine case.

Definition 22.

The sticky multiplication monoid of affine permutation matrices ~n is the set of all affine permutation matrices of degree n, with the monoid operation given by sticky affine matrix multiplication.

 Remark 23.

Sticky multiplication of affine permutation matrices extends to skew-affine permutation matrices in a natural way; the skew of the product is equal to the sum of the skews of the multiplicands, while applying the skews commutes with taking the actual product.

A smooth transition from the finite objects and their properties to their affine counterparts stops with the Tiskin–Sakai algorithm for multiplication in n (Theorem 14): the algorithm depends crucially on the permutations and their matrices being finite, and does not seem to allow for an easy generalisation to ~n. However, an affine extension of the algorithm to n can still be obtained: this is one of the contributions of this paper.

An affine extension of the definition of the sticky braid monoid in Coxeter presentation (Definition 15) is by a standard but elegant trick. It is achieved by adding an extra generator to the presentation, and extending the relations to this new generator in a natural way.

Definition 24.

The affine sticky braid monoid ~n of degree n, as a finitely presented monoid in Coxeter presentation, is defined by the formal identity element ι, n formal generators τ0, τ1, τ2, …, τn1, the idempotence relations

τi2=τi i[0:n1] (2a)
the far commutativity relations
τiτj=τjτi i,j[0:n1],2jin2 (2b)
and the braid relations
τiτjτi=τjτiτj i,j[0:n1],ji{1,n+1} (2c)

Iterative combing and relative reduction extend directly to their affine counterparts.

Pflueger [22] has established the isomorphism between the two definitions ~n given above, mirroring Theorem 19.

Theorem 25.

For a fixed degree n, the sticky multiplication monoid of affine permutation matrices (Definition 22) is isomorphic to the affine sticky braid monoid defined by the Coxeter presentation (Definition 24). Hence, we are justified in retaining the notation ~n for both monoids.

3.2 Affine replication and ΦΓ-decomposition

We now introduce some basic definitions for constructing and decomposing affine permutations.

Definition 26.

Let f:0:n^ be an injection whose n images are pairwise non-congruent modulo n. The affine replication of f, denoted replnf, is the unique affine permutation of degree n whose restriction to 0:n equals f. Formally, replnf(x^)=f(x^0)+x^x^0, where x^00:n, and n(x^x^0).

 Remark 27.

We can “choose a different period” of an affine permutation that we replicate: replnf is defined the same way for f:kn:(k+1)n^.

From now on, we are concerned with affine permutations of a fixed degree N.

Definition 28.

An affine permutation Φ~ is finite-type if its restriction Φ~|0:N is a (finite) permutation. An affine permutation Γ~ is Grassmannian if its restriction Γ~|0:N is monotone increasing.

In other words, an affine permutation is finite-type iff it can be obtained by the affine replication of an ordinary (finite) permutation. Note that the inverse of a finite-type permutation is still finite-type, while the inverse of a Grassmannian permutation is not necessarily Grassmannian.

Proposition 29.

Any affine permutation P~ can be represented as a product of two affine permutations P~=Φ~Γ~ such that Φ~ is finite-type and Γ~ is Grassmannian. Such a ΦΓ-decomposition can be computed in time O(NlogN) and linear space.

Proof.

Let Φ0 be the permutation that sorts elements of 0:N with respect to the order of their P~-images. It can be obtained from P~ in time O(NlogN). Then Φ~=replN(Φ0), and Γ~ is obtained as Φ~1P~, which is affine as a product of two affine permutations.

We define ΓΦ-decomposition symmetrically to ΦΓ-decomposition: P~=Γ~Φ~, where Γ~ is an inverse of a Grassmannian permutation, and Φ~ is finite-type. A ΓΦ-decomposition is essentially the ΦΓ-decomposition for the inverse permutation: P~1=(Φ~)1(Γ~)1, and can be obtained in the same running time. We shall write ΓΦN- or ΦΓN-decomposition to specify the exact degree if needed.

 Remark 30.

The definitions of Grassmannian and inverse Grassmannian permutations, ΦΓ- and ΓΦ-decompositions extend naturally from affine to skew-affine permutations; the definition and the role of finite-type permutations remains unchanged in such an extension.

 Remark 31.

The permutations comprising the ΦΓ-decomposition vary based on the exact N-period chosen of an affine permutation of degree N. In particular, the ΦΓ-decomposition of an affine permutation may change significantly when the permutation is cyclically shifted.

Finite-type permutation Φ~ in the ΦΓ-decomposition of P~ can be thought of as the restriction of P~ to 0:N with compressed coordinates: it maps the interval 0:N onto itself, and it swaps a pair of elements within 0:N if and only if P~ swaps them as well. This can be summarised by the following lemma.

Lemma 32.

Let P~=Γ~Φ~ be the ΓΦ-decomposition of an affine permutation of degree N. The sequences (P~1(ȷ^))ȷ^0:N and (Φ~1(ȷ^))ȷ^0:N have the same relative order.

Proof.

Follows from the fact that Γ~ preserves the order of the elements it maps to 0:N.

3.3 SLCS kernels and embedded braids

We finally are able to apply the algebraic framework presented in Subsections 2.3–2.5 to the SLCS problem introduced in Subsection 2.2; in doing so, we follow [24, 25].

The key property of SLCS matrices is captured by the following theorem.

Theorem 33 (SLCS matrix canonical representation).

The SLCS matrix 𝖧a,b is unit-anti-Monge:

𝖧a,b[i;j]=i+j+m𝖯a,bΣ[i;j]=m𝖯a,bΣ[i;j]

for all i,j, where 𝖯a,b is a permutation matrix.

Definition 34.

The SLCS kernel for strings a, b is the permutation matrix 𝖯a,bm:n;0:m+n, determined by Theorem 33. The string-substring LCS subkernel is the subpermutation matrix 𝖯a,b=𝖯a,b0:;:n

The intuition behind Theorem 33 is as follows. Let b be a substring of b. If substring b is extended on either the left or the right by one character, then its LCS score against string a either is unchanged, or increases by 1, depending on whether or not the new character of b can be matched to a character of a. The unit-anti-Monge property of matrix 𝖧a,b reflects the fact that, as substring b grows while a is fixed, obtaining a matching for each new character becomes relatively harder. Each nonzero in the SLCS kernel 𝖯a,b represents a unit obstacle to character matching: at such a point, extending b independently on the left or on the right gives a new match, and a corresponding increase by 1 in the LCS score; however, these two new matches are incompatible, so extending b simultaneously on the left or on the right gives a total increase in the LCS score by 1 instead of 2. The above is only very general intuition, since “obtaining a match” for a given character is not absolute, but depends on the choice of a particular highest-scoring path through the LCS grid. Even more generally, one can think of the nonzeros in 𝖯a,b as “the places where string-substring LCS score, as a function of the substring’s endpoints, is nonlinear”.

SLCS kernels, along with the Tiskin–Sakai sticky multiplication algorithm, provide the basis for a divide-and-conquer approach to the SLCS problem in Tiskin’s framework.

Theorem 35 (SLCS kernel composition).

Let a=aa′′, where the substrings a, a′′ are of length m, m′′, respectively. The string-substring LCS subkernel can be obtained as a sticky product 𝖯a,b=𝖯a,b𝖯a′′,b in time O(nlogN), where N=min{m,m′′,n}.

An analogous, slightly more technical statement holds for full SLCS kernels. As a basis for affine extension, Theorem 35 will be sufficient in its given form.

Just like a permutation can be represented by a sticky braid, an SLCS kernel can be represented with a sticky braid embedded in a special way in the LCS grid (both representations are only unique up to equivalence in n).

Definition 36.

An embedded (sticky) braid in an LCS grid is a sticky braid of degree m+n, where the strands are realised by edge-disjoint directed cycles in the dual grid; each cycle is viewed as originating and terminating at the external face. A strand’s head (respectively, tail) is the edge on the corresponding cycle that is the outgoing (respectively, incoming) edge of the external face.

Definition 36 implies that every cell in the grid is traversed by exactly two strands of an embedded braid, entering across the cell’s west and north edges, and leaving across the cell’s east and south edges (not necessarily in that order). A diagonal edge, if it exists, is never traversed by a strand. Thus, the strands never cross in a match cell, but may or may not cross in an mismatch cell.

Definition 37.

An embedded braid in G is called saturated, if its strands cross in all the mismatch cells.

Note that a saturated braid is generally unreduced. Upon reduction, such a braid corresponds precisely to the SLCS kernel.

Theorem 38.

Let B be a reduced braid, equivalent to the saturated braid on the LCS grid 𝖦a,b. The canonical projection of B is the SLCS kernel: π(B)=𝖯a,b.

 Remark 39.

Theorem 33 states that SLCS score is equal to the number of all the strands leaving through the east boundary of the subgrid (of which there are exactly m), excluding those emerging from its west boundary: 𝖯a,bΣ[i;j] gives the number of the strands that start before i and end after j, thus piercing both vertical boundaries of the subgrid. Therefore SLCS score is equal to the number of strands that start at the north boundary and leave through the east boundary of the corresponding subgrid.

The iterative combing procedure can be realised on an LCS grid in a straightforward way. We initialise an embedded braid as a saturated embedded braid, and then iterate over the cells in an arbitrary order compatible with the north-to-south and east-to west partial order of the cells. In a match cell, we do nothing; in a mismatch cell, we prevent the strands from becoming entangled in exactly the same way as in iterative combing on generic sticky braids. Just like with generic braids, an entanglement check can be performed in time O(1), so the whole iterative combing procedure runs in time O(mn).

We now consider an extension of the semi-local LCS problem to the inputs a, b, where a is an ordinary string, and b is an infinite repeat. The analogue for the SLCS problem for such a setting is the string-substring problem on a, b; the other three components of the SLCS problem are not required. The definition of SLCS matrix extends naturally to its affine counterpart. The definitions of an SLCS kernel and an embedded braid also extend directly, except that

  • we must assume that every character of a appears in b at least once (otherwise a character in a not appearing in b would create a spurious unattached strand in the embedded braid); this condition can obviously be guaranteed without loss of generality;

  • every cell in the grid, either match or mismatch one, should be considered as giving a total skew of 1 to the pair of strands passing through the cell. For a given character of a, a horizontal row of cells across a single period of the LCS grid will therefore give a skew of 1 to every strand in the braid. The resulting affine braid across the whole grid will acquire a total skew of m, so the affine LCS kernel will be a skew-affine permutation with skew m, which we denote 𝖯a,b.

Theorem 35 on string-substring LCS kernel composition extends to the affine setting with a proof that is vertually identical to the finite case (for now, without any claims on running time).

Theorem 40 (Affine string-substring LCS kernel composition).

The affine string-substring LCS kernel can be obtained as a sticky product 𝖯a,b=𝖯a,b𝖯a′′,b.

Here, the sticky product of skew-affine permutations is understood as in Remark 23.

The definitions of an LCS grid (Definition 4), an embedded braid (Definition 36) and a saturated braid (Definition 37) extend directly to the case of the inputs a, b. In order for the iterative combing procedure to be realised on an infinite periodic LCS grid in such a setting, Tiskin [27] modified it to the following procedure, called wraparound combing. We initialise an affine embedded braid as a saturated affine embedded braid. We then iterate over the cells in the following order. In the outer loop, we iterate over the characters of a, moving north-to-south through the grid. Recall that we assume that every character of a occurs in b at least once; therefore, there will be a match cell in the row of the grid corresponding to the current character. In the inner loop, we iterate over one whole period of b, beginning at a match cell in the current row (if there is more that one per period, one is chosen arbitrarily); note that the choice of the period may differ between the iterations of the outer loop. The combing within each cell of a period is now performed as in ordinary iterative combing, except that the entanglement check should now respect the infinite periodic structure of the grid. Such a check can still be performed in time O(1), and the whole iterative combing procedure still runs in time O(mn).

4 Main results

4.1 Affine sticky multiplication

We reduce sticky multiplication of affine permutations P~, Q~ to sticky multiplication of finite permutations. The reduction is achieved by computing the sticky product of one fixed period of P~, Q~, taken with respect to ranP~=domQ~, joined by one extra period on either side (in total, three periods are multiplied). We then take the restriction of the product to the original fixed period and replicate it infinitely, obtaining an affine permutation. We claim that this procedure is well-defined, and that the resulting permutation is P~Q~. Formally, we describe this procedure using the concept of ΓΦ-decomposition, see Algorithm 1.

Algorithm 1 Affine Sticky Multiplication.

Input: affine permutations P~, Q~ of degree n.

Output: affine permutation P~Q~.

Description:

  1. 1.

    Compute ΓΦ3n-decomposition P~=Γ~Φ~ and ΦΓ3n-decomposition Q~=Φ~Γ~.

    Let P=Φ~|0:3n, Q=Φ~|0:3n.

  2. 2.

    Compute sticky product PQ. Compute PQ=P1(PQ).

  3. 3.

    Compute S=PQ|n:2nΓ~, which is an injection n:2n^. We have P~Q~=replnS​.

  4. 4.

    Output the affine permutation P~P~Q~ as the required product P~Q~.

(a) Affine permutations P~, Q~.
(b) ΓΦ3n- and ΦΓ3n-decompositions.
(c) Computing finite sticky product.
(d) Replicating the middle period.
Figure 1: Performing Algorithm 1 for a pair of affine permutations P~, Q~.
Example 41.

Figure 1 shows the application of Algorithm 1 to affine permutations P~ and Q~, both of degree 4. Figure 1(a) shows permutations P~, Q~. For clarity, we draw them as reduced affine braids, one on top of the other.

Figure 1(b) shows ΓΦ3n-decomposition P~=Γ~Φ~ and ΦΓ3n-decomposition Q~=Φ~Γ~, which are both affine permutations of degree 12. The finite permutation formed by the 12-period of Φ~ is P, and the finite permutation formed by the 12-period of Φ~ is Q.

Figure 1(c) shows the sticky product of finite permutations P, Q and, in particular, relatively reduced permutation PQ. Lower half of the strands highlighted in purple corresponds to the subpermutation S=PQ|n:2nΓ~, and their upper half connects elements of the domain of S to their P~-preimages.

Finally, Figure 1(d) shows the result of affine replication of highlighted subpermutation, which is the required product P~Q~.

Theorem 42.

Algorithm 1 computes the sticky product P~Q~ correctly.

Proof.

See Appendix B.

 Remark 43.

Due to Remark 23, sticky multiplication is well-defined also for skew-affine permutations. Our algorithm makes no use of zero skew in the input permutations, therefore it also works for skew-affine permutations.

Now, with the correctness of the algorithm proven, it only remains to show its time and space complexities. We assume that an affine permutation is represented in the algorithm as a mapping of one single period into integers.

Theorem 44.

Algorithm 1 runs in time O(nlogn) and space O(n).

Proof.

We go through the steps of the algorithm and verify that each step is computable in the time and space claimed.

The algorithm starts by computing decompositions of the input permutations. Due to Proposition 29, both ΦΓ- and ΓΦ-decompositions can be computed in time O(nlogn) and linear space. The sticky multiplication of finite permutations is performed by the Sakai–Tiskin algorithm [28] that also requires time O(nlogn) and linear space.

The only remaining part of the algorithm is the composition (i.e. standard multiplication in ~n) of affine permutations. This can be done trivially in linear time and space similarly to the multiplication of finite permutations.

4.2 Doubly-periodic LCS

Theorem 45.

Given strings a, b, kernel 𝖯ak,b can be computed in time O(mn+nlognlogk) and space O(n), where m and n are the lengths of a and b, respectively.

Proof.

First we use wraparound combing to construct kernel 𝖯a,b. This can be done in time O(mn) and linear space.

Then we compute kernel 𝖯ak,b. By Theorem 40, it can be done by raising 𝖯a,b to the power k in ~n (respecting the skew). We do this by binary exponentiation using Algorithm 1 as a subroutine. As mentioned in Remark 43, the affine sticky multiplication can be performed directly on skew-affine permutations. Thus, this part works in time O(nlognlogk) and linear space.

We now show how the doubly-periodic LCS score can be retrieved from kernel 𝖯ak,b. We will no longer be using the periodic structure of the first string, so for now let us denote this string by u. Let the second string be a repeat b; the case of a general periodic string is considered in the full version of the paper.

Theorem 46.

Given kernel 𝖯u,b, the score lcs(u,b) can be computed in time O(n), where n=|b|.

Proof.

Recall (Remark 39) that the string-substring LCS length is equal to the number of kernel strands that start at the top of the subgrid enclosed by the query substring and leave through its right boundary. In the periodic case, the strands in the corresponding skew-affine kernel braid need to be counted with their multiplicities.

Given an affine permutation P~ of degree n and a half-integer ȷ^, 0<ȷ^<n, any particular border between two n-periods is crossed by exactly P~(ȷ^)n copies of the strand connecting ȷ^ and P~(ȷ^). In our case of string-substring LCS, string b encloses a subgrid comprised of periods of the full grid, and the boundary in question is the right boundary of this subgrid. We are only counting the strand copies originating within this subgrid, of which there are at most . Thus, the required count can be obtained in linear time by the following formula:

lcs(u,b)=0<ȷ^<nmin{,𝖯u,b(ȷ^)n}.

We now have a solution to the original problem of finding the LCS length of two repeats.

Corollary 47.

lcs(ak,bl) can be computed in time O(mn+nlognlogk).

Proof.

Follows from Theorem 45 and Theorem 46 with u=ak.

 Remark 48.

It is straightforward to generalise Theorem 45, replacing string ak with an arbitrary SLP-compressed string (i.e. a string generated by a context-free grammar). In this setting, each concatenation specified by the grammar will take time O(nlogn). The rest of the algorithm is independent of the structure of the first input string. Therefore, our approach also provides an efficient algorithm for the more general problem of finding the LCS length for an SLP-compressed string against a periodic string.

5 Experimental implementation

Having such an elementary formulation, the problem of doubly-periodic LCS lends itself to being posed in programming contests. It was set by the first author as Problem L in LOUD Enough Contest 2 in 44th Petrozavodsk Programming Camp. Petrozavodsk Programming Camps are long-established training camps that serve as a conference and an experimental ground for some of the prominent ICPC-style problem setters. This contest was also later used as Stage 5: Northern of the 2nd Universal Cup [1], the regular season of the contests, the most popular among the strongest competitive programming teams in the world, including many ICPC World Champions of the past.

The model solution coded by the first author, based on the algorithm described in this paper, was able to process pairs of periodic input strings with lengths far beyond the reach of all known alternative algorithms. The algorithm was later reimplemented in the form of an open-source library written in Rust [11].

6 Conclusion

In this paper, we have addressed a natural problem of obtaining the LCS score for a pair of periodic strings. The solution is based on Tiskin’s algebraic framework. We have extended this framework by making explicit the concept of affine permutations and affine braids, previously used in the solution of the singly-periodic LCS problem by wraparound combing. We have also added to the framework some additional elements necessary for dealing with affine permutations. Based on these developments, we have proposed an efficient algorithm for sticky multiplication of affine permutations, which may be of independent interest, and an efficient algorithm for the doubly-periodic LCS problem.

The theoretical results of this paper are supported by an experimental implementation, which is available online [11].

Our approach in fact provides, without much change, an efficient algorithm for the more general problem of obtaining the LCS length between an SLP-compressed string and a periodic string; its running time is bounded by a low-degree polynomial in the lengths of the SLP representing the first input string, and the period length of the second input string. This should be contrasted with the problem of obtaining the LCS score between two SLP-compressed strings, which, despite recent algorithmic advances in the area of processing SLP-compressed strings without decompression, is known to be NP-hard. The complexity gap between the two problems remains open; it may be an interesting challenge to study the complexity of analogous problems with inputs that have an intermediate form between a periodic string and a generic SLP-compressed string.

References

  • [1] LOUD Enough Contest 2. Lcslcslcs. 44th Petrozavodsk Programming Camp, february 4 2023. URL: https://qoj.ac/contest/1388/problem/6557.
  • [2] A. Abboud, A. Backurs, and V. Vassilevska Williams. Tight Hardness Results for LCS and Other Sequence Similarity Measures. In Proceedings of FOCS, pages 59–78, 2015. doi:10.1109/FOCS.2015.14.
  • [3] S W Al-Haj Baddar and K E Batcher. Designing Sorting Networks: A New Paradigm. Springer, 2011. doi:10.1007/978-1-4614-1851-1.
  • [4] Arturs Backurs and Piotr Indyk. Edit Distance Cannot Be Computed in Strongly Subquadratic Time (Unless SETH is False). SIAM Journal on Computing, 47(3):1087–1097, January 2018. doi:10.1137/15M1053128.
  • [5] K. E. Batcher. Sorting networks and their applications. In Proceedings of AFIPS, volume 32, pages 307–314, 1968. doi:10.1145/1468075.1468121.
  • [6] Philip Bille and Martin Farach-Colton. Fast and compact regular expression matching. Theoretical Computer Science, 409(3):486–496, 2008. doi:10.1016/j.tcs.2008.08.042.
  • [7] A Björner and F Brenti. Combinatorics of Coxeter Groups, volume 231 of Graduate Texts in Mathematics. Springer, 2005.
  • [8] B. Bukh and C. Cox. Periodic words, common subsequences and frogs. The Annals of Applied Probability, 32(2):1295–1332, 2022. arXiv:1912.03510.
  • [9] T H Cormen, C E Leiserson, R L Rivest, and C Stein. Introduction to Algorithms. The MIT Electrical Engineering and Computer Science Series. The MIT Press and McGraw–Hill, second edition, 2001.
  • [10] M. Crochemore, G. M. Landau, and M. Ziv-Ukelson. A subquadratic sequence alignment algorithm for unrestricted score matrices. SIAM Journal on Computing, 32:1654–1673, 2003. doi:10.1137/S0097539702402007.
  • [11] Nikita Gaevoy. Seaweed. On Github. URL: https://github.com/nikgaevoy/seaweed.
  • [12] P. Gawrychowski. Faster algorithm for computing the edit distance between SLP-compressed strings. In Proceedings of SPIRE, volume 7608 of Lecture Notes in Computer Science, pages 229–236, 2012. doi:10.1007/978-3-642-34109-0-24.
  • [13] D. Hermelin, G. M. Landau, S. Landau, and O. Weimann. Unified Compression-Based Acceleration of Edit-Distance Computation. Algorithmica, 65(2):339–353, 2013. doi:10.1007/s00453-011-9590-6.
  • [14] Toby Kenney. Coxeter groups, Coxeter monoids and the Bruhat order. Journal of Algebraic Combinatorics, 39(3):719–731, 2014. doi:10.1007/s10801-013-0464-7.
  • [15] D. E. Knuth. The Art of Computer Programming: Sorting and Searching, volume 3. Addison Wesley, 1998.
  • [16] Joel Brewster Lewis. Affine symmetric group. WikiJournal of Science, 4(1):3, 2021. doi:10.15347/WJS/2021.003.
  • [17] Yury Lifshits and Markus Lohrey. Querying and Embedding Compressed Texts. In Proceedings of MFCS, volume 4162 of Lecture Notes in Computer Science, pages 681—-692, 2006. doi:10.1007/11821069_59.
  • [18] Markus Lohrey. Algorithmics on SLP-compressed strings: A survey. Groups - Complexity - Cryptology, 4(2):241–299, January 2012. doi:10.1515/gcc-2012-0016.
  • [19] Neal Madras and Justin M. Troyka. Bounded affine permutations I. Pattern avoidance and enumeration. Discrete Mathematics and Theoretical Computer Science, 22, 2021. doi:10.46298/dmtcs.6178.
  • [20] W. J. Masek and M. S. Paterson. A faster algorithm computing string edit distances. Journal of Computer and System Sciences, 20(1):18–31, 1980. doi:10.1016/0022-0000(80)90002-1.
  • [21] Saul B. Needleman and Christian D. Wunsch. A general method applicable to the search for similarities in the amino acid sequence of two proteins. Journal of Molecular Biology, 48(3):443–453, 1970. doi:10.1016/0022-2836(70)90057-4.
  • [22] N Pflueger. An extended Demazure product on integer permutations via min-plus matrix multiplication. Technical report, arXiv 2206.14227, 2022. arXiv:2206.14227.
  • [23] Yoshifumi Sakai. A fast algorithm for multiplying min-sum permutations. Discrete Applied Mathematics, 159:2175–2183, 2011. doi:10.1016/j.dam.2011.06.022.
  • [24] A. Tiskin. Semi-local longest common subsequences in subquadratic time. Journal of Discrete Algorithms, 6(4):570–581, 2008. doi:10.1016/j.jda.2008.07.001.
  • [25] A. Tiskin. Semi-local string comparison: Algorithmic techniques and applications. Mathematics in Computer Science, 1(4):571–603, 2008. doi:10.1007/s11786-007-0033-3.
  • [26] A. Tiskin. Faster subsequence recognition in compressed strings. Journal of Mathematical Sciences, 158:759—-769, 2009. doi:10.1007/s10958-009-9396-0.
  • [27] A. Tiskin. Periodic String Comparison. In Proceedings of CPM, volume 5577 of Lecture Notes in Computer Science, pages 193–206, 2009. doi:10.1007/978-3-642-02441-2_18.
  • [28] A. Tiskin. Fast Distance Multiplication of Unit-Monge Matrices. Algorithmica, 71:859–888, 2015. doi:10.1007/s00453-013-9830-z.
  • [29] S.V. Tsaranov. Representation and Classification of Coxeter Monoids. European Journal of Combinatorics, 11(2):189–204, March 1990. doi:10.1016/S0195-6698(13)80073-X.
  • [30] R. A. Wagner and M. J. Fischer. The String-to-String Correction Problem. Journal of the ACM, 21(1):168–173, 1974. doi:10.1145/321796.321811.

Appendix A Transposition networks

In order to provide the proof for the correctness of the Affine Sticky Multiplication, we need to introduce another classical object, transposition networks, which is closely related to sticky braids. Those are a subclass of comparison networks, introduced by Batcher [5] (see also [15, 9, 3]).

Definition 49.

A comparison network is a dag (directed acyclic graph), where the internal vertices act as (comparator) gates. Such a gate has indegree and outdegree 2, and operates by sorting the values of its two incoming edges in increasing order, and returning the minimum and the maximum each on a prescribed outgoing edge. A comparison network as a whole represents a computation on values, which are passed along the circuit’s edges; the source and the sink vertices represent the input and output values, respectively.

Comparison networks can be represented by wire diagrams (also known as Knuth diagrams), where the values propagate along an array of parallel wires. A comparator gate is represented by a directed line segment, joining orthogonally any two (not necessarily adjacent) wires. We will be dealing with the following restricted type of comparison network.

Definition 50.

A transposition network (also sometimes called a primitive comparison network) is a comparison network that can be represented by a wire diagram, where every comparator connects a pair of adjacent wires.

A sticky braid corresponds to a transposition network in an obvious way, with each strand crossing represented by a gate. Iterative combing of the braid corresponds to an operation of the network on an input array that is sorted in reverse, and the strands of the resulting reduced braid are given by the trajectories of the values. Sticky multiplication PQ, when performed by iterative combing of the composition of sticky braids (P), (Q), corresponds to the transposition network of (P) applying permutation P to an anti-sorted array, and the resulting permuted array then being fed to the transposition network of (Q).

 Remark 51.

In the proofs below, we will be using a specific anti-sorted input array xı^=ı^, ı^0:n. Permutation P applied to this array produces the output array yȷ^=P1(ȷ^), ȷ^0:n.

Appendix B Proof of main theorem

We now prove the correctness of Algorithm 1: namely, that the injection S=PQ|n:2nΓ~ can be replicated to form an affine permutation, and that the result of the replication provides the correct value for P~Q~. Equivalently, we need to prove that S coincides with a single period of P~Q~, that is, S=P~Q~|n:2n as functions n:2n^. Let us now introduce the main ingredients of the proof.

In order to relate finite injection S and affine permutation P~Q~, recall how affine permutation P~Q~ and finite permutation PQ are interpreted using transposition networks:

  • P~Q~ is realised by the affine network (Q~) on input sequence y=(P~1(ȷ^))ȷ^^;

  • PQ is realised by the network (Q) on the input array y=(P1(ȷ^))ȷ^0:3n.

Note that y and y have the same relative order within the interval 0:3n, this is due to P being a restriction of the finite-type permutation Φ~, where P~=Γ~Φ~, and Lemma 32. Therefore, we can replace y with the restricted sequence y0:3n as an input for (Q); this network will still realise PQ on the new input sequence. Recall that we need PQ to obtain the injection S: in Algorithm 1 we restrict PQ to n:2n, and then decompress its output by Γ~.

It turns out that injection S can also be realised by the affine network (Q~). For convenience, let us introduce a notation for the trajectories of input elements through this network and terminology to distinguish two types of such trajectories.

Definition 52.

Let (xȷ^)ȷ^^ be an input sequence for (Q~),

  • we denote the trajectory of an element xȷ^ through this network by sx(ȷ^),

  • we call an element xȷ^ inner if ȷ^0:3n, otherwise we call it outer. We use the adjectives inner and outer also for the trajectory sx(ȷ^).

In our argument, injection S will be realised by the inner trajectories originating within the range n:2n of an appropriately chosen input sequence. We construct this input sequence by modifying sequence y so that

  • only outer elements are changed,

  • the trajectory of a specific inner element yȷ^0 is preserved; here ȷ^0n:2n can be chosen arbitrarily in advance.

Theorem 53.

Let ȷ^0n:2n. There exists an input sequence u for (Q~) such that:

  1. 1.

    (Q~) sends uȷ^0 to the position P~Q~(ȷ^0) of the output,

  2. 2.

    for each ȷ^n:2n, (Q~) sends uȷ^ to the position S(ȷ^) of the output.

In particular, substituting ȷ^=ȷ^0 gives P~Q~(ȷ^0)=S(ȷ^0).

Proof.

Recall that y is the input sequence for (Q~) on which it realises the permutation P~Q~. We obtain the required sequence u by two transformations applied to y in succession. We design the transformations so that they preserve the trajectory of yȷ^0=uȷ^0. We then show that the network (Q~) behaves on the input sequence u the same as the network (Q) with output decompressed by Γ~, thus realising S.

First, substract yȷ^0 from every yȷ^, so that the input sequence y becomes integer, while keeping the relative order of its elements, and yȷ^0=0. The position of yȷ^0 at the output of (Q~) equals P~Q~(ȷ^0) by definition. Also note that yȷ^0±n=n, as P~ is affine. Now for the first transformation of y, let

yȷ^={+12ȷ^<0 and yȷ^<0,12ȷ^>3n and yȷ^>0,yȷ^otherwise

(see Figure 2(b)). The positions of yȷ^0±n at the output of (Q~) have remained the same as those of yȷ^0±n. To prove that, we first show that for any ȷ^, sy(ȷ^) crosses sy(ȷ^0±n) if and only if sy(ȷ^) crossed sy(ȷ^0±n). Consider the possible cases for ȷ^ (except the trivial ȷ^=ȷ^0±n):

  • ȷ^0n<ȷ^<ȷ^0+n: all such input elements remain unchanged, as well as the comparisons between them and yȷ^0±n at the gates of (Q~).

  • ȷ^<ȷ^0n: whenever such an input element yȷ^ is positive or inner negative, it remains unchanged; otherwise it is outer negative and becomes +12, which is still less than any positive integer, including yȷ^0n=n. This means that all trajectories that are at some point to the left of sy(ȷ^0n) behave as previously, up to a permutation of negative numbers and +12-s.

  • ȷ^>ȷ^0+n: symmetrically, to the right of sy(ȷ^0+n) all trajectories behave as previously, up to a permutation of positive numbers and 12-s.

(a) Network (Q~) and input sequence y.
(b) Sequence y.
(c) Sequence u.
Figure 2: Transforming the input sequence of the transposition network (Q~) in order to simulate the behaviour of (Q). Trajectories of negative (respectively, zero, positive) values are shown in red (respectively, yellow, blue).

It follows that the elements whose trajectories cross sy(ȷ^0±n) remained the same, as well as the gates at which the crossings happen. Therefore, all input elements yj, ȷ^0nȷ^ȷ^0+n, have kept their positions at the output of (Q~), including yȷ^0. Also note that outer inputs yȷ^ with ȷ^>3n are now all negative, and outer inputs yȷ^ with ȷ^<0 are now all positive.

Finally we obtain sequence u from y by letting

uȷ^={ȷ^>3n+ȷ^<0yȷ^otherwise

(see Figure 2(c)). The position of yȷ^0=0 at the output of (Q~) equals P~Q~(ȷ^0): it has not changed compared to the position of yȷ^0, because no values have changed sign, whereas the position of zero at the output of a transposition network is determined completely by the signs of input elements. Thus, part  1 of the theorem statement holds.

We now show that for each ȷ^n:2n, the position of uȷ^ at the output of (Q~) equals S(ȷ^). Note that the inner subsequences of u and y coincide, and all the outer elements of u to the left of 0 (respectively, to the right of 3n) are all + (respectively, ). Therefore if an inner element of u and an outer one meet at a gate of (Q~), they are always swapped.

Recall that the injection S is obtained by Γ~ decompressing the output of (Q). The network (Q) only operates within the range 0:3n, and when given the array y0:3n as input, it obviously only compares elements within this range at every gate.

Recall also that the permutation Γ~ is defined by Q~=Φ~Γ~. It swaps elements from different 3n-periods whenever Q~ swaps them. Therefore in order for (Q~) to behave the same as (Q) with decompressed output, we want (Q~) to treat pairs of inner values exactly as (Q) does, and, moreover, to always swap whenever an inner value meets a outer one. As shown above, the sequence u achieves exactly that. Thus, part  2 of the statement holds.

Theorem 54.

Algorithm 1 computes the sticky product P~Q~ correctly.

Proof.

For each ȷ^0n:2n by substituting ȷ^=ȷ^0 into part  2 of Theorem 53 we have that P~Q~(ȷ^0)=S(ȷ^0). Hence S=P~Q~|n:2n, as required for correctness of the algorithm.