0% found this document useful (0 votes)
205 views146 pages

Chapter 2 Finite Automata PDF

Finite automata are abstract models of computation that can recognize patterns in strings. They have a finite set of states, transitions between states that are triggered by individual input symbols, a start state, and one or more accepting states. A finite automaton accepts a string if it ends in an accepting state after reading the entire input string according to the state transitions. Common applications of finite automata include pattern matching, lexical analysis in compilers, and modeling network protocols and other systems with a finite number of states.

Uploaded by

Yaffa
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)
205 views146 pages

Chapter 2 Finite Automata PDF

Finite automata are abstract models of computation that can recognize patterns in strings. They have a finite set of states, transitions between states that are triggered by individual input symbols, a start state, and one or more accepting states. A finite automaton accepts a string if it ends in an accepting state after reading the entire input string according to the state transitions. Common applications of finite automata include pattern matching, lexical analysis in compilers, and modeling network protocols and other systems with a finite number of states.

Uploaded by

Yaffa
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/ 146

2.1.

Finite Automata
• An automaton is an abstract model of a digital
computer.
• An automaton has a mechanism to read input, which
is a string over a given alphabet.
• This input is actually written on an “input file”,
which can be read by the automaton but cannot
change it.
• A Finite Automata is:
– a mechanism to recognize a set of valid
inputs before carrying out an action.
– a notation for describing a family of
language recognition algorithms.
1
Operation of the machine
0 0 1 Input Tape
Tape Head
q0 Finite Control
• Read the current letter of input
under the tape head.
• Transit to a new state depending on
the current input and the current
state, as dictated by the transition
function.
• Halt after consuming the entire input.
2
Operation of the machine
• Transitions show the initial state, input,
and next state
– Form: (q,a)=b
• Example:
– (q0,0)=q1 (q0,1)=q2
• Tape head advances to next cell, in state q1
• What happens now?
– What is (q1,0)?

0 0 1

q1
3
Definition of a Finite Automaton
1. Finite set of states, typically Q.
2. Alphabet of input symbols, typically 
3. One state is the start/initial state, typically q0
// q0  Q
4. Zero or more final/accepting states; the set is
typically F. // F  Q
5. A transition function, typically δ. This function
• Takes a state and input symbol as arguments.
• Returns a state.
• One “rule” would be written δ(q, a) = p, where q and p
are states, and a is an input symbol.
• Intuitively: if the FA is in state q, and input a is
received, then the FA goes to state p (note: q = p OK).
6. A FA is represented as the five-tuple: A = (Q, ,
δ,q0, F). Here, F is a set of accepting states.
4
Definition of Computation
• Let M = (Q, , δ,q0, F) be a finite
automaton and let w = w1w2…wn be a
string where each wi is a member of
alphabet ∑.
• M accepts w if a sequence of states
r0r1…rn in Q exists with three
conditions:
1. r0 = q0
2. δ(ri, wi+1) = ri+1 for i=0, … , n-1
3. rn  F
We say that M recognizes language A if A = {w | M accepts w }
In other words, the language is all of those strings that are accepted
by the finite automata.
5
Construct
• A Finite Automaton Accepting the Language
of Strings Ending in aa, i.e.
L= {x ∈ {a, b}* | x ends with aa}

• An FA Accepting the Language


of Strings Ending in b and Not Containing
the Substring aa
L = {x ∈ {a, b}∗ | x ends with b
and does not contain the
substring aa}

6
Algebra for Languages
1. The union of two languages L and M is the
set of strings that are in both L and M.
• Example: if L = { 0, 1} and M = {111} then L 
M is {0, 1, 111}.
2. The concatenation of languages L and M is
the set of strings that can be formed by
taking any string in L and concatenating it
with any string in M. Concatenation is
denoted by LM although sometimes we’ll
use LM (pronounced “dot”).
• Example, if L = {0, 1} and M = {ε, 010} then LM
is { 0, 1, 0010, 1010}.
12
Closure Properties of Regular Languages
• Closure refers to some operation on a
language, resulting in a new language that is
of the same “type” as those originally
operated on
– i.e., regular in our case

• The regular languages are closed under


union, concatenation, and *. I.e., if A1 and
A2 are regular languages then
– A1  A2 is also regular
– A1A2 is also regular
– A1* is also regular

8
Finite Automata\Some Applications
– Software for designing and checking the
behavior of digital circuits
– Lexical analyzer of a typical compiler
– 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)

9
• Two types of FA –both describe regular
languages
– Deterministic (DFA) – There is a fixed number of
states and we can only be in one state at a time. It is
one in which each move (transition from one state to
another) is unequally determined by the current
configuration.
– Nondeterministic (NFA) –There is a fixed number of
states but we can be in multiple states at one time
• While NFA’s are more expressive than DFA’s, we
will see that adding nondeterminism does not let
us define any language that cannot be defined by
a DFA.
• One way to think of this is we might write a
program using a NFA, but then when it is
“compiled” we turn the NFA into an equivalent
DFA.
10
A deterministic finite automaton (DFA) is
represented by a quintuple (5-tuple) M = (Q, Σ, , q0,
F) : Q is the set of states (finite)
Σ is the alphabet (finite) λ  Σ
 : Q  Σ → Q is the transition function
q0  Q is the start state
F  Q is the set of accept states
Let w1, ... , wn  Σ and w = w1... wn  Σ*
Then M accepts w if there are r0, r1, ..., rn  Q, s.t. r0=q0
• (ri, wi+1 ) = ri+1, for i = 0, ..., n-1, and rn  F
•The input mechanism can move only from left to right and reads exactly one
symbol on each step.
•The transition from one internal state to another are governed by the transition
function .
•If (q0 , a) =q1 ,then if the DFA is in state q0 and the current input symbol is a, the
DFA will go into state q1.
(DFA)

states accept states (F)


0 q1 1
0,1
1
q0 q2
0 0
1
start state (q0) q3 states

The machine accepts a string if the process


ends in a double circle
NOTATION
An alphabet Σ is a finite set (e.g., Σ = {0,1})

A string over Σ is a finite-length sequence of


elements of Σ

Σ* denotes the set of finite length sequences of


elements of Σ

For x a string, |x| is the length of x


The unique string of length 0 will be denoted by ε
and will be called the empty or null string

A language over Σ is a set of strings over Σ, ie, a


subset of Σ*
Transition Graph
a, b

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

initial accepting
state state
transition
state
14
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

15
head Initial Configuration
Input Tape
a b b a
Input String
a, b

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

Initial state
16
Scanning the Input

a b b a

a, b

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

17
a b b a

a, b

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

18
a b b a

a, b

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

19
Input finished

a b b a

a, b

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

Last state determines the outcome


20
A Rejection Case

a b a
Input String
a, b

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

21
a b a

a, b

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

22
a b a

a, b

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

23
Input finished

a b a

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

Last state determines the outcome


24
Another Rejection Case

Tape is empty
( )

Input Finished (no symbol read)


a, b

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

reject
25
This automaton accepts only one string

Language Accepted: L  abba 

a, b

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

26
Another Example

L  , ab , abba 
a, b

q5
b a a a, b
b
q0 a q1 b q2 b q3 a q4
Accept Accept Accept
state state state
27
Empty Tape
( )

Input Finished a, b

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

accept
28
29
Language Accepted: L  {a b : n  0}
n

a a, b

q0 b q1 a, b q2

30
Another Example
Alphabet:   {1}
1

q0 q1
1
Language Accepted:

EVEN  {x : x   and x is even}


*

 {, 11, 1111, 111111 , }


31
Set of States Q
Example

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

32
Input Alphabet 

  :the input alphabet never contains 

Example
  a, b a, b

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

33
Initial State q0

Example
a, b

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

34
Set of Accepting States F  Q

Example
F  q4  a, b

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

35
Transition Function  :Q  Q

 (q , x )  q 

q x q

Describes the result of a transition


from state q with symbol x

36
Example:
 q0 , a   q1

a, b

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

37
 q0 , b   q5

a, b

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

38
 q2 , b   q3

a, b

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

39
Transition Table for 
symbols
 a b
q0 q1 q5
q1 q5 q2
states

q2 q5 q3
a, b
q3 q4 q5
q4 q5 q5 q5
q5 q5 q5 a, b
b a a b
q0 a q1 b q2 b q3 a q4
40
41
Extended Transition Function

 :Q   Q
* *

 (q ,w )  q 
*

Describes the resulting state


after scanning string w from state q

42
Example:  q0 , ab   q2
*

a, b

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

a, b

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

44
 q1, bba   q4
*

a, b

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

45
Special case:

for any state q

 q ,    q
*

46
In general:  q ,w   q 
*

implies that there is a walk of transitions

w   1 2  k
1 2 k
q q
states may be repeated

q w q
47
More DFA Examples
  {a , b }
a, b
a, b

q0 q0

L(M )  { } L(M )   *

Empty language All strings


48
  {a , b }

a, b

q0 a, b q1

L(M )  {}
Language of the empty string
49
  {a , b }
LM  = { all strings with prefix ab }
a, b

q0 a q1 b q2

b a accept

q3 a, b

50
LM  = { all binary strings containing
substring 001 }

0,1
1 0
1

 0 0 00 1 001
0

51
LM  = { all binary strings without
substring 001 }

1 0 0,1
1

 0 1
0 00 001

0
52

L(M )  awa : w  a , b  *
 a
b
b
q0 a q2 q3

b a
q1

a, b
53
Regular Languages
Definition:
A language L is regular if there is
a DFA M that accepts it ( L(M )  L ) or
A language L is regular if it is recognized by a
deterministic finite automaton (DFA),
i.e. if there is a DFA M such that L = L (M).

The languages accepted by all DFAs


form the family of regular languages

54
Example regular languages:

abba ab, abba


{a b : n  0} awa : w  a , b  
n *

{ all strings in {a,b}* with prefix ab }


{ all binary strings without substring 001}
{x : x  {1} and x is even}
*

{ } {  } {a , b } *

55
L = { w | w contains 001} is regular
L = { w | w has an even number of 1’s} is regular
Theorem: The union of two regular languages is
also a regular language

Intersection THEOREM
Given two languages, L1 and L2, define the intersection of
L1 and L2 as L1  L2 = { w | w  L1 and w  L2 }
Theorem: The intersection of two regular languages is
also a regular language

56
2.3 Nondeterministic Finite Automata
• A NFA (nondeterministic finite automata) is able
to be in several states at once.
– In a DFA, we can only take a transition to a single
deterministic state
– In a NFA we can accept multiple destination states for
the same input.
– You can think of this as the NFA “guesses” something
about its input and will always follow the proper path if
that can lead to an accepting state.
– Another way to think of the NFA is that it travels all
possible paths, and so it remains in many states at once.
As long as at least one of the paths results in an
accepting state, the NFA accepts the input.
• NFA is a useful tool
– More expressive than a DFA.
– BUT we will see that it is not more powerful! For any
NFA we can construct a corresponding DFA
57
An NFA
• Similar to a DFA
1. Finite set of states, typically Q.
2. Alphabet of input symbols, typically 
3. One state is the start/initial state, typically q0
4. Zero or more final/accepting states; the set is typically F.
5. A transition function, typically  . This function:
 Takes a state and input symbol as arguments.
 Returns a set of states instead of a single state, as a DFA
6. A FA is represented as the five-tuple: A = (Q, ,  ,q0, F). Here, F is a set of accepting
states.

58
NFA Example
• NFA accepts ε, a, baba, baa, and aa, but
that it doesn’t accept b, bb, and babba.

q1
b a
ε
a q2 q3
a,b

59
• there are three major differences
– In the NFA:
• The range of δ in the power set 2Q, its value is not a
single element of Q, but a subset of it.
• Allow  as the second argument of δ; make a
transition without consuming an input symbol
• The set δ(qi, a) may be empty; there is no transition
defined

60
Nondeterministic Finite Automaton (NFA)

Alphabet = {a}

q1 a q2
a
q0
a
q3

61
Alphabet = {a}

Two choices q1 a q2
a
q0
a
q3

62
Alphabet = {a}

Two choices q1 a q2 No transition


a
q0
a
q3 No transition

63
First Choice

a a

q1 a q2
a
q0
a
q3

64
First Choice

a a

q1 a q2
a
q0
a
q3

65
First Choice

a a
All input is consumed

q1 a q2 “accept”
a
q0
a
q3

66
Second Choice

a a

q1 a q2
a
q0
a
q3

67
Second Choice

a a
Input cannot be consumed

q1 a q2
a
q0 Automaton Halts
a
q3 “reject”

68
aa is accepted by the NFA:

“accept”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3 q3 “reject”
because this
this computation
computation
is ignored
accepts aa
69
Rejection example

q1 a q2
a
q0
a
q3

70
First Choice

a
“reject”
q1 a q2
a
q0
a
q3

71
Second Choice

q1 a q2
a
q0
a
q3

72
Second Choice

q1 a q2
a
q0
a
q3 “reject”

73
Another Rejection example

a a a

q1 a q2
a
q0
a
q3

74
First Choice

a a a

q1 a q2
a
q0
a
q3

75
First Choice

a a a
Input cannot be consumed

q1 a q2 “reject”
a
q0
a
Automaton halts
q3

76
Second Choice

a a a

q1 a q2
a
q0
a
q3

77
Second Choice

a a a
Input cannot be consumed

q1 a q2
a
q0 Automaton halts
a
q3 “reject”

78
An NFA rejects a string:
if there is no computation of the NFA
that accepts the string.

For each computation:


• All the input is consumed and the
automaton is in a non final state

OR
• The input cannot be consumed
79
a is rejected by the NFA:

“reject”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3 “reject” q3

All possible computations lead to rejection


80
aaa is rejected by the NFA:

“reject”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3 q3 “reject”

All possible computations lead to rejection


81
Language accepted: L  {aa}

q1 a q2
a
q0
a
q3

82
Lambda Transitions

q0 a q1  q2 a q3

83
a a

q0 a q1  q2 a q3

84
a a

q0 a q1  q2 a q3

85
input tape head does not move

a a

q0 a q1  q2 a q3

86
all input is consumed

a a

“accept”

q0 a q1  q2 a q3

String aa is accepted
87
Rejection Example

a a a

q0 a q1  q2 a q3

88
a a a

q0 a q1  q2 a q3

89
(read head doesn’t move)

a a a

q0 a q1  q2 a q3

90
Input cannot be consumed

a a a

Automaton halts
“reject”

q0 a q1  q2 a q3

String aaa is rejected


91
Language accepted: L  {aa}

q0 a q1  q2 a q3

92
Another NFA Example

q0 a q1 b q2  q3


93
a b

q0 a q1 b q2  q3


94
a b

q0 a q1 b q2  q3


95
a b

“accept”

q0 a q1 b q2  q3


96
Another String

a b a b

q0 a q1 b q2  q3


97
a b a b

q0 a q1 b q2  q3


98
a b a b

q0 a q1 b q2  q3


99
a b a b

q0 a q1 b q2  q3


100
a b a b

q0 a q1 b q2  q3


101
a b a b

q0 a q1 b q2  q3


102
a b a b

“accept”

q0 a q1 b q2  q3


103
Language accepted

L  ab, abab, ababab, ...



 ab

q0 a q1 b q2  q3


104
Another NFA Example

0
q0 q1 0, 1 q2
1

105
Language accepted

L(M ) = {λ, 10, 1010, 101010, ...}


= {10}*
0
q0 q1 0, 1 q2
1 (redundant
state)

106
Transition Function 

 q , x   q1, q2,, qk 

q1
x resulting states with
q x
q1 following one transition
x
with symbol x

qk
107
 q0 , 1  q1

0
q0 q1 0, 1 q
2
1

108
 (q1,0)  {q0 , q2}

0
q0 q1 0, 1 q
2
1

109
 (q0 ,  )  {q2 }

0
q0 q1 0, 1 q
2
1

110
 (q2 ,1)  

0
q0 q1 0, 1 q
2
1

111
*
Extended Transition Function 
Same with  but applied on strings

 q0 , a   q1 
*

q4 q5
a a
q0 a q1 b q2  q3

112
 q0 , aa   q4 , q5 
*

q4 q5
a a
q0 a q1 b q2  q3

113
 q0 , ab   q2, q3, q0 
*

q4 q5
a a
q0 a q1 b q2  q3

114
Special case:

for any state q

q   q ,  
*

115
In general
q j   qi ,w  : there is a walk from qi to q j
*

with label w

qi w qj

w  1 2  k
1 2 k
qi qj

116
The Language of an NFA M
The language accepted by M is:

LM   w1,w2,...wn 

where  (q0 ,wm )  {qi ,..., qk ,, q j }


*

and there is some qk  F (accepting state)


117
wm  LM 
 (q0 ,wm )
*

qi
wm

q0 w
qk qk  F
m

wm qj

118
F  q0 ,q5
q4 q5
a a
q0 a q1 b q2  q3


 q0 , aa   q4 , q5
*
 aa  L(M )
F
119
F  q0 ,q5
q4 q5
a a
q0 a q1 b q2  q3


 q0 , ab   q2, q3, q0
*
 ab  LM 
F
120
q4 q5
a a
q0 a q1 b q2  q3

LM   ab  *  ab  * {aa }

121
Example of equivalent machines

NFA M1
LM1   {10} * 0
q0 q1
1

DFA M2 0,1
LM 2   {10} * 0
q0 q1 1 q2
1
0
122
Theorem:


Languages
Regular
accepted
Languages
by NFAs
Languages
accepted
by DFAs

NFAs and DFAs have the same computation power,


accept the same set of languages
123
Equivalence of DFA’s and NFA’s
• For most languages, NFA’s are easier to
construct than DFA’s
• But it turns out we can build a corresponding
DFA for any NFA
– The downside is there may be up to 2n states in
turning a NFA into a DFA. However, for most
problems the number of states is approximately
equivalent.
• Theorem: A language L is accepted by some
DFA if and only if L is accepted by some
NFA; i.e. : L(DFA) = L(NFA) for an
appropriately constructed DFA from an
NFA.

124
Conversion NFA to DFA
NFA M
a
q a
0 q 1
 q 2
b

DFA M
q0 

125
 * (q0 , a )  {q1, q2 }
NFA M a
q0 a q1  q2
b

DFA M
q0  a
q1, q2 

126
 * (q0 , b )   empty set

NFA M a
q0 a q1  q2
b

DFA M
q0  a
q1, q2 
b

 trap state
127
 (q1, a )  {q1 , q2 }
*

NFA M a  * (q2, a )  
q0 a q1  q2 union

b q1, q2 

a
DFA M
q0  a
q1, q2 
b


128
 (q1, b )  {q0 }
*

NFA M a  * (q2, b )  {q0 }


a  union
q0 q1 q2
b q0 

a
DFA M b

q0  a
q1, q2 
b


129
NFA M a
q0 a q1  q2
b

a
DFA M b

q0  a
q1, q2 
b

 a, b trap state
130
END OF CONSTRUCTION

NFA M a
q0 a q1  q2 q1  F
b
a
DFA M b

q0  a
q1, q2 
q1, q2  F 
b

 a, b
131
General Conversion Procedure

Input: an NFA M

Output: an equivalent DFA M 


with LM   L(M )

132
The NFA has states q0 , q1, q2 ,...

The DFA has states from the power set

, q0 , q1 , q0 , q1 , q1, q2, q3, ....

133
Conversion Procedure Steps

step
1. Initial state of NFA: q0

Initial state of DFA: q0 

134
Example
NFA M a
q0 a q1  q2
b

DFA M
q0 

135
step
2. For every DFA’s state {qi , q j ,..., qm }

compute in the NFA


 * qi , a 
  * q j , a 
Union
 {qk , ql,..., qn }
...
  * qm , a 
add transition to DFA
 {qi , q j ,..., qm }, a   {qk , ql,..., qn }
136
Example  * (q0 , a)  {q1 , q2 }
NFA M a
q0 a q1  q2
b

DFA M  q0 , a   q1 , q2 


q0  a
q1, q2 

137
step
3. Repeat Step 2 for every state in DFA and
symbols in alphabet until no more states
can be added in the DFA

138
Example
NFA M a
q0 a q1  q2
b

a
DFA M b

q0  a
q1, q2 
b

 a, b
139
step
4. For any DFA state {qi , q j ,..., qm }

if some q j is accepting state in NFA

Then, {qi , q j ,..., qm }


is accepting state in DFA

140
Example
NFA M a
q0 a q1  q2 q1  F
b
a
DFA M b

q0  a
q1, q2 
q1, q2  F 
b

 a, b
141
Lemma:
If we convert NFA M to DFA M 
then the two automata are equivalent:
L M   L M  

Proof:
We only need to show: L M   L M  
AND
L M   L M  
142
2.4 Reduction of the number of states in
finite automata
• two states p and q of a dfa are called
indistinguishable if * (p,w) € F implies
 * (q,w) € F , and  *(p,w) not in F implies
 * (q,w) not in F. for all w € E*.

143
Theorem

144
145
146

You might also like