prev

next

out of 3

Published on

02-Jul-2016View

216Download

1

Transcript

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: libarra@gulf.uvic.ca.

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 Ps 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 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. Ibarra/lnformation Processing Letters 61 (1997) 293-295 295

L values for the root V (after the L, H values for Vs 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.