Presentation is loading. Please wait.

Presentation is loading. Please wait.

Course 4 Finite Automata (part 2)

Similar presentations


Presentation on theme: "Course 4 Finite Automata (part 2)"— Presentation transcript:

1 Course 4 Finite Automata (part 2)
Cpt S 317: Spring 2009 Course 4 Finite Automata (part 2) The structure and the content of the lecture is based on School of EECS, WSU

2 Cpt S 317: Spring 2009 Finite Automaton (FA) Informally, a state diagram that comprehensively captures all possible states and transitions that a machine can take while responding to a stream or sequence of input symbols Recognizer for “Regular Languages” Deterministic Finite Automata (DFA) The machine can exist in only one state at any given time Non-deterministic Finite Automata (NFA) The machine can exist in multiple states at the same time School of EECS, WSU

3 Cpt S 317: Spring 2009 Finite Automaton (FA) Construct DFA and NFA for recognizing the language: 𝐿 𝐹𝐴 = 𝑎𝑤𝑎 𝑤 ∈ {𝑏,𝑐} ∗ } Solution: see whiteboard. In many cases it is easier to construct the NFA for a language. Theorem: A language L is accepted by a DFA if and only if it is accepted by an NFA. Proof. later School of EECS, WSU

4 Examples of NFA for different types of languages
Cpt S 317: Spring 2009 Examples of NFA for different types of languages 𝐿 𝐹𝐴 = {𝑎𝑏 𝑛 𝑛≥0 ∪ {𝑎𝑏 𝑛 𝑎𝑏 𝑚 𝑛,𝑚≥0 (Union of languages) 𝐿 𝐹𝐴 = 𝐼𝐹,𝐹𝑂𝑅,𝐹𝑂𝑅𝐾 (Generation of finite language) 𝐿 𝐹𝐴 = {𝑎 𝑛 | 𝑛≥1}, 𝐿 𝐴𝐹 = {𝑎 𝑛 𝑛≥0 (Repetition of symbols) 𝐿 𝐹𝐴 = 𝑤 𝑤 ∈ 𝑎,𝑏,𝑐 ∗ , 𝐿 𝐴𝐹 = 𝑤 𝑤 ∈ 𝑎,𝑏,𝑐 + (Mix of letters) 𝐿 𝐹𝐴 = 𝑤 𝑤 ∈ 0,…,9 ∗ ,𝑤 𝑒𝑛𝑑𝑠 𝑤𝑖𝑡ℎ 0 𝐿 𝐹𝐴 = 𝑎 𝑛 𝑏 𝑚 𝑐 𝑘 |𝑚,𝑛,𝑘≥1 𝐿 𝐹𝐴 = 𝑤 𝑤 ∈ 0,1 ∗ , 𝑤 𝑐𝑜𝑛𝑡𝑎𝑖𝑛𝑠 𝑎𝑡 𝑚𝑜𝑠𝑡 𝑜𝑛𝑒 1 𝐿 𝐹𝐴 = 𝑤 𝑤 𝑐𝑜𝑛𝑡𝑎𝑖𝑛𝑠 𝑎𝑛 𝑒𝑣𝑒𝑛 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑧𝑒𝑟𝑜𝑠 𝑎𝑛𝑑 𝑎𝑛𝑦 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 1 𝐿 𝐹𝐴 = 𝑤𝑎𝑏𝑎𝑤′ 𝑤,𝑤′ ∈ {𝑎,𝑏,𝑐} ∗ } 𝐿 𝐹𝐴 = 𝑤 𝑤 𝑐𝑜𝑛𝑡𝑎𝑖𝑛𝑠 𝑎𝑛 𝑒𝑣𝑒𝑛 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 0′𝑠 𝑎𝑛𝑑 1 ′ 𝑠 𝐿 𝐹𝐴 = 𝑎 𝑖 𝑏 𝑗 |𝑖,𝑗>0 𝐿 𝐹𝐴 = 𝑤 ∈ 𝑎,𝑏,𝑐 ∗ 𝑤 𝑐𝑜𝑛𝑡𝑎𝑖𝑛𝑠 𝑎𝑏𝑏𝑎} 𝐿 𝐹𝐴 = {𝑤 𝑖𝑛𝑡𝑒𝑔𝑒𝑟 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡 𝑖𝑛 𝐶 𝑤𝑖𝑡ℎ 𝑜𝑝𝑡𝑖𝑜𝑛𝑎𝑙 𝑠𝑖𝑔𝑛} 𝐿 𝐹𝐴 = 𝑤 𝑤 ∈ 0,1 ∗ 𝑎𝑛𝑑 𝑖𝑠 𝑑𝑖𝑣𝑖𝑠𝑖𝑏𝑙𝑒 𝑏𝑦 3} 𝐿 𝐹𝐴 = {𝑤|𝑤 ∈ 0,1 ∗ 𝑎𝑛𝑑 𝑤≡1 𝑚𝑜𝑑 3} Solution: see whiteboard. School of EECS, WSU

5 Equivalence of DFA & NFA
Cpt S 317: Spring 2009 Equivalence of DFA & NFA Theorem: A language L is accepted by a DFA if and only if it is accepted by an NFA i.e. 𝐿 𝐷𝐹𝐴 =𝐿(𝑁𝐹𝐴). Proof: If part i.e 𝐿 𝐷𝐹𝐴 ⊇𝐿(𝑁𝐹𝐴) : Prove by showing every NFA can be converted to an equivalent DFA (in the next few slides…) Only-if part i.e 𝐿 𝐷𝐹𝐴 ⊆𝐿(𝑁𝐹𝐴) is trivial: Every DFA is a special case of an NFA where each state has exactly one transition for every input symbol. Therefore, if L is accepted by a DFA, it is accepted by a corresponding NFA. Should be true for any L School of EECS, WSU

6 Proof for the if-part If-part: Show that 𝐿 𝐷𝐹𝐴 ⊇𝐿(𝑁𝐹𝐴) rephrasing…
Cpt S 317: Spring 2009 Proof for the if-part If-part: Show that 𝐿 𝐷𝐹𝐴 ⊇𝐿(𝑁𝐹𝐴) rephrasing… Given any NFA N, we can construct a DFA D such that L(N)=L(D) How to convert an NFA into a DFA? Observation: In an NFA, each transition maps to a subset of states Idea: Represent: each “subset of NFA_states”  a single “DFA_state” Subset construction School of EECS, WSU

7 NFA to DFA by subset construction
Cpt S 317: Spring 2009 NFA to DFA by subset construction Let N = {QN,∑,δN,q0,FN} Goal: Build D={QD,∑,δD,{q0},FD} s.t. L(D)=L(N) Construction: QD= all subsets of QN (i.e., power set) FD=set of subsets S of QN s.t. S∩FN≠Φ δD: for each subset S of QN and for each input symbol a in ∑: δD(S,a) = U δN(p,a) p ∈ s School of EECS, WSU

8 NFA to DFA construction: Example 1
Cpt S 317: Spring 2009 Idea: To avoid enumerating all of power set, do “lazy creation of states” NFA to DFA construction: Example 1 L = {w | w ends in 01} [q0] 1 [q0,q1] [q0,q2] NFA: DFA: 0,1 1 q0 q1 q2 δD 1 Ø [q0] {q0}U{q1} {q0} [q1] {q2} *[q2] [q0,q1] {q0,q1}U∅ {q0}U{q2} *[q0,q2] {q0,q1} *[q1,q2] *[q0,q1,q2] {q0,q2} δD 1 [q0] [q0,q1] [q0,q2] *[q0,q2] δN 1 q0 {q0,q1} {q0} q1 Ø {q2} *q2 It does not make sense to construct q1, q2, {q1,q2} – see next slide 0. Enumerate all possible subsets 1. Determine transitions 2. Retain only those states reachable from {q0} School of EECS, WSU

9 NFA to DFA construction: Example 2
Cpt S 317: Spring 2009 Idea: To avoid enumerating all of power set, do “lazy creation of states” NFA to DFA construction: Example 2 Construct the NFA recognizing the following language, then transform it into an DFA: 𝐿= 𝑥 ∈ 0,1 ∗ 𝑡ℎ𝑒 𝑠𝑒𝑐𝑜𝑛𝑑 𝑠𝑦𝑚𝑏𝑜𝑙 𝑓𝑟𝑜𝑚𝑡ℎ𝑒 𝑟𝑖𝑔ℎ𝑡 𝑖𝑠 1}, Solution: see whiteboard. School of EECS, WSU

10 NFA to DFA: Repeating the example using LAZY CREATION
Cpt S 317: Spring 2009 NFA to DFA: Repeating the example using LAZY CREATION L = {w | w ends in 01} [q0] 1 [q0,q1] [q0,q2] NFA: DFA: 0,1 1 q0 q1 q2 δN 1 q0 {q0,q1} {q0} q1 Ø {q2} *q2 δD 1 [q0] [q0,q1] [q0,q2] *[q0,q2] Main Idea: Introduce states as you go (on a need basis) School of EECS, WSU

11 Correctness of subset construction
Cpt S 317: Spring 2009 Correctness of subset construction Theorem: If D is the DFA constructed from NFA N by subset construction, then L(D)=L(N) Proof: Show that δD({q0},w) ≡ δN(q0,w} , for all w Using induction on w’s length: Let w = xa δD({q0},xa) ≡ δD( δN(q0,x}, a ) ≡ δN(q0,w} School of EECS, WSU

12 A bad case where #states(DFA)>>#states(NFA)
Cpt S 317: Spring 2009 A bad case where #states(DFA)>>#states(NFA) Typically #states(DFA) = #states(NFA) However, L = {w | w is a binary string s.t., the kth symbol from its end is a 1} NFA has k+1 states But an equivalent DFA needs to have at least 2k states School of EECS, WSU

13 Applications Text indexing Find pattern P in text T
Cpt S 317: Spring 2009 Applications Text indexing inverted indexing For each unique word in the database, store all locations that contain it using an NFA or a DFA Find pattern P in text T Example: Google querying Extensions of this idea: PATRICIA tree, suffix tree School of EECS, WSU

14 A few subtle properties of DFAs and NFAs
The machine never really terminates. It is always waiting for the next input symbol or making transitions. The machine decides when to consume the next symbol from the input and when to ignore it. (but the machine can never skip a symbol) => A transition can happen even without really consuming an input symbol (think of consuming  as a free token) – if this happens, then it becomes an -NFA (see next lecture). A single transition cannot consume more than one (non-) symbol.


Download ppt "Course 4 Finite Automata (part 2)"

Similar presentations


Ads by Google