Automata
Automata
DEPARTMENT OF CSE
R V College of Engineering
Go, change the world
2
Go, change the world
3
Go, change the world
Alphabet
An alphabet is a finite, non-empty set of symbols
• We use the symbol ∑ (sigma) to denote an alphabet
• Examples:
• Binary: ∑ = {0,1}
• All lower case letters: ∑ = {a,b,c,..z}
• Alphanumeric: ∑ = {a-z, A-Z, 0-9}
• Digits:∑ = { 0-9}
4
Go, change the world
Strings
A string or word is a finite sequence of symbols chosen from ∑
• Empty string is (or “epsilon”)
5
Go, change the world
Powers of an alphabet
Let ∑ be an alphabet.
• ∑* = ∑0 U ∑1 U ∑2 U …
• ∑+ = ∑1 U ∑2 U ∑3 U …
6
Go, change the world
Languages
L is a said to be a language over alphabet ∑, only if L ∑*
➔ this is because ∑* is the set of all strings (of all possible length including 0) over the given
alphabet ∑
Examples:
1. Let L be the language of all strings consisting of n 0’s followed by n 1’s:
L = {, 01, 0011, 000111,…}
2. Let L be the language of all strings of with equal number of 0’s and 1’s:
L = {, 01, 10, 0011, 1100, 0101, 1010, 1001,…}
Finite Automata-Applications
❖Software for designing and checking the behavior of digital circuits
❖Software for scanning large bodies of text (e.g., web pages) for pattern
finding
❖Software for verifying systems of all types that have a finite number of states
(e.g., stock market transaction, communication/network protocol)
8
Go, change the world
9
Go, change the world
11
Go, change the world
12
Go, change the world
Regular Languages
• Let L(A) be a language recognized by a DFA A.
• Then L(A) is called a “Regular Language”.
Regular
Context-
(DFA)
free Context-
(PDA) sensitive Recursively-
(LBA) enumerable
(TM)
Example #1
• Build a DFA for the following language:
• L = {w | w is a binary string that contains 01 as a substring}
• Steps for building a DFA to recognize L:
• ∑ = {0,1}
• Decide on the states: Q
• Designate start state and final state(s)
• δ: Decide on the transitions:
• “Final” states == same as “accepting states”
• Other states == same as “non-accepting states”
14
Go, change the world
Regular expression: (0+1)*01(0+1)*
1 0 0,1 • ∑ = {0,1}
• start state = q0
start 0 1
q0 q1 q2 • F = {q2}
Accepting • Transition table
state symbols
0 1
q0 q1 q0
• What if the language allows
states
q1 q1 q2
empty strings? *q2 q2 q2
15
Go, change the world
Example #2
• Build a DFA for the following language:
L = { w | where leftmost symbol differs from rightmost symbol, ∑ ={a,b}}
16
Go, change the world
Example #3
• Build a DFA for the following language:
L = { Except containing substring aab, ∑ ={a , b} }
17
Go, change the world
Example #4
• Build a DFA for the following language:
L = {W: |W| mod 5 !=0, W €(0,1)}
18
Go, change the world
Example #5
• Build a DFA for the following language:
L={ab3Wb2 : W € {a,b}*}
19
Go, change the world
Example #6
• Build a DFA for the following language:
L = {aWa | ∑ ={a , b} }
20
Go, change the world
• δ (q,wa) = δ (δ(q,w), a)
21
Go, change the world
• δ (q,wa) = δ (δ(q,w), a)
22
Go, change the world
δ (q0, ) =q0
24
Go, change the world
δ (q0, ) =q0
25
Go, change the world
26
Go, change the world
1 qj
qi … • Each transition function therefore
1 maps to a set of states
qk
27
Go, change the world
28
Go, change the world
29
Regular expression: (0+1)*01(0+1)* Go, change the world
states
an input of 0 is received? q1 Φ {q2}
*q2 {q2} {q2}
30
Go, change the world
Example #2
• Build an NFA for the following language:
L = { w | w ends in 01}
31
Go, change the world
Example #3
• Build an NFA for the following language:
L = {aWa | ∑ ={a , b} }
32
Go, change the world
Example #4
• Build an NFA for the following language:
L={ab3Wb2 : W € {a,b}*}
33
Go, change the world
Example #5
• Build an NFA for the following language:
L = { w | where leftmost symbol differs from rightmost symbol, ∑
={a,b}}
34
Go, change the world
Example #6
• Build an NFA for the following language:
L = { w | w is ababn or aban |n>=0}
35
Go, change the world
• Induction:
• Let δ (q0,w) = {p1,p2…,pk}
• δ (pi,a) = Si for i=1,2...,k
• Then, δ (q0,wa) = S1 U S2 U … U Sk
36
Go, change the world
Language of an NFA
• An NFA accepts w if there exists at least one path from the start state
to an accepting (or final) state that is labeled by w
• L(N) = { w | δ(q0,w) ∩ F ≠ Φ }
37
Go, change the world
38
Go, change the world
But, DFAs and NFAs are equivalent in their power to capture langauges !!
39
Go, change the world
40
Go, change the world
41
Go, change the world
• Let N = {QN,∑,δN,q0,FN}
• Goal: Build D={QD,∑,δD,{q0},FD} s.t. L(D)=L(N)
• Construction:
1. QD= all subsets of QN (i.e., power set)
2. FD=set of subsets S of QN s.t. S∩FN≠Φ
3. δD: for each subset S of QN and for each input symbol a in ∑:
• δD(S,a) = U δN(p,a)
p in s
42
Idea: To avoid enumerating all of
Go, change the world
power set, do
“lazy creation of states”
NFA to DFA construction: Example
• L = {w | w ends in 01}
1 0
NFA: DFA: 0 1
0,1 [q0] [q0,q1] [q0,q2]
0
0 1 1
q0 q1 q2
δD 0 1 δD 0 1
δN 0 1
Ø Ø Ø [q0] [q0,q1] [q0]
q0 {q0,q1} {q0} [q0] {q0,q1} {q0} [q0,q1] [q0,q1] [q0,q2]
q1 Ø {q2} [q1] Ø {q2} *[q0,q2] [q0,q1] [q0]
*q2 Ø Ø *[q2] Ø Ø
[q0,q1] {q0,q1} {q0,q2}
*[q0,q2] {q0,q1} {q0} 0. Enumerate all possible subsets
*[q1,q2] Ø {q2} 1. Determine transitions
*[q0,q1,q2] {q0,q1} {q0,q2} 2. Retain only those states
reachable from {q0}
43
Go, change the world
δN 0 1 δD 0 1
q0 {q0,q1} {q0} [q0] [q0,q1] [q0]
q1 Ø {q2} [q0,q1] [q0,q1] [q0,q2]
*q2 Ø Ø *[q0,q2] [q0,q1] [q0]
Main Idea:
Introduce states as you go
(on a need basis)
44
Go, change the world
45
Go, change the world
46
Go, change the world
Applications
• Text indexing
• inverted indexing
• For each unique word in the database, store all locations that contain it using
an NFA or a DFA
• Find pattern P in text T
• Example: Google querying
• Extensions of this idea:
• PATRICIA tree, suffix tree
47
Go, change the world
48
Go, change the world
FA with -Transitions
• We can allow explicit -transitions in finite automata
• i.e., a transition from one state to another state without consuming any
additional input symbol
• Explicit -transitions between different states introduce non-determinism.
• Makes it easier sometimes to construct NFAs
Definition: -NFAs are those NFAs with at least one explicit -
transition defined.
• -NFAs have one more column in their transition table
49
Go, change the world
Example of an -NFA
L = {w | w is empty, or if non-empty will end in 01}
0,1
0 1
• -closure of a state q, ECLOSE(q), is
q0 q1 q2 the set of all states (including itself)
that can be reached from q by
start q’0 repeatedly making an arbitrary
number of -transitions.
δE 0 1
ECLOSE(q’0)
*q’0 Ø Ø {q’0,q0}
q0 {q0,q1} {q0} {q0} ECLOSE(q0)
q1 Ø {q2} {q1} ECLOSE(q1)
*q2 Ø Ø {q2} ECLOSE(q2) 50
Go, change the world
Example of an -NFA
To simulate any transition:
Step 1) Go to all immediate destination states.
Step 2) From there go to all their -closure states as well.
L = {w | w is empty, or if non-empty will end in 01}
0,1
Simulate for w=101:
0 1
q0 q1 q2
q 0’
start q’0
q 0’ q0
1 1
δE
q0
0 1 Ø
x 0
ECLOSE(q’0)
*q’0 Ø Ø {q’0,q0} q1
q0 {q0,q1} {q0} {q0} ECLOSE(q0) 1
q1 Ø {q2} {q1} q2
*q2 Ø Ø {q2} 51
Go, change the world
δE 0 1
*q’0 Ø Ø {q’0,q0,q3}
q0 {q0,q1} {q0} {q0,q3}
q1 Ø {q2} {q1}
*q2 Ø Ø {q2}
q3 Ø {q2} {q3}
52
Go, change the world
• Implication:
• DFA ≡ NFA ≡ -NFA
• (all accept Regular Languages)
53
Go, change the world
Eliminating -transitions
Let E = {QE,∑,δE,q0,FE} be an -NFA
Goal: To build DFA D={QD,∑,δD,{qD},FD} s.t. L(D)=L(E)
Construction:
1. QD= all reachable subsets of QE factoring in -closures
2. qD = ECLOSE(q0)
3. FD=subsets S in QD s.t. S∩FE≠Φ
4. δD: for each subset S of QE and for each input symbol a∑:
• Let R= U δE(p,a) // go to destination states
p in s
54
Go, change the world
0,1
0 1
q0 q1 q2
start q’0
δE δD
0 1
0 1
0,1 0
0
0 1 {q0,q1}
q0 q1 q2 0 1 {q0,q2}
0
1
start start {q’0, q0} 1
q’0 q0 1
ECLOSE union
δE δD
0 1
0 1
57
Go, change the world
Applications of interest
• Comparing two DFAs:
• L(DFA1) == L(DFA2)?
58
Go, change the world
p
w
q
➔ p≡q
59
Go, change the world
A =
0 1
B = =
0 1 0
A C E G C x x =
1 0 1
0 1 D x x x =
1 1 0 E x x x x =
B D F H
1 0 F x x x x x =
0
Pass #0 G x x x = x x =
1. Mark accepting states ≠ non-accepting states
Pass #1
H x x = x x x x =
1. Compare every pair of states A B C D E F G H
2. Distinguish by one symbol transition
3. Mark = or ≠ or blank(tbd)
Pass #2
1. Compare every pair of states
2. Distinguish by up to two symbol transitions (until different or same or tbd)
…. 60
(keep repeating until table complete)
Go, change the world
A =
0 1
B =
0 1 0
A C E G C =
1 0 1
0 1 D =
1 1 0 E =
B D F H
1 0 F =
0
G =
H =
A B C D E F G H
61
Go, change the world
A =
0 1
B =
0 1 0
A C E G C =
1 0 1
0 1 D =
1 1 0 E X X X X =
B D F H
1 0 F X =
0
G X =
1. Mark X between accepting vs. non-accepting state H X =
A B C D E F G H
62
Go, change the world
A =
0 1
B =
0 1 0
A C E G C X =
1 0 1
0 1 D X =
1 1 0 E X X X X =
B D F H
1 0 F X =
0
G X X =
1. Mark X between accepting vs. non-accepting state H X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H
63
Go, change the world
A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X =
1 1 0 E X X X X =
B D F H
1 0 F X =
0
G X X X =
1. Mark X between accepting vs. non-accepting state H X X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H
64
Go, change the world
A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
1 1 0 E X X X X =
B D F H
1 0 F X X =
0
G X X X X =
1. Mark X between accepting vs. non-accepting state H X X = X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H
65
Go, change the world
A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
1 1 0 E X X X X =
B D F H
1 0 F X X X =
0
G X X X = X =
1. Mark X between accepting vs. non-accepting state H X X = X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H
66
Go, change the world
A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
1 1 0 E X X X X =
B D F H
1 0 F X X X =
0
G X X X = X X =
1. Mark X between accepting vs. non-accepting state H X X = X X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H
67
Go, change the world
A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
1 1 0 E X X X X =
B D F H
1 0 F X X X =
0
G X X X = X X =
1. Mark X between accepting vs. non-accepting state H X X = X X X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H
68
Go, change the world
A =
0 1
B = =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
1 1 0 E X X X X =
B D F H
1 0 F X X X X X =
0
G X X X = X X =
1. Mark X between accepting vs. non-accepting state H X X = X X X X =
2. Pass 1: A B C D E F G H
Look 1- hop away for distinguishing states or strings
3. Pass 2:
Look 1-hop away again for distinguishing states or strings
continue….
69
Go, change the world
A =
0 1
B = =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
1 1 0 E X X X X =
B D F H
1 0 F X X X X X =
0
G X X X = X X =
1. Mark X between accepting vs. non-accepting state H X X = X X X X =
2. Pass 1: A B C D E F G H
Look 1- hop away for distinguishing states or strings
3. Pass 2:
Look 1-hop away again for distinguishing states or strings Equivalences:
continue…. • A=B
• C=H
• D=G 70
Go, change the world
0 1 0 1
0 1 0 0 1
A C E G A C E
1 0 1 1 0
0 1 0
1 1 0 1
B D F H D F
1 0 1 0
0
Equivalences:
• A=B
• C=H
• D=G 71
Go, change the world
A =
0 1
B =
0 1 0
A C E G C =
1 0 1
0 1 D =
1 1 0 E ? =
B D F H
1 0 F =
0
G =
H =
73
Go, change the world
q0 …
Is q0 ≡ q0’?
: if yes, then DFA1≡DFA2
DFA2 : else, not equiv.
q 0’ …
Regular Expressions
75
Go, change the world
• Offers a declarative way to express the pattern of any string we want to accept
• E.g., 01*+ 10*
Regular Expressions
Regular
Languages
Formal language
classes
77
Go, change the world
Language Operators
• Union of two languages:
• L U M = all strings that are either in L or M
• Note: A union of two languages produces a third language
78
Go, change the world
“i” here refers to how many strings to concatenate from the parent language L to produce
strings in the language Li
80
Go, change the world
81
Go, change the world
83
Go, change the world
Precedence of Operators
• Highest to lowest
• * operator (star)
• . (concatenation)
• + operator
• Example:
• 01* + 1 = ( 0 . ((1)*) ) + 1
84
Go, change the world
-NFA NFA
Theorem 1
Kleene Theorem
Reg Ex DFA
Theorem 2
85
Go, change the world
DFA Reg Ex
DFA to RE construction Theorem 2
Example: 1 0 0,1
q0 0 q1 1 q2
1* 00* 1 (0+1)*
Q) What is the language?
1*00*1(0+1)* 86
Go, change the world
State Elimination
• Consider the figure below, which shows a generic state s about to be eliminated. The labels on all edges are
regular expressions.
• To remove s, we must make labels from each qi to p1 up to pm that include the paths we could have made through
s.
R1m
R11+Q1S*P1
q1 p1 q1 p1
R11
Q1 P1 R1m+Q1S*Pm
S
. . . .
. s . . .
. . . .
QK Pm Rk1+QkS*P1
Rkm
qk pm qk pm
Rkm+QkS*Pm
Rk1
Note: q and p may be the same state!
Go, change the world
R U
S
Start
Start
DFA→RE Example
• Convert the following to a RE
0 0,1
1 1
Start 3 1 2
• First convert the edges to RE’s:
0
0 0+1
1 1
Start 3 1 2
0
Go, change the world
1 1
Start 3 1 2
• To: 0
0+10 0+1
Note edge from 3→3
11
Start 3 2
Answer: (0+10)*11(0+1)*
Go, change the world
Second Example
• Automata that accepts even number of 1’s
0 0 0
1 1
Start 1 2 3
• Eliminate state 2:
1
0 0+10*1
10*1
Start 1 3
Go, change the world
10*1
Start 1 3
10*1
Start 1 3
This is just 0*; can ignore going to state 3 since we would “die”
Go, change the world
10*1
Start 1 3
0 0+10*1
97
Go, change the world
ε S ε
R=S+T
ε ε
T
ε
R=ST
S T
ε ε
R=S*
S
ε
Go, change the world
RE to ε-NFA Example
• Convert R= (ab+a)* to an NFA
• We proceed in stages, starting from simple elements and working our way up
a
a
b
b
a ε b
ab
Go, change the world
a
ε ε
(ab+a)*
a ε b
ε ε
ε ε
a
ε ε
ε
Go, change the world
-NFA
RE to -NFA construction
Reg Ex
Theorem 1
Example: (0+1)*01(0+1)*
(0+1)* 01 (0+1)*
0
0
0 1
1
1
101
Go, change the world
Algebraic Laws…
• Distributive:
• E(F+G) = EF + EG
• (F+G)E = FE+GE
• Idempotent: E + E = E
• Involving Kleene closures:
• (E*)* = E*
• Φ* =
• * =
• E+ =EE*
• E? = +E
103
Go, change the world
True or False?
Let R and S be two regular expressions. Then:
1. ((R*)*)* = R* ?
2. (R+S)* = R* + S* ?
104
Go, change the world
Summary
• DFA –Definition, Transition diagrams & tables
• Regular language
• NFA- Definition, Transition diagrams & tables
• DFA vs. NFA
• NFA to DFA conversion using subset construction
• Equivalency of DFA & NFA
• Removal of redundant states and including dead states
• -transitions in NFA
• Text searching applications
• Simplification of DFAs
• How to remove unreachable states?
• How to identify and collapse equivalent states?
• How to minimize a DFA?
• How to tell whether two DFAs are equivalent? 105
Go, change the world
Summary
• Regular expressions
• Equivalence to finite automata
• DFA to regular expression conversion
• Regular expression to -NFA conversion
• Algebraic laws of regular expressions
• Unix regular expressions and Lexical Analyzer
106