210 likes | 285 Views
Nondeterministic Finite Automata. Section 2.2 Wed, Sep 22, 2004. A Tricky DFA. Consider a DFA A 1 that accepts any string containing 010. Consider a second DFA A 2 that accepts any string containing 101. How would we design a DFA that accepts any string containing both 010 and 101?
E N D
Nondeterministic Finite Automata Section 2.2 Wed, Sep 22, 2004
A Tricky DFA • Consider a DFA A1 that accepts any string containing 010. • Consider a second DFA A2 that accepts any string containing 101. • How would we design a DFA that accepts any string containing both 010 and 101? • The occurrences might be separate (…010…101…) or combined (…0101…).
Nondeterminism • Nondeterminism means that the next move is not uniquely determined by the current state and the current input symbol. • In some cases, there is more than one possible transition to follow. • The automaton will follow the “right” one, i.e., the one that leads to acceptance, if there is one.
A Nondeterministic Solution to the Tricky Automaton • By using nondeterminism, we may set up transitions from the initial state on • ‘0’ if it is the beginning of 010, but not 0101. • ‘0’ if it is the beginning of 0101. • ‘1’ if it is the beginning of 101, but not 1010. • ‘1’ if it is the beginning of 1010. • Otherwise, eat symbols.
0, 1 1 0 1 0 1 0 1 0 0 1 0, 1 0, 1 1 0 0 1 1 0 0 1 0 1 0, 1 A Nondeterministic Solution to the Tricky Automaton
Nondeterministic Finite Automata • A nondeterministic finite automaton (NFA) is a quintuple (K, , , s, F) where • K is a finite set of states. • is a finite input alphabet. • sK is the initial state. • FK is the set of final states. • is the transition relation from K ( {e}) to K.
DFA vs. NFA • The difference between a DFA and an NFA is that the function has been replaced by the relation. • This allows • Transitions such as (p, a, q1) and (p, a, q2) together, where q1q2. • Transitions such as (p, e, q), called “e-moves”. • Without reading a symbol, i.e., “reading” e, move from p to q.
Acceptance by an NFA • An NFA accepts a string w if there exists a sequence of transitions from the initial state to a final state that processes w. • Note that there may also exist sequences that lead to non-final states. • The language of the NFA is the set of all strings that it accepts.
Examples of NFAs • Design an NFA that accepts all strings containing 001100. • Design an NFA that accepts the strings 32, 33, …, 99, 100, …, 758. • Design an NFA that accepts all strings that either contain 010 or do not contain 0.
1 0, 1 0 0 1 0 1 0 1 Example: A DFA • A DFA that accepts all strings that contain 010 or do not contain 0.
Converting a Relation into a Function • It is possible to rewrite a relationR from A to B as a functionf from 2A to 2B. • Define a function f : 2A2B as follows: • Given S2A, define f(S) = {bB | (a, b) R for some aS}. • In other words, f maps each subset S of A to everything in B that is related to some element in S.
Example: Converting a Relation into a Function • Let A = {2, 3, 4, 5} and B = {6, 7, 8, 9}. • Define the relation R from A to B as (a, b) R if “a divides b.” • Then R = {(2, 6), (2, 8), (3, 6), (3, 9), (4, 8)}.
Example of Converting a Relation to a Function • The function f : 2A 2B is • f() = • f({2}) = {6, 8} • f({3}) = {6, 9} • f({4}) = {8} • f({5}) = • f({2, 3}) = f({2, 3, 4, 5}) = {6, 8, 9} • f({2, 4}) = f({2, 4, 5}) = {6, 8} • f({2, 5}) = {6, 8} • f({3, 4}) = f({2, 3, 4, 5}) = {6, 8, 9}
Example of Converting a Relation to a Function • f({3, 5}) = {6, 9} • f({4, 5}) = {8} • f({2, 3, 4}) = {6, 8, 9} • f({2, 3, 5}) = {6, 8, 9} • f({2, 4, 5}) = {6, 8} • f({3, 4, 5}) = {6, 8, 9} • f({2, 3, 4, 5}) = {6, 8, 9}
Examples of Converting an NFA to a DFA • Use this idea to rewrite the relation of the NFA for all strings that contain 001100 as the function of an equivalent DFA.
Equivalence of NFAs and DFAs • Theorem: For each NFA M there exists a DFA M' that is equivalent to M. • Outline of the proof: • Convert the relation to the function . • Then form the “e-closure” of this set. • Let q be any state in M. • Define the e-closure of q to be all the states reachable from q by using only e-moves. E(q) = {p K | (q, e) M* (p, e)}.
Proof, continued • Define '(S) = E((S)). • Define F' = {S 2K | S contains a final state}. • Verify that (2K, Σ, δ', E(s), F'}) is a DFA that accepts the same language as M.
1 e 1 0 1 0 e e Converting an NFA to a DFA • Convert the following NFA to an equivalent DFA.
Examples of Converting an NFA to a DFA • Design an NFA and a DFA for all strings that contain either 101 or 010. • Redesign the NFA for all strings that either contain 101 or do not contain 0 as a DFA. • Redesign the NFA that accepts the strings 32, 33, …, 99, 100, 101, …, 758 as a DFA.