flat unit1

9
Unit1 Formal Languages and Automata Theory 06CS56 DEFINITION OF DETERMINISTIC FINITE AUTOMATON Let Q be a finite set and let be a finite set of symbols. Also let be a function from Q X to Q, let q0 be a state in Q and let A be a subset of Q. We call the elements of Q a state, the transition function, q0 the initial state and A the set of accepting states. Then a deterministic finite automaton is a 5-tuple < Q, , q0 , ,A> 1. The set Q in the above definition is simply a set with a finite number of elements. Its elements can, however, be interpreted as a state that the system (automaton) is in. Thus in the example of vending machine, for example, the states of the machine such as "waiting for a customer to put a coin in", "have received 5 cents" etc. are the elements of Q. "Waiting for a customer to put a coin in" can be considered the initial state of this automaton and the state in which the machine gives out a soda can be considered the accepting state. 2. The transition function is also called a next state function meaning that the automaton moves into the state (q, a) if it receives the input symbol a while in state q. Thus in the example of vending machine, if q is the initial state and a nickel is put in, then (q, a) is equal to "have received 5 cents". 3. Note that is a function. Thus for each state q of Q and for each symbol a of , (q, a) must be specified. 4. The accepting states are used to distinguish sequences of inputs given to the finite automaton. If the finite automaton is in an accepting state when the input ceases to come, the sequence of input symbols given to the finite automaton is "accepted". Otherwise it is not accepted. For example, in the Example 1 below, the string a is accepted by the finite automaton. But any other strings such as aa, aaa, etc. are not accepted. 5. A deterministic finite automaton is also called simply a "finite automaton". Abbreviations such as FA and DFA are used to denote deterministic finite automaton. DFAs are often represented by digraphs called (state) transition diagram. The vertices (denoted by single circles) of a transition diagram represent the states of the DFA and the arcs labeled with an input symbol correspond to the transitions. An arc ( p , q ) from vertex p to vertex q with label represents the transition (p, ) = q . The accepting states are indicated by double circles. Transition functions can also be represented by tables as seen below. They are called transition table. Examples of finite automaton Example 1: Q = { 0, 1, 2 }, = { a }, A = { 1 }, the initial state is 0 and is as shown in the following table. State (q) a 0 1 1 2 2 2

Transcript of flat unit1

Page 1: flat unit1

Unit1 Formal Languages and Automata Theory 06CS56

DEFINITION OF DETERMINISTIC FINITE AUTOMATON

Let Q be a finite set and let be a finite set of symbols. Also let be a function from Q X to Q, let q0 be a

state in Q and let A be a subset of Q. We call the elements of Q a state, the transition function, q0 the initial

state and A the set of accepting states.

Then a deterministic finite automaton is a 5-tuple < Q, , q0 , , A >

1. The set Q in the above definition is simply a set with a finite number of elements. Its elements can, however,

be interpreted as a state that the system (automaton) is in. Thus in the example of vending machine, for

example, the states of the machine such as "waiting for a customer to put a coin in", "have received 5 cents"

etc. are the elements of Q. "Waiting for a customer to put a coin in" can be considered the initial state of this

automaton and the state in which the machine gives out a soda can be considered the accepting state.

2. The transition function is also called a next state function meaning that the automaton moves into the state

(q, a) if it receives the input symbol a while in state q.

Thus in the example of vending machine, if q is the initial state and a nickel is put in, then (q, a) is equal

to "have received 5 cents".

3. Note that is a function. Thus for each state q of Q and for each symbol a of , (q, a) must be

specified.

4. The accepting states are used to distinguish sequences of inputs given to the finite automaton. If the finite

automaton is in an accepting state when the input ceases to come, the sequence of input symbols given to

the finite automaton is "accepted". Otherwise it is not accepted. For example, in the Example 1 below, the

string a is accepted by the finite automaton. But any other strings such as aa, aaa, etc. are not accepted.

5. A deterministic finite automaton is also called simply a "finite automaton". Abbreviations such as FA and

DFA are used to denote deterministic finite automaton.

DFAs are often represented by digraphs called (state) transition diagram. The vertices (denoted by single

circles) of a transition diagram represent the states of the DFA and the arcs labeled with an input symbol

correspond to the transitions. An arc ( p , q ) from vertex p to vertex q with label represents the transition

(p, ) = q . The accepting states are indicated by double circles.

Transition functions can also be represented by tables as seen below. They are called transition table.

Examples of finite automaton

Example 1: Q = { 0, 1, 2 }, = { a }, A = { 1 }, the initial state is 0 and is as shown in the following table.

State (q) a

0 1

1 2

2 2

Page 2: flat unit1

A state transition diagram for this DFA is given below

If the alphabet of the Example 1 is changed to { a, b } in stead of { a }, then we need a DFA such as shown in

the following example to accept the same string a. It is a little more complex DFA.

Example 2: Q = { 0, 1, 2 }, = { a, b }, A = { 1 }, the initial state is 0 and is as shown in the following table.

Note that for each state there are two rows in the table for corresponding to the symbols a and b, while in

the Example 1 there is only one row for each state.

A state transition diagram for this DFA is given below.

A DFA that accepts all strings consisting of only symbol a over the alphabet { a, b } is the next example.

Example 3: Q = { 0, 1 }, = { a, b }, A = { 0 }, the initial state is 0 and is as shown in the following table.

A state transition diagram for this DFA is given below.

State (q) a b

0 1 2

*1 2 2

2 2 2

State (q) a b

0 0 1

1 1 1

Page 3: flat unit1

A finite automaton as a machine

A finite automaton can also be thought of as the device shown below consisting of a tape and a control circuit

which satisfy the following conditions:

1. The tape has the left end and extends to the right without an end.

2. The tape is divided into squares in each of which a symbol can be written prior to the start of the operation

of the automaton.

3. The tape has a read only head.

4. The head is always at the leftmost square at the beginning of the operation.

5. The head moves to the right one square every time it reads a symbol.

It never moves to the left. When it sees no symbol, it stops and the automaton terminates its operation.

6. There is a finite control which determines the state of the automaton and also controls the movement of the

head

Operation of finite automata

Let us see how an automaton operates when it is given some inputs. As an example let us consider the DFA of

Example 3 above.

Initially it is in state 0. When zero or more a's are given as an input to it, it stays in state 0 while it reads all the a's

(without breaks) on the tape. Since the state 0 is also the accepting state, when all the a's on the tape are read,

the DFA is in the accepting state. Thus this automaton accepts any string of a's. If b is read while it is in state 0

(initially or after reading some a's), it moves to state 1. Once it gets to state 1, then no matter what symbol is read,

this DFA never leaves state 1. Hence when b appears anywhere in the input, it goes into state 1 and the input

string is not accepted by the DFA. For example strings aaa, aaaaaa etc. are accepted but strings such as aaba, b

etc. are not accepted by this automaton.

Draw a DFA to accept string of 0’s and 1’s ending with the string 011.

Obtain a DFA to accept strings of a’s and b’s having a sub string aa

q3q1 q2q0

1 0

0

0

1

1 10

q2q1

b

q0a a

b

a,b

Page 4: flat unit1

Obtain a DFA to accept strings of a’s and b’s except those containing the substring aab.

Obtain DFAs to accept strings of a’s and b’s having exactly one a,

Obtain a DFA to accept strings of a’s and b’s having even number of a’s and b’s

Obtain a DFA to accept strings of a’s and b’s having even number of a’s and odd number of b’s.

Obtain a DFA to accept strings of a’s and b’s having odd number of a’s and even number of b’s.

Obtain a DFA to accept strings of a’s and b’s having odd number of a’s and odd number of b’s.

Regular language

Definition: Let M = (Q, , , q0, A) be a DFA. The language L is regular if there exists a machine M such that L

= L(M).

q3a ba

q2q1q0

a,bb

b

a

q1q0

b a,b

a q2

b

a

q1q0

b

q2

b

a

a

b

q3

ba

a

q1q0

b

q2

b

a

a

b

q3

b

a

a

q1q0

b

q2

b

a

a

b

q3

b

a

a

q1q0

b

q2

b

a

a

b

q3

b

a

a

Page 5: flat unit1

Applications of Finite Automata

String matching/processing

Compiler Construction

The various compilers such as C/C++, Pascal, FORTRAN or any other compiler is designed using the finite

automata. The DFAs are extensively used in the building the various phases of compiler such as

Lexical analysis (To identify the tokens, identifiers, to strip of the comments etc.)

Syntax analysis (To check the syntax of each statement or control statement used in the program)

Code optimization (To remove the un wanted code)

Code generation (To generate the machine code)

Other applications

The concept of finite automata is used in wide applications. It is not possible to list all the applications as there

are infinite numbers of applications. This section lists some applications:

1. Large natural vocabularies can be described using finite automaton which includes the applications such as

spelling checkers and advisers, multi-language dictionaries, to indent the documents, in calculators to

evaluate complex expressions based on the priority of an operator etc. to name a few. Any editor that we use

uses finite automaton for implementation.

2. Finite automaton is very useful in recognizing difficult problems i.e., sometimes it is very essential to solve

an un-decidable problem. Even though there is no general solution exists for the specified problem, using

theory of computation, we can find the approximate solutions.

3. Finite automaton is very useful in hardware design such as circuit verification, in design of the hardware

board (mother board or any other hardware unit), automatic traffic signals, radio controlled toys, elevators,

automatic sensors, remote sensing or controller etc.

4. In game theory and games wherein we use some control characters to fight against a monster, economics,

computer graphics, linguistics etc., finite automaton plays a very important role.

Non deterministic finite automata (NFA)

Definition: An NFA is a 5-tuple or quintuple M = (Q, , , q0, A) where

Q is non-empty, finite set of states.

is non-empty, finite set of input alphabets.

is transition function which is a mapping from

Q x { U } to subsets of 2Q. This function shows the change of state from one state to a set of states

based on the input symbol.

q0 Q is the start state.

A Q is set of final states.

Acceptance of language

Definition: Let M = (Q, , , q0, A) be a DFA where Q is set of finite states, is set of input alphabets (from

which a string can be formed), is transition function from Q x {U} to 2Q, q0 is the start state and A is the final

or accepting state. The string (also called language) w accepted by an NFA can be defined in formal notation as:

L(M) = { w | w *and *(q0, w) = Q with atleast one

Component of Q in A}

Obtain an NFA to accept the following language L = {w | w ababn or aban where n 0}

The machine to accept either ababn or aban where n 0 is shown below:

b

q1a q3q2

b a q4

q5

a

q6a b q7

q0

Page 6: flat unit1

2.1 Conversion from NFA to DFA

Let MN = (QN, N, N, q0, AN) be an NFA and accepts the language L(MN). There should be an equivalent DFA MD =

(QD, D, D, q0, AD) such that L(MD) = L(MN). The procedure to convert an NFA to its equivalent DFA is shown

below:

Step1:

The start state of NFA MN is the start state of DFA MD. So, add q0(which is the start state of NFA) to QD

and find the transitions from this state. The way to obtain different transitions is shown in step2.

Step2:

For each state [qi, qj,….qk] in QD, the transitions for each input symbol in can be obtained as shown

below:

1. D([qi, qj,….qk], a) = N(qi, a) U N(qj, a) U ……N(qk, a)

= [ql, qm,….qn] say.

2. Add the state [ql, qm,….qn] to QD, if it is not already in QD.

3. Add the transition from [qi, qj,….qk] to [ql, qm,….qn] on the input symbol a iff the state [ql, qm,….qn] is

added to QD in the previous step.

Step3:

The state [qa, qb,….qc] QD is the final state, if at least one of the state in qa, qb, ….. qc AN i.e., at least one

of the component in [qa, qb,….qc] should be the final state of NFA.

Step4:

If epsilon () is accepted by NFA, then start state q0 of DFA is made the final state.

Convert the following NFA into an equivalent DFA.

Step1: q0 is the start of DFA (see step1 in the conversion procedure).

So, QD = {[q0]} (2.7)

Step2: Find the new states from each state in QD and obtain the corresponding transitions.

Consider the state [q0]:

When a = 0

D([q0], 0) =

=

N([q0], 0)

[q0, q1]

(2.8)

When a = 1

D([q0], 1) =

=

N([q0], 1)

[q1]

(2.9)

Since the states obtained in (2.8) and (2.9) are not in QD(2.7), add these two states to QD so that

QD = {[q0], [q0, q1], [q1] } (2.10)

The corresponding transitions on a = 0 and a = 1 are shown below.

0

[q0] [q0, q1]

[q0, q1]

[q1]

q1q0

0 10,1 q2

0, 1

1

[q1]

Q
Page 7: flat unit1

Consider the state [q0, q1]:

When a = 0

D([q0, q1],

0)

=

=

=

=

N([q0, q1], 0)

N(q0, 0) U N(q1, 0)

{q0, q1} U {q2}

[q0, q1, q2] (2.11)

When a = 1

D([q0, q1],

1)

=

=

=

=

N([q0, q1], 1)

N(q0, 1) U N(q1, 1)

{q1} U {q2}

[q1, q2] (2.12)

Since the states obtained in (2.11) and (2.12) are the not defined in QD(see 2.10), add these two states to QD so

that

QD = {[q0], [q0, q1], [q1], [q0, q1, q2], [q1, q2] } (2.13)

and add the transitions on a = 0 and a = 1 as shown below:

Consider the state [q1]:

When a = 0

D([q1], 0) =

=

N(

[q2

When a = 1

D([q1], 1) =

=

N(

[q2

Since the states obtained in (2.14) and

QD so that

QD = {[q0], [q0, q1], [q1], [q0, q1, q2],

and add the transitions on a = 0 and a

0

[q0] [q0, q1]

[q0, q1] [q0, q1, q

[q1]

[q0, q1, q2]

[q1, q2]

0

[q0] [q0, q1]

[q0, q1] [q0, q1, q

[q1] [q2]

[q0, q1, q2]

[q1, q2]

[q2]

1

[q1]

2] [q1, q2]

Q

[q1], 0)

] (2.14)

[q1], 1)

] (2.15)

(2.15) are same and the state q2 is not in QD(see 2.13), add the state q2 to

[q1, q2], [q2]} (2.16)

= 1 as shown below:

1

[q1]

2] [q1, q2]

[q2]

Q
Page 8: flat unit1

Consider the state [q0,q1,q2]:

When a = 0

D([q0,q1,q2], 0) =

=

=

=

N([q0,q1,q2], 0)

N(q0, 0) U N(q1, 0) U N(q2, 0)

{q0,q1} U {q2} U {}

[q0,q1,q2] (2.17)

When a = 1

D([q0,q1,q2], 1) =

=

=

=

N([q0,q1,q2], 1)

N(q0, 1) U N(q1, 1) U N(q2, 1)

{q1} U {q2} U {q2}

[q1, q2]

(2.18)

Since the states obtained in (2.17) and (2.18) are not new states (are already in QD, see 2.16), do not add these

two states to QD. But, the transitions on a = 0 and a = 1 should be added to the transitional table as shown

below:

Consider the state [q1,q2]:

When a = 0

D([q1,q2], 0) =

=

=

=

N([q1,

N(q1,

{q2} U

[q2]

When a = 1

D([q1,q2], 1) =

=

=

=

N([q1,

N(q1,

{q2} U

[q2]

Since the states obtained in (2.19) and

two states to QD. But, the transitions

below:

0

[q0] [q0, q1]

[q0, q1] [q0, q1, q

[q1] [q2]

[q0, q1, q2] [q0,q1,q2

[q1, q2]

[q2]

0

[q0] [q0, q1]

[q0, q1] [q0, q1, q

[q1] [q2]

[q0, q1, q2] [q0,q1,q2

[q1, q2] [q2]

[q2]

1

[q1]

2] [q1, q2]

[q2]

Q

q2], 0)

0) U N(q2, 0)

{}

(2.19)

q2], 1)

1) U N(q2, 1)

{q2}

(2.20)

(2.20) are not new states (are already in QD see 2.16), do not add these

on a = 0 and a = 1 should be added to the transitional table as shown

] [q1, q2]

1

[q1]

2] [q1, q2]

[q2]

Q

] [q1, q2]

[q2]

Page 9: flat unit1

Consider the state [q2]:

When a = 0

D([q2], 0) =

=

N([q2], 0)

{} (2.21)

When a = 1

D([q2], 1) =

=

N([q2], 1)

[q2] (2.22)

Since the states obtained in (2.21) and (2.22) are not new states (are already in QD, see 2.16), do not add these

two states to QD. But, the transitions on a = 0 and a = 1 should be added to the transitional table. The final

transitional table is shown in table 2.14. and final DFA is shown in figure 2.35.

The DFA

0 1

[q0] [q0, q1] [q1]

[q0, q1, q2] [q1, q2]

[q2] [q2]

[q0,q1,q2] [q1, q2]

[q2] [q2]

[q2] [q2]

[q2]

[q1]

[q0]

[q0, q

1]

[q1, q

2][q

0, q

1, q

2]

0 1

0 1 0, 1

0, 1

0 1

1

[q0,q1]

[q1]

[q0,q1,q2]

[q1,q2]