0% found this document useful (0 votes)
2 views92 pages

FiniteAutomata_myppt-1

The document provides an overview of Finite Automata (FA), detailing both Deterministic Finite Automata (DFA) and Non-deterministic Finite Automata (NFA), including their definitions, structures, and how they process input strings. It explains the construction of DFAs and NFAs, their equivalence in language recognition, and introduces concepts like ε-transitions. Additionally, the document highlights applications of finite automata in text indexing and pattern matching.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views92 pages

FiniteAutomata_myppt-1

The document provides an overview of Finite Automata (FA), detailing both Deterministic Finite Automata (DFA) and Non-deterministic Finite Automata (NFA), including their definitions, structures, and how they process input strings. It explains the construction of DFAs and NFAs, their equivalence in language recognition, and introduces concepts like ε-transitions. Additionally, the document highlights applications of finite automata in text indexing and pattern matching.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 92

Finite Automata

1
Finite Automaton (FA)
◼ Informally, a state diagram that comprehensively
captures all possible states and transitions that a
machine can take while responding to a stream or
sequence of input symbols
E.g. Recognizer for “Regular Languages”

2
3
4
◼ Deterministic Finite Automata (DFA)
◼ The machine can exist in only one state at
any given time
◼ Non-deterministic Finite Automata
(NFA)
◼ The machine can exist in multiple states at
the same time

5
Deterministic Finite Automata
- Definition
◼ A Deterministic Finite Automaton (DFA)
consists of:
◼ Q ==> a finite set of states
◼ ∑ ==> a finite set of input symbols (alphabet)
◼ q0 ==> a start state
◼ F ==> set of accepting states
◼ δ ==> a transition function, which is a mapping
between Q x ∑ ==> Q
◼ A DFA is defined by the 5-tuple:
◼ {Q, ∑ , q0,F, δ }

6
What does a DFA do on
reading an input string?
◼ Input: a word w in ∑*
◼ Question: Is w acceptable by the DFA?
◼ Steps:
◼ Start at the “start state” q0
◼ For every input symbol in the sequence w do
◼ Compute the next state from the current state, given the
current input symbol in w and the transition function
◼ If after all symbols in w are consumed, the current
state is one of the accepting states (F) then accept
w;
◼ Otherwise, reject w.
7
Regular Languages

Let L(A) be a language recognized by a


DFA A.
Then L(A) is called a “Regular Language”.

8
The Chomsky Hierachy
• A containment hierarchy of classes of formal languages

Regular Context-
(DFA) Context-
free Recursively-
sensitive
(PDA) enumerable
(LBA) (TM)

9
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”

10
Regular expression: (0+1)*01(0+1)*

DFA for strings containing 01

• What makes this DFA deterministic?


• Q = {q0,q1,q2}
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
states

• What if the language allows q1 q1 q2


empty strings? *q2 q2 q2

11
Question-1

◼ Build a DFA for the following language:


L = { w | w is a binary string that has even
number of 1s and even number of 0s}

12
13
Extension of transitions (δ) to
Paths (δ)

◼ δ (q,w) = destination state from state q


on input string w

◼ δ (q,wa) = δ (δ(q,w), a)

14
Consider the finite automata/state machine whose transition function is given by the
following transition table. Here, Q= {q0, q1, q2, q3}, ∑ = {0,1}, F={q0}. Give
the entire sequence of 110101

15
16
Language of a DFA
A DFA A accepts string w if there is a
path from q0 to an accepting (or final)
state that is labeled by w

◼ i.e., L(A) = all strings that lead to an


accepting state from q0

17
18
Non-deterministic Finite
Automata (NFA)
◼ A Non-deterministic Finite Automaton
(NFA)
◼ is of course “non-deterministic”
◼ Implying that the machine can exist in more
than one state at the same time
◼ Transitions could be non-deterministic
1 qj
qi … • Each transition function therefore
1 maps to a set of states
qk
19
Non-deterministic Finite
Automata (NFA)
◼ A Non-deterministic Finite Automaton (NFA)
consists of:
◼ Q ==> a finite set of states
◼ ∑ ==> a finite set of input symbols (alphabet)
◼ q0 ==> a start state
◼ F ==> set of accepting states
◼ δ ==> a transition function, which is a mapping
between Q x ∑ ==> subset of Q
◼ An NFA is also defined by the 5-tuple:
◼ {Q, ∑ , q0,F, δ }

20
How to use an NFA?
◼ Input: a word w in ∑*
◼ Question: Is w acceptable by the NFA?
◼ Steps:
◼ Start at the “start state” q0
◼ For every input symbol in the sequence w do
◼ Determine all possible next states from all current states, given
the current input symbol in w and the transition function
◼ If after all symbols in w are consumed and if at least one of
the current states is a final state then accept w;
◼ Otherwise, reject w.

21
Regular expression: (0+1)*01(0+1)*

NFA for strings containing 01

Why is this non-deterministic?


• Q = {q0,q1,q2}
0,1 0,1 •  = {0,1}
• start state = q0
start 0 1
q0 q1 q2 • F = {q2}
Final • Transition table
state symbols
0 1
What will happen if at state q1 q0 {q0,q1} {q0}
states

an input of 0 is received? q1 Φ {q2}


*q2 {q2} {q2}

22
Question-2
◼ Build an NFA for the following language:
L = { w | w ends in 01}
◼ Other examples
◼ Keyword recognizer (e.g., if, then, else,
while, for, include, etc.)
◼ Strings where the first symbol is present
somewhere later on at least once

23
Advantages & Caveats for NFA
◼ Great for modeling regular expressions
◼ String processing - e.g., grep, lexical analyzer

◼ Could a non-deterministic state machine be


implemented in practice?
◼ Yes but in a limited way
◼ Probabilistic models could be viewed as extensions of non-
deterministic state machines
(e.g., toss of a coin, a roll of dice)
◼ They are not the same though

◼ A parallel computer could exist in multiple “states” at the same time

24
But, DFAs and NFAs are equivalent in their power to capture langauges !!

Differences: DFA vs. NFA


◼ DFA ◼ NFA
1. All transitions are 1. Some transitions could be
deterministic non-deterministic
◼ Each transition leads to ◼ A transition could lead to a
exactly one state subset of states
2. Accepts input if the last state 2. Accepts input if one of the last
visited is in F states is in F
3. Sometimes harder to 3. Generally easier than a DFA
construct because of the to construct
number of states 4. Practical implementations
4. Practical implementation is limited but emerging (e.g.,
feasible Micron automata processor)

25
26
27
Equivalence of DFA & NFA
◼ Theorem:
Should be ◼ A language L is accepted by a DFA if and only if
true for
any L
it is accepted by an NFA.
◼ Proof:
1. If part:
◼ Prove by showing every NFA can be converted to an
equivalent DFA (in the next few slides…)

2. Only-if part is trivial:


◼ Every DFA is a special case of an NFA where each
state has exactly one transition for every input symbol.
Therefore, if L is accepted by a DFA, it is accepted by
a corresponding NFA.
28
Proof for the if-part
◼ If-part: A language L is accepted by a DFA if
it is accepted by an NFA
◼ rephrasing…
◼ Given any NFA N, we can construct a DFA D
such that L(N)=L(D)
◼ How to convert an NFA into a DFA?
◼ Observation: In an NFA, each transition maps to a
subset of states
◼ Idea: Represent:
each “subset of NFA_states” ➔ a single “DFA_state”
Subset construction
29
NFA to DFA by subset construction
◼ 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

30
Idea: To avoid enumerating all of
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}
31
NFA to DFA: Repeating the example
using LAZY CREATION
◼ 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

δ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)
32
Question-3

33
34
Question-4

35
36
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

39
A few subtle properties of
DFAs and NFAs
◼ The machine never really terminates.
◼ It is always waiting for the next input symbol or making

transitions.
◼ The machine decides when to consume the next symbol from
the input and when to ignore it.
◼ (but the machine can never skip a symbol)

◼ => A transition can happen even without really consuming an


input symbol (think of consuming  as a free token) – if this
happens, then it becomes an -NFA (see next few slides).
◼ A single transition cannot consume more than one (non-)
symbol.

40
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 41
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,
q0 q1 q2 ECLOSE(q), is the set

of all states (including
start q’0 itself) that can be
reached from q by
δE repeatedly making an
0 1

*q’0 Ø Ø {q’0,q0}
ECLOSE(q’0) arbitrary number of -
q0 {q0,q1} {q0} {q0} ECLOSE(q0) transitions.
q1 Ø {q2} {q1} ECLOSE(q1)
*q2 Ø Ø {q2}
ECLOSE(q2) 42
To simulate any transition:
Step 1) Go to all immediate destination states.
Step 2) From there go to all their -closure states as well.

Example of an -NFA
L = {w | w is empty, or if non-empty will end in 01}
0,1

0 1
Simulate for w=101:
q0 q1 q2
 q0’
 
start q’0
q0’ 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}
43
To simulate any transition:
Step 1) Go to all immediate destination states.
Step 2) From there go to all their -closure states as well.

Example of another -NFA


0,1 Simulate for w=101:
q0
0
q1
1
q2 ?
  1

start q’0 q3

δE 0 1

*q’0 Ø Ø {q’0,q0,q3}
q0 {q0,q1} {q0} {q0,q3}
q1 Ø {q2} {q1}
*q2 Ø Ø {q2}
q3 Ø {q2} {q3}

44
Equivalency of DFA, NFA, -NFA
◼ Theorem: A language L is accepted by
some -NFA if and only if L is accepted by
some DFA

◼ Implication:
◼ DFA ≡ NFA ≡ -NFA
◼ (all accept Regular Languages)

45
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
◼ δD(S,a) = U ECLOSE(r) // from there, take a union
r in R of all their -closures

46
Example: -NFA ➔ DFA
L = {w | w is empty, or if non-empty will end in 01}
0,1

0 1
q0 q1 q2

start q’0

δE δD
0 1
 0 1

*q’0 Ø Ø {q’0,q0} *{q’0,q0}


q0 {q0,q1} {q0} {q0} …
q1 Ø {q2} {q1}
*q2 Ø Ø {q2}
47
Example: -NFA ➔ DFA
L = {w | w is empty, or if non-empty will end in 01}
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

*q’0 Ø Ø {q’0,q0} *{q’0,q0} {q0,q1} {q0}


q0 {q0,q1} {q0} {q0} {q0,q1} {q0,q1} {q0,q2}
q1 Ø {q2} {q1} {q0} {q0,q1} {q0}
*q2 Ø Ø {q2} *{q0,q2} {q0,q1} {q0}
48
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
49
Internet sources
1.https://www.eecs.wsu.edu/~ananth/CptS317/Lectures/
index.htm
2. https://nptel.ac.in/courses/106/104/106104028/
3. https://nptel.ac.in/courses/106/104/106104148/

50
◼ Draw a DFA for the language accepting strings
ending with ‘abba’ over input alphabets ∑ = {a,
b}
◼ Draw a DFA for the language accepting strings
starting with ‘ab’ over input alphabets ∑ = {a, b}
◼ Draw a non-deterministic finite automate which
accepts 00 or 11 at the end of a string
containing 0, 1 in it, e.g., 01010100 but not
000111010.
◼ 0101011 or
◼ 011 or
◼ 100
◼ 0011
0101011 or
011 or
100
0011
◼ Draw a non-deterministic finite automata which
accept a string containing “ing” at the end of a
string in a string of {a-z}, e.g., “anything” but not
“anywhere”.
◼ Convert the following Non-Deterministic Finite
Automata (NFA) to Deterministic Finite Automata
(DFA)-
◼ Convert the following Non-Deterministic Finite
Automata (NFA) to Deterministic Finite Automata
(DFA)-
Find the string recognized by
this FA
Describe in English the set
accepted by this FA
NFA
DFA
67
68
69
Equivalence & Minimization of
DFAs

70
Applications of interest
◼ Comparing two DFAs:
◼ L(DFA1) == L(DFA2)?

◼ How to minimize a DFA?


1. Remove unreachable states
2. Identify & condense equivalent states into one

71
When to call two states in a DFA
“equivalent”?
Two states p and q are said to be
equivalent iff:
i) Any string w accepted by starting at p is also accepted by
starting at q;
p
w
AND q

i) Any string w rejected by starting at p is also rejected by


starting at q.
p
w
q
➔ p≡q
72
73
74
Computing equivalent states
in a DFA Table Filling Algorithm
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)
….
(keep repeating until table complete) 75
Table Filling Algorithm - step
by step
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

76
Table Filling Algorithm - step
by step
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

77
Table Filling Algorithm - step
by step
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

78
Table Filling Algorithm - step
by step
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

79
Table Filling Algorithm - step
by step
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

80
Table Filling Algorithm - step
by step
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

81
Table Filling Algorithm - step
by step
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

82
Table Filling Algorithm - step
by step
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

83
Table Filling Algorithm - step
by step
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….

84
Table Filling Algorithm - step
by step
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
85
Table Filling Algorithm - step
by step
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

Retrain only one copy for


each equivalence set of stat
Equivalences:
• A=B
• C=H
• D=G
86
Table Filling Algorithm –
special case
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
Q) What happens if the input DFA
has more than one final state?
Can all final states initially be treated
as equivalent to one another? 87
Putting it all together …

How to minimize a DFA?


◼ Goal: Minimize the number of states in
a DFA
Depth-first traversal from the start sta
◼ Algorithm:
1. Eliminate states unreachable from the
start state Table filling algorithm
2. Identify and remove equivalent states
3. Output the resultant DFA

88
Are Two DFAs Equivalent?
Unified DFA DFA1

q0 …
Is q0 ≡ q0’?
: if yes, then DFA1≡DFA2
DFA2 : else, not equiv.

q0’ …

1. Make a new dummy DFA by just putting together both DFAs


2. Run table-filling algorithm on the unified DFA
3. IF the start states of both DFAs are found to be equivalent,
THEN: DFA1≡ DFA2
ELSE: different
89
90
91
92
93
94

You might also like