Finite Automata (Finite State Machine) Lecture 4-5 Ref. Handout p18-24 1 2 Controlling Things ... If x has happened then If in state y then do something do something change to new state If x happened while in state y do something change to new state 3 State Transition Table State Event happy work due next week anxious work due tomorrow Work cancelled anxious work done happy Action New State none start work happy none hand in work anxious happy happy 4 State Diagrams a state transition A x B 5 State Transition Diagram cancelled start Happy tomorrow Anxious done next week 6 A Finite Automata cancelled Happy next week tomorrow Anxious done 7 Components starting state (one per machine) d input symbol transition halting state (one or more) non-halting state 8 Sequence of Input Symbols What input sequences leave this FA in its halt state? a S1 S2 b ac yes ab no abc no abac yes c S3 { (ab)nac | n>=0 } { ac, abac, ababac, abababac, ... } 9 The Original Example ... Some acceptable inputs cancelled Happy next week tomorrow done Anxious nnnntc ntd Λ nntcntd 10 Recognising Strings the set of all strings accepted/recognised by an FA = the language accepted/recognised by the FA 11 Rejecting Strings An FA rejects a string if with some symbols remaining there is no transition for the next symbol or with no symbols remaining the FA is not in a halt state 12 Rejected / Accepted Strings tdnnn tdnnt n ntc ntcd c Happy n t yes/no yes/no yes/no yes/no yes/no Anxious d 13 Recall some simple languages ... Can you draw FA to recognise them? { Λ, a, aa, aaa, aaaa, ...} = { an | n >= 0 } { a, aa, aaa, aaaa, ...} = { an | n > 0 } { 00, 01, 10, 11 } 14 An Assignment Related Example Draw an FA to recognise non-negative integers Extend it to all integers (i.e. allowing +,- signs) 15 Memo for In-class test 4 [ questions my answers correct answers /5] comments 1 2 3 4 5 16 Non-Deterministic Automata a 1 b c cd cdacd 2 What happens here? d 3 d 4 17 Non-Deterministic Automata In deterministic automata, there can never be two transitions to different state for the same input symbol. If there are more than one transition to different states for the same input symbol, we call it a non-deterministic automata. 18 Deterministic FAs and Non-Deterministic FAs For every non-deterministic FA (NFA) there is a deterministic FA (DFA) which accepts the same language 19 A Simple Example 2 a 1 NFA b 3 a DFA a b 20 Step by Step (1) 2 a 1 NFA b 3 a {1} a { 2,3 } after ‘a’, must be in state 2 or 3 21 Step by Step (2) 2 a 1 NFA b 3 a from state 2, read ‘b’, move to state 3 {1} a { 2,3 } b {3} 22 Last Step 2 a 1 NFA b 3 a {1} a { 2,3 } b {3} 3 is a halt state 23 A General Algorithm 1. Write down {1} as the start state of the DFA 2. Choose a state in the DFA which hasn’t been processed 3. Work out all transitions from it adding new states 4. Repeat from step 2 until all states have been processed 5. Mark halting states 24 Building DFAs start Machine A Language L halt start Machine B Language K halt 25 What does this do? Machine A Language L halt Machine B Language K halt start 26 What does this do? start Machine A Language L ?? Machine B Language K halt 27 What does this do? start Machine A Language L halt 28 Using Building Blocks For example, you are asked to define a format for a collection of pictures’ file names G = { a1.gif, a2.gif, a3.gif, ... a33.gif, .... } 29 Using Building Blocks Now you are asked to define a format for another collection of pictures’ file names P = { b1.jpg, b2.jpg, b3.jpg, ... b33.jpg, .... } 30 What if we need an FA for the union of G and P i.e. GUP a 1-9 1-9 . i g b f 1-9 1-9 . j p g 31 Using Building Blocks Let’s change the language P to P = { a1.jpg, a2.jpg, a3.jpg, ... a33.jpg, .... } (replace ‘b’ by ‘a’) a 1-9 1-9 . j p g 32 What happens now? If we need an FA for the union of G and P. i.e. G U P a 1-9 1-9 . i g a f 1-9 1-9 . It’s a NFA! j p g 33 Limitations of FAs Can you draw an FA to accept this? { a nb n | n > 0 } 34 Memo for In-class test 5 [ questions my answers correct answers /5] comments 1 2 3 4 5 35