You are on page 1of 29

Theory Of Automata

Lec 03 – Finite Automata


Instructor
Mahwish Shahid

1
Text and Reference Material

1. Introduction to Computer Theory, by Daniel I. Cohen, John Wiley and


Sons, Inc., Second Edition

2
Languages

3
Determinism
• Deterministic finite automata (or DFA) are finite
state machines
• Accept or reject strings of characters by parsing them
through a sequence that is uniquely determined by
each string
• The machine changes its state from previous to next
 based on input sequence and previous state

4
Theorem: Finite Languages are Regular
• Finite Languages: Languages with finitely many
words are considered as finite such as null String
• If L is a finite language (a language with only finitely
many words),
 then L can be defined by a regular expression.
 In other words, all finite languages are regular.
• Example: for Σ={a,b},
• All words of length 3
RE: (a+b) (a+b)(a+b)  ; (a+b)3 
Words: {aaa, aab, aba, abb, baa, bab, bba, bbb}

5
Theorem: Finite Languages are Regular
• Proof: Let L be a finite language. To make one regular
expression that defines L,
 we turn all the words in L into boldface type and insert plus
signs between them.

• For example, the regular expression that defines the language


• L = {baa, abbba, bababa} is (baa + abbba + bababa)

• This algorithm only works for finite languages because an


infinite language would become a regular expression that is
infinitely long, which is forbidden.

6
Equivalent Regular Expressions
• Two regular expressions are said to be equivalent if they
generate the same language.
• Example: Consider the following regular expressions
• r1 = (a + b)* (aa + bb)
• r2 = (a + b)*aa + ( a + b)*bb
• both regular expressions define the language of strings
ending in aa or bb
• Derivation:
• abba + ab
•  ab (ba + Λ)
• abba + ba
•  (ab + Λ) ba
Equivalent RE
• All words with exactly 2 ‘b’s or 3 ‘b’s
Words: {aaa, aab, aba, abb, baa, bab, bba, bbb}
RE: L1 = exactly 2 ‘b’s = a* b a* b a*
L2 = exactly 3 ‘b’s =a* b a* b a*b a*
L= L1+L2 = a* b a* b a* + a* b a* b a*b a*
L= a* b a* b a* (Λ + b a*)
• Language of all words that have at least two a’s
(a + b)*a(a + b)*a(a + b)*
b*ab*a(a + b)*
• Hence, we can write
(a + b)*a(a + b)*a(a + b)* = b*ab*a(a + b)*

8
Equivalent RE - Example

• Language of all strings having all b’s, or else an a


followed by some b’s where Σ={a,b},
V = {Λ, a, b, ab, bb, abb, bbb, abbb, bbbb, …}
• RE  b* + ab*
• where Λ is included in b*.
• (Λ + a) b*
• which means that in front of the string of some b’s, we
have either an a or nothing.

9
Regular Languages
• If a language’s Regular expression Exist, then language is
Regular other wise non-regular
• Any language that can be represented by a regular
expression is called a regular language
• If r1, r2 are regular expressions, corresponding to the
languages L1 and L2
 then the languages generated by
 (r1+ r2), (r1. r2 ) or (r2. r1) and (r1)* or (r2)*
 are also regular languages.
RL Example
• If r1=(aa+bb) and r2=(a+b) then
• the language of strings generated by r1+r2, is also a regular language,
expressed by
 (aa+bb)+(a+b)
• If r1=(aa+bb) and r2=(a+b) then
• the language generated by r1r2, is also a regular language, expressed by
 (aa+bb)(a+b)
• If r=(aa+bb) then
• the language of strings generated by r*, is also a regular language,
expressed by
 (aa+bb)*
11
Infinite Languages
• infinite Languages: Language which Regular expression
doesn’t exist
• Example: for Σ={a,b},
• All words with equal no of ‘a’s in the beginning and equal no
of ‘b’s in the end
 Words: {ab, aabb, aaabbb, aaaabbbb, ….}
 RE: 
• Regular expressions has no restriction on counting

To express Infinite language one can use following expression


but these are NOT regular expressions
 an bn

12
RE for EVEN EVEN
• strings with even number of a’s and even number of
b’s, defined over Σ={a,b},
• can be written as
{Λ, aa, bb, aaaa,aabb,abab, abba, baab, baba, bbaa,
bbbb,…}
• regular expression can be written as
(aa + bb + (ab + ba) (aa + bb)*(ab + ba))*

String 1 = aaaaaabbbbbb
String 2 = ababbbbabbbaaabbab
13
Differentiate

• r1=a*+b* VS r2=(a+b)*
• r1 does not generate any string of concatenation
of a and b,
• r2 generates such strings.

14
Finite Automata (FA)
• Method of defining languages
• A Finite automaton (FA), is a collection of the followings
1) Finite number of states, having one initial and some
(maybe none) final states.
2) Finite set of input letters (Σ) from which input strings
are formed.
3) Finite set of transitions i.e. for each state and for each
input letter there is a transition showing how to move
from one state to another.

15
Finite Automata
• Three basic elements of FA
• Constitutes by 3 states and represented with
 Start / Initial State
-
A machine can have only one start state
 Final State +
A machine can have one or multiple final state(s)
 Not final / Intermediate State
• Input: for an input(alphabets) we switch from one state to
another
• Transition / Directed Edges: arrows associated with each input
 Incoming Edge
 Outgoing Edge
• Loops: are optional. Not a mandatory part to execution
 Loop is both an incoming and outgoing edge for a transition
16
Finite Automata - FA
• To indicate the initial state, an arrow head can also be
placed before that state
• The final state can be depicted with double circle
• Null cannot be represent in the form of FA.
• For each state, there must be an input
• For each input, there should be exactly one unique path
• Rejected inputs must end on a non final state

a, b

a, b

17
Finite Automata - FA
a,b
• Universal Set: (a+b)* - FA of universal set has all
states as Final States
+-

• Blackhole: a state which doesn’t have any transition


leading to final or any other state
• Example: Language of all words that accepts baa

- +

b b
a a,b

a,b
18
FA -Example
• Σ = {a,b}
• States: x, y, z where x is an initial state and z is final
state.
• Transitions:
1. At state x reading a go to state z,
2. At state x reading b go to state y,
3. At state y reading a, b go to state y a,b

4. At state z reading a, b go to state z y


b

x–
a,b
a
19
Z+
Transition Table
• These transitions can be expressed by the following table
a,b
called transition table
Old States New States y
b
Reading a Reading b
x- z y x–
y y y a,b
a
z+ z z
Z+
• The information of an FA, given in the table, can also be
depicted by the transition diagram, of the given FA
• It is an FA accepting the words of language starting with
‘a’.
• Regular expression  a (a + b)*
20
Transition Diagram
• The information of an FA, given in the table, can also be
depicted by the transition diagram, of the given FA
• It is an FA accepting the words of language starting with
‘a’.
 Regular expression  a (a + b)*
• While expressing an FA by its transition diagram, the a,b
labels of states are not necessary.
y
b

x–
a,b
a
Z+
21
Example
• For Σ = {a,b}, States: x, y, where x is both initial and final
state.
Old New States
Transitions: States

1. At state x reading a or b go to state y. Reading Reading


a b
2. At state y reading a or b go to state x. x± y y
a, b
y x x
x y

a, b
• FA accepting the language of strings, defined over Σ={a, b}
of even length.
• Regular expression  ((a+ b) (a + b))*
22
FA - Example
a
a,b
– +
• For a String ab a,b
b – +
• (a+b)
b
• (a+b) + a


a
• (a+b) b* a +
a,b
• Null String b

a,b
• Universal set with two states +-
• Universal set with three states
a,b

a,b
+– +
a,b

a,b
+– + + 23
FA - Example
• Consider the language L of strings, starting with b, defined over Σ={a, b}
• RE  b(a + b)* a,b

b
–– +

a,b
• a
Language L of strings ending in a over Σ={a, b}
• RE  (a+b)*a 1

b a a

– +

b
FA - Example
• For a given language there may be more than one FA
accepting that language,
• But for a given FA there is a unique language accepted
by that FA.
• Language L of strings ending in a over Σ={a, b}
• RE  (a+b)*a
a
a
b a a –– +


+ a b
b

b
b
25
Class Activity - FA
• Build an FA for the language L of strings, defined over
Σ={a, b}, of odd length.
a,b

– +

a,b

26
Class Activity
• Constructtan FA accepting the Language L of Strings, defined
over Σ = {a, b}, beginning with and ending in same letters.

27
Applications of FA

• Lexical Analyzer of Compiler


• Search mechanism in word processor

28
QUIZ
Next Class
29

You might also like