ELSEVIER
Information Processing Letters
Information Processing Letters 61 (1997) 293-295
Finding pattern matchings for permutations
Louis Ibarra’ Department of Computer Science, University of Victoria, Kctoria, BC V8W 3P6, Canada
Received 1 May 1996; revised 1 January 1997 Communicated by S.G. AkI
Abstract
GivenapermutationPof{l,..., k}andTof{l,..., n}, k < n, the pattern matching problem for permutations is to determine whether there is a length k subsequence of T whose elements are ordered in the same way as the elements of P. We present an 0( kn4) time and 0( kn3) space algorithm for finding a match of P into T or determining that no match exists, given that P is separable, i.e. contains neither (2,4,1,3) nor (3,1,4,2) as a subpattem. @ 1997 Elsevier Science B.V.
Keywords: Algorithms; Combinatorial problems
1. Introduction
Let P = (PI,... ,~k) beapermutationof {l,..., k}, called the pattern, and let T = (tl, . . . , t,) be a permutation of { 1, . . . , n}, called the text, k < n. A match of P into T is a subsequence T’ of T that is ordered in the same way as P, i.e. T’ = (ti, , . . * 9 tik > such that il < . . . < ik and ti, < ti, iff pr < ps for all 1 < r, s < k. If there is a match of P into T, then we say that T contains P. The pattern matching problem for permutations is to decide whether there is a match of P into T. (This problem is ascribed to Herb Wilf by Bose et al. [ 1 ] .) The general decision problem is NP-complete, but there is a polynomial time algorithm when P is separable, i.e. P contains neitherthepattem(2,4,1,3)nor(3,1,4,2) [l].The significance of separable permutations is the following property.
’ Email: [email protected].
Given a permutation P = (~1, . . . , pk) , a sepa- rating tree for P is a binary tree such that ( 1) the leaves are (p1 , . . . ,pk) in this order, (2) for any node
V, the leaves of the subtree rooted at V form a sub- range of {l,...,k}, i.e. if the leaves of the subtree
are (Pi,, ..~,pi,),then{pi,,...,pi,}={Z,E+l,..., I+ m} for some 1 < 1 < k and 0 6 m < k - 1. For example, a separating tree of P = (6,3,2, 1,4,5) is (6, ((3, (2, l)), (4,5))). The set of leaves of the subtree rooted at V is the range of V. It follows from the definition that if node V has left child V, and right child V,, then the range of y immediately precedes or succeeds the range of V,; in the first case, V is a posi- tive node and in the second case, V is a negative node. The key result is that a permutation P is separable iff P has a separating tree [ 1,2]. Furthermore, there is a simple linear time algorithm to decide if a permuta- tion P is separable and if so, to construct a separating tree for P [ I].
The algorithm for the pattern matching problem for permutations in [ 1 ] counts the number of matches
0020-0190/97/$17.00 @ 1997 Elsevier Science B.V. All rights reserved. PIISOO20-0190(97)00029-X
294 L. Ibarra/Information Processing Letters 61 (1997) 293-295
of P into T and runs in O(kn6) time with 0(kn4) space, given that P is separable. The algorithm uses dynamic programming by defining a set of subprob- lems for each node of P’s separating tree, so that the subproblems at an internal node can be solved from the subproblem solutions for its children. In this pa- per, we present an algorithm that finds a match of P
into T or determines that no match exists, in 0( kn4)
time and 0( kn3 ) space, given that P is separable. Our approach is also dynamic programming, but there are fewer subproblems per node of the tree and computing the solutions for an internal node from the solutions for its children is done more quickly than in [ 11.
We will use some additional definitions. We slightly generalize the definition of pattern matching for per-
mutations as follows. Let P = (PI,. . . ,pk),T =
(tl,..., tn) be sequences of distinct positive integers. We define a match of P into in in the same way as for
permutations, namely, a match of P into T is a subse-
quence ( til , . . .,ti,) ofiisuchthatit <...<ikand
ti~<ti~iffp,<p,foralll~r,s6k.If(ti,,...,ti,) is a match of P into F’, then the match’s elements are
ti, 7 . . . 3 ti,. For example, let V be the root of a subtree with leaves (2,1,3) and let T = (3,1,4,2,5). Then (3,2,5) is a match of V into T and this match’s ele- ments are 2,3,5. (For brevity, we use V to mean both a node V and the sequence of leaves of the subtree
rooted at V.)
2. The algorithm
The input is a text permutation T = (tl , . . . , tn), a separable pattern permutation P = (~1, . . . , pk) and a separating tree for P.
For every node V, we define the values L( Vi, j, x) and H( Yj, k, x) for all 1 6 i < j < k f n, 1 < x < n, as follows.
L( Vi,j,x) = max{{O} U {y 1 there is a match M
OfVinto(ti,..., tj) such that M’s smallest element is y and M’s largest element is < x}}.
Thus, L( vi, j, x) > 0 iff there is a match of V into
(ti7. . . , tj> such that the match’s elements are < x, and furthermore, L( Vi,j,x) is the maximin match element over all such matches. Continuing the exam- ple from above, the matches of V into T are (3,1,4), (3,1,5), (3,2,5), (4,2,5). Then L(Vl,5,1) =
L(Y1,5,2) = L(V1,5,3) = 0 and L(Yl,5,4) =
l,L(Yl,5,5) = 2. Notice that L(Vi,j,x) cannot decrease if we decrease i or increase j or increase x.
H(Yj,k,x) =min{{n+l}U{y 1 thereisamatch MofVinto(ti,..., tj) such that M’S largest element is y and M’s smallest element is 3 x}}.
Thus, H( Vj, k, x) < n + 1 iff there is a match of Vinto (ti,.. . , tj) such that the match’s elements are > x, and furthermore, H( Vi, j, x) is the mini-
max match element over all such matches. Continuing the example, H( V 1,5,1) = 4, H(Vl,5,2) = 5 and H(Vl,5,3) =H(V1,5,4) = H(V1,5,5) = 6.No- tice that H( Vj, k, x) cannot increase if we decrease j or increase k or decrease x.
We now show how to compute a node’s L, H val- ues in 0(n4) time per node. Since L( Yi,j,x) 3 y
iff H( Vi, j, y) < x, we can compute a node’s H val- ues from its L values in O(n3> time. Consequently, we only give the pseudocode for computing the L val- ues. (Alternatively, we can compute the H values by using routines symmetric to the following ones.) All
L values are initialized to 0. For any leaf and any x, i < j, we have L(Z& i, j, X) = mmi<l<j{tt ) tt <
x}. Thus, we can compute the L values for a leaf in 0(n4) time.
Let V be a positive node with left child V; and right child V,. The following routines compute the L values for V from the L, H values for fl, V,. (The routines and correctness proof for a negative node are symmetric.)
for j = 2 to n do
for x = 2 to n do (1)
ifL(V,,I,j-l,n-l)>OandH(V,,j,n,x)<n+l
then do
// There is a match of V into T
i’+ largestnumbersuchthat L(Vj,i’,j- 1,x- I) >O
k’ + smallest number such that H( V,, j, k’, x) < n + 1 for i = 1 to i’ do
for k = k’ to n do
lOWi + L(Vt,i, j - 1,X - 1)
high, + H(K j, k, 1) L(Yi, k, high,) +- max{L(Yi, k, highk), lowi}
for i = 1 to n do
for k = i to n do
best + 0 for x = I to n do
(2)
best + max{best, L( Vi, k, x)} L( Vi, k, x) + max{best, L( Vi, k, x)}
For example, let P = (1,2,4,3) and T = ( 1,2,5,4,3). Let the separating tree for P be ( ( 1,2), (4,3) ) and consider the computation of the
L. Ibarra/lnformation Processing Letters 61 (1997) 293-295 295
L values for the root V (after the L, H values for V’s
children have been computed). The first iteration of the outer loop of (1) that finds a match (i.e. evalu- ates the if statement as true) is j = 3,x = 3. Then i’ = 1, k’ = 4. The first iteration of the inner loop sets
low, = L(fl,1,2,2) = 1, high, = H(V,,3,4,3) = 5, and L( y 1,4,5) = 1; the second iteration sets
low, = L(V,1,2,2) = 1,highs =H(V,,3,5,3) =4, and L( y 1,5,4) = 1. The remaining iterations of
the outer loop do not change the L values. After
( 1) halts, L( Y 1,5,5) is still 0, but after (2) halts,
L(Vl,5,5) = 1, as required. We now show that the algorithm correctly computes
the L values for V.
Lemma 1. There exists a match ufV into (t;, . . . , tk) with smallest element > min and largest element < max if and only if L( vi, k, max) > min when the
algorithm halts.
Proof. (=+) Since V is a positive node, there exist X, j such that there is a match of 5 into (ti, . . . , tj-1) with smallest (respectively largest) element > min (re-
spectively < x - 1) and a match of V, into ( tj, . . . , tk) with smallest (respectively largest) element Z x (re- spectively < mar). By induction, L( V,, i,j - 1,x - 1) 2 min and H( V,, j, k, x) 6 max. Consider the outer loop iteration of ( 1) corresponding to j, X; note i < i’ < k’ 6 k in this iteration. After the inner
loop iteration corresponding to i, k, L( Yi, k, highk) b L( fl, i, j - 1, x - 1) 2 min. Since high, < max, then
after (2) halts, L( vi, k, max) 2 min.
(+) We have two cases. Case 1. Suppose L( Yi, k, max) is not increased by
procedure (2). Consider the last time L( vi, k, max)
is increased and let j, x be the corresponding values of the outer loop variables. Since L( Vi, k, max) must be increased by an iteration of the inner loop such that
high, = max, then L(@,i,j - 1,x - 1) > min and H( V,, j, k, x) = max. By induction, there is a match of kj into (ti,.. . , tj- t ) with smallest (respectively largest) element > min (respectively < x - 1) and a match of V, into (tj, . . . , tk) with smallest (respec- tively largest) element > x (respectively = max). Thus, there is a match of V into (ti, . . . , tk) with small- est element > min and largest element = max.
Case 2. Suppose L( Yi, k, mar) is increased by pro-
cedure (2). Then there exists x < max such that
L( Yi, k, x) > min when (1) halts and L( Yi, k, x) is not increased by (2). By Case 1, there is a match of V into (ti, . . . , tk) with smallest element > min and largest element = X. 0
Once all the L, H values are computed, there is a match of P into T iff L(root, 1, n, n) > 0 iff H(root, l,n, 1) < n+l. (Infact, L(root, l,n,n) and H( root, 1, n, 1) are the best lower and upper bounds
on the elements of a match of P into T, respectively.) In order to obtain a match, we modify the algorithm
so that each time an L value is changed, the current value of j, x is recorded. Since there are O(k) nodes in the tree and computing the L, H values requires O(n4) time for each node, we have the following.
Theorem 2. The given algorithm solves the pattern
matching problem for permutations in 0( kn4) time and 0( kn3) space.
References
[l] P Bose, J.F. Bluss and A. Lubiw, Pattern matching for permutations, in: Proc. Workshop on Algorithms and Data Structures, Lecture Notes in Computer Science, Vol. 709 (Springer, Berlin, 1993) 200-299.
[2] D.G. Comeil, H. Lerchs and L.S. Burlingham, Complement- reducible graphs, Discrere Appl. Marh. 3 (1981) 163-174.