Parametrized Matching Amir, Farach, Muthukrishnan Orgad Keller.

Presentation on theme: "Parametrized Matching Amir, Farach, Muthukrishnan Orgad Keller."— Presentation transcript:

Parametrized Matching Amir, Farach, Muthukrishnan Orgad Keller

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

Orgad Keller - Algorithms 2 - Recitation 9 3 Conditions

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

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

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

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

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

Orgad Keller - Algorithms 2 - Recitation 9 9 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: M-match

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

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

Orgad Keller - Algorithms 2 - Recitation 9 12 Building the Array 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 13 The Matching Itself We go forward in the automaton if either  and.  We’ll hold and update a balanced BST as we go over the text as well.  Time: So overall algorithm time is Can we improve this further?

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