Handout 11.docx

7
Tahir Naseem/Handout 1 -1- Theory of Automata and Formal Languages Lecture 11 Objectives Language of FSA Non-determinism o Nondeterministic Automaton Formal definition Meaning of NFA Choosing where to go The ability to make choices How to run an NFA Examples Language of NFA ε- transitions DFA to NFA The transition function of a DFA takes a state and an element of the alphabet and returns a state, but the transition function of an NFA takes a state and either an element of the alphabet or nothing, and returns an element of the power set of the states of the NFA. As a on sequence of the above, there is usually more than one way to read in a string with an NFA. The rules for acceptance and rejection of strings are the same for an NFA as they are for a DFA: if the final state is an accept state, the string is accepted; otherwise, it is rejected. If the NFA is in a state that does not define a transition for the next element of the string (and defines no empty transition), the string is rejected. While it would initially seem that an NFA is more capable than a DFA, due to its extra 'features,' that turns out to not be the case. A DFA can be constructed that is equivalent to any NFA. The procedure is as follows: 1. The list of states in the DFA will consist of one state for every element of the powerset of the NFA's states. For example, if an NFA has states A, B, and C, the DFA will have states A, B, C, AB, AC, BC, ABC, and an empty state. 2. The alphabet for the DFA remains the same as it was for the NFA. 3. The initial state of the DFA is the one with the same

Transcript of Handout 11.docx

Page 1: Handout 11.docx

Tahir Naseem/Handout 1 -1- Theory of Automata and Formal Languages

Lecture 11

Objectives

Language of FSA Non-determinism

o Nondeterministic Automaton Formal definition Meaning of NFA Choosing where to go The ability to make choices How to run an NFA Examples Language of NFA ε- transitions

DFA to NFA

The transition function of a DFA takes a state and an element of the alphabet and returns a state, but the transition function of an NFA takes a state and either an element of the alphabet or nothing, and returns an element of the power set of the states of the NFA. As a on sequence of the above, there is usually more than one way to read in a string with an NFA. The rules for acceptance and rejection of strings are the same for an NFA as they are for a DFA: if the final state is an accept state, the string is accepted; otherwise, it is rejected. If the NFA is in a state that does not define a transition for the next element of the string (and defines no empty transition), the string is rejected.

While it would initially seem that an NFA is more capable than a DFA, due to its extra 'features,' that turns out to not be the case. A DFA can be constructed that is equivalent to any NFA. The procedure is as follows:

1. The list of states in the DFA will consist of one state for every element of the powerset of the NFA's states. For example, if an NFA has states A, B, and C, the DFA will have states A, B, C, AB, AC, BC, ABC, and an empty state.

2. The alphabet for the DFA remains the same as it was for the NFA.

3. The initial state of the DFA is the one with the same name as the initial state of the NFA.

4. The transition function is constructed in a manner that almost has to be seen to be understood, but is basically as follows. Starting at the initial state, find which combination of states each element of the alphabet results in; the concatenation of these states is the state you go to in the DFA. For example, if reading a 0 from state A allows you to go to either state B or C in the NFA, then in the DFA reading a 0 from state A will lead to state BC. Repeat the above procedure for each resulting state until all possible transitions are covered. It will often turn out that not all combinations of NFA states are actually reachable,

Page 2: Handout 11.docx

Tahir Naseem/Handout 1 -2- Theory of Automata and Formal Languages

and as such can be completely omitted from the DFA.

5. The accept states of the DFA are all states that include at least one of the accept states of the NFA. For example, if the NFA has states A, B, and C, with A and C being the accept states, the accept states of the DFA are A, C, AC, and ABC.

Once this process is complete, the DFA will be functionally identical to the NFA.

Language of FSA: We can write a language from an FSA by analyzing it. To write a language for given FSA we have to keep our eyes on acceptance words of that particular FSA. It is elaborated with the help of examples below

Example 1:

Language for the given FSA is : Language having words of odd lengthi.e. (a + b)((a + b)(a + b))*

Language for the given FSA is : Language having two words aa and bbi.e. aa + bb

Nondeterministic Finite Automata (NFA)Formal Definition

A nondeterministic finite automaton (NFA) is a 5-tuple (Q, ∑, δ, q0, F) whereo Q is a finite set of stateso ∑ is an alphabeto δ: Q × (∑ U {ε}) → Q is a transition functiono q0 є Q is the initial stateo F ⊆ Q is a set of accepting states (or final states).

a, b

a, b

a, b-1 +2 3

a

a, b

a, bb

a, bb

b

a-1 2 +3

4 6+5

a

Page 3: Handout 11.docx

Tahir Naseem/Handout 1 -3- Theory of Automata and Formal Languages

Differences from DFA:o transition function d can go into several stateso It allows e-transitions

Meaning of NFA

Choosing Where to Go

The ability to make choicesCase 1

1 0 1

0, 1

q0 q1 q2 q3

Guess if you are 3 symbols away from

end of input

If so, guess you will see the pattern

101

Check that you are at the end of

input

1 0 1

0, 1

q0 q1 q2 q3

State q0 has two transitions labeled 1

Upon reading 1, we have the choice of staying in q0 or moving to q1

Page 4: Handout 11.docx

Tahir Naseem/Handout 1 -4- Theory of Automata and Formal Languages

Case 2

How to run an NFA

1 0 1

0, 1

q0 q1 q2 q3

State q1 has no transition labeled 1

Upon reading 1 in q1, we die; upon reading 0, we continue to q2

1 0 1

0, 1

q0 q1 q2 q3

State q3 has no transition going out

Upon reading 0 or 1 in q3, we die

Page 5: Handout 11.docx

Tahir Naseem/Handout 1 -5- Theory of Automata and Formal Languages

Example 1:Construct an NFA over alphabet {0, 1} that accepts those strings that contain the pattern 001 somewhere.

Language of NFA:

The NFA accepts string x ε ∑* if there is some path that, starting from q0, leads to an accepting state as the string is read left to right.The language of an NFA is the set of all strings that the NFA accepts.

1 0 1

0

q0 q1 q2 q3

input:01101

,1

The NFA can have several active statesat the same time

It accepts if at the end, one of its active states is final

0 0 1

0, 1

q0 q1 q2 q3

0, 1

+q3

Page 6: Handout 11.docx

Tahir Naseem/Handout 1 -6- Theory of Automata and Formal Languages

ε – Transitions

Example 2:

Example 3:• Construct an NFA that accepts all strings with an even number of 0s or an odd

number of 1s

q0

q1

q2

a

a b

accepts:

a, b, aab, bab, aabab, …

rejects:, aa, ba, bb, …

alphabet S = {0, 1}states Q = {q0, q1, q2}initial state q0

accepting states F = {q2}state

s

inputs0 1

q0q1q2

{q1}

table oftransition function :d

Æ{q0, q1}

ÆÆÆ

q0, 1 0

0q1 q2

{q1}{q2}

Æ

Page 7: Handout 11.docx

Tahir Naseem/Handout 1 -7- Theory of Automata and Formal Languages

r0 r1

1 10

0even number of 0s

s0 s1

0 01

1odd number of 1s

q0