You are on page 1of 89

Finite Automata

Lecture 9

Faryal Shamsi
Lecturer Computer Science

Sukkur IBA University


Finite Automaton (FA)

• Finite Automata is another way of defining languages

• We have already covered Set Theory and Regular Expressions


Finite Automaton (FA)
• Finite automata are good models for computers with an extremely
limited amount of memory.
Finite Automaton (FA)
Simple Automaton

5
Acceptance of Inputs

• Given a sequence of inputs (input string ), start in the start state and
follow the transition from each symbol in turn.

• Input is accepted if you wind up in a final (accepting) state after all


inputs have been read.
Finite Accepter
Input
String
Output
“Accept”
Finite or
Automaton “Reject”
Finite Accepter
Input
then
Output

Finite “Accept”
Automaton
Finite Accepter
Input
them
Output

Finite “Reject”
Automaton
Simple Automaton
Binary String Accepter
Input
String
Output
“Accept”
Finite or
Automaton “Reject”
Binary String Accepter
Input
0101
Output

Finite “Accept”
Automaton
Binary String Accepter
Input
1012
Output

Finite “Reject”
Automaton
Formal Definition of FA
Binary String Accepter
class FiniteAutomata { S0
public static void main(String[] args) {
int state=0;
String input = “1010"; 0,1
for(int i=0;i<input.length();i++) {
state=transitionFunction(state,input.charAt(i));
} S1
if (state==1) // State S1 0,1
System.out.println("String Accepted");
if (state==0) // State S2
System.out.println("String Rejected");
}
Binary String Accepter
static int transitionFunction (int q, char c) { S0
if (q==0)
if(c=='0' || c=='1') 0,1
{
System.out.println("State 0 to 1");
return 1; S1
} 0,1
else{
System.out.println("ERROR");
}
Binary String Accepter
if (q==1)
{
if(c=='0' || c=='1')
{
System.out.println("State 1 to 1");
S0
return 1;
}
else{
System.out.println("ERROR");
0,1
return 0;
}
}
else
S1
{
System.out.println("ERROR"); 0,1
return 0;
} } }
Formal Definition of FA
https://
www.geeksforgeeks.org/c-program-to-simulate-nondeterminist
ic-finite-automata-nfa
/
Input Alphabet

  a, b
a, b

q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4

19
Set of States

Q  q0 , q1, q2 , q3 , q4 , q5 
a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Initial State q0

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Set of Final States

a, b
F = {q4}
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Set of Final States

F = {q3,q4}
Transition Function
 :Q  Q
 (q , x )  q 
q x q

Describes the result of a transition


from state q with symbol x
 q0 , a   q1

a, b

q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
 q0 , b   q5

a, b

q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
 q2 , b   q3

a, b

q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
Transition Table
 a b
q0 q1 q5
q1 q5 q2 a, b
q2 q5 q3
q3 q4 q5 q5
q4 q5 q5 a, b
b a a b
q5 q5 q5 q0 a q1 b q2 b q3 a q4
Extended Transition Function
 :Q    Q
* *

 (q ,w )  q 
*

Describes the resulting state


after scanning string w from state q
 * q0 , ab   q2

a, b

q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
 * q0 , abba   q4

a, b

q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
 * q0 , abbbaa   q5

a, b

q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
Observation: There is a walk from q to q
with label w

 * q, w  q

q w q

states may be repeated

w   1 2  k
1 2 k
q q
Courtesy Costas Busch - RPI
Special case:

for any state q

 q ,    q
*

Courtesy Costas Busch - RPI


Language of an Automaton

• The set of strings accepted by an automaton A is the language of A.


• Denoted L(A).

• Different sets of final states -> different languages.


• Example: As designed, L(Tennis) = strings that determine the winner.
Why FAs ??? – Applications

• Used for both design and verification of circuits and communication


protocols.

• Used for many text-processing applications.

• An important component of compilers.

• Used in Machine Learning to define patterns of events, etc.


A Complex Automaton
• Consider a Tennis Match
Scoring a Game
• One person serves throughout.
• To win, you must score at least 4 points.
• You also must win by at least 2 points.

• Inputs are s = “server wins point” and o = “opponent wins point.”


Server
s Wins
40-Love s
s s Ad-in
o s
30-Love
s 40-15 o
o s o
Start 15-Love s
30-15 40-30
s s s
o o o
Love 15-all 30-all o deuce
s s s
o o
15-30 30-40
Love-15 s o
s
o o s
Love-30 15-40
s o
o
o Ad-out
Love-40
o
o
Opp’nt
Wins
Formal Definition of Finite Automata
Alphabet   {a , b }
a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4

For every state, there is a transition for every


symbol in the alphabet
Initial Configuration
Input String
a b b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Reading the Input
a b b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Input finished

a b b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4

Output: “accept”
Rejection
a b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b a

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Input finished

a b a

a, b
Output:
q5 “reject”

a a, b
b a b
q0 a q1 b q2 b q3 a q4
Another Rejection

a, b

q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Input

Input Finished (no symbol read)
a, b

Output: q5
“reject” a a, b
b a b
q0 a q1 b q2 b q3 a q4
Another Example
a a b

a a, b

q0 b q1 a, b q2
a a b

a a, b

q0 b q1 a, b q2
a a b

a a, b

q0 b q1 a, b q2
a a b

a a, b

q0 b q1 a, b q2
Input finished

a a b

a a, b
Output: “accept”

q0 b q1 a, b q2
Rejection
b a b

a a, b

q0 b q1 a, b q2
b a b

a a, b

q0 b q1 a, b q2
b a b

a a, b

q0 b q1 a, b q2
b a b

a a, b

q0 b q1 a, b q2
Input finished

b a b

a a, b

q0 b q1 a, b q2

Output: “reject”
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s s s
o o o
Love 15-all 30-all deuce
o s
* o
Love-15
s
o
15-30
s
s
30-40
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s

Start
*
15-Love
s
o s
40-15 o
s
o
30-15 40-30
s s s
o o o
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s
o * s
o
s
o
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
40-15
Start 15-Love
s
o *
30-15
s
o
40-30 s
o
s s s
o o o
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s
Love
o
15-all
s
o *
30-all
s
o
deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
40-15
Start 15-Love
s
o
30-15
s
o
40-30
* s
o
s s s
o o o
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s
o
s
o
s
o *
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins
Example: Processing a String
sosososososs
Server
s Wins

s
40-Love s *
s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s s s
o o o
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s
Love
o
15-all
s
o
30-all
s
o *
deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins
Example: Processing a String
sosososososs
Server
s Wins

s
40-Love s *
s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s s s
o o o
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins
Example: Processing a String
sosososososs
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s
Love
o
15-all
s
o
30-all
s
o *
deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins
Example: Processing a String
sosososososs
Server
s Wins

s
40-Love s *
s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s s s
o o o
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins
Example: Processing a String
sosososososs *
Server
s Wins
40-Love s
s s Ad-in
30-Love o s
s 40-15 o
o s o
Start 15-Love 30-15 40-30 s
s s s
o o o
Love 15-all 30-all deuce
o s
s s
o o
15-30 30-40
Love-15 s
s o
o o s
Love-30 15-40
s o
o Ad-out
Love-40 o
o
o Opp’nt
Wins
Variation in FAs

• DFA
• Deterministic Finite Automata

• NFA
• Non-deterministic Finite Automata
Formal Definition of DFA
Formal Definition of NFA
Difference between DFA and NFA

• Every state of a DFA always has exactly one exiting transition arrow for
each symbol in the alphabet, an NFA violates this rule.
A Non Deterministic Finite Automata
Transition Graph
An Example
of a
TG
or
Transition Graph
Every FA is a TG
Understanding EVEN-EVEN
• Consider a language that accepts only even number of a’s and b’s
•  = {aa,bb,aabb,abba,baab,bbaa,….}

• Write a Regular Expression


• Draw an FA
TG for EVEN-EVEN
FA for EVEN-EVEN
a

q1 q2
a

b b b
b
a

q3
q4
a
RE for EVEN-EVEN

• (aa + bb)* + ( (ab+ba) (aa+bb)* (ab+ba) )*

You might also like