570 likes | 1.17k Views
Lecture Three: Finite Automata. Amjad Ali. DFA: Deterministic Finite Automaton. An informal definition (formal version later): A diagram with a finite number of states represented by circles An arrow points to one of the states, the unique start state
E N D
Lecture Three:Finite Automata Amjad Ali
DFA: Deterministic Finite Automaton • An informal definition (formal version later): • A diagram with a finite number of states represented by circles • An arrow points to one of the states, the unique start state • Double circles mark any number of the states as accepting states • For every state, for every symbol in , there is exactly one arrow labeled with that symbol going to another state (or back to the same state)
DFAs Define Languages • Given any string over , a DFA can read the string and follow its state-to-state transitions • At the end of the string, if it is in an accepting state, we say it accepts the string • Otherwise it rejects • The language defined by a DFA is the set of strings in * that it accepts
The 5-Tuple A DFAM is a 5-tuple M = (Q, , , q0, F), :where Qis the finite set of states is the alphabet (that is, a finite set of symbols) (QQ) is the transition functionq0Q is the start stateFQ is the set of accepting states • Q is the set of states • Drawn as circles in the diagram • We often refer to individual states as qi • The definition requires at least one: q0, the start state • is the alphabet (that is, a finite set of symbols)
The 5-Tuple A DFA M is a 5-tuple M = (Q, , , q0, F), where:Q is the finite set of states is the alphabet (that is, a finite set of symbols) (QQ) is the transition functionq0Q is the start stateFQ is the set of accepting states • F is the set of all those in Q that are accepting states • Drawn as double circles in the diagram • is the transition function • A function (q,a) that takes the current state q and next input symbol a, and returns the next state • Represents the same information as the arrows in the diagram
Example: • This DFA defines {xa | x {a,b}*} • Formally, M = (Q, , , q0, F), where • Q = {q0,q1} • = {a,b} • F = {q1} • Start sate q0 • (q0,a) = q1, (q0,b) = q0, (q1,a) = q1, (q1,b) = q0 • We could just say M = ({q0,q1}, {a,b}, , q0, {q1}) a b q0 q0 q1 q1 q1 q0
Regular Languages • To show that a language is regular, give a DFA for it. We'll see additional ways later • To show that a language is not regular is much harder; we'll see how later For any DFA M = (Q, , , q0, F), L(M) denotes the language accepted by M, which is L(M) = {x* | *(q0, x) F}. A regular language is one that is L(M) for some DFA M.
a a/b/c a c c q0 q1 q2 b b Example #1: Q = {q0, q1, q2} Σ = {a, b, c} Start state is q0 F = {q2} a b c q0 q0q0q1 q1 q1q1q2 q2 q2q2q2 • Since δ is a function, at each step M has exactly one option. • It follows that for a given string, there is exactly one computation. Finite Automata, Lecture 3, slide 8
1 0 q1 q0 1 0 Example #2: 1 0 0 1 1 q0 q0q1 q0 q0q0 • One state is final/accepting, all others are rejecting. • The above DFA accepts those strings that contain an even number of 0’s Finite Automata, Lecture 3, slide 9
a a/b/c a c c q0 q1 q2 b b Example #3: a c cc b accepted q0 q0q1 q2 q2q2 a a crejected q0 q0q0q1 • Accepts those strings that contain at least two c’s Finite Automata, Lecture 3, slide 10
0/1 q2 0/1 0/1 q0 q1 Example #4: • Give a DFA M such that: L(M) = {x | x is a string of 0’s and 1’s and |x| >= 2} Finite Automata, Lecture 3, slide 11
b a/b q0 q1 a a q2 b Example #5: • Give a DFA M such that: L(M) = {x | x is a string of (zero or more) a’s and b’ssuch that x does not contain the substring aa} Finite Automata, Lecture 3, slide 12
b a a/b q3 a a b q0 q1 q2 b Example #6: • Give a DFA M such that: L(M) = {x | x is a string of a’s, b’s and c’s such that x contains the substring aba} Finite Automata, Lecture 3, slide 13
b a b b a q4 q1 q5 q2 q6 q3 q7 b a b a/b a q0 a b a b a Example #7: • Give a DFA M such that: L(M) = {x | x is a string of a’s and b’s such that x contains both aa and bb} Finite Automata, Lecture 3, slide 14
0/1 0/1 0/1 q1 q0 q0 q0 q1 0/1 0/1 q0 Example #8: • Let Σ = {0, 1}. Give DFAs for {}, {ε}, Σ*, and Σ+. For {}: For {ε}: For Σ*: For Σ+: 0/1 Finite Automata, Lecture 3, slide 15
Definitions for DFAs • Let M = (Q, Σ, δ,q0,F) be a DFA and let w be in Σ*. Then w is accepted by M iff δ^(q0,w) = p for some state p in F. • Let M = (Q, Σ, δ,q0,F) be a DFA. Then the language accepted by M is the set: L(M) = {w | w is in Σ* and δ^(q0,w) is in F} • Another equivalent definition: L(M) = {w | w is in Σ* and w is accepted by M} • Let L be a language. Then L is a regular language iff there exists a DFA M such that L = L(M). • Let M1 = (Q1, Σ1, δ1, q0, F1) and M2 = (Q2, Σ2, δ2, p0, F2) be DFAs. Then M1 and M2 are equivalent iff L(M1) = L(M2). Finite Automata, Lecture 3, slide 16
Notes: • A DFA M = (Q, Σ, δ,q0,F) partitions the set Σ* into two sets: L(M) and Σ* - L(M). • If L = L(M) then L is a subset of L(M) and L(M) is a subset of L. • Similarly, if L(M1) = L(M2) then L(M1) is a subset of L(M2) and L(M2) is a subset of L(M1). • Some languages are regular, others are not. For example, if L1 = {x | x is a string of 0's and 1's containing an even number of 1's} and L2 = {x | x = 0n1n for some n >= 0} then L1 is regular but L2 is not. Finite Automata, Lecture 3, slide 17
Questions: • How do we determine whether or not a given language is regular? • How could a program “simulate” a DFA? Finite Automata, Lecture 3, slide 18
The * Function • The function gives 1-symbol moves • We'll define * so it gives whole-string results (by applying zero or more moves) • A recursive definition: • *(q,) = q • For all w in Σ* and a in Σ *(q,wa) = (*(q,w),a) • That is: • For the empty string, no moves • For any string wa (w is any string and a is any final symbol) first make the moves on w, then one final move on a
M Accepts x • *(q,w) is the state of Mends up in, starting from state q and reading all of string w • So *(q0,w) tells us whether M accepts w : A string w* is accepted by a DFA M = (Q, , , q0, F) if and only if *(q0, w) F.
1 0 q1 q0 1 0 Example #1: • What is δ*(q0, 011)? Informally, it is the state entered by M after processing 011 having started in state q0. • Formally: δ*(q0, 011) = δ (δ*(q0,01), 1) by rule #2 = δ (δ ( δ*(q0,0), 1), 1) by rule #2 = δ (δ (δ (δ*(q0, λ), 0), 1), 1) by rule #2 = δ (δ (δ(q0,0), 1), 1) by rule #1 = δ (δ (q1, 1), 1) by definition of δ = δ (q1, 1) by definition of δ = q1 by definition of δ • Is 011 accepted? No, since δ*(q0, 011) = q1 is not a final state. Finite Automata, Lecture 3, slide 21
q2 1 1 1 0 0 q0 q1 0 Example #2: • What is δ(q0, 011)? Informally, it is the state entered by M after processing 011 having started in state q0. • Formally: δ(q0, 011) = δ (δ*(q0,01), 1) by rule #2 = δ (δ (δ*(q0,0), 1), 1) by rule #2 = δ (δ(δ(δ*(q0 , λ), 0),1), 1) by rule #2 = δ (δ(δ(q0 , 0), 1),1) by definition of δ = δ (δ(q1 , 1),1) by definition of δ = δ (q1 , 1)by definition of δ = q1 • Is 011 accepted? No, since δ(q0, 011) = q1 is not a final state. Finite Automata, Lecture 3, slide 22
1 1 1 0 q0 q1 q2 0 0 Example #3: • What is δ(q0, 100)? δ(q0, 100) = δ (δ*(q0,10), 0) by rule #2 = δ (δ (δ*(q0,1), 0), 0) by rule #2 = δ (δ(δ(δ*(q0 , λ), 1),0), 0) by rule #2 = δ (δ(δ(q0 , 1), 0),0) by definition of δ = δ (δ(q0 , 0),0) by definition of δ = δ (q1 , 0)by definition of δ = q2 • Is 100 accepted? Yes, since δ(q0, 100) = q2is a final state. Finite Automata, Lecture 3, slide 23
Formal Definition of DFA A DFAM is a 5-tuple M = (Q, , , q0, F) : where Q is the finite set of states is the alphabet (that is, a finite set of symbols) (QQ) is the transition functionq0Q is the start stateFQ is the set of accepting states Formal Definition of Computation Let M = (Q, , , q0, F) and w = w1w2w3w4…wn be a string where each wi ∑. Then M acceptsw if a sequence of states r0, r1, …, rn in Q exists with three conditions: 1. r0 = q0 2. (ri, wi+1) = ri+1 for i = 0, …, n-1 3. rn F
a, b b a a q0 q1 q2 b Example #1: Q = {q0, q1, q2} Σ = {a, b} Start state is q0 F = {q2} δ : ab q0 q1q0 q1 q2 q0 q2 q2q2 Finite Automata, Lecture 3, slide 25
a, b b a a q0 q1 q2 b w = a b a b a b a a b w = w1 w2 w3 w4 w5 w6 w7 w8 w9 , |w| = 9 , n = 9 r0 = q0 (ri, wi+1) = ri+1 for i = 0, …, 8 a b a b a b a a b q0 q1 q0 q1 q0 q1 q0 q1 q2q2 r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 1. (r0 , w1) = r1 2. (r1 , w2) = r2 3. (r2 , w3) = r3 4. (r3 , w4) = r4 (q0 , a) = q1 (q1 , b) = q0 (q0 , a) = q1 (q1 , b) = q0 5. (r4 , w5) = r5 6. (r5 , w6) = r6 7. (r6 , w7) = r7 8. (r7 , w8) = r8 (q0 , a) = q1 (q1 , b) = q0 (q0 , a) = q1 (q1 , a)= q2 9. (r8 , w9) = r9 (q2 , b) = q2 , As r9=q2 and q2 F, then String is accepted Finite Automata, Lecture 3, slide 26
DFA Applications Finite Automata, Lecture 3, slide 27
We have seen how DFAs can be used to define formal languages. In addition to this formal use, DFAs have practical applications. DFA-based pieces of code lie at the heart of many commonly used computer programs. Finite Automata, Lecture 3, slide 28
DFA Applications • Programming language processing • Scanning phase: dividing source file into "tokens" (keywords, identifiers, constants, etc.), skipping whitespace and comments • Command language processing • Typed command languages often require the same kind of treatment • Text pattern matching • Unix tools like awk, egrep, and sed, mail systems like ProcMail, database systems like MySQL, and many others Finite Automata, Lecture 3, slide 29
More DFA Applications • Signal processing • Speech processing and other signal processing systems use finite state models to transform the incoming signal • Controllers for finite-state systems • Hardware and software • A wide range of applications, from industrial processes to video games Finite Automata, Lecture 3, slide 30