1 / 35

CSE 311 Foundations of Computing I

CSE 311 Foundations of Computing I. Lecture 24 FSM Limits, Pattern Matching Autumn 2011. Announcements. Reading assignments 7 th Edition, Section 13.4 6 th Edition, Section 12.4 5 th Edition, Section 11.4. Last lecture highlights. NFAs from Regular Expressions. (01 1 )*0. 0.

kylar
Download Presentation

CSE 311 Foundations of Computing I

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 311 Foundations of Computing I Lecture 24 FSM Limits, Pattern Matching Autumn 2011 CSE 311

  2. Announcements • Reading assignments • 7thEdition, Section 13.4 • 6th Edition, Section 12.4 • 5th Edition, Section 11.4 CSE 311

  3. Last lecture highlights • NFAs from Regular Expressions (01 1)*0 0  1     0     1 CSE 311

  4. Last lecture highlights 0,1 0 • “Subset construction”: NFA to DFA  a,b a 1 0 1  1 1 b c 0 1 b c 0 0 0 0,1 1 b,c a,b,c 0 NFA DFA CSE 311

  5. What can Finite State Machines do? • We’ve seen how we can get DFAs to recognize all regular languages • What about some other languages we can generate with CFGs? • { 0n1n : n≥0 }? • Binary Palindromes? • Strings of Balanced Parentheses? CSE 311

  6. A={0n1n: n≥0} cannot be recognized by any DFA Consider the infinite set of strings S={, 0, 00, 000, 0000, ...} Claim: No two strings in S can end at the same state of any DFA for A, so no such DFA can exist Proof: Suppose nm and 0n and 0m end at the same state p. Since 0n1n is in A, following 1nafter state p must lead to a final state. But then the DFA would accept 0m1n which is a contradiction CSE 311

  7. The set B of binary palindromes cannot be recognized by any DFA Consider the infinite set of strings S={, 0, 00, 000, 0000, ...} Claim: No two strings in S can end at the same state of any DFA for B, so no such DFA can exist Proof: Suppose nm and 0n and 0m end at the same state p. Since 0n10n is in B, following 10n after state p must lead to a final state. But then the DFA would accept 0m10n which is a contradiction CSE 311

  8. The set P of strings of balanced parentheses cannot be recognized by any DFA CSE 311

  9. 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 • stop at a failed match and try the next position

  10. 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

  11. 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 xy y x y x y x x

  12. 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

  13. 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 y x y y x y x y x x

  14. 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 yyx y x y x x

  15. 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 xy x y y x y x y x x

  16. String s = x y x x yx y x y y x y x y xy 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 xx

  17. 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

  18. 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 xy y x y x y x x

  19. 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

  20. 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

  21. String s = x y x x y x y x y yx y x yxy 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

  22. 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

  23. 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

  24. 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 xy x Lots of wasted work x y xy x yy x x yx y y x y x y xx x x y x x x x y x y y x x y x y y x y x y x x

  25. Better Pattern Matching via Finite Automata • Build a DFA for the pattern (preprocessing) of size O(m) • Keep track of the ‘longest match currently active’ • The DFA will have only m+1 states • Run the DFA on the string nsteps • Obvious construction method for DFA will be O(m2) but can be done in O(m) time. • Total O(m+n) time

  26. x y y x x x x x y y y Building a DFA for the pattern Patternp=x y x y y x y x y x x

  27. 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

  28. 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

  29. 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

  30. x y 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 y x x x y y y y y

  31. Generalizing • Can search for arbitrary combinations of patterns • Not just a single pattern • Build NFA for pattern then convert to DFA ‘on the fly’. • Compare DFA constructed above with subset construction for the obvious NFA.

  32. A Quick Note... • On how to convert NFAs and DFAs to equivalent regular expressions... • We’ve already seen • DFAs and NFAs recognize the same languages • NFAs (and therefore DFAs) recognize any language given by a regular expression • This completes the equivalence of DFAs and regular expressions CSE 311

  33. Generalized NFAs • Like NFAs but allow • Parallel edges • Regular Expressions as edge labels • NFAs already have edges labeled  or a • An edge labeled by A can be followed by reading a string of input chars that is in the language represented by A • A string x is accepted iff there is a path from start to final state labeled by a regular expression whose language contains x CSE 311

  34. Starting from NFA • Add new start state and final state • Then eliminate original states one by one, keeping the same language, until it looks like: • Final regular expression will be A λ A λ λ CSE 311

  35. Only two simplification rules: • Rule 1: For any two states q1 and q2 with parallel edges (possibly q1=q2), replace • Rule 2: Eliminate non-start/final state q3 by replacing all for every pair of states q1, q2 (even if q1=q2) A A⋃B q2 by q2 q1 q1 q2 q1 B B A C AB*C q2 q3 q1 by CSE 311

More Related