Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Three: Finite Automata Finite Automata, Lecture 3, slide 1 Amjad Ali.

Similar presentations


Presentation on theme: "Lecture Three: Finite Automata Finite Automata, Lecture 3, slide 1 Amjad Ali."— Presentation transcript:

1 Lecture Three: Finite Automata Finite Automata, Lecture 3, slide 1 Amjad Ali

2 Finite Automata, Lecture 3, slide 2 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)

3 Finite Automata, Lecture 3, slide 3 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

4 Finite Automata, Lecture 3, slide 4 The 5-Tuple Q is the set of states Drawn as circles in the diagram We often refer to individual states as q i The definition requires at least one: q 0, the start state  is the alphabet (that is, a finite set of symbols) A DFA M is a 5-tuple M = (Q, , , q 0, F), : where Q is the finite set of states  is the alphabet (that is, a finite set of symbols)   (Q    Q) is the transition function q 0  Q is the start state F  Q is the set of accepting states

5 Finite Automata, Lecture 3, slide 5 The 5-Tuple 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 A DFA M is a 5-tuple M = (Q, , , q 0, F), where: Q is the finite set of states  is the alphabet (that is, a finite set of symbols)   (Q    Q) is the transition function q 0  Q is the start state F  Q is the set of accepting states

6 Finite Automata, Lecture 3, slide 6 Example: This DFA defines {xa | x  {a,b}*} Formally, M = (Q, , , q 0, F), where Q = {q 0,q 1 }  = {a,b} F = {q 1 } Start sate q 0  (q 0,a) = q 1,  (q 0,b) = q 0,  (q 1,a) = q 1,  (q 1,b) = q 0 We could just say M = ({q 0,q 1 }, {a,b}, , q 0, {q 1 }) q0q0 q1q1 q1q1 q0q0 q1q1 q0q0 a b

7 Finite Automata, Lecture 3, slide 7 A regular language is one that is L(M) for some DFA M. For any DFA M = (Q, , , q 0, F), L(M) denotes the language accepted by M, which is L(M) = {x   * |  *(q 0, x)  F}. 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

8 Finite Automata, Lecture 3, slide 8 Example #1: Q = {q 0, q 1, q 2 } Σ = {a, b, c} Start state is q 0 F = {q 2 } abc q 0 q 0 q 0 q 1 q 1 q 1 q 1 q 2 q 2 q 2 q 2 q 2 Since δ is a function, at each step M has exactly one option. It follows that for a given string, there is exactly one computation. q1q1 q0q0 q2q2 a b a b c c a/b/c

9 Finite Automata, Lecture 3, slide 9 Example #2: 1 0 0 1 1 q 0 q 0 q 1 q 0 q 0 q 0 One state is final/accepting, all others are rejecting. The above DFA accepts those strings that contain an even number of 0’s q0q0 q1q1 0 0 1 1

10 Finite Automata, Lecture 3, slide 10 Example #3: a c c c baccepted q 0 q 0 q 1 q 2 q 2 q 2 a a crejected q 0 q 0 q 0 q 1 Accepts those strings that contain at least two c’s q1q1 q0q0 q2q2 a b a b c c a/b/c

11 Finite Automata, Lecture 3, slide 11 Example #4: Give a DFA M such that: L(M) = {x | x is a string of 0’s and 1’s and |x| >= 2} q1q1 q0q0 q2q2 0/1

12 Finite Automata, Lecture 3, slide 12 Example #5: Give a DFA M such that: L(M) = {x | x is a string of (zero or more) a’s and b’s such that x does not contain the substring aa} q2q2 q0q0 a a/b a q1q1 b b

13 Finite Automata, Lecture 3, slide 13 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} q2q2 q0q0 a a/b b q1q1 ba b q3q3 a

14 Finite Automata, Lecture 3, slide 14 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} q0q0 b q7q7 q5q5 q4q4 q6q6 b b b a q2q2 q1q1 q3q3 a a a b a/b b a a ab

15 Finite Automata, Lecture 3, slide 15 Example #8: Let Σ = {0, 1}. Give DFAs for {}, {ε}, Σ *, and Σ +. For {}:For {ε}: For Σ * :For Σ + : 0/1 q0q0 q0q0 q1q1 q0q0 q0q0 q1q1

16 Finite Automata, Lecture 3, slide 16 Definitions for DFAs Let M = (Q, Σ, δ,q 0,F) be a DFA and let w be in Σ *. Then w is accepted by M iff δ^(q 0,w) = p for some state p in F. Let M = (Q, Σ, δ,q 0,F) be a DFA. Then the language accepted by M is the set: L(M) = {w | w is in Σ * and δ^(q 0,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 M 1 = (Q 1, Σ 1, δ 1, q 0, F 1 ) and M 2 = (Q 2, Σ 2, δ 2, p 0, F 2 ) be DFAs. Then M 1 and M 2 are equivalent iff L(M 1 ) = L(M 2 ).

17 Finite Automata, Lecture 3, slide 17 Notes: A DFA M = (Q, Σ, δ,q 0,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(M 1 ) = L(M 2 ) then L(M 1 ) is a subset of L(M 2 ) and L(M 2 ) is a subset of L(M 1 ). Some languages are regular, others are not. For example, if L 1 = {x | x is a string of 0's and 1's containing an even number of 1's} and L 2 = {x | x = 0 n 1 n for some n >= 0} then L 1 is regular but L 2 is not.

18 Finite Automata, Lecture 3, slide 18 Questions: How do we determine whether or not a given language is regular? How could a program “simulate” a DFA?

19 Finite Automata, Lecture 3, slide 19 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

20 Finite Automata, Lecture 3, slide 20 M Accepts x  *(q,w) is the state of M ends up in, starting from state q and reading all of string w So  *(q 0,w) tells us whether M accepts w : A string w   * is accepted by a DFA M = (Q, , , q 0, F) if and only if  *(q 0, w)  F.

21 Finite Automata, Lecture 3, slide 21 Example #1: What is δ*(q 0, 011)? Informally, it is the state entered by M after processing 011 having started in state q 0. Formally: δ*(q 0, 011) = δ (δ*(q 0,01), 1)by rule #2 = δ (δ ( δ*(q 0,0), 1), 1)by rule #2 = δ (δ (δ (δ*(q 0, λ), 0), 1), 1)by rule #2 = δ (δ (δ(q 0,0), 1), 1)by rule #1 = δ (δ (q 1, 1), 1)by definition of δ = δ (q 1, 1)by definition of δ = q 1 by definition of δ Is 011 accepted? No, since δ*(q 0, 011) = q 1 is not a final state. q0q0 q1q1 0 0 1 1

22 Finite Automata, Lecture 3, slide 22 Example #2: What is δ(q 0, 011)? Informally, it is the state entered by M after processing 011 having started in state q 0. Formally: δ(q 0, 011) = δ (δ*(q 0,01), 1)by rule #2 = δ (δ (δ*(q 0,0), 1), 1)by rule #2 = δ (δ(δ(δ*(q 0, λ), 0),1), 1)by rule #2 = δ (δ(δ(q 0, 0), 1),1)by definition of δ = δ (δ(q 1, 1),1) by definition of δ = δ (q 1, 1)by definition of δ = q 1 Is 011 accepted? No, since δ(q 0, 011) = q 1 is not a final state. q1q1 q0q0 q2q2 11 0 0 1 0

23 Finite Automata, Lecture 3, slide 23 Example #3: What is δ(q 0, 100)? δ(q 0, 100) = δ (δ*(q 0,10), 0)by rule #2 = δ (δ (δ*(q 0,1), 0), 0)by rule #2 = δ (δ(δ(δ*(q 0, λ), 1),0), 0)by rule #2 = δ (δ(δ(q 0, 1), 0),0)by definition of δ = δ (δ(q 0, 0),0) by definition of δ = δ (q 1, 0)by definition of δ = q 2 Is 100 accepted? Yes, since δ(q 0, 100) = q 2 is a final state. 0 q1q1 q0q0 q2q2 11 0 1 0

24 Finite Automata, Lecture 3, slide 24 Formal Definition of Computation Let M = (Q, , , q 0, F) and w = w 1 w 2 w 3 w 4 …w n be a string where each w i  ∑. Then M accepts w if a sequence of states r 0, r 1, …, r n in Q exists with three conditions: 1. r 0 = q 0 2.  (r i, w i+1 ) = r i+1 for i = 0, …, n-1 3. r n  F Formal Definition of DFA A DFA M is a 5-tuple M = (Q, , , q 0, F) : where Q is the finite set of states  is the alphabet (that is, a finite set of symbols)   (Q    Q) is the transition function q 0  Q is the start state F  Q is the set of accepting states

25 Finite Automata, Lecture 3, slide 25 Example #1: Q = {q 0, q 1, q 2 } Σ = {a, b} Start state is q 0 F = {q 2 } δ :ab q 0 q 1 q 0 q 1 q 2 q 0 q 2 q 2 q 2 q1q1 q0q0 q2q2 b b a a a, b

26 Finite Automata, Lecture 3, slide 26 w = a b a b a b a a b w = w 1 w 2 w 3 w 4 w 5 w 6 w 7 w 8 w 9, |w| = 9, n = 9 r 0 = q 0  (r i, w i+1 ) = r i+1 for i = 0, …, 8 ab a b a b a a b q 0 q 1 q 0 q 1 q 0 q 1 q 0 q 1 q 2 q 2 r 0 r 1 r 2 r 3 r 4 r 5 r 6 r 7 r 8 r 9 1.  (r 0, w 1 ) = r 1 2.  (r 1, w 2 ) = r 2 3.  (r 2, w 3 ) = r 3 4.  (r 3, w 4 ) = r 4  (q 0, a) = q 1  (q 1, b) = q 0 5.  (r 4, w 5 ) = r 5 6.  (r 5, w 6 ) = r 6 7.  (r 6, w 7 ) = r 7 8.  (r 7, w 8 ) = r 8  (q 0, a) = q 1  (q 1, b) = q 0  (q 0, a) = q 1  (q 1, a)= q 2 9.  (r 8, w 9 ) = r 9  (q 2, b) = q 2, As r 9 =q 2 and q 2  F, then String is accepted q1q1 q0q0 q2q2 b b a a a, b

27 DFA Applications Finite Automata, Lecture 3, slide 27

28 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

29 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

30 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


Download ppt "Lecture Three: Finite Automata Finite Automata, Lecture 3, slide 1 Amjad Ali."

Similar presentations


Ads by Google