CSE 311 Foundations of Computing I Lecture 27 FSM Limits, Pattern Matching Autumn 2012 CSE 311 1.

Slides:



Advertisements
Similar presentations
CSE 311 Foundations of Computing I
Advertisements

Complexity and Computability Theory I Lecture #4 Rina Zviel-Girshin Leah Epstein Winter
CSE 105 Theory of Computation Alexander Tsiatas Spring 2012 Theory of Computation Lecture Slides by Alexander Tsiatas is licensed under a Creative Commons.
Regular Expressions and DFAs COP 3402 (Summer 2014)
CSE 311: Foundations of Computing Fall 2013 Lecture 23: Finite state machines and minimization.
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012.
Introduction to Computability Theory
Finite Automata Great Theoretical Ideas In Computer Science Anupam Gupta Danny Sleator CS Fall 2010 Lecture 20Oct 28, 2010Carnegie Mellon University.
1 Introduction to Computability Theory Lecture2: Non Deterministic Finite Automata Prof. Amos Israeli.
Introduction to Computability Theory
1 Introduction to Computability Theory Discussion1: Non-Deterministic Finite Automatons Prof. Amos Israeli.
Courtesy Costas Busch - RPI1 Non Deterministic Automata.
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 15 Instructor: Paul Beame.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Nondeterminism.
CS5371 Theory of Computation Lecture 6: Automata Theory IV (Regular Expression = NFA = DFA)
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
CS5371 Theory of Computation Lecture 4: Automata Theory II (DFA = NFA, Regular Language)
1 Non-Deterministic Automata Regular Expressions.
1.Defs. a)Finite Automaton: A Finite Automaton ( FA ) has finite set of ‘states’ ( Q={q 0, q 1, q 2, ….. ) and its ‘control’ moves from state to state.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
CSE 311: Foundations of Computing Fall 2014 Lecture 23: State Minimization, NFAs.
Lexical Analysis — Part II: Constructing a Scanner from Regular Expressions Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
NFA ε - NFA - DFA equivalence. What is an NFA An NFA is an automaton that its states might have none, one or more outgoing arrows under a specific symbol.
CSE 311 Foundations of Computing I Lecture 30 Computability: Other Undecidable Problems Autumn 2012 CSE 3111.
Lexical Analysis — Part II: Constructing a Scanner from Regular Expressions.
1 Unit 1: Automata Theory and Formal Languages Readings 1, 2.2, 2.3.
CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Autumn 2011 CSE 3111.
CSE 311 Foundations of Computing I Lecture 21 Finite State Machines Spring
Lexical Analysis Constructing a Scanner from Regular Expressions.
Overview of Previous Lesson(s) Over View  An NFA accepts a string if the symbols of the string specify a path from the start to an accepting state.
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
CSCI 2670 Introduction to Theory of Computing August 26, 2004.
CSE 311 Foundations of Computing I Lecture 17 Structural Induction Spring
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.
Regular Expressions and Languages A regular expression is a notation to represent languages, i.e. a set of strings, where the set is either finite or contains.
CSE 311 Foundations of Computing I Lecture 29 Computability: Turing machines, Undecidability of the Halting Problem Autumn 2012 CSE 3111.
CS 3240 – Chapter 4.  Closure Properties  Algorithms for Elementary Questions:  Is a given word, w, in L?  Is L empty, finite or infinite?  Are L.
CSE 311 Foundations of Computing I Lecture 24 Finite State Machines Autumn 2012 CSE 3111.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
Brian Mitchell - Drexel University MCS680-FCS 1 Patterns, Automata & Regular Expressions int MSTWeight(int graph[][], int size)
Conversions & Pumping Lemma CPSC 388 Fall 2001 Ellen Walker Hiram College.
Exercise 1 Consider a language with the following tokens and token classes: ID ::= letter (letter|digit)* LT ::= " " shiftL ::= " >" dot ::= "." LP ::=
UNIT - I Formal Language and Regular Expressions: Languages Definition regular expressions Regular sets identity rules. Finite Automata: DFA NFA NFA with.
CSE 311 Foundations of Computing I Lecture 25 Pattern Matching, Cardinality, Computability Spring
CSE 311: Foundations of Computing Fall 2013 Lecture 26: Pattern matching, cardinality.
CSCI 3130: Formal languages and automata theory Tutorial 1 Lee Chin Ho.
using Deterministic Finite Automata & Nondeterministic Finite Automata
CS 154 Formal Languages and Computability February 9 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron.
CSE 311 Foundations of Computing I Lecture 24 FSM Limits, Pattern Matching Autumn 2011 CSE 3111.
Finite Automata Great Theoretical Ideas In Computer Science Victor Adamchik Danny Sleator CS Spring 2010 Lecture 20Mar 30, 2010Carnegie Mellon.
 2004 SDU Lecture4 Regular Expressions.  2004 SDU 2 Regular expressions A third way to view regular languages. Say that R is a regular expression if.
LECTURE 5 Scanning. SYNTAX ANALYSIS We know from our previous lectures that the process of verifying the syntax of the program is performed in two stages:
Regular Languages Chapter 1 Giorgi Japaridze Theory of Computability.
1 Lexical Analysis Uses formalism of Regular Languages Uses formalism of Regular Languages Regular Expressions Regular Expressions Deterministic Finite.
CSE 311 Foundations of Computing I Lecture 19 Recursive Definitions: Context-Free Grammars and Languages Autumn 2012 CSE
1 Section 11.2 Finite Automata Can a machine(i.e., algorithm) recognize a regular language? Yes! Deterministic Finite Automata A deterministic finite automaton.
CS412/413 Introduction to Compilers Radu Rugina Lecture 3: Finite Automata 25 Jan 02.
CSE 311 Foundations of Computing I Lecture 18 Recursive Definitions: Context-Free Grammars and Languages Autumn 2011 CSE 3111.
CSE 311 Foundations of Computing I Lecture 20 Context-Free Grammars and Languages Autumn 2012 CSE
 2005 SDU Lecture11 Decidability.  2005 SDU 2 Topics Discuss the power of algorithms to solve problems. Demonstrate that some problems can be solved.
CSE 311 Foundations of Computing I
Complexity and Computability Theory I
Chapter 2 FINITE AUTOMATA.
CSE 311 Foundations of Computing I
CSE 311: Foundations of Computing
CSE 2001: Introduction to Theory of Computation Fall 2009
CSE 311: Foundations of Computing
CSE 311 Foundations of Computing I
Announcements - P1 part 1 due Today - P1 part 2 due on Friday Feb 1st
Presentation transcript:

CSE 311 Foundations of Computing I Lecture 27 FSM Limits, Pattern Matching Autumn 2012 CSE 311 1

Announcements Reading assignments – 7 th Edition, Section 13.4 – 6 th Edition, Section 12.4 – 5 th Edition, Section 11.4 Next week – 7th edition: 2.5 (Cardinality) + p. 201 and 13.5 – 6th edition: pp (Cardinality)+ p 177 and 12.5 – 5th edition: Pages (Cardinality) and 11.5 Topic list and sample final exam problems have been posted Comprehensive final, roughly 67% of material post midterm Review session, Saturday, December 8, 10 am – noon (tentatively) Final exam, Monday, December 10 – 2:30-4:20 pm or 4:30-6:20 pm, Kane 220. – If you have a conflict, contact instructors ASAP Autumn 2012CSE 311 2

Last lecture highlights NFAs from Regular Expressions Autumn 2012CSE (01  1)*

Last lecture highlights “Subset construction”: NFA to DFA Autumn 2012CSE c a b 0 0,1 1 0 NFA a,b DFA 0 c 1 b b,c 1 0 a,b,c  1 0,

Converting an NFA to a regular expression Consider the DFA for the mod 3 sum – Accept strings from {0,1,2}* where the digits mod 3 sum of the digits is 0 Autumn 2012CSE 3115 t0t0 t2t2 t1t

Splicing out a node Label edges with regular expressions Autumn 2012CSE 3116 t0t0 t2t2 t1t t 0 →t 1 →t 0 : 10*2 t 0 →t 1 →t 2 : 10*1 t 2 →t 1 →t 0 : 20*2 t 2 →t 1 →t 2 : 20*1

Finite Automaton without t 1 Autumn 2012CSE 3117 t0t0 t2t2 R1R1 R 1 : 0 | 10*2 R 2 : 2 | 10*1 R 3 : 1 | 20*2 R 4 : 0 | 20*1 R 5 : R 1 | R 2 R 4 *R 3 R4R4 R2R2 R3R3 t0t0 R5R5 Final regular expression: (0 | 10*2 | (2 | 10*1)(0 | 20*1)*(1 |20*2))*

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 Autumn 2012CSE 311 8

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 Autumn 2012CSE λ λ λ A

Only two simplification rules: Rule 1: For any two states q 1 and q 2 with parallel edges (possibly q 1 =q 2 ), replace Rule 2: Eliminate non-start/final state q 3 by replacing all for every pair of states q 1, q 2 (even if q 1 =q 2 ) Autumn 2012CSE q1q1 q2q2 A B by A⋃BA⋃B q1q1 q2q2 A B CAB*C q1q1 q3q3 q2q2 q1q1 q2q2 by

Automata Theory Summary Every DFA is an NFA Every NFA can be converted into a DFA that accepts the same language – However there may be an exponential increase in the number of states Given a regular expression, we can construct an NFA that recognizes it Given an NFA we can construct an regular for the strings accepted by it Autumn 2012CSE 31111

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? – { 0 n 1 n : n≥0 }? – Binary Palindromes? – Strings of Balanced Parentheses? Autumn 2012CSE

A={0 n 1 n : 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 0 n and 0 m end at the same state p. Since 0 n 1 n is in A, following 1 n after state p must lead to a final state. But then the DFA would accept 0 m 1 n which is a contradiction Autumn 2012CSE

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 0 n and 0 m end at the same state p. Since 0 n 10 n is in B, following 10 n after state p must lead to a final state. But then the DFA would accept 0 m 10 n which is a contradiction Autumn 2012CSE

The set P of strings of balanced parentheses cannot be recognized by any DFA Autumn 2012CSE

The set P of strings {1 j | j = n 2 } cannot be recognized by any DFA Autumn 2012CSE Suppose 1 j and 1 k reach the same state p with j < k 1 k 1 k(k-1) must reach an accepting state q 1 j 1 k(k-1) must reach the same accepting state q Thus, j + k(k-1) = k 2 – k + j must be a perfect square Is that possible?

17 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 Autumn 2012CSE 311

18 Pattern p = x x x x x y String s = x x x x x x x x x y x x x x x x x x x x y x x Autumn 2012CSE 311

19 Pattern p = x y x y y x y x y x x 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 Autumn 2012CSE 311

20 x y x y y x y x y x x 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 Autumn 2012CSE 311

21 x y 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 y x y x y x x Autumn 2012CSE 311

22 x y 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 y x y y x y x y x x Autumn 2012CSE 311

23 x y 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 y x y x y y x y x y x x Autumn 2012CSE 311

24 x y 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 y x y x y y x y x y y x y x y x x Autumn 2012CSE 311

25 x y 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 y x y x y y x x y x y y x y x y x x Autumn 2012CSE 311

26 x y 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 y x y x y y x x y x y y x y x y x x Autumn 2012CSE 311

27 x y 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 y x y x y y x x y x y y x y x y x x x Autumn 2012CSE 311

28 x y 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 y x y x y y x x y x y y x y x y x x x x y x x y x y y x y x y x x Autumn 2012CSE 311

29 x y 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 y x y x y y x x y x y y x y x y x x x x y x x x y x y y x y x y x x Autumn 2012CSE 311

30 x y 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 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 x y x y x x Autumn 2012CSE 311

31 x y 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 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 x y x y y x y x y x x Autumn 2012CSE 311

32 x y 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 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 x x y x y y x y x y x x Worst-case time O(mn) Autumn 2012CSE 311

33 x y 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 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 x x y x y y x y x y x x Lots of wasted work Autumn 2012CSE 311

34 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 n steps Obvious construction method for DFA will be O(m 2 ) but can be done in O(m) time. Total O(m+n) time Autumn 2012CSE 311

35 Building a DFA for the pattern Pattern p=x y x y y x y x y x x y yy xxxxy xy x Autumn 2012CSE 311

36 Building a DFA for the pattern Pattern p=x y x y y x y x y x x y yy xxxxy xy x Autumn 2012CSE 311 y

37 Building a DFA for the pattern Pattern p=x y x y y x y x y x x y yy xxxxy xy x Autumn 2012CSE 311 y x y x x

38 Building a DFA for the pattern Pattern p=x y x y y x y x y x x y yy xxxxy xy x Autumn 2012CSE 311 y x y x y x y x x

39 Building a DFA for the pattern Pattern p=x y x y y x y x y x x y yy xxxxy xy x Autumn 2012CSE 311 y x y x y x y x y y y x x

40 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. Autumn 2012CSE 311