880 likes | 1.32k 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 Before formally defining finite state systems let us consider an example. An Example.
E N D
A Finite-State Machine (1) • A finite state machine is a mathematical model of a system, with discrete inputs and outputs • Before formally defining finite state systems let us consider an example
An Example • A man with a wolf, goat, and cabbage is on the left bank of a river • There is a boat large enough to carry the man and only one of the other three • The man and his staff wish to cross to the right bank. • The wolf will eat the goat and the goat will eat the cabbage when the man is not watching them • Is it possible to cross the river without the goat or cabbage being eaten?
States -MWGC • Notation • M-man W-wolf • G-goat C-cabbage • States are labeled by “-” pairs, e.g. • The man and the goat are on the left bank • The wolf and the cabbage are on the right bank • denotes nothing is left on the bank • Start state: • Final state: only one final state in this e.g. MG-WC MWGC-
Inputs • The inputs to the system are the actions the man takes • m - the man crosses alone • w - the man crosses with the wolf • g - the man crosses with goat • c - the man crosses with the cabbage m MG-WC G-MWC m
The Transition Diagram g m MWGC- WC-MG MWC-G g m c w w c C-MWG W-MGC Start g g g g MGC-W MWG-C c c w w g m -MWGC MG-WC G-MWC g m
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. • The current status of DFA is called configuration.
Deterministic Finite Automata (4) • A configuration is represented by [qi, w], where • qi is the current state, and • wS*, is the unprocessed string • The notation [qi, aw]├[qj, w] indicates that configuration [qj, w] is obtained from [qi, aw] • The symbol ├ read “yields”, defines a function from Q S+ to Q S* by • [qi, aw]├[d(qi, a), w], for aS and wS*, • where d is the transition function of DFA. • The notation [qi, u]├*[qj, v] indicates that configuration [qj, v] can be obtained from [qi, u] by zero or more transitions.
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 • [q0, abba] ├ [q0, bba] ├ [q1, ba] ├ [q2, a] ├ [q2, e] • e or is called empty string. • Is abab L(M)? No, since the computation halts in state q1, which is NOT a final state • [q0, abab] ├ [q0, bab] ├ [q1, ab] ├ [q0, b] ├ [q1, e]
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 • |w| = 1. Then w = a for some a S,and d*(qi, a) = d(qi, a) • Recursive step: Let |w| 1. Then d*(qi, aw) = d*(d(qi, a), w) • qi Q, aS, wS*
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 w iff the computation of M on w ends up (halts) in an accepting configuration • [q0, w]├* [q, e] where q F (d*(q0, w) F) • M rejects w • M rejects w iff the computation of M on w ends up (halts) in a rejecting configuration • [q0, w] ├* [q, e] where q F (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 (3) 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 (4) • 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 (5) • 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 (6) • 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 (7) • 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 (8) • 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 (9) • 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 (10) • 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 (11) • 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 a a a q0 q1 q2 q3 b b b q4 q5 q6 b b
State Diagrams and Examples (12) 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 (13) S 0 1 2 S S strings over {a,b} with length mod 3 = 0
State Diagrams and Examples (14) 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 (15) 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 (16) b a start a b b 3 0 2 1 a b a What Language is Accepted?
State Diagrams and Examples (17) • FSA M = (Q, S, d, q0 , F) • Q = {1, 2, 3} • S = {a, b} • q0 = 1 • F = {2,3} • d(1, a) = 1 d(1, b) = 2 • d(2, a) = 2 d(2, b) = 3 • d(3, a) = 3 d(3, b) = 1 • Draw this DFA as a transition diagram
State Diagrams and Examples (18) a a b 2 1 b b 3 a
State Diagrams and Examples (19) • See examples 2.1, 2.2, 2.3, 2.4, 2.5, and 2.6
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 alternative action for the same input symbol. Has at most one action for a given input symbol. Both types are used to recognize regular expressions.
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 • Configurations: same as they are for DFA’s • Computations are different • Initial configuration is identical • However, there may be several next configurations or there may be none. • Definition of acceptance must be modified
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 [0, ababb] ├[1, babb] ├ [2, abb] since (2, a) undefined, then reject -OR- [0, ababb] ├[0, babb] ├ [0, abb] ├ [1, bb] ├ [2, b] ├ [3, ] Since NFA halts in an accepting (final) state, thenaccept
Nondeterministic Finite Automata (8) a, b 0 1 start b b 2 i n p u t a b state 0 { 0 } { 0, 1 } 1 { 2 } 2 Q = { 0, 1, 2 } q0 = 0 F = { 2 } = { a, b } [0, ababb] ├ [0, babb] ├ [1, abb] REJECT (did NOT halt) [0, ababb] ├ [0, babb] ├ [0, abb] ├ [0, bb] ├ [1, b] ├ [2, e] ACCEPT (halts in an accepting sate) [0, ababb] ├ [0, babb] ├ [0, abb] ├ [0, bb] ├ [0, b] ├ [0, e] REJECT (halts in a rejecting sate)
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 3 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