Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 Finite Automata

Similar presentations


Presentation on theme: "Chapter 2 Finite Automata"— Presentation transcript:

1 Chapter 2 Finite Automata

2 A Finite-State Machine (1)
A finite state machine is a mathematical model of a system, with discrete inputs and outputs

3 A Finite-State Machine (2)
Finite Automata FA: a finite set of states a set of transitions (edges) a start state a set of final states Defining a FA is a kind of programming. Problem definition Includes defining possible actions & accepting condition. States  structure of program Includes designating which are initial & final. Transitions  program

4 2.1: Deterministic Finite Automata (1)
DFA M = (Q, S, d, q0, F) Q = a finite set of states S = a finite set called the alphabet d = transition function total function Q  S  Q q0 = start state q0  Q F = final or accepting states F  Q

5 Deterministic Finite Automata (2)
DFA M Q = {q0, q1} S = {a, b} F = {q1} The transition function d is given in a tabular form called the transition table d(q0, a) = q1 d(q0, b) = q0 d(q1, a) = q1 d(q1, b) = q0

6 Deterministic Finite Automata (3)
A DFA M can be considered to be a language acceptor. The language of M, L(M), is the set of strings S* accepted by M. A DFA M reads an input string from left to right. The next state depends on the current state and the unread (unprocessed) symbol.

7 Deterministic Finite Automata (5)
The DFA M accepts the set of strings over {a, b} that contain the substring bb M : Q = {q0, q1, q2}, S = {a, b}, F = {q2} The transition function d is given in a tabular form called the transition table d(q0, a) = q0 d(q0, b) = q1 d(q1, a) = q0 d(q1, b) = q2 d(q2, a) = q2 d(q2, b) = q2 Is abba  L(M)? Yes, since the computation halts in state q2, which is a final state Is abab  L(M)? No, since the computation halts in state q1, which is NOT a final state

8 Deterministic Finite Automata (6)
Extended transition function d* of a DFA with transition function d is a function from Q  S*  Q defined recursively on the length of the input string w Basis: |w| = 0. Then w = e and d*(qi, e) = qi Recursive step: Let |w| = 1. Then d*(qi, av) = d*(d(qi, a), v)  qi  Q,  a  S,  v  S*

9 Deterministic Finite Automata (7)
A string w is accepted if d*(q0, w)  F The language of a DFA M is L(M) = {w  S* | d*(q0, w)  F} DFA M = (Q, S, d, q0, F) accepts w  S*  d*(q0, w)  F

10 Deterministic Finite Automata (8)
Two possibilities for DFA M running on w M accepts w M accepts w iff the computation of M on w ends up (halts) in an accepting configuration d*(q0, w)  F M rejects w M rejects w iff the computation of M on w ends up (halts) in a rejecting configuration d*(q0, w)  F

11 State Diagrams and Examples (1)
The state diagram of a DFA M = (Q, S, d,q0, F) is a labeled graph G defined by the following conditions: The nodes of G are the elements of Q The labels on the arcs of G are elements of S q0 is the start node, denoted by F is the set of accepting nodes, denoted by There is an arc from node qi to qj labeled a if d(qi, a) = qj For every node qi and symbol a  S, there is exactly one arc labeled a leaving qi a q0

12 State Diagrams and Examples (2)
Deterministic Finite Automata DFA all outgoing edges are labelled with an input character no state has e- transition, transition on input e no two edges leaving a given state have the same label for each state s and input symbol a, there is at most one edge label a leaving s Therefore: the next state can be determined uniquely, given the current state and the current input character

13 State Diagrams and Examples (1)
strings over {a,b} with at least 3 a’s 1 a 2 a 3+ a 0 a a b S

14 State Diagrams and Examples (2)
strings over {a,b} with length mod 3 = 0 1 2 S

15 State Diagrams and Examples (3)
strings over {a,b} without 3 consecutive a’s A simple example of “strings not of the form …”. 1 a 2 a Has aaa 0 a a b S

16 State Diagrams and Examples (4)
Draw a state diagram for DFA M that accepts the set of strings over {a, b} that contain the substring bb The string ababb is accepted since the halting state is the accepting state q2 q1 q2 q0 b a a, b

17 State Diagrams and Examples (5)
The DFA accepts (b|ab)*(a|e) the set of strings over {a, b} that do not contain the substring aa b S 0 a a 1 a a 2 a b

18 State Diagrams and Examples (6)
The language {anbn, n0} is not regular, so we can not build a DFA that accept this language It needs an infinite number of states But {anbn, 1  n  3} is regular and its DFA is This DFA is NOT Complete a a a q0 q1 q2 q3 b b b q4 q5 q6 b b

19 State Diagrams and Examples (7)
strings over {a, b} that contain the substring bb OR do not contain the substring aa This language is the union of the languages of the previous examples a 0 a a 1 a a 2 a 2 a b a b a b b 1 b b 2 b S

20 State Diagrams and Examples (8)
strings over {a, b} that contain an even number of a’s AND an odd number of b’s b [ea, eb] [ea, ob] b a a a a [oa, eb] b [oa, ob] b

21 State Diagrams and Examples (9)
Let M be the DFA previous slide A DFA M’ that accepts all strings over {a, b} that do not contain an even number of a’s AND an odd number of b’s is shown below L(M’) = {a, b}* - L(M) = S* - L(M) Any string accepted by M is rejected by M’ and vice versa b [ea, eb] [ea, ob] b a a a a [oa, eb] b [oa, ob] b

22 State Diagrams and Examples (10)
Let S = {0, 1, 2, 3}. A string in S* is a sequence of integers from S The DFA M determines whether the sum of elements of a string is divisible by 4 The string and 0130 should be accepted and 0111 rejected by M w mod 4 = 0 1 w mod 4 = 1 3 1 3 1 3 2 2 2 2 w mod 4 = 2 1 w mod 4 = 3 3

23 State Diagrams and Examples (11)
DFA M1 accepts (ab)*c M1 is incomplete determinism The string abcc is rejected since M1 is unable to process the final c from state q2 a q0 q1 b c q2 M1

24 State Diagrams and Examples (12)
M2 accepts the same language as M1 in previous example (ab)*c The state qe is the error state (dead end) a q0 q1 b c a, c b q2 qe a, b, c M2 S

25 State Diagrams and Examples (13)
strings over {a,b} with next-to-last symbol = a …aa …ab a …ba …bb e b

26 State Diagrams and Examples (14)
start 3 b 2 1 a What Language is Accepted?

27 2.2: Nondeterministic Finite Automata (1)
A recognizer that takes an input string & determines whether it’s a valid sentence of the language Deterministic : Has at most one action for every given input symbol. Non-Deterministic : Has more than one (or no) alternative action for the same input symbol.

28 Nondeterministic Finite Automata (2)
NFA: Formal Definition NFA M = (Q, S, d, q0, F) Q = a finite set of states S = a finite set alphabet d = transition function total function Q  S  (Q) = 2Q - power set of Q q0 = initial/starting state q0  Q F = final or accepting states F  Q

29 Nondeterministic Finite Automata (3)
start 3 b 2 1 a Q = { 0, 1, 2, 3 } q0 = 0 F = { 3 }  = { a, b } What Language is defined ? What is the Transition Table ? i n p u t a b state { 0, 1 } { 0 } 1 { 2 } 2 { 3 }

30 Nondeterministic Finite Automata (4)
Change in d For an DFA M, d(q, a) results in one and only one state for all states q and alphabet a For an NFA M, d(q, a) can result in a set of states, zero, one, or more states: qi a a qn qi qn qn qj a a qk d(qn, a) = {qi} d(qn, a) = {qi, qj , qk } d(qn, a) = {} = 

31 Nondeterministic Finite Automata (5)
a,b a,b 1 2 3 4 5 a a b a Why is this only an NFA and not an DFA?

32 Nondeterministic Finite Automata (6)
Computing with NFA’s Computations are different We always start from start state. Call it the root of the computation. Then we might go to different states on one symbol. Then from those states to new sets of states, creating a tree-like computation. If one path ends up in a final state, then ACCEPT, else REJECT

33 Nondeterministic Finite Automata (7)
start 3 b 2 1 a Given an input string, we trace moves If no more input & in final state, ACCEPT EXAMPLE: Input: ababb Path 1: 0 -> 0 -> 0 -> 0 -> 0 -> 0 (REJECT) Path 2: 0 -> 0 -> 0 -> 1 -> 2 -> 3 (ACCEPT)

34 Nondeterministic Finite Automata (8)
Extended transition function d* of a NFA with transition function d is a function from Q  S*  2Q (power set) defined recursively on the length of the input string w Basis: |w| = 0. Then w = e and d*(qi, e) = {qi} Recursive step: Let |w| = 1. Then d*(qi, av) = U d*(qj, v), qj  d(qi, a)  qi  Q,  qj  Q , a  S,  v S* The language of a NFA M is L(M) = {w  S* | d*(q0, w)  F ≠ }

35 NFA (9) The state diagram DFA M1 and NFA M2 accepts (a|b)*bb(a|b)* 2 1
start 2 1 b a M1 start 2 1 b a, b M2

36 NFA (10) An NFA that accepts string over {a, b} with substring aa or bb There are 2 distinct acceptance paths for the string abaaabb a, b a, b start a 1 a 2 b a, b 3 b 4

37 NFA (11) The state diagram DFA M1 and NFA M2 accepts (a|b)*abba(a|b)*
start a 1 b 2 b 3 a 4 a b a, b a, b M2 start a 1 b 2 b 3 a 4

38 NFA with e-Transitions (1)
NFA with e-transitions, denoted by NFA-e Formal Definition NFA-e M = (Q, S, d, q0, F) Q = a finite set of states S = a finite set alphabet d = transition function total function Q  (S  {e})  (Q) q0 = initial/starting state q0  Q F = final or accepting states F  Q  transitions j i Switch state but do not use any input symbol

39 e-Transitions (2) The input string w is accepted using NFA-e if there is a computation that processes the entire string and halts in an accepting state A computation may continue using e-transition after the input string has been completely processed

40 e-Transitions (3) L(M1) = (a|b)*bb(a|b)*, L(M2) = (b|ab)*(a|e)
L(M) = L(M1)  L(M2) b a, b a, b a q2,0 q2,1 q1,0 b q1,1 b q1,2 M2 M1 b a, b a, b q1,0 b q1,1 b q1,2 M b a q2,0 q2,1 b

41 e-Transitions (4) L(M1) = (a|b)*bb(a|b)*, L(M2) = (b|ab)*(a|e)
L(M) = L(M1)L(M2) b a, b a, b a q2,0 q2,1 q1,0 b q1,1 b q1,2 M2 M1 b b a, b a, b a q1,0 b q1,1 b q1,2 q2,0 q2,1 M b

42 e-Transitions (5) M accepts all strings over {a, b} of even length e
q0 a, b q1 a, b q2 e

43 e-Transitions (6) Let M1 and M2 be 2 NFA-e’s as follows
q1,0 M1 q1,f q2,0 M2 q2,f M1 = (Q1, S, d1, q1,0, F1) M2 = (Q2, S, d2, q2,0, F2)

44 e-Transitions (7) L(M) = L(M1)  L(M2) M1   M2
q1,0 M1 q1,f q0 q2,0 M2 q2,f M = (Q1  Q2  {q0}, S, d, q0, F1  F2) d is the same as in d1 and d2, but add: d(q0, ) = {q1,0, q2,0}

45 e-Transitions (8) L(M) = L(M1)L(M2)  M1 M2
q1,0 q2,0 M1 q1,f M2 q2,f M = (Q1  Q2, S, d, q1,0, F2) d is the same as in d1 and d2, but add: d(q, ) = {q2,0}, where q  F1

46 e-Transitions (9) L(M) = L(M1)*  M1
q1,0 q1,f M1 qf q0 M = (Q1  {q0, qf}, S, d, q0, {qf}) d is the same as in d1, but add: d(q0, ) = {q1,0 ,qf} d(q1,f, ) = {q1,0 ,qf}, where q1,f F1

47 Given the regular expression : a(b*c) | a( b | c+ | e)
e-Transitions (10) Given the regular expression : a(b*c) | a( b | c+ | e) Find a transition diagram NFA- that recognizes it.

48 e-Transitions (11) a( b | c+ | e) 3 2 1 a (b*c) 6 5 7 4
Now that you have the individual diagrams, “or” them as follows:

49 e-Transitions (12) 3 2 b c a 1 6 5 7 4

50 e-Transitions (12) See examples 2.7, 2.8, 2.9, and 2.10

51 2.3: Equivalence of DFA & NFA (1)
Is NFA more powerful than DFA? NO! NFA inefficient to implement directly, so convert to a DFA that recognizes the same strings Is there a language accepted by an NFA that is not accepted by any DFA? No NFA is equivalent to DFA. Each state in DFA corresponds to a SET of states of the NFA

52 Removing Nondeterminism (2)
-closure In an NFA-, the -closure(q) of a state q is the set of all states that can be reached from q by following a path whose edges are all labeled by . a Start q0 q1 q3 -closure(q0) = {q0, q1, q2, q3} -closure(q1) = {q1, q3} -closure(q2) = {q1, q2, q3} b b q2

53 Removing Nondeterminism (3)
NFA-e N = (Q, , d, q0, F) -closure(q) : q  Q : set of NFA-e states that are reachable from q on -transitions only -closure(T) : T  Q : NFA-e states reachable from some state t  T on -transitions only move(T, a) : T  Q, a   : set of states to which there is a transition on input a from some state t  T move({q1, q2, …, qi}, a) = (q1, a)  (q2, a)  …  (qi, a) No input is consumed These 3 operations are utilized by algorithms / techniques to facilitate the conversion process

54 Removing Nondeterminism (4)
-closure(0) = {0, 1, 2, 4, 7} (all states reachable from 0 on -moves) -closure(0, 6) = -closure(0)  -closure(6) = {0, 1, 2, 4, 7}  {6, 1, 2, 4,7} = {0, 1, 2, 4, 6, 7} move({0, 1, 2, 4, 7}, a) = {3, 8} (since move(2, a)=3 and move(7, a)=8) 1 2 3 5 4 6 7 8 9 10 a b start

55 NFA-e DFA with Subset Construction
NFA-e N  DFA M construction: Given N = (Q, S, d, q0, F), define M = (Q’, S, d’, q’0, F’) Q’  P(Q), all the possible NFA-e states M could be in. q’0 = e-closure(q0) F’ = {q’  Q’ | q’  F  } d’(q’, a) = e-closure(move(q’, a)) DFA M has a state ERR  Q’ = P(Q) serves as the “error” state, when needed ’(ERR, a) = ERR, a   By definition of ’

56 Subset Construction Algorithm Concepts
1 2 3 5 4 6 7 8 9 10 a b start Start with NFA-e: (a | b)*abb First we calculate: -closure(0) (i.e., state 0) -closure(0) = {0, 1, 2, 4, 7} (all states reachable from 0 on -moves) Let A = {0, 1, 2, 4, 7} be a state of new DFA M

57 adds {3, 8} (since move(2, a) = 3 and move(7, a) = 8)
Conversion Example (1) 2nd , we calculate : a : -closure(move(A, a)) and b : -closure(move(A, b)) a : -closure(move(A, a)) = -closure(move({0,1,2,4,7}, a))} adds {3, 8} (since move(2, a) = 3 and move(7, a) = 8) From this we have : -closure({3, 8}) = {1,2,3,4,6,7,8} (since 3614, 67, and 12 all by -moves) Let B = {1,2,3,4,6,7,8} be a new state in D. Define M(A, a) = B b : -closure(move(A,b)) = -closure(move({0,1,2,4,7}, b)) adds {5} (since move(4, b) = 5) From this we have : -closure({5}) = {1,2,4,5,6,7} (since 5614, 67, and 12 all by -moves) Let C = {1,2,4,5,6,7} be a new state in D. Define M(A, b) = C

58 Conversion Example (2) 3rd, we calculate for state B on {a, b}
a : -closure(move(B, a)) = -closure(move({1,2,3,4,6,7,8}, a))} = {1,2,3,4,6,7,8} = B Define M(B, a) = B b : -closure(move(B, b)) = -closure(move({1,2,3,4,6,7,8}, b))} = {1,2,4,5,6,7,9} = D Define M(B, b) = D 4th, we calculate for state C on {a, b} a : -closure(move(C, a)) = -closure(move({1,2,4,5,6,7}, a))} = {1,2,3,4,6,7,8} = B Define M(C, a) = B b : -closure(move(C, b)) = -closure(move({1,2,4,5,6,7}, b))} = {1,2,4,5,6,7} = C Define M(C, b) = C

59 Conversion Example (3) 5th , we calculate for state D on {a, b}
a : -closure(move(D, a)) = -closure(move({1,2,4,5,6,7,9}, a))} = {1,2,3,4,6,7,8} = B Define M(D, a) = B b : -closure(move(D, b)) = -closure(move({1,2,4,5,6,7,9}, b))} = {1,2,4,5,6,7,10} = E Define M(D, b) = E Finally, we calculate for state E on {a, b} a : -closure(move(E, a)) = -closure(move({1,2,4,5,6,7,10}, a))} = {1,2,3,4,6,7,8} = B Define M(E, a) = B b : -closure(move(E, b)) = -closure(move({1,2,4,5,6,7,10}, b))} = {1,2,4,5,6,7} = C Define M(E, b) = C

60 Conversion Example (4) This gives the transition table for the DFA M of: State Input Symbol a b A B C B B D C B C E B C D B E b …b C b b a … A a …a B b …abD b …abbE start a a a

61 Converting NFA-e to DFA – 2nd Example
8 5 4 7 3 6 2 1 b a c RE: c*(ab)*c* 1st we calculate: -closure(0) -closure(0) = {0, 1, 2, 6, 8} (all states reachable from 0 on -moves) Let A={0, 1, 2, 6, 8} be a state of new DFA M

62 adds {3} (since move(2, a) = 3) adds {7} (since move(6, c) = 7)
Conversion Example (1) 2nd , we calculate : a : -closure(move(A, a)) and b : -closure(move(A, b)) c : -closure(move(A, c)) a : -closure(move(A, a)) = -closure(move({0,1,2,6,8}, a))} adds {3} (since move(2, a) = 3) From this we have : -closure({3}) = {3} (since 33 by -moves) Let B = {3} be a new state. Define M(A, a) = B b : -closure(move(A,b)) = -closure(move({0,1,2,6,8}, b)) There is NO transition on b for all states 0,1,2,6 and 8 Define M(A, b) = Reject c : -closure(move(A, c)) = -closure(move({0,1,2,6,8}, c)) adds {7} (since move(6, c) = 7) From this we have : -closure({7}) = {1,2,5,6,7,8} (since 758, 7512, and 7516 by -moves) Let C = {1,2,5,6,7,8} be a new state. Define M(A, c) = C

63 adds {4} (since move(3, b) = 4)
Conversion Example (2) 3rd , we calculate : a : -closure(move(B, a)) and b : -closure(move(B, b)) c : -closure(move(B, c)) a : -closure(move(B, a)) = -cclosure(move({3}, a))} There is NO transition on a for state 3 Define M(B, a) = Reject b : -closure(move(B, b)) = -closure(move({3}, b)) adds {4} (since move(3, b) = 4) From this we have : -closure({4}) = {1,2,4,5,6,8} (since 458, 4512, and 4516 by -moves) Let D = {1,2,4,5,6,8} be a new state. Define M(B, b) = D c : -closure(move(B, c)) = -closure(move({3}, c)) There is NO transition on c for state 3 Define M(B, c) = Reject

64 adds {3} (since move(2, a) = 3) adds {7} (since move(6, c) = 7)
Conversion Example (3) 4th , we calculate : a : -closure(move(C, a)) and b : -closure(move(C, b)) c : -closure(move(C, c)) a : -cclosure(move(C, a)) = -closure(move({1,2,5,6,7,8},a))} adds {3} (since move(2, a) = 3) -closure({3}) = {3} Define M(C, a) = B b : -closure(move(C, b)) = -closure(move({1,2,5,6,7,8},b)) There is NO transition on b for all states 1,2,5,6, and 7 Define M(C, b) = Reject c : -closure(move(C, c)) = -closure(move({1,2,5,6,7,8},c)) adds {7} (since move(6, c) = 7) From this we have : -closure({7}) = {1,2,5,6,7,8} (since 758, 7512, and 7516 by -moves) Define M(C, c) = C

65 adds {3} (since move(2, a) = 3) adds {7} (since move(6, c) = 7)
Conversion Example (4) 5th , we calculate : a : -closure(move(D, a)) and b : -closure(move(D, b)) c : -closure(move(D, c)) a : -closure(move(D, a)) = -closure(move({1,2,4,5,6,8},a))} adds {3} (since move(2, a) = 3) -closure({3}) = {3} Define M(D, a) = B b : -closure(move(D, b)) = -closure(move({1,2,4,5,6,8}, b)) There is NO transition on b for all states 1,2,4,5,6, and 8 Define M(D, b) = Reject c : -closure(move(D, c)) = -closure(move({1,2,4,5,6,8},c)) adds {7} (since move(6, c) = 7) From this we have : -closure({7}) = {1,2,5,6,7,8} (since 758, 7512, and 7516 by -moves) Define M(D, c) = C

66 The Resulting DFA M Which States are FINAL States ? 1, 2, 5, 6, 7, 8
1, 2, 4, 5, 6, 8 0, 1, 2, 6, 8 3 c b a Which States are FINAL States ? D C A B c b a

67 strings over {a,b} with next-to-last symbol = a
NFA-e  DFA: Example strings over {a,b} with next-to-last symbol = a a/b d a b e q0 {q0, q1} {q0} q1 {q2} q2 a a/b q0 q1 q2 M = ({q0, q1, q2}, {a,b}, d, q0, {q2}) b q0 q1 q0 a a a a b b q0 q2 b q0 q1 q2 Equivalent DFA

68 An Example of NFA-e  DFA
Consider a simple NFA: Construct a corresponding DFA: 1 Start q0 q1 {q0} {q0, q1} {q1} ERR Start 1 1,0

69 Examples See examples 2.11, 2.12, and 2.13

70 Class Discussion Construct a DFA equivalent to this NFA: 0,1 1 Start 1
q2 1 {q0, q1} Start 1 {q0} {q0,q2}

71 Class Discussion {0,1,2} {2} a {1,2} b c Converted DFA start b,c b
start b,c b a,b,c An NFA a 1 2 b,c c start Notice that the state with label {0, 1, 2} is from the set of states given by the nondeterministic transition (0, a) = {0, 1, 2}. Also notice that any state whose label contains an accepting state is defined as an accepting state in the deterministic machine.

72 2.4 Reduction of the Number of States in FA
Self study but not included in the exams material


Download ppt "Chapter 2 Finite Automata"

Similar presentations


Ads by Google