Sie sind auf Seite 1von 40

Chapter 2 : Finite Automata

1
Overview

● Finite Automata
● Regular Language
● DFA
● NFA
● Finite Automata with Epsilon moves

2 : Chandan J, Dept of ISE, NIE - Mysuru


Computational Model

3 : Chandan J, Dept of ISE, NIE - Mysuru


Example of Man, Wolf, Goat and Cabbage

4 : Chandan J, Dept of ISE, NIE - Mysuru


Parts of a Transition Diagram

● Start Start
● Accept State
● Transition
● Dead State

5 : Chandan J, Dept of ISE, NIE - Mysuru


Formal Definition of Finite Automata

● A finite automaton is a 5-tuple (Q, Σ, δ, q0, F ), where


○ Q is a finite set called the states,
○ Σ is a finite set called the alphabet
○ δ : Q × Σ−→ Q is the transition function
○ q0 ∈ Q is the start state
○ F ⊆ Q is the set of accept states

If A is the set of all strings that machine M accepts, we say that A is the
language of machine M and write L(M ) = A
6 : Chandan J, Dept of ISE, NIE - Mysuru
Finite Automata

● The Finite Automaton is a mathematical model of a system, with discrete


inputs and outputs.
● The system can be in any one of a finite number of internal configurations or
states.
● After processing the input, if the system is in any one of the state of set ‘F’,
they we say the input is accepted else rejected

7 : Chandan J, Dept of ISE, NIE - Mysuru


Eg.

Transition
0 1 Diagram
→ q1 q1 q2

* q2 q3 q2
Transition q3 q2 q2
Table

8 : Chandan J, Dept of ISE, NIE - Mysuru


Transition Diagram
● For each state in Q there is a node.
● For each state q in Q and each input a in Σ, let δ(q,a) = p. Then the transition diagram has
an arc from node q to node p, labeled a. If there are several input symbols that causes
transition from q to p, then the transition diagram can have one arc labeled by the list of
these symbols.
● There is an arrow into the start state q0, labeled Start. This arrow does not originate at any
node.
● Nodes corresponding to the accepting states( those of F) are marked by double circle.
States not in F have a single circle.
● Sometimes writing a transition diagram may be too big, in these cases we use formal
defination 9 : Chandan J, Dept of ISE, NIE - Mysuru
Eg. Draw state Transition diagrams for the
following

● L(M2) = {w| w ends in a 1}


● L(M3) = {w| w even number of 1}
● L(M4) = {w| w is the empty string ε or ends in a 0}
● L(M5) = {w| w is the empty string ε or ends in a 001}
● L(M6) = {w| w is the empty string ε or even number of zeros and ones}
● L(M7) = {w| w is even length and begins with 01}
● Set of all strings containing 1101 as a substring
● The set of all strings with exactly 3 consecutive 0
10 : Chandan J, Dept of ISE, NIE - Mysuru
Describe this Machine

11 : Chandan J, Dept of ISE, NIE - Mysuru


Regular Language

A language is called a regular language if some finite automaton recognizes it.

Eg. for a language that is not regular

L(A) = { 0n1n | n>= 1} : An FA cannot remember number of 0’s it has seen

● Balanced parentheses in an arithmetic expression

12 : Chandan J, Dept of ISE, NIE - Mysuru


{ w | w in {0,1}* and w, viewed as a binary
integer is divisible by 23}

13 : Chandan J, Dept of ISE, NIE - Mysuru


{ w | w in {0,1}* and w, viewed as the reverse
of a binary integer is divisible by 23 }

14 : Chandan J, Dept of ISE, NIE - Mysuru


Extended Transition Function

● To describe the behavior of an FA on the string, we must extend the


transition function δ to apply to a state and a string rather than a state and a
symbol.
● δ : Q x 𝝨 -> Q but δ` : Q x 𝝨* -> Q
● δ`(q,w) = p, is the state of FA (p) will be in after reading the string w starting
in the state q or ( is a unique path p s.t there is a path in the transition
diagram from q to p with label w)

15 : Chandan J, Dept of ISE, NIE - Mysuru


Extended Transition Function cont...

● δ`(q,𝝐) = q
● For all strings w and input symbol a,

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

● Put w = 𝝐 in equation 2, which will prove that there is no disagreement


between δ` and δ

16 : Chandan J, Dept of ISE, NIE - Mysuru


Extended Transition Function cont...

● Consider L(M7) = {w| w is even length and begins with 01} , what will be the
state of the machine after the string 011101
○ δ`(q0,𝝐) = q0

○ δ`(q0,0) = δ(δ`(q0,𝜺),0) = δ(q0,0) = q1

○ δ`(q0,01) = δ(δ`(q0,0),1) = δ(q1,1) = q2

…………….

17 : Chandan J, Dept of ISE, NIE - Mysuru


Language of DFA

Consider a DFA, A = { Q, 𝝨, 𝛅, q0,F }. The language accepted by this DFA is L(A)

L(A) = { w | 𝛅` (q0,w) is in F }

18 : Chandan J, Dept of ISE, NIE - Mysuru


Nondeterministic Finite Automata - NFA

● When the machine is in a given state and reads the next input symbol, we
know what the next state will be—it is determined. We call this deterministic
computation.
● In a nondeterministic machine, several choices may exist for the next state
at any point.
● Example for NFA : guessing the words while typing ( many states after
getting a single input )

19 : Chandan J, Dept of ISE, NIE - Mysuru


NFA contd...

● Like DFA, NFA also has finite set of states, finite set of input symbols, one
start state and a set of accepting states.
● Difference lies in transition function ( 𝛅 ), it takes a state and an input
symbol and returns zero, one or more states ( DFA’s transition function
gives exactly one state)
● Any set accepted by NFA can also be accepted by DFA
● If a string w is accepted by DFA, then from q0 there is one and only one path
labeled w to any one of the final state
● In case of NFA there may be many paths labeled w, and all must be checked
to see whether one or more terminate at a final state.
20 : Chandan J, Dept of ISE, NIE - Mysuru
Computing in NFA
● If there are multiple transitions on a particular symbol, the machine splits into multiple
copies of itself and follows all the possibilities in parallel
● If a state with an ε symbol on an exiting arrow is encountered, something similar
happens. Without reading any input, the machine splits into multiple copies, one
following each of the exiting ε-labeled arrows and one staying at the current state.
● Nondeterminism may be viewed as a kind of parallel computation wherein multiple
independent “processes” or “threads” can be running concurrently.
● When the NFA splits to follow several choices, that corresponds to a process “forking”
into several children, each proceeding separately.
● If at least one of these processes accepts, then the entire computation accepts.
21 : Chandan J, Dept of ISE, NIE - Mysuru
22 : Chandan J, Dept of ISE, NIE - Mysuru
● Consider the following NFA and the input 010110
● From state q1, there are 2 transitions on 1
● From state q2, there is a transition labeled 𝝐

23 : Chandan J, Dept of ISE, NIE - Mysuru


24 : Chandan J, Dept of ISE, NIE - Mysuru
NFA vs DFA

● Every NFA can be converted into an equivalent DFA.


● Constructing NFAs is sometimes easier than directly constructing DFAs
● An NFA may be much smaller than its deterministic counterpart, or its
functioning may be easier to understand
● In a DFA, the transition function takes a state and an input symbol and
produces the next state. In an NFA, the transition function takes a state and
an input symbol or the empty string and produces the set of possible next
states.
25 : Chandan J, Dept of ISE, NIE - Mysuru
Let A be the language consisting of all strings over {0,1} containing a 1 in
the third position from the end (e.g., 000100 is in A but 0011 is not).

26 : Chandan J, Dept of ISE, NIE - Mysuru


Formal Definition of NFA

● A nondeterministic finite automaton is a 5-tuple (Q,Σ,δ,q0,F) where


○ Q is a finite set of states,
○ Σ is a finite alphabet,
○ δ : Q × Σε −→ P (Q) is the transition function,
○ q0 ∈ Q is the start state
○ F ⊆ Q is the set of accept states.
■ P (Q) is the power set of Q.
■ Σε to be Σ ∪ {ε}

27 : Chandan J, Dept of ISE, NIE - Mysuru


*

28 : Chandan J, Dept of ISE, NIE - Mysuru


Extended Transition Function

● 𝞭 ‘ (q,𝜺) = {q} -> A change in state without an input


● 𝞭 ‘ (q,wa) = { p | for some state r in 𝞭 ‘ (q,w) , p is in 𝞭 (r,a) } : starting in state
q, and reading the string w followed by input symbol a we can be in the state
p if and only if one possible state we can be in after reading w is r and from r
we may go to p upon reading a.

● 𝞭 ( P , w) = ∪ q in p 𝛅(q,w) , for each set of states P ⊆ Q

29 : Chandan J, Dept of ISE, NIE - Mysuru


Consider the above NFA and the input 010110

𝛅(q1,0) = q1

𝛅(q1,01) = 𝛅(𝛅(q1,0) , 1) = 𝛅(q1 , 1) = { q1,q2}

𝛅(q1,010) = 𝛅(𝛅(q1,01) , 0)= 𝛅({q1,q2} ,0) = 𝛅(q1,0) ⋃ 𝛅(q2,0) = {q1,q3}

…...

30 : Chandan J, Dept of ISE, NIE - Mysuru


Language of NFA

Consider a NFA, A = { Q, 𝝨, 𝛅, q0,F }. The language accepted by this NFA is L(A)

L(A) = { w | 𝛅` (q0,w) ∩ F ≠ ∅ }

31 : Chandan J, Dept of ISE, NIE - Mysuru


Equivalence of DFA and NFA

● Both NFA and DFA recognize the same set of languages


● Describing an NFA would be much more easier than DFA
● DFA in practice has about as many states as the NFA ( best case)
● The smallest DFA can have 2n(power set of set of states of NFA) states,
while the smallest NFA for the same language has n states

32 : Chandan J, Dept of ISE, NIE - Mysuru


Equivalence of DFA and NFA contd..

● NFA N = { QN, 𝝨, 𝞭N, q0, FN } and DFA D = { QD, 𝝨, 𝞭D, {q0}, FD }


● QD is the set of subsets of QN ( QD is the power set of QN )
● |QD| = 2 |QN| , and some of these states may not be accessible called “Thrown
away”. So number of states in D may be less than 2n
● FD is set of subsets of QN = S and S ∩ FN ≠ ჶ (FD is all set of N’s state that
include at least one accepting state of N)

33 : Chandan J, Dept of ISE, NIE - Mysuru


Equivalence of DFA and NFA contd..

● For each set S ⊆ QN, and for each input symbol a in 𝝨

δD ( S, a ) = ⋃ p in S δN( p,a )

34 : Chandan J, Dept of ISE, NIE - Mysuru


Equivalence of DFA and NFA contd..

0,1

1 0,1
q0 q1 * q2

35 : Chandan J, Dept of ISE, NIE - Mysuru


𝛅 0 1

ჶ ჶ ჶ
𝛅 0 1
{q0} {q0} {q0, q1}
ჶ ჶ ჶ
{q1} {q2} {q2}
{q0} {q0} {q0, q1}
{q2} ჶ ჶ
{q0, q1} {q0, q2} {q0, q1, q2}
{q0, q1} {q0, q2} {q0, q1, q2}
{q0, q2} {q0} {q0, q1}
{q0, q2} {q0} {q0, q1}
{q0, q1, q2} {q0, q2} {q0, q1, q2}
{q1, q2} {q2} {q2}

{q0, q1, q2} {q0, q2} {q0, q1, q2}

36 : Chandan J, Dept of ISE, NIE - Mysuru


37 : Chandan J, Dept of ISE, NIE - Mysuru
FA with 𝝐 Transitions

● NFA = { Q, 𝝨, 𝞭, q0, F }, then NFA with its 𝜺 transition has all the components
same except 𝞭.
● 𝞭 = Q x ( 𝝨 ⋃ { 𝜺 } ) to 2Q . i.e, 𝞭 (q,a) will contain all states p s.t there is a
transition labeled a from q to p, where a is either 𝜺 or a symbol from 𝝨

38 : Chandan J, Dept of ISE, NIE - Mysuru


Epsilon Closures

● Epsilon close of a state q is set of all states which has transition from q
labeled epsilon

39 : Chandan J, Dept of ISE, NIE - Mysuru


SLE

● Extended Transitions and Languages for 𝜺 - NFA’s

40 : Chandan J, Dept of ISE, NIE - Mysuru

Das könnte Ihnen auch gefallen