Nondeterministic Finite State Machines Chapter 5.

Slides:



Advertisements
Similar presentations
Context-Free and Noncontext-Free Languages
Advertisements

CSE 311 Foundations of Computing I
CMPS 3223 Theory of Computation
Complexity and Computability Theory I Lecture #4 Rina Zviel-Girshin Leah Epstein Winter
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 2 Mälardalen University 2005.
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
Pushdown Automata Chapter 12. Recognizing Context-Free Languages We need a device similar to an FSM except that it needs more power. The insight: Precisely.
Finite Automata Great Theoretical Ideas In Computer Science Anupam Gupta Danny Sleator CS Fall 2010 Lecture 20Oct 28, 2010Carnegie Mellon University.
61 Nondeterminism and Nodeterministic Automata. 62 The computational machine models that we learned in the class are deterministic in the sense that the.
Conformance Simulation Relation ( ) Let and be two automata over the same alphabet simulates () if there exists a simulation relation such that Note that.
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.
Lecture 18 NFA’s with -transitions –NFA- ’s Formal definition Simplifies construction –LNFA- –Showing LNFA  is a subset of LNFA and therefore a subset.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Nondeterminism.
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.
MA/CSSE 474 Theory of Computation
CS Chapter 2. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Formal Language Finite set of alphabets Σ: e.g., {0, 1}, {a, b, c}, { ‘{‘, ‘}’ } Language L is a subset of strings on Σ, e.g., {00, 110, 01} a finite language,
Finite State Machines Chapter 5. Languages and Machines.
CMPS 3223 Theory of Computation
Deterministic Finite State Machines Chapter 5. Languages and Machines 2.
Lecture 23: Finite State Machines with no Outputs Acceptors & Recognizers.
1 Unit 1: Automata Theory and Formal Languages Readings 1, 2.2, 2.3.
Pushdown Automata (PDA) Intro
MA/CSSE 474 Theory of Computation DFSM Canonical Form Proof of NDFSM  DFSM ALGORITHM (as much as we have time for) This version includes the "answers"
1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a compiler PART II:
Pushdown Automata Part I: PDAs Chapter Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)
Computability Construct TMs. Decidability. Preview: next class: diagonalization and Halting theorem.
MA/CSSE 474 Theory of Computation Decision Problems DFSMs.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.
CS 321 Programming Languages and Compilers Lectures 16 & 17 Introduction to Formal Languages Regular Languages Lexical Analysis.
Regular Expressions Chapter 6 1. Regular Languages Regular Language Regular Expression Finite State Machine L Accepts 2.
Copyright © Curt Hill Finite State Automata Again This Time No Output.
Context-Free and Noncontext-Free Languages Chapter 13 1.
CMSC 330: Organization of Programming Languages Finite Automata NFAs  DFAs.
MA/CSSE 474 Theory of Computation Minimizing DFSMs.
Modeling Computation: Finite State Machines without Output
UNIT - I Formal Language and Regular Expressions: Languages Definition regular expressions Regular sets identity rules. Finite Automata: DFA NFA NFA with.
Costas Busch - LSU1 Turing’s Thesis. Costas Busch - LSU2 Turing’s thesis (1930): Any computation carried out by mechanical means can be performed by a.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2007.
Lecture Notes 
Nondeterministic Finite Automata (NFAs). Reminder: Deterministic Finite Automata (DFA) q For every state q in Q and every character  in , one and only.
Chapter 5 Finite Automata Finite State Automata n Capable of recognizing numerous symbol patterns, the class of regular languages n Suitable for.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen.
Finite State Machines Chapter 5. Languages and Machines.
Complexity and Computability Theory I Lecture #5 Rina Zviel-Girshin Leah Epstein Winter
Algorithms and Decision Procedures for Regular Languages Chapter 9.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2006.
Finite Automata A simple model of computation. 2 Finite Automata2 Outline Deterministic finite automata (DFA) –How a DFA works.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
CS412/413 Introduction to Compilers Radu Rugina Lecture 3: Finite Automata 25 Jan 02.
MA/CSSE 474 Theory of Computation Decision Problems, Continued DFSMs.
MA/CSSE 474 Theory of Computation Regular Expressions Intro.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Turing’s Thesis Costas Busch - LSU.
Non Deterministic Automata
Properties of Regular Languages
Chapter 2 Finite Automata
Hierarchy of languages
Turing Machines Chapter 17.
Non-Deterministic Finite Automata
Non Deterministic Automata
Finite Automata Reading: Chapter 2.
MA/CSSE 474 Theory of Computation
Instructor: Aaron Roth
MA/CSSE 474 Theory of Computation Nondeterminism NFSMs.
MA/CSSE 474 Theory of Computation
Chapter 1 Regular Language
Minimize # states in a DFSM
Presentation transcript:

Nondeterministic Finite State Machines Chapter 5

Imagine adding to a programming language the function choice in either of the following forms: 1. choose (action 1;; action 2;; … action n ) 2. choose(x from S: P(x)) Nondeterminism

before the first choice choose makes first call to choosefirst call to choose choice 1 choice 2 second call to choosesecond call to choose choice 1 choice 2 Implementing Nondeterminism

Nondeterminism What it means/implies –We could guess and our guesses would lead us to the answer correctly (if there is an answer). 4

Definition of an NDFSM M = (K, , , s, A), where: K is a finite set of states  is an alphabet s  K is the initial state A  K is the set of accepting states, and  is the transition relation. It is a finite subset of (K  (   {  }))  K state input or empty string state 5  Cartesian product Where you are Where you go What you see

Accepting by an NDFSM M accepts a string w iff there exists some path along which w drives M to some element of A. The language accepted by M, denoted L(M), is the set of all strings accepted by M. 6

Sources of Nondeterminism 7 What differ from determinism?

Two approaches: Explore a search tree: Follow all paths in parallel Analyzing Nondeterministic FSMs 8

Optional Substrings L = {w  { a, b }* : w is made up of an optional a followed by aa followed by zero or more b ’s}. 9

Optional Substrings L = {w  { a, b }* : w is made up of an optional a followed by aa followed by zero or more b ’s}. 10

Optional Substrings L = {w  { a, b }* : w is made up of an optional a followed by aa followed by zero or more b ’s}. 11 M = (K, , , s, A) = ({q 0, q 1, q 2, q 3 }, {a, b}, , q 0, {q 3 }), where  = {((q 0, a), q 1 ), ((q 0,  ), q 1 ), ((q 1, a), q 2 ), ((q 2, a), q 3 ), ((q 3, b), q 3 )} How many elements does  have?

Multiple Sublanguages L = {w  { a, b }* : w = aba or |w| is even}. 12

Multiple Sublanguages L = {w  { a, b }* : w = aba or |w| is even}. 13

Multiple Sublanguages L = {w  { a, b }* : w = aba or |w| is even}. 14

Multiple Sublanguages L = {w  { a, b }* : w = aba or |w| is even}. 15 M = (K, , , s, A) = ({q 0, q 1, q 2, q 3, q 4, q 5, q 6 }, {a, b}, , q 0, {q 4, q 5 }), where  = {((q 0,  ), q 1 ), ((q 0,  ), q 5 ), ((q 1, a), q 2 ), ((q 2, b), q 3 ), ((q 3, a), q 4 ), ((q 5, a), q 6 ), ((q 5, b), q 6 ), ((q 6, a), q 5 ), ((q 6, b), q 5 )} Do you start to feel the power of nondeterminism?

The Missing Letter Language Let  = { a, b, c, d }. Let L Missing = {w : there is a symbol a i   not appearing in w} 16 Recall the complexity of DFSM!

The Missing Letter Language 17

Pattern Matching L = {w  { a, b, c }* :  x, y  { a, b, c }* (w = x abcabb y)}. A DFSM: 18

Pattern Matching L = {w  { a, b, c }* :  x, y  { a, b, c }* (w = x abcabb y)}. A DFSM: An NDFSM: 19

Pattern Matching with NDFSMs L = {w  { a, b }* :  x, y  { a, b }* : w = x aabbb y or w = x abbab y } 20

Multiple Keywords L = {w  { a, b }* :  x, y  {a, b }* ((w = x abbaa y)  (w = x baba y))}. 21

Checking from the End L = {w  { a, b }* : the fourth to the last character is a } 22

Checking from the End L = {w  { a, b }* : the fourth to the last character is a } 23

Another Pattern Matching Example L = {w  { 0, 1 }* : w is the binary encoding of a positive integer that is divisible by 16 or is odd} 24

Another NDFSM L 1 = {w  { a, b }*: aa occurs in w} L 2 = {x  { a, b }*: bb occurs in x} L 3 = {y :  L 1 or L 2 } L 4 = L 1 L 2 M 1 = M 2 = M 3 = M 4 = 25

A “Real” Example 26

Analyzing Nondeterministic FSMs Does this FSM accept: baaba Remember: we just have to find one accepting path. 27

Two approaches: Explore a search tree: Follow all paths in parallel Analyzing Nondeterministic FSMs 28

Dealing with  Transitions eps(q) = {p  K : (q, w) |-* M (p, w)}. eps(q) is the closure of {q} under the relation {(p, r) : there is a transition (p, , r)   }. How shall we compute eps(q)? It simply means the states reachable without consuming input. 29

An Algorithm to Compute eps(q) result = {q}. While there exists some p  result and some r  result and some transition (p, , r)   do: Insert r into result. Return result. eps(q: state) = 30

An Example of eps eps(q 0 ) = eps(q 1 ) = eps(q 2 ) = eps(q 3 ) = 31

Simulating a NDFSM ndfsmsimulate(M: NDFSM, w: string) = 1.current-state = eps(s). 2. While any input symbols in w remain to be read do: 1.c = get-next-symbol(w). 2.next-state = . 3.For each state q in current-state do: For each state p such that (q, c, p)   do: next-state = next-state  eps(p). 4.current-state = next-state. 3.If current-state contains any states in A, accept. Else reject. 32

Nondeterministic and Deterministic FSMs Clearly: {Languages accepted by a DFSM}  {Languages accepted by a NDFSM} More interestingly: Theorem: For each NDFSM, there is an equivalent DFSM. 33

Nondeterministic and Deterministic FSMs Theorem: For each NDFSM, there is an equivalent DFSM. Proof: By construction: Given a NDFSM M = (K, , , s, A), we construct M' = (K', ,  ', s', A'), where K' = P (K) s' = eps(s) A' = {Q  K : Q  A   }  '(Q, a) =  {eps(p): p  K and (q, a, p)   for some q  Q} 34 May create many unreachable states

An Algorithm for Constructing the Deterministic FSM 1. Compute the eps(q)’s. 2. Compute s' = eps(s). 3. Compute  ‘. 4. Compute K' = a subset of P (K). 5. Compute A' = {Q  K' : Q  A   }. 35 The algorithm 1.Proves NDFSM  DFSM 2.Allows us to solve problems using NDFSM then construct equivalent DFSM

The Algorithm ndfsmtodfsm ndfsmtodfsm(M: NDFSM) = 1. For each state q in K M do: 1.1 Compute eps(q). 2. s' = eps(s) 3. Compute  ': 3.1 active-states = {s'}. 3.2  ' = . 3.3 While there exists some element Q of active-states for which  ' has not yet been computed do: For each character c in  M do: new-state = . For each state q in Q do: For each state p such that (q, c, p)   do: new-state = new-state  eps(p). Add the transition (Q, c, new-state) to  '. If new-state  active-states then insert it. 4. K' = active-states. 5. A' = {Q  K' : Q  A   }. 36

An Example – Optional Substrings L = {w  { a, b }* : w is made up of 0 to 2 a ’s followed by zero or more b ’s}. 37  b

Another Example 38

The Number of States May Grow Exponentially No. of states after 0 chars: = 1 No. of new states after 1 char: = n No. of new states after 2 chars: = n(n-1)/2 No. of new states after 3 chars: = n(n-1)(n-2)/6 Total number of states after n chars: 2 n |  | = n 39

Another Hard Example L = {w  { a, b }* : the fourth to the last character is a } 40

If the Original FSM is Deterministic 1. Compute the eps(q)s: 2. s' = eps(q0) = 3. Compute  ' ({q0}, odd, {q1})({q0}, even, {q0}) ({q1}, odd, {q1})({q1}, even, {q0}) 4. K' = {{q0}, {q1}} 5. A' = { {q1} } M' = M M 41

The Real Meaning of “Determinism” Is M deterministic? An FSM is deterministic, in the most general definition of determinism, if, for each input and state, there is at most one possible transition. DFSMs are always deterministic. Why? NDFSMs can be deterministic (even with  -transitions and implicit dead states), but the formalism allows nondeterminism, in general. Determinism implies uniquely defined machine behavior. Let M = 42

Deterministic FSMs as Algorithms L = {w  { a, b }* : w contains no more than one b }: 43

Deterministic FSMs as Algorithms until accept or reject do: S:s = get-next-symbol if s = end-of-file then accept else if s = a then go to S else if s = b then go to T T: s = get-next-symbol if s = end-of-file then accept else if s = a then go to T else if s = b then reject end 44

Deterministic FSMs as Algorithms until accept or reject do: S:s = get-next-symbol if s = end-of-file then accept else if s = a then go to S else if s = b then go to T T: s = get-next-symbol if s = end-of-file then accept else if s = a then go to T else if s = b then reject end Length of Program: |K|  (|  | + 2) Time required to analyze string w: O (|w|  |  |) We have to write new code for every new FSM. 45

A Deterministic FSM Interpreter dfsmsimulate(M: DFSM, w: string) = 1. st = s. 2. Repeat 2.1 c = get-next-symbol(w). 2.2 If c  end-of-file then st =  (st, c). until c = end-of-file. 3. If st  A then accept else reject. Input: aabaa 46

Nondeterministic FSMs as Algorithms Real computers are deterministic, so we have three choices if we want to execute a NDFSM: 1. Convert the NDFSM to a deterministic one: Conversion can take time and space 2 |K|. Time to analyze string w: O (|w|) 2. Simulate the behavior of the nondeterministic one by constructing sets of states "on the fly" during execution No conversion cost Time to analyze string w: O (|w|  |K| 2 ) 3. Do a depth-first search of all paths through the nondeterministic machine. 47

A NDFSM Interpreter ndfsmsimulate(M = (K, , , s, A): NDFSM, w: string) = 1. Declare the set st. 2. Declare the set st1. 3. st = eps(s). 4. Repeat 4.1 c = get-next-symbol(w). 4.2 If c  end-of-file then do st1 =  For all q  st do For all r   (q, c) do st1 = st1  eps(r) st = st If st =  then exit. until c = end-of-file. 6. If st  A   then accept else reject. 48