# Parametrized Matching Amir Farach Muthukrishnan Orgad Keller Parametrized

- Slides: 14

Parametrized Matching Amir, Farach, Muthukrishnan Orgad Keller

Parametrized Match Relation n Definition: Two strings over the alphabet , parametrized match (p-match) if the following 3 conditions apply : Orgad Keller - Algorithms 2 - Recitation 9 2

Conditions n n n Orgad Keller - Algorithms 2 - Recitation 9 3

Example n n n We can see it as a bijection Orgad Keller - Algorithms 2 - Recitation 9 : 4

Parametrized Matching Input: n Output: All locations p-matches. n where Orgad Keller - Algorithms 2 - Recitation 9 5

Observation We can reduce the problem, to the same problem with (m-match). n Given we’ll define : n Orgad Keller - Algorithms 2 - Recitation 9 6

Observation Now is over and. n We get the algorithm for p-match: n is ¨ Create ¨ Find all the places appears in KMP) ¨ Find all the places m-matches in (We’ll show later how) ¨ Return Orgad Keller - Algorithms 2 - Recitation 9 7 (using

Exercise Why is that enough? n In other words: Prove there is a p-match at location iff. n We are left with the question: How do we solve step 3 efficiently? n Orgad Keller - Algorithms 2 - Recitation 9 8

M-match n n Is m-match transitive? We can use KMP-like automaton method For each index in pattern, we want to find the longest suffix that m-matches the prefix. For instance: Orgad Keller - Algorithms 2 - Recitation 9 9

Failure Links Where to link the failure link from ? n In KMP it is simple: If then link to Otherwise go back again and repeat. n In our case: n ¨ If never appeared before, i. e. We link if. ¨ Otherwise, we link if such that , it holds that. Orgad Keller - Algorithms 2 - Recitation 9 10 .

Failure Links Can we do this efficiently? n We’ll build an array n n : So, if , we know hasn’t appeared before. Otherwise, we’ll know exactly where it had appeared last. Orgad Keller - Algorithms 2 - Recitation 9 11

Building the Array n n n We’ll hold a Balanced Binary Search Tree for the symbols of the alphabet. Initially it will be empty. We’ll go over the pattern. For each symbol, if it isn’t in the tree, we’ll add it with it’s index and update. Otherwise, we know exactly where it had last appeared, so we’ll update and then update the symbol in the tree with the new index. Time: where. Orgad Keller - Algorithms 2 - Recitation 9 12

The Matching Itself n We go forward in the automaton if either ¨ and . ¨ n We’ll hold and update a balanced BST as we go over the text as well. ¨ Time: So overall algorithm time is n Can we improve this further? n Orgad Keller - Algorithms 2 - Recitation 9 13

The Trick n We’ll split the text into of size like this: overlapping segments ¨ So every match in the text must appear in whole in one of the segments. n n We’ll run the algorithm for each such segment. Time: where. Overall for all segments: Orgad Keller - Algorithms 2 - Recitation 9 14