Nondeterministic Finite Automata CS 130 Theory of Computation

  • Slides: 23
Download presentation
Nondeterministic Finite Automata CS 130: Theory of Computation HMU textbook, Chapter 2 (Sec 2.

Nondeterministic Finite Automata CS 130: Theory of Computation HMU textbook, Chapter 2 (Sec 2. 3 & 2. 5)

NFAs: Nondeterministic Finite Automata n Same as a DFA, except: n n n On

NFAs: Nondeterministic Finite Automata n Same as a DFA, except: n n n On input a, state q may have more than one transition out, implying the possibility of multiple choices when processing an input symbol On input a, state q may have no transition out, implying the possibility of “being stuck” A string w is acceptable as long as there exists an admissible state sequence for w

NFAs n A nondeterministic finite automaton M is a five-tuple M = (Q, ,

NFAs n A nondeterministic finite automaton M is a five-tuple M = (Q, , , q 0, F), where: n n n Q is a finite set of states of M is the finite input alphabet of M : Q power set of Q, is the state transition function mapping a state-symbol pair to a subset of Q q 0 is the start state of M F Q is the set of accepting states or final states of M

Example NFA n NFA that recognizes the language of strings that end in 01

Example NFA n NFA that recognizes the language of strings that end in 01 0, 1 q 0 0 q 1 1 note: (q 0, 0) = {q 0, q 1} (q 1, 0) = {} q 2 Exercise: draw the complete transition table for this NFA

 ^ definition for an NFA n n n ^: Q X * power

^ definition for an NFA n n n ^: Q X * power set of Q ^(q, ) = {q} ^(q, w), w = xa (where x is a string and a is a symbol) is defined as follows: n n Let ^(q, x) = {p 1, p 2, …pk} Then, ^(q, w) = (pi, a)

Language recognized by an NFA n A string w is accepted by an NFA

Language recognized by an NFA n A string w is accepted by an NFA M if ^(q 0, w) F is non-empty n n n Note that ^(q 0, w) represents a subset of states since the automaton is nondeterministic Equivalent definition: there exists an admissible state sequence for w in M The language L(M) recognized by an NFA is the set of strings accepted by M n L(M) ={ w | ^(q 0, w) F is non-empty }

Converting NFAs to DFAs n Given a NFA, M = (Q, , , q

Converting NFAs to DFAs n Given a NFA, M = (Q, , , q 0, F), build a DFA, M’ = (Q’, , ’, {q 0}, F’) as follows. n n n Q’ contains all subsets S of states in Q. The initial state of M’ is the set containing q 0 F’ is the set of all subsets of Q that contain at least one element in F (equivalently, the subset contains at least one final state)

Converting NFAs to DFAs n ’ is determined by putting together, for each state

Converting NFAs to DFAs n ’ is determined by putting together, for each state in the subset and each symbol, all states that may result from a transition: ’(S, a) = (q, a) q S n May remove “unreachable” states in Q’

Example conversion n NFA 0, 1 0 q 0 n 1 q 2 DFA

Example conversion n NFA 0, 1 0 q 0 n 1 q 2 DFA 1 {q 0 } 0 0 {q 0, q 1} 1 {q 0, q 2} 0 1

NFA with -transitions n NFA that allows the transition of an empty string from

NFA with -transitions n NFA that allows the transition of an empty string from a state n n Jumping to a state is possible even without input Revision on NFA definition simply allows the “symbol” for

NFA with -transitions n A nondeterministic finite automaton with transitions (or -NFA) is a

NFA with -transitions n A nondeterministic finite automaton with transitions (or -NFA) is a five-tuple M = (Q, , , q 0, F), where: n n n Q is a finite set of states of M is the finite input alphabet of M : Q ( + ) power set of Q, is the state transition function mapping a state-symbol pair to a subset of Q q 0 is the start state of M F Q is the set of accepting states or final states of M

Converting -NFAs to NFAs n n n Task: Given an -NFA M = (Q,

Converting -NFAs to NFAs n n n Task: Given an -NFA M = (Q, , , q 0, F), build a NFA M’ = (Q, , ’, q 0, F’) Need to eliminate -transitions Need epsilon closure concept Add transitions to enable transitions previously allowed by the -transitions Note: the conversion process in the textbook instead builds a DFA from an -NFA n The conversion described in these slides is simpler

Epsilon closure n n n In an NFA M, let q Q ECLOSE(q) represents

Epsilon closure n n n In an NFA M, let q Q ECLOSE(q) represents all states r that can be reached from q using only -transitions Recursive definition for ECLOSE n n n If (q, ) is empty, ECLOSE(q) = {q} Else, Let (q, ) = {r 1, r 2, …, rn}. ECLOSE(q) = ECLOSE(ri) {q} Note: check out constructive definition of ECLOSE in the textbook

Additional transitions n n n NFA M’ = (Q, , ’, q 0, F’)

Additional transitions n n n NFA M’ = (Q, , ’, q 0, F’) such that ’ is described as follows Suppose ECLOSE(q) = {r 1, r 2, …, rn }. For each transition from state ri to state sj on (non-epsilon) symbol a, add a transition from q to sj on symbol a (For each transition from state sj to state q on (non-epsilon) symbol a, add a transition from sj to rj on symbol a, for each rj) ’ = minus the epsilon transitions plus the additional transitions mentioned above

Final states n n NFA M’ = (Q, , ’, q 0, F’) such

Final states n n NFA M’ = (Q, , ’, q 0, F’) such that F’ is described as follows F’ = F plus all states q such that ECLOSE(q 0) contains a state in F

Equivalence of Finite Automata n n n Conversion processes between DFAs, NFAs, and -NFAs

Equivalence of Finite Automata n n n Conversion processes between DFAs, NFAs, and -NFAs show that no additional expressive capacity (except convenience) is introduced by nondeterminism or -transitions All models represent regular languages Note: possible exponential explosion of states when converting from NFA to DFA

Closure of Regular Languages under certain operations n n n Union Complementation Intersection Concatenation

Closure of Regular Languages under certain operations n n n Union Complementation Intersection Concatenation L 1 L 2 L 1 L 2 Goal: ensure a FA can be produced from the FAs of the “operand” languages

Union n n Given that L 1 and L 2 are regular, then there

Union n n Given that L 1 and L 2 are regular, then there exists FAs M 1 = (Q 1, 1, q 10, F 1) and M 2 = (Q 2, 2, q 20, F 2) that recognize L 1 and L 2 respectively Let L 3 = L 1 L 2. Define M 3 as follows: M 3 = ({q 30} Q 1 Q 2, 1 2, 3, q 30, F 1 F 2) n n where 3 is just 1 2 plus the following epsilon transitions: q 30 q 10 and q 30 q 20 M 3 recognizes L 3

Complementation n Given that L 1 is regular, then there exists DFA M 1

Complementation n Given that L 1 is regular, then there exists DFA M 1 = (Q, , , q 0, F) that recognizes L 1 Define M 2 as follows: M 2 = (Q, , , q 0, Q - F) M 2 recognizes L 1 n n Strings that used to be accepted are not, strings not accepted are now accepted Note: it is important that M 1 is a DFA; starting with an NFA poses problems. Why?

Intersection n By De Morgan’s Law, L 1 L 2 = ( L 1

Intersection n By De Morgan’s Law, L 1 L 2 = ( L 1 L 2 ) n Applications of the constructions provided for and complementation provide a construction for

Concatenation n n Given that L 1 and L 2 are regular, then there

Concatenation n n Given that L 1 and L 2 are regular, then there exists FAs M 1 = (Q 1, 1, q 10, F 1) and M 2 = (Q 2, 2, q 20, F 2) that recognize L 1 and L 2 respectively Let L 3 = L 1 L 2. Define M 3 as follows: M 3 = (Q 1 Q 2, 1 2, 3, q 10, F 2) n n where 3 is just 1 2 plus the following epsilon transitions: q 1 i q 20 for all q 1 i in F 1 M 3 recognizes L 3

Finite Automata with Output n Moore Machines n n Mealy Machines n n Output

Finite Automata with Output n Moore Machines n n Mealy Machines n n Output symbol for each state encountered Output symbol for each transition encountered Exercise: formally define Moore and Mealy machines

Next: Regular Expressions n n Defines languages in terms of symbols and operations Example

Next: Regular Expressions n n Defines languages in terms of symbols and operations Example n n (01)* + (10)* defines all even-length strings of alternating 0 s and 1 s Regular expressions also model regular languages and we will demonstrate equivalence with finite automata