05 Finite Automata
05 Finite Automata
05 Finite Automata
Lecture 9
Faryal Shamsi
Lecturer Computer Science
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.
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
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
*
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
w 1 2 k
1 2 k
q q
Courtesy Costas Busch - RPI
Special case:
q , q
*
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
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,….}
q1 q2
a
b b b
b
a
q3
q4
a
RE for EVEN-EVEN