1 / 31

CSE 417: Algorithms and Computational Complexity

CSE 417: Algorithms and Computational Complexity. Winter 2001 Lecture 15 Instructor: Paul Beame. Pattern Matching. Given a string, s , of n characters a pattern, p , of m characters usually m<<n Find all occurrences of the pattern p in the string s Obvious algorithm:

tsheppard
Télécharger la présentation

CSE 417: Algorithms and Computational Complexity

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 15 Instructor: Paul Beame

  2. Pattern Matching • Given • a string, s, of n characters • a pattern, p, of m characters • usually m<<n • Find • all occurrences of the pattern p in the string s • Obvious algorithm: • try to see if p matches at each of the positions in s, stopping at a failed match

  3. String s = x y x x y x y x y y x y x y x y y x y x y x x Pattern p = x y x y y x y x y x x

  4. String s = x y xxy x y x y y x y x y x y y x y x y x x x y xyy x y x y x x

  5. String s = xyx x y x y x y y x y x y x y y x y x y x x x y x y xy x y y x y x y x x

  6. String s = x yxxy x y x y y x y x y x y y x y x y x x x y x y x x yx y y x y x y x x

  7. String s = x y xx y x yxy y x y x y x y y x y x y x x x y x y x x y x y x yy x y x y x x

  8. String s = x y x xyx y x y y x y x y x y y x y x y x x x y x y x x y x y x y y x y x y y x y x y x x

  9. String s = x y x x yx y x y y x y x y xyy x y x y x x x y x y x x y x y x y y x x y x y y x y x y xx

  10. String s = x y x x y xyx y y x y x y x y y x y x y x x x y x y x x y x y x y y x x y x y y x y x y x x xy x y y x y x y x x

  11. String s = x y x x y x yx yyx y x y x y y x y x y x x x y x y x x y x y x y y x x y x y y x y x y x x x x y x y y x y x y x x

  12. String s = x y x x y x y xyy x y x y x y y x y x y x x x y x y x x y x y x y y x x y x y y x y x y x x x x y x xy x y y x y x y x x

  13. String s = x y x x y x y x yyx y x y x y y x y x y x x x y x y x x y x y x y y x x y x y y x y x y x x x x y x x xy x y y x y x y x x

  14. String s = x y x x y x y x y y x y x yx y y x y x y x x x y x y x x y x y x y y x x y x y y x y x y x x x x y x x x x y x yyx y x y x x

  15. String s = x y x x y x y x y y xyx y x y y x y x y x x x y x y x x y x y x y y x x y x y y x y x y x x x x y x x x x y x y y xy x y y x y x y x x

  16. String s = x y x x y x y x y y x yx y x y y x y x y x x x y x y x x y x y x y y x x y x y y x y x y x x x Worst-case time O(mn) x y x x x x y x y y x x y x y y x y x y x x

  17. String s = x y x x y x y x y y x yx y x y y x y x y x x x y x y x Lots of wasted work x y x y x y y x x yx y y x y x y x x Suppose we knew the pattern well x x y x x x x y x y y Since we know earlier agreement of the string with the pattern, these can’t possibly match x x y xy y x y x y x x

  18. Preprocess the Pattern • After each character in the pattern figure out ahead of time what the next useful work would be if it failed to match there • i.e. how much can one shift over the pattern for the next match

  19. String s = x y x x y x y x y y x y x y x y y x y x y x x x y x y x y x y y x yxy y x y x y x x x y x yy x y x y x x

  20. Preprocessing the pattern • At each mismatch • Look at the last part that matched plus extra mismatched character • Try to fit pattern as far to the left in this as possible • i.e. look for the longest prefix of the pattern that matches the end of the sequence so far.

  21. x y y x x x x x y y y Preprocessing the pattern Patternp=x y x y y x y x y x x Each dot represents how far in the pattern things are matched

  22. x y y x x x x x y y y Preprocessing the pattern Patternp=x y x y y x y x y x x y

  23. x y y x x x x x y y y Preprocessing the pattern Patternp=x y x y y x y x y x x x y x x y

  24. x y y x x x x x y y y Preprocessing the pattern Patternp=x y x y y x y x y x x x x x y x x y y y

  25. x y y x x x x x y y y Preprocessing the pattern Patternp=x y x y y x y x y x x x x x y x x y y y y y

  26. x y y x x x x x y y y Running on the string String s = x y x x y x y x y y x y x y x y y x y x y x x x x x y x x y y y y y

  27. Knuth-Morris-Pratt Algorithm • Once the preprocessing is done there are only n steps on any string of size n • just follow your nose • Obvious algorithm for doing preprocessing is O(m2) steps • still usually good since m<<n • Knuth-Morris-Pratt Algorithm can do the pre-processing in O(m) steps • Total O(m+n) time

  28. Finite State Machines • The diagram we built is a special case of a finite automaton • start state • goal or accepting state(s) • an arc out of each state labeled by each of the possible characters • Finite automata take strings of characters as input and decide what to do with them based on the paths they follow

  29. Finite State Machines • Many communication protocols, cache-coherency protocols, VLSI circuits, user-interfaces, even adventure games are designed by making finite state machines first. • The “strings” that are the input to the machines can be • a sequence of actions of the user • the bits that arrive on particular ports of the chip • a series of values on a bus

  30. Finite State Machines • Can search for arbitrary combinations of patterns not just a single pattern • Given two finite automata can build a single new one that accepts strings that either of the original ones accepted • Typical text searches are based on finite automata designs • Perl builds this in as a first-class component of the programming language.

  31. Next time • We start the computability and complexity portion of the course. • We discuss Turing machines which are similar in style to finite-state machines but much more powerful • as powerful as any programming language

More Related