Lecture11_StringMatching

Lecture11_StringMatching - String algorithms 6.046...

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

View Full Document Right Arrow Icon
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?
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online