0% found this document useful (0 votes)
43 views72 pages

Lecture 4-FSMs-NFA-DFA

The document discusses finite state automata including definitions, concepts like regular expressions and languages, and examples of converting regular expressions to non-deterministic finite automata. It also provides examples of using non-deterministic finite automata to accept and reject strings through computations.

Uploaded by

mariemahmoud28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views72 pages

Lecture 4-FSMs-NFA-DFA

The document discusses finite state automata including definitions, concepts like regular expressions and languages, and examples of converting regular expressions to non-deterministic finite automata. It also provides examples of using non-deterministic finite automata to accept and reject strings through computations.

Uploaded by

mariemahmoud28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 72

Theory of Computation

Lecture 4
Finite State Automata (FSA)

1
Syllabus and Terminologies
 Regular Languages .. Regular Sets
 REs (Regular Expressions)
 FSMs (or FSA/FA) … Finite State Machines/Automata
 DFA vs. NFA … Deterministic vs. Non-deterministic FSA
 Comparison and conversion
 Examples & Closure Operations
 Pumping Lemma
 Context Free Languages
 CFGs … Context Free Grammars
 PDA … Push Down Automata
 Parsing: CFG generating strings vs. PDA recognizing strings
 Turing Machine
05/11/2024 FCI-CU-EG 2
Finite Automaton

Input
String

Output

“Accept”
Finite
or
Automaton
“Reject”
Finite Automata State Graphs

 A state

• The start state

• An accepting/final state

a
• A transition

4
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
Regular Expressions to Finite Automata

 High-level sketch
NFA

Regular
expressions DFA

Lexical Table-driven
Specification Implementation of DFA

6
Regular Expressions to NFA (1)

 For each kind of rexp, define an NFA


 Notation: NFA for rexp A

A
• For 

• For input a
a

7
Regular Expressions to NFA (2)

 For AB
A  B

• For A | B
B 


 A

8
Regular Expressions to NFA (3)

 For A*

A

9
EXAMPLE OF (RE → NFA) CONVERSION

 Choose the NFA that accepts the following regular


expression: 1* + 0

10
Example of (RE → NFA) conversion

 Consider the regular expression


(1 | 0)*1
 The NFA is (there are multiple valid answers)

 C 1 E 
A B 1
 0 F G H  I J
 D 

11
NFA

 NFA 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.

Alphabet ={a}
q1 a q2
a
q0
a
q3

12
NFA

Alphabet = {a}
Two choices
q1 a q2
a
q0
a
q3

13
NFA

An NFA accepts a string:


if there is a computation (scenario/path) of
the NFA that accepts the string

i.e., all the input string is processed and the


automaton is in an accepting state

14
NFA

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
15
NFA Acceptance Example 1

a a

q1 a q2
a
q0
a
q3

16
NFA

a a

q1 a q2
a
q0
a
q3

17
NFA

a a

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

18
NFA

aa is accepted by the NFA:

“accept”
q1 a q2 q1 a q2
a a
q0
a
q0
a
q3 q3 “reject”
because this
computation this computation
acceptsaa is ignored
19
NFA

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


20
NFA

aaa is rejected by the NFA:

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

All possible computations lead to rejection


21
LAMBDA TRANSITIONS

Remember!
Bothe Lambda λ and Epsilon є can be used to represent an empty move

q0 a q1  q2 a q3
LAMBDA TRANSITIONS
Acceptance Example 2

a a

q0 a q1  q2 a q3
LAMBDA TRANSITIONS

a a

q0 a q1  q2 a q3
LAMBDA TRANSITIONS
input tape head does not move

a a

q0 a q1  q2 a q3

25
LAMBDA TRANSITIONS
all input is consumed

a a

“accept”

q0 a q1  q2 a q3

String aa is accepted
LAMBDA TRANSITIONS
Rejection Example 3

a a a

q0 a q1  q2 a q3
LAMBDA TRANSITIONS

a a a

q0 a q1  q2 a q3
LAMBDA TRANSITIONS
(read head doesn’t move)

a a a

q0 a q1  q2 a q3
LAMBDA TRANSITIONS
Input cannot be consumed

a a a

Automaton halts

“reject”
q0 a q1  q2 a q3

String aaa is rejected


LAMBDA TRANSITIONS

Language accepted: L  {aa}

q0 a q1  q2 a q3
Example 4

what is the language accepted by this


automata?

q0 a q1 b q2  q3


a b

q0 a q1 b q2  q3


a b

q0 a q1 b q2  q3


a b

“accept”

q0 a q1 b q2  q3


Another String

a b a b

q0 a q1 b q2  q3


a b a b

q0 a q1 b q2  q3


a b a b

q0 a q1 b q2  q3


a b a b

q0 a q1 b q2  q3


a b a b

q0 a q1 b q2  q3


a b a b

q0 a q1 b q2  q3


a b a b

“accept”
q0 a q1 b q2  q3


Language accepted

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



 ab

q0 a q1 b q2  q3


43
Example 5

L( M )  ??

0
q0 q1 0, 1 q2
1

44
Language accepted

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


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

45
Example

46
Another Example

 Can you gets its corresponding


RE?

47
Extended Transition  *
 Definition: The language accepted by an NFA M = (Q, , ,
q0, F) is the set of all strings on  accepted by M.
 Formally,
L(M) = {w  * : *(q0, w)  F   }
 * q0 , a   ??
q1 q4 q5
a a
q0 a q1 b q2  q3

48
Extended Transition

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

q4 q5
a a
q0 a q1 b q2  q3

49
Extended Transition

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

q4 q5
a a
q0 a q1 b q2  q3

50
Extended Transition

If
F  q0 ,q5   * q0 , aa   ??
q4 , q5 

q4 q5
a a
q0 a q1 b q2  q3

then
aa  L(M )
51
Extended Transition

F  q0 ,q5   * q0 , ab   ??


q2 , q3 , q0 

q4 q5
a a
q0 a q1 b q2  q3

ab  LM 
52
Extended Transition


F  q0 ,q5   * q0 , abaa   ??
q4 , q5 

q4 q5
a a
q0 a q1 b q2  q3

abaa  L(M )

53
Extended Transition

F  q0 ,q5   * q0 , aba   ??


q1

q4 q5
a a
q0 a q1 b q2  q3

aba  LM 
54
Extended Transition


q4 q5
a a
q0 a q1 b q2  q3

*
L(M ) = {ab }* È {ab} {aa}

55
NFA example: Multiple Paths
b a
b
q0 q1
a b
q2 b

b b a b b b b a b b
q0 q 0 q 1 q 1 q 2 q 1 q0 q 1 q 2 q 0 q 0 q 1
q0 q 1 q 2 q 0 q 0 q 0 q0 q 1 q 2 q 0 q 1 q 2
56
Nondeterministic Finite Automata
 Definition: The language accepted by a nfa M = (Q, , ,
q0, F) is the set of all strings on  accepted by M. Formally,

L(M) = {w  * : *(q0, w)  F   }

qi
w
qk
q0 w
w qj

57
Deterministic Finite Automata (DFA)

 Example:
b

a 1 a

0 3 a, b
a
b b
2

The above automaton accepts the strings aba, baaabab.


However it rejects any string of the form abn.

58
DFA
Example
 M =({q0, q1, q2}, {0,1}, , q0, {q1}),

 where  is

(q0, 0) = q0 (q0, 1) = q1 (q1, 0) = q0


(q1, 1) = q2 (q2, 0) = q2 (q2, 1) = q1
0 1
0 0

q0 q1 q2

1 1

59
Deterministic finite automata (DFA)

 Informally, a deterministic finite automaton over an alphabet 


can be thought of as a finite directed graph with the property
that each node emits one labeled edge for each distinct
element of , the nodes are called states. There is one special
state called the start or the initial state, and there is at least one
final state.
 A DFA accepts a string w in * if there is a path from the start
state to some final states such that w is the concatenation of
the labels on the edges of the path. Otherwise, the DFA rejects
w.
 The set of all strings accepted by a DFA M is called the
language of M and denoted by L(M).
60
Languages and DFA

 The language accepted by a DFA M = (Q, , , q0, F)


is the set of all strings on  accepted by M.
Formally,
L(M) = {w  * : *(q0, w)  F}
L(M) = {w  * : *(q0, w)  F} non
accepted words

where * : Q X *  Q is the extended transition function.


The second argument of * is a string rather than a single
symbol, and its value gives the state the automaton will be in
after reading that string.
61
Examples
What are the languages of these automata?
S = {a, b} q0
a b S = {a, b}
b a
a
a q1 q3 b
q0 q1
b b a a b

b q2 q4 a

0 1 0,
1 0 1
q0 q1 q2
S = {0, 1}

62
Languages and Dfa’s
 Example:

a a,b

q0 b q1 a, b q2
trap state

L = { anb: n >= 0}

63
Deterministic Finite Automata
 Find a deterministic finite accepter that recognizes the set of all
strings on  = {a, b} staring with a prefix ab.
a,b
q0 a q1 b q2

b a
q3 a,b

 Find a dfa that accepts all strings on {0, 1}, except those containing
the substring 001.
1 0 0,1
1
⋀ 0 0 00 1 001
0
64
Ex: DFA
 All words with even count of letters.
((a+b)(a+b))*

a, b

1± 2
a, b

65
Ex: DFA
 All words that start with “a”
a(a+b)*
a,b b

b 2 2
1- 1-

a 3+ a 3+ a,b
a,b
Does not accept all inputs

66
Ex: DFA
 All words that start with “a”
a(a+b)* a,b
b 2
1- a,b

a a,b
3+ 4+

Special accept state for string “a”, might give better


performance in hardware implementation

67
Ex: DFA (not well defined)
 All words that start with triple letter
(aaa+bbb)(a+b)*

a a a
2 3

1- 6+ a,b
4 5
b b b

68
NFA vs. DFA

69
NFA and DFA

Example:
The following are NFA and the DFA that describes the
same language:
L={(10)n : n≥0}
0
0, 1 q DFA 0,1
q0 q1 2 0
1
q0 q1 1 q2
NFA  1
0

70
NFA vs. DFA (2)

 For a given language the NFA can be simpler than


the DFA
1
0 0
NFA
0

1 0
0 0
DFA
1
1
• DFA can be exponentially larger than NFA

71
NFA vs. DFA
Compactibility Readability Speed
NFA Good Good Slow
DFA Bad Bad Fast
• DFAs are widely used to build lexical analyzers.

NFA
DFA
The language that recognizes (a+b) * a b 76

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy