720 likes | 754 Views
Chapter 2 Finite Automata. A Finite-State Machine (1). A finite state machine is a mathematical model of a system, with discrete inputs and outputs. 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
E N D
A Finite-State Machine (1) • A finite state machine is a mathematical model of a system, with discrete inputs and outputs
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
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
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) = q1d(q0, b) = q0 • d(q1, a) = q1d(q1, b) = q0
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.
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) = q0d(q0, b) = q1 • d(q1, a) = q0d(q1, b) = q2 • d(q2, a) = q2d(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
Deterministic Finite Automata (6) • Extended transition functiond* 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, aS, v S*
Deterministic Finite Automata (7) • A string w is accepted if d*(q0, w) F • The language of a DFA M is • L(M) = {wS* | d*(q0, w) F} • DFA M = (Q, S, d, q0, F) accepts wS* • d*(q0, w) F
Deterministic Finite Automata (8) • Two possibilities for DFA M running on w • M accepts w • M accepts wiff the computation of M on w ends up (halts) in an accepting configuration • d*(q0, w) F • M rejects w • M rejects wiff the computation of M on w ends up (halts) in a rejecting configuration • d*(q0, w) F
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 aS, there is exactly one arc labeled a leaving qi a q0
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
State Diagrams and Examples (1) b b b S 0 a a 1 a a 2 a a 3+ a strings over {a,b} with at least 3 a’s
State Diagrams and Examples (2) S 0 1 2 S S strings over {a,b} with length mod 3 = 0
State Diagrams and Examples (3) b S b 0 a a 1 a a 2 a a Has aaa b strings over {a,b} without 3 consecutive a’s A simple example of “strings not of the form …”.
State Diagrams and Examples (4) a a, b q0 b q1 b q2 a • 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
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
State Diagrams and Examples (6) • The language {anbn, n0} 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
State Diagrams and Examples (7) • strings over {a, b} that contain the substring bbORdo 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
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
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
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 12302 and 0130 should be accepted and 0111 rejected by M 0 0 w mod 4 = 0 w mod 4 = 1 1 3 1 3 1 3 2 2 2 2 w mod 4 = 2 1 w mod 4 = 3 0 3 0
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
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
State Diagrams and Examples (13) a b …aa …ab b a a a e b a b a b b …ba …bb a a b b strings over {a,b} with next-to-last symbol = a
State Diagrams and Examples (14) b a start a b b 3 0 2 1 a b a What Language is Accepted?
2.2: Nondeterministic Finite Automata (1) Finite Automata : A recognizer that takes an input string & determines whether it’s a valid sentence of the language Non-Deterministic : Deterministic : Has more than one (or no) alternative action for the same input symbol. Has at most one action for every given input symbol.
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
Nondeterministic Finite Automata (3) a start a b b 0 2 1 3 b 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 { 0, 1 } { 0 } 1 { 2 } 2 { 3 }
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) = {} =
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?
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
Nondeterministic Finite Automata (7) a start a b b 0 2 1 3 b • 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)
Nondeterministic Finite Automata (8) • Extended transition functiond* 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), qjd(qi, a) • qi Q, qjQ , aS, vS* • The language of a NFA M is • L(M) = {wS* | d*(q0, w) F ≠ }
NFA (9) 1 0 0 1 M1 start b b 2 a a, b a, b start b b M2 2 • The state diagram DFA M1 and NFA M2 accepts (a|b)*bb(a|b)* a, b a
NFA (10) 0 1 3 • 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 a 2 b a, b b 4
NFA (11) 3 3 2 1 0 2 0 1 • The state diagram DFA M1 and NFA M2 accepts (a|b)*abba(a|b)* a a, b b M1 start a b b a 4 a b a, b a, b M2 start a b b a 4
NFA with e-Transitions (1) j i transitions Switch state but do not use any input symbol 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
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
e-Transitions (3) 0 • 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
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
e-Transitions (5) • M accepts all strings over {a, b} of even length e q0 a, b q1 a, b q2 e
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)
e-Transitions (7) • L(M) = L(M1) L(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}
e-Transitions (8) • L(M) = L(M1)L(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
e-Transitions (9) • L(M) = L(M1)* q0 q1,0 qf M1 q1,f 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
e-Transitions (10) Given the regular expression : a(b*c) | a( b | c+ | e) Find a transition diagram NFA- that recognizes it.
e-Transitions (11) b a c 1 2 3 6 a b 4 5 c c 7 a (b*c) a( b | c+ | e) Now that you have the individual diagrams, “or” them as follows:
e-Transitions (12) b a c 1 0 2 3 6 a b 4 5 c c 7
e-Transitions (12) • See examples 2.7, 2.8, 2.9, and 2.10