Sunteți pe pagina 1din 7

Finite Automata

Finite automata - also known as nite state machines - are one of the simplest models of computation. They are excellent models for computers with an extremely limited amount of memory. Examples: automatic door at supermarket elevator controls vending machine calculators Finite automata are also the basis for programs that perform spell checking, grammar checking, indexing. They are fundamental in the study of compilers.

Finite Automata
CIS 3150
Joe Sawada - University of Guelph

CIS 3150

Finite Automata

1 / 25

CIS 3150

Finite Automata

2 / 25

Automatic Door

Automatic Door State Diagram


The door controller moves from state to state depending on the input it receives and the current state it is in.
REAR BOTH NEITHER FRONT FRONT REAR BOTH

Example: Automatic Door


It has two input pads - one on each side of the door. The door is in one of two states: OPEN or CLOSED. It opens from front to rear (not sliding). It is possible for both input pads to be active (a person standing on each pad). So there are four scenarios for input: FRONT: someone only on front pad REAR: someone only on rear pad BOTH: people standing on both pads NEITHER: no-one standing on the pads What should happen if the door is CLOSED and someone is on REAR pad? BOTH?

CLOSED

OPEN

NEITHER

Note that the controller only needs a single bit of memory to remember what state it is in.

CIS 3150

Finite Automata

3 / 25

CIS 3150

Finite Automata

4 / 25

Automatic Door Transition Table


REAR BOTH NEITHER FRONT FRONT REAR BOTH

Finite Automata

From last example, notice that we can represent the Automatic Door Contoller either using a state diagram or a transition table. Depending on the size of your problem (controller) one method may be easier to understand than the other. When designing more complicated devices, using a standard methodology and terminology becomes very important. We must be a bit more formal when dening both state diagrams and nite automatons.

CLOSED

OPEN

NEITHER

CLOSED OPEN

NEITHER CLOSED CLOSED

FRONT OPEN OPEN

REAR CLOSED OPEN

BOTH CLOSED OPEN

CIS 3150

Finite Automata

5 / 25

CIS 3150

Finite Automata

6 / 25

Formal Denition: FA

State Diagrams for Finite Automata


State diagrams can be used to represent nite automata as follows: The states are represented as vertices Edges labeled with the input represent the transitions

Denition
A nite (state) automaton is a 5-tuple M = (Q, , , q0 , F ) where: Q: a nite set of states : a nite input alphabet : Q Q a transition function F Q: the set of accept (nal) states q0 Q: the initial or start state

Final states are drawn with double circles A special arrow indicates the start state Example: Three consecutive 1s
1 0 1 1

A
s

0,1

Can you give a formal denition for the automatic door?

This FA ends up in an accept state if and only if the input contains 3 consecutive ones. Note: can be determined from the input on the arcs.
CIS 3150 Finite Automata 7 / 25 CIS 3150 Finite Automata 8 / 25

FA Terminology
In the last example, we say that all input strings with 3 consecutive 1s are recognized or accepted by the nite automaton M - since they all end up in an accept state.
Denitions
We say that an input string is accepted by a nite state automaton M if it starts from q0 and ends at an accept state. The language recognized by a nte automaton M , denoted L(M ) is the set of all strings that are accepted by M .

Draw State Diagrams

Draw the state diagram corresponding to the formal denitions.


FA 1 1. Q = {A, B, C, D} 2. = {0, 1} 3. = A B C D 0 B C C D FA 2 1. Q = {q0 , q1 , q2 } 2. = {a, b} 3. = q0 q1 q2 a q1 q1 q2

1 D A A D

b q2 q2 q2

4. q0 is the start state 5. F = {q1 }

4. A is the start state

An important question we will ask later: Does every language L1 have a corresponding nite automaton M such that L(M ) = L1 ?

5. F = {B, D}

CIS 3150

Finite Automata

9 / 25

CIS 3150

Finite Automata

10 / 25

Give Formal Denition


Give the formal denitions for the following automata.
0 0

Describe Languages
What languages are recognized by the following FA?
0 0

A C
1

A
s

1 1

0,1

1 1

0,1

1 1 0 0 0 1 1

1 1 0 0 0 1 1

A B
0

B
0

A
s

0,1

0,1

(1) L1 = { (01)n | n 1} (2) L2 = all binary strings except the substring 000 or ending 10.
CIS 3150 Finite Automata 11 / 25 CIS 3150

(empty string), 0, and those containing

Finite Automata

12 / 25

Designing Finite Automata


Suppose you are given some language L. If possible, you want to design a nite automaton that will recognize it. The input is received one element at a time and you can never backtrack. You never know when the input will end, so after each symbol, you must always be ready with an answer (accept or not). States are meant to be used as memory. Each state remembers some piece of information. If the language is nite with the longest string of length n, then we can build a trivial nite automaton that has one state for each node in a standard computation tree with height n. Each node corresponds to a nite string w of length less than n. If w L, we make that state (node) an accepting state. The above is not true if n is not xed - since we only have a nite number of states. Not all languages can be recognized by an FA. Those that can are called regular languages.

Example
Example
Design a FA that will recognize the set {1, 00}.

A
s 0

B
0,1 1

C
0

E
0,1

0,1

Can you draw one with only four states?

CIS 3150

Finite Automata

13 / 25

CIS 3150

Finite Automata

14 / 25

More Examples

Formal Denition of Computation

Design FA that recognize the following languages over the alphabet {0, 1}: L1 = {w | w has an odd number of 1s} L2 = {w | w contains substring 110} L3 = {w | w contains more 0s than 1s} What is the underlying reason why we cant build one for L3 ?

Formally, we want a more precise notion of what it means for a string to be accepted by a nite automaton M = (Q, , , q0 , F ).
Denition
M accepts a string w = w1 w2 wn if a sequence of states r0 , r1 , . . . rn in Q exists with the three conditions: r0 = q0 (ri , wi+1 ) = ri+1 rn F for i = 0 to n 1

We say that M recognizes A if A = {w | M accepts w}.

CIS 3150

Finite Automata

15 / 25

CIS 3150

Finite Automata

16 / 25

Regular Operations
Denition
A language is called a regular language if some nite automaton recognizes it.

Regular Operations
Theorem
The class of regular languages is closed under each of the three regular operations: union, concatenation, star.

Regular Operations
Let A and B be languages. Concatenation: A B = {xy | x A and y B} Union: A B = {x | x A or y B}

What does closed mean?


Consider the natural numbers N = {1, 2, 3, . . .}.

Star: A = {x1 x2 xk | k 0 and each xi A}

N is closed under addition since for any x, y N , x + y N

N is not closed under subtraction since x = 2 and y = 1 are both in this set but 1 2 = 1 is not.

Example: A = {a, b} and B = {1, 2, 3}


A B = {a, b, 1, 2, 3}

In other words, if you are given two regular languages A and B, you can apply any sequence of the operations union, concatenation, star to these languages and the resulting language will also be regular. We will prove this theorem after we introduce non-determinism.

A B = {a1, a2, a3, b1, b2, b3}

A = { , a, b, aa, ab, ba, bb, aaa, aab, aba, abb, baa, bab, bba, bbb, . . .}
CIS 3150 Finite Automata 17 / 25

CIS 3150

Finite Automata

18 / 25

Non-determinism
All the examples we have looked at so far have been deterministic: for each state and input pair, there was a unique state given by the transition function. If for each such pair, there is more than one possible next state, then we say the machine is non-deterministic: the next state is not necessarily uniquely determined. To dene such a machine, we simply need to redene the transition function. Denition
A non-deterministic nite automaton (NFA) is a 5-tuple M = (Q, , , q0 , F ) where: Q: a nite set of states : a nite input alphabet : Q P(Q) is a transition function q0 S: called the initial or start state F a subset of Q called the accepting or nal states The new transition function maps an input state pair to a set of states which may be empty. Also the input may be empty: includes the empty string .

Non-Determinism
Example of a non-deterministic FA
0

A
s

B
1

notice the null transition notice that the input/state pair (A,0) has multiple transitions notice that the input/state pair (A,1) has no transition

NOTE: If there is no transition for an input, state pair, then the machine stops, rejecting the input. This simplies the diagram by eliminating the need to have a sink state.

CIS 3150

Finite Automata

19 / 25

CIS 3150

Finite Automata

20 / 25

Non-Determinism
Another example of a non-deterministic FA
1 1 0 0 0 0 0
Where could the input 0001 end up? A or B or C.

Non-Determinism

A
s 1

B
1 1 0

0,1

The denitions for recognize or accept are slightly dierent for non-deterministic FA (NFA), since the same input string could end up at more than one state. Denition

A NFA is said to recognize or accept an input string x, if there exists a nal state that can be obtained from s0 using x. The language recognized by a NFA M is the set of all strings recognized by M .

CIS 3150

Finite Automata

21 / 25

CIS 3150

Finite Automata

22 / 25

Non-Determinism
Find the language recognized by the following NFAs.
0

Non-Determinism
Design
Design NFAs that recognize the following languages. Try to use the least number of states possible.
1

A
s

B
0 1

L2 = {1, 00}

L1 = {0}

L3 = {1n | n 2}

A
s

0,1

1
s

s 0

1 0

B A
s 1

(1) L1 = {0} (empty string) {0m 1n | m, n > 0} (2) L2 = { , 0, 1} {1n 0 | n > 0}

L1

L2

L3

CIS 3150

Finite Automata

23 / 25

CIS 3150

Finite Automata

24 / 25

Non-Determinism

Theorem
If the language L is recognized by some NFA M0 , then it can also be recognized by some deterministic FA M1 .

Corollary
A language is regular if and only if some NFA recognizes it.

It turns out that not all languages can be recognized by NFA (ie. there exists non-regular languages). An example of a language that is not regular, and hence can not be recognized by any NFA is: L = {0n 1n | n = 0, 1, 2, . . .}

CIS 3150

Finite Automata

25 / 25