Lecture11_StringMatching

# Lecture11_StringMatching - String algorithms 6.046...

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

String algorithms I & II Prof. Manolis Kellis 6.046 – Introduction to Algorithms – Spring ’05 Lecture 11 String algorithms • Today: String matching I – The exact string matching problem – Naïve algorithm – Preprocessing the query: • Fundamental pre-processing • Knuth-Morris-Pratt algorithm • Boyer-Moore algorithm • Z algorithm algorithm – Semi-numerical string matching • Rabin-Karp algorithm • Wednesday: String matching II – Finite state machines – Suffix-trees – Inexact matching The exact matching problem • Inputs: –a s t r ing P , called the pattern – a longer string T , called the text • Output: – Find all occurrences, if any, of pattern P in text T •E x am p l e baabacabab ad T= 1234567891 01 11 2 aba P= Basic string definitions •A string S – Ordered list of characters – Written contiguously from left to write substring S[i. .j] – all contiguous characters from i to j – Example: S[3. .7] = abaxa prefix is a substring starting at 1 suffix is a substring ending at |S| • |S| denotes the number of characters in string S S= 2 The naïve string-matching algorithm • NAÏVE STRING MATCHING –n Å length[T] –m Å length[P] for shift Å 0 to n do if P[1. .m] == T[shift+1 . . shift+m] then print “Pattern occurs with shift” shift • Where the test operation in line 4: – Tests each position in turn • If match, continue testing • else: stop • Running time ~ number of comparisons number of shifts (with one comparison each) + number of successful character comparisons 1 2 3 4 5 O(n) O(m) Running time: Comparisons made with naïve algorithm • Worst case running time: – Test every position – P=aaaa, T=aaaaaaaaaaa • Best case running time: – Test only first position – P=bbbb, T=aaaaaaaaaaa baabacab s=0 s=1 s=2 s=3 s=4 s=5 Can we do better?

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.

## This note was uploaded on 01/20/2012 for the course CS 6.006 taught by Professor Erikdemaine during the Spring '08 term at MIT.

### Page1 / 5

Lecture11_StringMatching - String algorithms 6.046...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online