{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture_22

# lecture_22 - Introduction to Algorithms 6.046J/18.401J...

This preview shows pages 1–8. Sign up to view the full content.

Introduction to Algorithms 6.046J/18.401J Lecture 22 Prof. Piotr Indyk

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Today String matching problems HKN Evaluations (last 15 minutes) Graded Quiz 2 (outside) © Piotr Indyk Introduction to Algorithms December 1, 2004 L22.2
String Matching Input: Two strings T[1…n] and P[1…m] , containing symbols from alphabet Σ . E.g. : Σ ={a,b,…,z} – T[1…18]= “to be or not to be” – P[1..2]= “be” Goal: find all “shifts” 0 s n-m such that T[s+1…s+m]=P E.g. 3, 16 © Piotr Indyk Introduction to Algorithms December 1, 2004 L22.3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Simple Algorithm for s 0 to n-m Match 1 for j 1 to m if T[ s+j ] P[ j ] then Match 0 exit loop if Match= 1 then output s © Piotr Indyk Introduction to Algorithms December 1, 2004 L22.4
Results Running time of the simple algorithm: Worst-case: O(nm) – Average-case (random text): O(n) • T s = time spent on checking shift s • E[T s ] 2 E [ s T s ] = s E[T s ] = O(n) © Piotr Indyk Introduction to Algorithms December 1, 2004 L22.5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Worst-case Is it possible to achieve O(n) for any input ? – Knuth-Morris-Pratt’77: deterministic – Karp-Rabin’81: randomized © Piotr Indyk Introduction to Algorithms December 1, 2004 L22.6
Karp-Rabin Algorithm A very elegant use of an idea that we have encountered before, namely… HASHING ! Idea: Hash all substrings T[1…m], T[2…m+1], …, T[m-n+1…n] Hash the pattern P[1…m] Report the substrings that hash to the same value as P Problem: how to hash n-m substrings, each of length m , in O(n) time ?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}