slides

A Fast Algorithm for Permutation Pattern Matching
Based on Alternating Runs
Marie-Louise Bruner
(Joint work with Martin Lackner)
Vienna University of Technology
June 13, 2012
Permutation Patterns, Glasgow
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
1
Permutation Pattern Matching
Permutation Pattern Matching (PPM)
Instance: A permutation T of length n (the
text) and a permutation P of length
k ≤ n (the pattern).
Question: Is there a matching of P into T ?
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
2
Permutation Pattern Matching
Permutation Pattern Matching (PPM)
Instance: A permutation T of length n (the
text) and a permutation P of length
k ≤ n (the pattern).
Question: Is there a matching of P into T ?
Is there an algorithm that runs faster than exponential time?
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
2
PPM is NP-complete
1993 (Bose, Buss, Lubiw): PPM is in general NP-complete.
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
3
PPM is NP-complete
1993 (Bose, Buss, Lubiw): PPM is in general NP-complete.
⇒ unless P = NP,
PPM cannot be solved in polynomial time
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
3
Tractable cases of PPM
I
Pattern avoids both 3142 and 2413
I
P = 12 . . . k or P = k . . . 21
I
P has length at most 4
I
Pattern and Text avoid 321
Marie-Louise Bruner
O(kn4 )
O(n log log n)
O(n log n)
A Fast Algorithm for PPM
O(k 2 n6 )
June 13, 2012
4
The general case
Anything better than the
O∗ (2n )
runtime of brute-force search?
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
5
Parameterized Complexity Theory
Idea: confine the combinatorial explosion to a parameter of the input
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
6
Parameterized Complexity Theory
Idea: confine the combinatorial explosion to a parameter of the input
Parameterized Problem: L ⊆ Σ∗ × N
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
6
Parameterized Complexity Theory
Idea: confine the combinatorial explosion to a parameter of the input
Parameterized Problem: L ⊆ Σ∗ × N
Fixed-parameter tractability
A parameterized problem L is fixed-parameter tractable if there is a
computable function f and an integer c such that there is an algorithm
solving L in time O(f (p) · |I |c ).
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
6
Alternating runs
12
11
10
9
8
7
6
5
4
3
2
1
1 8 12 (up), 4 (down), 7 11 (up), 6 3 2 (down), 9 (up), 5 (down), 10 (up)
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
7
Alternating runs
12
11
10
9
8
7
6
5
4
3
2
1
1 8 12 (up), 4 (down), 7 11 (up), 6 3 2 (down), 9 (up), 5 (down), 10 (up)
Notation
run(π)...the number of alternating runs in π,
r (i) = j if i lies in the j-th run
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
7
The alternating run algorithm
I
Matching functions:
Reduce the search space
I
Dynamic programming algorithm:
Checks for every matching function whether there is a compatible
matching
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
8
Matching functions
Pattern P
↓ matching function ↓
Text T
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
9
Matching functions - an example
Pattern P
Text T
12
11
10
9
4
8
7
3
6
5
2
4
3
1
2
1
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
10
Matching functions - an example
Pattern P
Text T
12
11
10
9
4
8
7
3
6
5
2
4
3
1
2
1
F (2)
F (1)
Marie-Louise Bruner
A Fast Algorithm for PPM
F (3)
June 13, 2012
10
The algorithm - finding a matching
The dynamic programming algorithm - Part 1
X0 := {(0, . . . , 0)}. For every κ ∈ [k] the data structure Xκ is recursively
constructed. For x ∈ Xκ−1 we search for ν ∈ [n] satisfying:
I
ν ∈ F (r (κ))
I
Among all elements that are larger than xr (κ−1) and on the correct
side of xr (κ) , ν has to be a valley.
I
If κ is not the largest element in its run in P, there has to be another
larger element in F (r (κ)) lying to the right (resp. left) of ν if κ lies in
a run up (resp. down).
Place xν := (x1 , . . . , xr (κ)−1 , ν, xr (κ)+1 , . . . , xrun(P) ) in Xκ0 .
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
11
The algorithm - finding a matching
The dynamic programming algorithm - Part 1
Xκ0 still consists of too many elements. In order to obtain the desired
runtime bounds, we apply the following rules:
I
We call a vale a subsequence of T consisting of a consecutive run
down and up. If the entries of xν and yµ all lie within the same vales,
it is sufficient to keep one of the two.
I
If κ is the largest element in its run in P, it is enough to keep one
choice for every x in Xκ−1 .
The remaining xν ’s then form Xκ .
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
12
The algorithm - finding a matching
Pattern P
Text T
12
11
10
9
4
8
7
3
6
5
2
4
3
1
2
1
F (2)
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
13
The algorithm - finding a matching
Pattern P
Text T
12
11
10
9
4
8
7
3
6
5
2
4
3
1
2
1
F (2)
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
13
The algorithm - finding a matching
Pattern P
Text T
12
11
10
9
4
8
7
3
6
5
2
4
3
1
2
1
F (1)
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
13
The algorithm - finding a matching
Pattern P
Text T
12
11
10
9
4
8
7
3
6
5
2
4
3
1
2
1
F (1)
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
13
The algorithm - finding a matching
Pattern P
Text T
12
11
10
9
4
8
7
3
6
5
2
4
3
1
2
1
F (1)
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
13
The algorithm - finding a matching
Pattern P
Text T
12
11
10
9
4
8
7
3
6
5
2
4
3
1
2
1
F (1)
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
13
The algorithm - finding a matching
Pattern P
Text T
12
11
10
9
4
8
7
3
6
5
2
4
3
1
2
1
F (1)
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
13
The algorithm - finding a matching
Pattern P
Text T
12
11
10
9
4
8
7
3
6
5
2
4
3
1
2
1
F (3)
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
13
The algorithm - finding a matching
Pattern P
Text T
12
11
10
9
4
8
7
3
6
5
2
4
3
1
2
1
F (3)
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
13
The algorithm - finding a matching
Pattern P
Text T
12
11
10
9
4
8
7
3
6
5
2
4
3
1
2
1
F (3)
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
13
Runtime
√
Matching functions:
Dynamic programming algorithm:
Marie-Louise Bruner
A Fast Algorithm for PPM
run(T )
2
O∗ (1.2611run(T ) )
June 13, 2012
14
Runtime
√
Matching functions:
run(T )
2
Dynamic programming algorithm:
O∗ (1.2611run(T ) )
In total:
O∗ (1.784run(T ) )
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
14
Runtime
√
Matching functions:
run(T )
2
Dynamic programming algorithm:
O∗ (1.2611run(T ) )
In total:
O∗ (1.784run(T ) )
→ This is a fixed-parameter tractable (FPT) algorithm,
i.e. a runtime of f (k) · nc .
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
14
Runtime
√
Matching functions:
run(T )
2
Dynamic programming algorithm:
O∗ (1.2611run(T ) )
In total:
O∗ (1.784run(T ) )
→ This is a fixed-parameter tractable (FPT) algorithm,
i.e. a runtime of f (k) · nc .
Since run(T ) ≤ n, we also obtain
Marie-Louise Bruner
A Fast Algorithm for PPM
O∗ (1.784n )
June 13, 2012
14
Alternating runs in the pattern run(P)
O∗ (1.784run(T ) )
O∗
run(P) n2
2run(P)
Marie-Louise Bruner
FPT, i.e. f (k) · nc
XP, i.e. nf (k)
A Fast Algorithm for PPM
June 13, 2012
15
Alternating runs in the pattern run(P)
O∗ (1.784run(T ) )
O∗
run(P) n2
2run(P)
FPT, i.e. f (k) · nc
XP, i.e. nf (k)
no FPT result possible (W[1]-hardness)
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
15
Conclusion
What have we done?
We found
I
a fast algorithm for the parameter run(T ),
I
a slow algorithm for the parameter run(P).
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
16
Conclusion
What have we done?
We found
I
a fast algorithm for the parameter run(T ),
I
a slow algorithm for the parameter run(P).
Future work
I
PPM parameterized by some other parameter of P? By k = run(P)?
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
16
Conclusion
What have we done?
We found
I
a fast algorithm for the parameter run(T ),
I
a slow algorithm for the parameter run(P).
Future work
I
PPM parameterized by some other parameter of P? By k = run(P)?
I
Other permutation statistics in the text?
Marie-Louise Bruner
A Fast Algorithm for PPM
June 13, 2012
16