160 likes | 266 Views
≡. Replacing magic with clockwork. Converting non-deterministic automata (NFA) into (equivalent) deterministic automata (DFA). Bad news: Magic doesn’t do “anything more” than what clockwork can do!. This was one of the main conclusions of the inventors of “magical YES-NO machines” (NFAs).
E N D
≡ Converting NFAs to DFAs
Replacing magic with clockwork Converting non-deterministic automata (NFA) into (equivalent) deterministic automata (DFA)
Bad news: Magic doesn’t do “anything more” than what clockwork can do! This was one of the main conclusions of the inventors of “magical YES-NO machines” (NFAs). Converting NFAs to DFAs
Bad news: Magic doesn’t do “anything more” than what clockwork can do! What does “doesn’t do anything more” mean? It simply means that magical YES-NO machines (NFAs) do NOT recognize languages (“solve” problems) which ordinary, clock-work YES-NO machines (DFAs) cannot. This follows from the fact that anything done by a magical machine can be simulated with an ordinary, clockwork machine. What does this amount to? Magical machines are often more elegant (with a fewer number of states) than their clockwork counterparts and are a lot easier to “design” (on paper). Yet their “power” (ability to recognize languages) remains the same. Here is an (almost) analogous situation: Consider two native speakers of English—A and B—A having a much better command of the language (has a better vocabulary, fluency, etc.) than B. While it will be more pleasurable listening to A’s graceful delivery, B also can, theoretically speaking, convey whatever “message” which A could convey. B might just take a longer time, use less arresting words, and perhaps stutter a bit! B, like a DFA, can be said to have the same “power of communication” as A. Converting NFAs to DFAs
If, according to the transition diagram, the machine could go “nowhere”, we will indicate this by moving the pointer to ‘0’ (just for the sake of illustration). 1 001 b 0 1 2 3 0 1 2 3 2 a b a b a b b a a b b a 1 a 3 {2, 3} 023 0 1 2 3 0 1 2 3 a a b b a a b b a Simulating an NFA with a DFA ≡ ≡ 023 001 Converting NFAs to DFAs
0 1 2 3 0 1 2 3 a b b a a b b a b 0 1 2 3 0 1 2 3 b 012 a b b a a b b a Simulating an NFA with a DFA {1, 2} 012 b ≡ 2 a b a b 1 a 3 {1, 2} 012 ≡ 023 a Converting NFAs to DFAs 001
0 1 2 3 0 1 2 3 a a a b b a b b 003 a Caution! This is NOT the DFA-equivalent of the NFA shown; this just shows the evolution of states (of the NFA-simulator) for one specific input string: “abba” 002 123 000 013 Simulating an NFA with a DFA {2, 3} 023 ≡ b 2 a b a b 1 a Those states of the simulator where one of its “arms” rests on an NFA-final-state are the final states of the (deterministic) simulator. Clearly, (only) when the allowable current states of the NFA include a final state, one of the “arms” of the simulator will be seen in a final state. Thus, if the NFA finally accepts a string w, so will its simulator. 3 b b 012 023 a 001 Converting NFAs to DFAs
012 000 001 002 003 012 013 023 123 012 0 1 2 3 a a b b a b b a Simulating an NFA with a DFA ≡ Converting NFAs to DFAs
Converting an NFA into a DFA • Algorithm NFA_to_DFA (version I) • Input: NFA N = (Q, ∑, δ, NFA_start, F ) • Output: DFA D = (Q’, ∑, δ’, DFA_start, F’ ) • Recall that a single DFA state is a “collection” of zero or more NFA states.The start state of the DFA DFA_start is the set { NFA_start }. (At this stage, this is the only state which the DFA has; newer states will be added to the DFA as we proceed. Whenever a new state DFA_state gets added, we will immediately compute δ’(DFA_state, a) for every aε ∑ using a simple method to be described; then we will attach the tag “I’m done” to DFA_state.) • Pick a state DFA_state from the currently available DFA states (call this list L; initially, L only has NFA_start) that doesn’t have the tag, “I’m done”. • Compute δ’(DFA_state, a) for every a ε ∑ as follows: • (Remember, DFA_state is itself a set, i.e. a collection of NFA states) • Find the set of states reached by the NFA N through a single a-transition from each individual state contained in DFA_stateand “combine” them all into a single set which represents δ’(DFA_state, a) (possibly, a new DFA state). Repeat step(2) for every DFA_state in L that doesn’t have the tag Converting NFAs to DFAs
Converting an NFA into a DFA i.e., δ’ (DFA_state, a) = UxεDFA_stateδ(x, a) (U represents the “union” operation) Attach the tag “I’m done” to DFA_state. Add δ’(DFA_state, a) to L if it’s not already in L. 3. List L now contains the set of DFA states; copy the contents of L to Q’. 4. Mark as “final” those DFA states (recall again that a single DFA state is a collection of NFA states) that contain an accept-state of NFA N (as its member). i.e., F ’ = { DFA_stateεQ’ | DFA_state contains a final state of N } Converting NFAs to DFAs
Converting an NFA into a DFA • Algorithm NFA_to_DFA (version II) • Input: NFA N = (Q, ∑, δ, NFA_start, F ) • Output: DFA D = (Q’, ∑, δ’, DFA_start, F’ ) • Recall that a single DFA state is a “collection” of zero or more NFA states.The start state of the DFA DFA_start is the set E( {NFA_start} ). (At this stage, this is the only state which the DFA has; newer states will be added to the DFA as we proceed. Whenever a new state DFA_state gets added, we will immediately compute δ’(DFA_state, a) for every aε ∑ using a simple method to be described; then we will attach the tag “I’m done” to DFA_state.) • Pick a state DFA_state from the currently available DFA states (call this list L ) that doesn’t have the tag, “I’m done”. • Compute δ’(DFA_state, a) for every a ε ∑ as follows: • (Remember, DFA_state is itself a set, i.e. a collection of NFA states) • Find the set of states reached by the NFA N through a single a-transition from each individual state contained in DFA_stateand combine them all into a single set; the ε-closure of such a set represents δ’(DFA_state, a) (possibly, a new DFA state). Repeat step(2) for every DFA_state in L that doesn’t have the tag Converting NFAs to DFAs
Converting an NFA into a DFA i.e., δ’ (DFA_state, a) = E (UxεDFA_stateδ(x, a)) (U represents the “union” operation) Attach the tag “I’m done” to DFA_state. Add δ’(DFA_state, a) to L if it’s not already in L (we’ve updated L now). 3. The list L now contains the DFA states; copy the contents of L to Q’. 4. Mark as “final” those DFA states (recall again that a single DFA state is a collection of NFA states) that contain an accept-state of NFA N (as its member). i.e., F ’ = { DFA_stateεQ’ | DFA_state contains an accept-state of N } The ε–closure: E (DFA_state) = { q | q can be reached from DFA_state by travelling along 0 or more ε -arrows } Converting NFAs to DFAs
3 b a a a 2 b { 1, 2, 3 } a a 1 { 1, 2 } a b b Equivalent DFA: { 1, 3 } b { 1 } a b Example I NFA recognizing L = { wє {a,b}* : w contains an ‘a’ in its last-but-one position } Converting NFAs to DFAs
0 1 0 ε C Example II NFA for L = { 0m1n0k : n, m >= 0 and k >= 1 } 0 B A Converting NFAs to DFAs
Example II (contd.) Computing the states of the equivalent DFA DFA_start = E ( {A} ) = {A, B} (state I) δ(I, 0) = E ( {A, C} ) = {A, B, C} (state II) δ(I, 1) = E ( {B} ) = {B} (state III) δ(II, 0) = E ( {A, C} ) = {A, B, C} (state II) δ(II, 1) = E ( {B} ) = {B} (state III) δ(III, 0) = E ( {C} ) = {C} (state IV) - marked states are the final states δ(III, 1) = E ( {B} ) = {B} (state III) δ(IV, 0) = E ( {C} ) = {C} (state IV) δ(IV, 1) = E ( { } ) = { } (state V) δ(V, 0) = E ( { } ) = { } (state V) δ(V, 1) = E ( { } ) = { } (state V) Converting NFAs to DFAs
0 0 0 Example II (contd.) II {A,B,C} Equivalent DFA: 0 I 1 {A,B} III IV {B} 0 {C} 1 { } V 1 1 1 Converting NFAs to DFAs