Download as pdf or txt
Download as pdf or txt
You are on page 1of 50

Theoretical Computer Science I:

Automata and Formal Languages

Prof. Dr. Sjouke Mauw

Slides for Chapter 4

Version 2024.1
Chapter 4. Nondeterministic FA
Motivating example for nondeterminism tab, abau˚

Construct a finite automaton for L “ tab, abau˚


Motivating example for nondeterminism tab, abau˚

Construct a finite automaton for L “ tab, abau˚

Backbone: aba.

a, b
q1
b a b
q0 a q2 b q3
a
b
a q4
Motivating example for nondeterminism tab, abau˚

Or should we have two backbones: ab and aba.

q3
b a
q0 a q1 b q2
a
Motivating example for nondeterminism tab, abau˚

Or should we have two backbones: ab and aba.

q3
b a
q0 a q1 b q2
a

But this automaton has two a edges from state q0 , which is not
allowed.
Motivating example for nondeterminism tab, abau˚

Another simple solution.

ε
q0 q1 q2 q3
a b a
ε
Motivating example for nondeterminism tab, abau˚

Another simple solution.

ε
q0 q1 q2 q3
a b a
ε

But this automaton has empty edges (labeled with ε).


Motivating example for nondeterminism tab, abau˚

A solution with even fewer nodes and transitions.

q2
a b
b
q0 q1
a
Nondeterministic Finite Automata

Such diagrams are nondeterministic finite automata.


Differences with deterministic finite automata are:
‚ An edge can be labeled with ε.
‚ A node can have two outgoing edges with the same label.
‚ A node does not need to have an outgoing edge for every
symbol from the alphabet.

Further,
‚ A string is accepted if at least one path labeled with that
string leads to a final state.
RECALL

Definition
A deterministic finite automaton is a 5-tuple pQ, Σ, δ, q0 , F q, where

1 Q is a finite set called the states,


2 Σ is a finite set called the alphabet,
3 δ : Q ˆ Σ Ñ Q is a total function, which is called the
transition function,
4 q0 P Q is the start state, and
5 F Ď Q is the set of accept states.
Nondeterministic Finite Automaton
Definition
A nondeterministic finite automaton is a 5-tuple pQ, Σ, δ, q0 , F q,
where
1 Q is a finite set of states,
2 Σ is a finite alphabet,
3 δ : Q ˆ Σε Ñ PpQq is the transition function,
4 q0 P Q is the start state, and
5 F Ď Q is the set of accept states.
Nondeterministic Finite Automaton
Definition
A nondeterministic finite automaton is a 5-tuple pQ, Σ, δ, q0 , F q,
where
1 Q is a finite set of states,
2 Σ is a finite alphabet,
3 δ : Q ˆ Σε Ñ PpQq is the transition function,
4 q0 P Q is the start state, and
5 F Ď Q is the set of accept states.

Notation:
‚ Σε denotes Σ Y tεu, so a transition can be labeled with a
symbol a P Σ or the empty string ε.
‚ PpQq contains all sets of states, so a transition labeled a P Σε
can go to more than one state.
Example of formal definition of an NFA

0, 1 0, 1
1 0, ε 1
q1 q2 q3 q4
Example of formal definition of an NFA

0, 1 0, 1
1 0, ε 1
q1 q2 q3 q4

1 Q “ tq1 , q2 , q3 , q4 u
2 Σ “ t0, 1u
δ 0 1 ε
q1 tq1 u tq1 , q2 u H
3 δ is given by q2 tq3 u H tq3 u
q3 H tq4 u H
q4 tq4 u tq4 u H
4 q1 is the start state
5 F “ tq4 u
RECALL

Definition
Let M be a deterministic finite automaton pQ, Σ, δ, q0 , F q.
String w “ w1 w2 . . . wn P Σ˚ is accepted by M if there exists a
sequence of states r “ r0 r1 r2 . . . rn P Q ˚ such that
1 r0 “ q0 ,
2 δpri , wi`1 q “ ri`1 , (for i “ 0, . . . , n ´ 1)
3 rn P F .

1w 2 w 3 w n w
q0 “ r0 ÝÑ r1 ÝÑ r2 ÝÑ r3 . . . rn´1 ÝÑ rn P F
Formal definition of string accepted by an NFA
Definition
Let M be a nondeterministic finite automaton pQ, Σ, δ, q0 , F q.
String w P Σ˚ is accepted by M if we can write

w “ y1 y2 . . . ym , where yi P Σε

and there exists a sequence of states r “ r0 r1 r2 . . . rm P Q ˚ such


that
1 r0 “ q0 ,
2 ri`1 P δpri , yi`1 q,
3 rm P F .
Formal definition of string accepted by an NFA
Definition
Let M be a nondeterministic finite automaton pQ, Σ, δ, q0 , F q.
String w P Σ˚ is accepted by M if we can write

w “ y1 y2 . . . ym , where yi P Σε

and there exists a sequence of states r “ r0 r1 r2 . . . rm P Q ˚ such


that
1 r0 “ q0 ,
2 ri`1 P δpri , yi`1 q,
3 rm P F .

1y 2y 3 y yn
q0 “ r0 ÝÑ r1 ÝÑ r2 ÝÑ r3 . . . rn´1 ÝÑ rn P F
(For yi possibly ε.)
Language recognized by an NFA

Definition
A language A is recognized by nondeterministic finite automaton
M “ pQ, Σ, δ, q0 , F q if A “ tw P Σ˚ | M accepts w u.
The language recognized by M is denoted by LpMq.

Notice that finding one path labeled with w ending in a final state
suffices, even if there are many more paths labeled with w that do
not end in a final state.
Example

0, 1 0, 1
1 0, ε 1
q1 q2 q3 q4

Is 11 P LpMq?
Example

0, 1 0, 1
1 0, ε 1
q1 q2 q3 q4

Is 11 P LpMq?
There are four paths labeled with the string 11:
1 1
‚ q1 Ý
Ñ q1 Ý
Ñ q1
1 1
‚ q1 Ý
Ñ q1 Ý
Ñ q2
1 1 ε
‚ q1 Ý
Ñ q1 Ý
Ñ q2 Ý
Ñ q3
1 ε 1
‚ q1 Ý
Ñ q2 Ý
Ñ q3 Ý
Ñ q4
Only the last one ends in a final state. Thus, 11 P LpMq.
Example

q1
b a
a ε
q2 q3
a, b

This NFA accepts the strings ε, a, baba, baa.


It does not accept b, bb, babba.
Comparing DFAs and NFAs

Observations:
‚ Every DFA can be considered an NFA.
‚ But not every NFA is a DFA.
Comparing DFAs and NFAs

Observations:
‚ Every DFA can be considered an NFA.
‚ But not every NFA is a DFA.

Questions:
‚ Are NFAs more expressive than DFAs?
‚ Can NFAs recognize exactly the same languages as DFAs?
Comparing DFAs and NFAs

Observations:
‚ Every DFA can be considered an NFA.
‚ But not every NFA is a DFA.

Questions:
‚ Are NFAs more expressive than DFAs?
‚ Can NFAs recognize exactly the same languages as DFAs?

Answer:
‚ NFAs and DFAs recognize the same languages, so NFAs are
NOT more expressive than DFAs.
DFAs and NFAs are equivalent

Definition
Two machines M and M 1 are equivalent if they recognize the same
language, i.e. LpMq “ LpM 1 q.
DFAs and NFAs are equivalent

Definition
Two machines M and M 1 are equivalent if they recognize the same
language, i.e. LpMq “ LpM 1 q.

Theorem
For every nondeterministic finite automaton there exists an
equivalent deterministic finite automaton.

@MPNFA DM 1 PDFA LpMq “ LpM 1 q


Proof idea (NFA Ñ DFA)

‚ Given an NFA we need to construct an equivalent DFA.


‚ Assume that the NFA has transitions
a a

Ñ q and p Ý
Ñr

‚ Then make for the DFA a new state tq, r u and a transition
a
tpu Ý
Ñ tq, r u

‚ So states in the DFA are labeled with sets of states from the
NFA.
‚ A set of states is a final state of the DFA if at least one of its
contained states is final in the original NFA.
Example: Consider the following fragment of an NFA

q1 ε q2

a
a q3
q0 a
ε q4

q5 a q6
Example: Consider the following fragment of an NFA

q1 ε q2

a
a q3
q0 a
ε q4

q5 a q6

This gives the following fragment of a DFA.

tq0 , q5 u a tq1 , q2 , q3 , q4 , q6 u
Example: Consider the following fragment of an NFA

q1 ε q2

a
a q3
q0 a
ε q4

q5 a q6

For a node without outgoing a edge, introduce a transition to a


trap node.

tq2 u a H
Proof (First without ε-arrows)
Let M “ pQ, Σ, δ, q0 , F q be an NFA. We construct DFA
M 1 “ pQ 1 , Σ1 , δ 1 , q01 , F 1 q such that LpMq “ LpM 1 q, as follows.
Proof (First without ε-arrows)
Let M “ pQ, Σ, δ, q0 , F q be an NFA. We construct DFA
M 1 “ pQ 1 , Σ1 , δ 1 , q01 , F 1 q such that LpMq “ LpM 1 q, as follows.
1 Q 1 “ PpQq;
The new states are sets of old states.
Proof (First without ε-arrows)
Let M “ pQ, Σ, δ, q0 , F q be an NFA. We construct DFA
M 1 “ pQ 1 , Σ1 , δ 1 , q01 , F 1 q such that LpMq “ LpM 1 q, as follows.
1 Q 1 “ PpQq;
The new states are sets of old states.
2 Σ1 “ Σ;
The new alphabet is the same as the old alphabet.
Proof (First without ε-arrows)
Let M “ pQ, Σ, δ, q0 , F q be an NFA. We construct DFA
M 1 “ pQ 1 , Σ1 , δ 1 , q01 , F 1 q such that LpMq “ LpM 1 q, as follows.
1 Q 1 “ PpQq;
The new states are sets of old states.
2 Σ1 “ Σ;
The new alphabet is the same as the old alphabet.
3 For every R P Q 1 and a P Σ let
δ 1 pR, aq “ tq P Q | Dr PR q P δpr , aqu;
There is a new transition with label a from a set to another
set if the second set consists of all old states reachable with
label a from the first set.
Proof (First without ε-arrows)
Let M “ pQ, Σ, δ, q0 , F q be an NFA. We construct DFA
M 1 “ pQ 1 , Σ1 , δ 1 , q01 , F 1 q such that LpMq “ LpM 1 q, as follows.
1 Q 1 “ PpQq;
The new states are sets of old states.
2 Σ1 “ Σ;
The new alphabet is the same as the old alphabet.
3 For every R P Q 1 and a P Σ let
δ 1 pR, aq “ tq P Q | Dr PR q P δpr , aqu;
There is a new transition with label a from a set to another
set if the second set consists of all old states reachable with
label a from the first set.
4 q01 “ tq0 u;
The new initial state is the singleton set containing the old
initial state only.
Proof (First without ε-arrows)
Let M “ pQ, Σ, δ, q0 , F q be an NFA. We construct DFA
M 1 “ pQ 1 , Σ1 , δ 1 , q01 , F 1 q such that LpMq “ LpM 1 q, as follows.
1 Q 1 “ PpQq;
The new states are sets of old states.
2 Σ1 “ Σ;
The new alphabet is the same as the old alphabet.
3 For every R P Q 1 and a P Σ let
δ 1 pR, aq “ tq P Q | Dr PR q P δpr , aqu;
There is a new transition with label a from a set to another
set if the second set consists of all old states reachable with
label a from the first set.
4 q01 “ tq0 u;
The new initial state is the singleton set containing the old
initial state only.
5 F 1 “ tR P Q 1 | R X F ‰ Hu.
A set is a final state if any of its elements is an old final state.
Proof (with ε-arrows)
Extend δ 1 with all states reachable through a sequence of ε-arrows.
For any state R P Q 1 (i.e. R Ď Q) we define E pRq

E pRq “ tq P Q | we can reach q from R through 0 or more ε-arrowsu


Proof (with ε-arrows)
Extend δ 1 with all states reachable through a sequence of ε-arrows.
For any state R P Q 1 (i.e. R Ď Q) we define E pRq

E pRq “ tq P Q | we can reach q from R through 0 or more ε-arrowsu

1 Q 1 “ PpQq;
2 Σ1 “ Σ;
3 For every R P Q 1 and a P Σ let
δ 1 pR, aq “ tq P Q | Dr PR q P E pδpr , aqqu;
Extend target set with all states reachable by one or more ε
transitions.
4 q01 “ E ptq0 uq;
The new initial state contains the old initial state plus all
states reachable with ε’s from the old initial state.
5 F 1 “ tR P Q 1 | R X F ‰ Hu.
Example NFA Ñ DFA

1
b a
a ε
2 3
a, b

NFA M “ pt1, 2, 3u, ta, bu, δ, 1, t1uq

Construct a DFA M 1 “ pQ 1 , Σ1 , δ 1 , q01 , F 1 q by


1 Q 1 “ tH, t1u, t2u, t3u, t1, 2u, t1, 3u, t2, 3ut1, 2, 3uu
2 Σ1 “ ta, bu
3 δ1 “ . . .
4 q01 “ E pt1uq “ t1, 3u
5 F 1 “ tt1u, t1, 2u, t1, 3u, t1, 2, 3uu
Example NFA Ñ DFA
1
δ a b ε
b a
a ε 1 H t2u t3u
2 3 2 t2, 3u t3u H
a, b 3 t1u H H
Example NFA Ñ DFA
1
δ a b ε
b a
a ε 1 H t2u t3u
2 3 2 t2, 3u t3u H
a, b 3 t1u H H

δ 1 pR, xq “ tq P Q | Dr PR q P E pδpr , xqqu


δ1 a b
H
t1u
t2u
t3u
t1, 2u
t1, 3u
t2, 3u
t1, 2, 3u
Example NFA Ñ DFA
1
δ a b ε
b a
a ε 1 H t2u t3u
2 3 2 t2, 3u t3u H
a, b 3 t1u H H

δ 1 pR, xq “ tq P Q | Dr PR q P E pδpr , xqqu


δ1 a b
H H H
t1u H t2u
t2u t2, 3u t3u
t3u t1, 3u H
t1, 2u t2, 3u t2, 3u
t1, 3u t1, 3u t2u
t2, 3u t1, 2, 3u t3u
t1, 2, 3u t1, 2, 3u t2, 3u
δ1 a b
H H H
1 t1u H t2u
b a t2u t2, 3u t3u
a ε t3u t1, 3u H
2 3 t1, 2u t2, 3u t2, 3u
a, b t1, 3u t1, 3u t2u
t2, 3u t1, 2, 3u t3u
t1, 2, 3u t1, 2, 3u t2, 3u
δ1 a b
H H H
1 t1u H t2u
b a t2u t2, 3u t3u
a ε t3u t1, 3u H
2 3 t1, 2u t2, 3u t2, 3u
a, b t1, 3u t1, 3u t2u
t2, 3u t1, 2, 3u t3u
t1, 2, 3u t1, 2, 3u t2, 3u

H t1u t2u t1, 2u

t3u t1, 3u t2, 3u t1, 2, 3u


δ1 a b
H H H
1 t1u H t2u
b a t2u t2, 3u t3u
a ε t3u t1, 3u H
2 3 t1, 2u t2, 3u t2, 3u
a, b t1, 3u t1, 3u t2u
t2, 3u t1, 2, 3u t3u
t1, 2, 3u t1, 2, 3u t2, 3u
a, b
H a t1u b t2u t1, 2u

b a a, b
b b
a a
a
t3u a t1, 3u t2, 3u t1, 2, 3u
b b
Simplification

We can remove unreachable states t1u and t1, 2u.

a, b
H t2u

b a
b b
a a
a
t3u a t1, 3u t2, 3u t1, 2, 3u
b b
Regularity

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

L P Reg ðñ DMPDFA L “ LpMq


Regularity

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

L P Reg ðñ DMPDFA L “ LpMq

Corollary
A language is regular if and only if some nondeterministic finite
automaton recognizes it.

L P Reg ðñ DMPNFA L “ LpMq


Overview

Reg

DFA NFA

You might also like