cs 275 automata and formal language theorycsetzer/lectures/automataformal... ·  · 2018-03-09cs...

80
CS 275 Automata and Formal Language Theory Course Notes Part II: The Recognition Problem (II) Chapter II.3.: Finite State Automata (13) Anton Setzer (Based on a book draft by J. V. Tucker and K. Stephenson) Dept. of Computer Science, Swansea University http://www.cs.swan.ac.uk/ csetzer/lectures/ automataFormalLanguage/current/index.html April 19, 2018 CS 275 Chapter II.3 1/ 80

Upload: nguyenque

Post on 09-May-2018

217 views

Category:

Documents


3 download

TRANSCRIPT

CS 275 Automata and Formal Language TheoryCourse Notes

Part II: The Recognition Problem (II)Chapter II.3.: Finite State Automata (13)

Anton Setzer(Based on a book draft by J. V. Tucker and K. Stephenson)

Dept. of Computer Science, Swansea University

http://www.cs.swan.ac.uk/∼csetzer/lectures/automataFormalLanguage/current/index.html

April 19, 2018

CS 275 Chapter II.3 1/ 80

II.3.1. String Recognition (13.1)

II.3.2. Nondeterministic Finite State Automata (13.2)

II.3.3. Examples of Automata (13.3)

II.3.4. Automata with Empty Move Transitions (13.4)

II.3.5. Deterministic Finite State Automata (13.6)

CS 275 Chapter II.3 2/ 80

II.3.1. String Recognition (13.1)

Example Recognising Strings, Step 1

We want to define a program which recognises strings “start” and “stop”.We start by defining a program which recognises the letter “s”.This can be given by a system given by the following diagram, which willbe our first automaton (automata will be introduced soon).

s

q0 q1

CS 275 Sect. II.3.1. 3/ 80

II.3.1. String Recognition (13.1)

Example Recognising Strings, Step 1

s

q0 q1

This automaton has the following ingredients.

I States q0, q1.

I State q0 is the starting state, indicated by the arrow into it comingfrom nowhere.

I State q1, the state indicating that we have recognised letter “s”.

I A transition, which when recognising letter “s”, goes from state q0 toq1.

CS 275 Sect. II.3.1. 4/ 80

II.3.1. String Recognition (13.1)

Example Recognising Strings, Step 2

In order to recognise the letter “t”, we extend this automaton as follows:

s

q0 q1 q2

t

CS 275 Sect. II.3.1. 5/ 80

II.3.1. String Recognition (13.1)

Example Recognising Strings, Step 2

s

q0 q1 q2

t

I q0 is the start state.

I q1 indicates we have read string “s”.

I q2 indicates we have read string “st”.

CS 275 Sect. II.3.1. 6/ 80

II.3.1. String Recognition (13.1)

Example Recognising Strings, Step 3

For the 3rd letter, we have two choices: “a” as part of the word “start”,and “o” as part of the word “stop”.

s

q0 q1 q2

tq3

q6

a

o

CS 275 Sect. II.3.1. 7/ 80

II.3.1. String Recognition (13.1)

Example Recognising Strings, Step 3

s

q0 q1 q2

tq3

q6

a

o

I q0 is the start state.

I q1 indicates we have read string “s”.

I q2 indicates we have read string “st”.

I q3 indicates we have read string “sta”.

I q6 indicates we have read string “sto”.

CS 275 Sect. II.3.1. 8/ 80

II.3.1. String Recognition (13.1)

Example Recognising Strings, Step 3

We can complete our automaton and obtain the following

s

q0 q1 q2

tq3

q6

a

o

q4 q5

q7

r t

p

CS 275 Sect. II.3.1. 9/ 80

II.3.1. String Recognition (13.1)

Example Recognising Strings, Step 3

s

q0 q1 q2

tq3

q6

a

o

q4 q5

q7

r t

p

This diagram contains a new ingredient:States q5 and q7 are

::::::::::accepting

:::::::states. If we have processed a word and

reached such a state then the word is accepted as a string of the languageof the automaton.(In our example L = {start, stop}.)

CS 275 Sect. II.3.1. 10/ 80

II.3.1. String Recognition (13.1)

Automata with Loops

In order to recognise infinite languages, we need automata with loops. Thefollowing automaton recognises L =???

q0 q1

a

b

b

CS 275 Sect. II.3.1. 11/ 80

II.3.1. String Recognition (13.1)

Automata with Loops

The following automaton recognises L =???

q0 q1

a

b

b

CS 275 Sect. II.3.1. 12/ 80

II.3.1. String Recognition (13.1)

Automata with Loops

The following automaton recognises L =???

q0 q1

a

b

CS 275 Sect. II.3.1. 13/ 80

II.3.1. String Recognition (13.1)

Nondeterministic Automata

The language {start, stop } can as well be recognised by the following

::::::::::::::::::nondeterministic

:::::finite

::::::state

::::::::::::automaton:

q0

q3

o

q4 q5

r t

p

s q1

t

q2

a

t

sq6 q7 q8 q9

CS 275 Sect. II.3.1. 14/ 80

II.3.1. String Recognition (13.1)

Nondeterministic Automata

q0

q3

o

q4 q5

r t

p

s q1

t

q2

a

t

sq6 q7 q8 q9

I The automaton chooses in state q0, non-deterministically, when instate q0 and recognising a letter s, whether to go to q1 or q6.

I The accepted language is the set of strings such that for each of themwe obtain an accepting state for at least one non-deterministicchoice.

CS 275 Sect. II.3.1. 15/ 80

II.3.1. String Recognition (13.1)

Nondeterministic Automata

q0

q3

o

q4 q5

r t

p

s q1

t

q2

a

t

sq6 q7 q8 q9

I If we try to accept the word “stop” by moving q0s−→ q1

t−→ q2 weget stuck at q2.

I That we fail to accept a word for one specific non-deterministicchoice doesn’t imply that this word is not in the language.

I A word is not accepted only if for all non-deterministic choices thecorresponding run of the automaton doesn’t accept the string.

CS 275 Sect. II.3.1. 16/ 80

II.3.1. String Recognition (13.1)

Why Nondeterministic Automata?

I When translating regular grammars into automata, we will obtainnon-deterministic automata.

I We will show later that from a non-deterministic automaton we canobtain an equivalent deterministic automaton.

I Non-deterministic machine models play an important role in thetheory of algorithms and complexity.

I In some cases (as for automata), deterministic and non-deterministicare equivalent.

I Sometimes they are not.I In other cases it is an open problem whether they are equivalent.I One example is the famous open “P = NP?” problem.

Whether the problems recognised by deterministic (P) andnon-deterministic (NP) Turing machines in polynomial time coincide ornot.

CS 275 Sect. II.3.1. 17/ 80

II.3.1. String Recognition (13.1)

Common Mistake in Exams

I In many exams I have seen students writing transitions, which arelabelled by strings rather than single terminals:

q0 q1

ab

I Transitions of an automaton are labelled only by single elements ofthe alphabet.

I Solution is to introduce intermediate states:

q0 q1q′0

ab

CS 275 Sect. II.3.1. 18/ 80

II.3.2. Nondeterministic Finite State Automata (13.2)

II.3.1. String Recognition (13.1)

II.3.2. Nondeterministic Finite State Automata (13.2)

II.3.3. Examples of Automata (13.3)

II.3.4. Automata with Empty Move Transitions (13.4)

II.3.5. Deterministic Finite State Automata (13.6)

CS 275 Sect. II.3.2. 19/ 80

II.3.2. Nondeterministic Finite State Automata (13.2)

Definition NFA

Definition

A::::::::::::::::::non-deterministic

::::::finite

::::::state

::::::::::::automaton, in short

::::NFA

(Q, q0,F ,T ,−→) is given by

I A finite set Q of::::::states.

I A single::::::initial

:::::state q0 ∈ Q.

I A set F ⊆ Q of::::::::::accepting

:::::::states.

I A finite set of:::::::::terminal

:::::::::symbols T .

I −→ is a relation between states q ∈ Q, a ∈ T and states q′ ∈ Q. Wewrite q

a−→ q′ if q, a, q′ are in this relation.

More formally −→ is a subset of Q × T × Q and qa−→ q′ is an

abbreviation for (q, a, q′) ∈−→.

CS 275 Sect. II.3.2. 20/ 80

II.3.2. Nondeterministic Finite State Automata (13.2)

Presentation of NFA by Picture

An NFA can be presented by a picture, like the following diagram:

q0

q3

o

q4 q5

r t

p

s q1

t

q2

a

t

sq6 q7 q8 q9

Alphabet = {a, . . . , z}I The arrow from nowhere into state q0 denotes that q0 is the initial

state.I Circles denote states.I Arrows from a state q to q′ labelled by a mean that q

a−→ q′.I Double circle like for q5 and q9 denote the accepting states.I The alphabet needs to be stated, unless all elements of the alphabet

occur as labels of arrows, or it is clear from the context.

CS 275 Sect. II.3.2. 21/ 80

II.3.2. Nondeterministic Finite State Automata (13.2)

Presentation of NFA by Table

automaton A

states q0, . . . , qn

terminals a0, . . . , am

start qi

final qi0 , qi1 ,. . ., qim .

transitions qk1al1−→ qm1

· · ·

CS 275 Sect. II.3.2. 22/ 80

II.3.2. Nondeterministic Finite State Automata (13.2)

Example

The automaton for alphabet {a, . . . , z} given by the diagram

q0

q3

o

q4 q5

r t

p

s q1

t

q2

a

t

sq6 q7 q8 q9

is represented by the table on the next slide.

CS 275 Sect. II.3.2. 23/ 80

II.3.2. Nondeterministic Finite State Automata (13.2)

Example

automaton Astart,stop

states q0, q1, q2, q3, q4, q5, q6, q7terminals a, . . . , zstart q0final q5, q9.

transitions q0s−→ q1

q0s−→ q6

q1t−→ q2

q2a−→ q3,

q3r−→ q4,

q4t−→ q5,

q6t−→ q7,

q7o−→ q8,

q8p−→ q9.

CS 275 Sect. II.3.2. 24/ 80

II.3.2. Nondeterministic Finite State Automata (13.2)

The Extended Transition Relation

We define an extended transition relation which determines, whether theNFA from a state q when reading word w ∈ T ∗ can reach state q′:

Definition

Let A = (Q, q0,F ,T ,−→) be an NFA. Then we define −→∗ betweenstates q ∈ Q, words w ∈ T ∗ and q′ ∈ Q.

We write q w ∗−→ q′, if q,w , q′ are in this relation.

I If w = a0 · · · an, then

q w ∗−→ q′ iff qa0−→ q0

a1−→ q1a2−→ · · · an−→ qn = q′

for some qi ∈ Q

Especially qε−→ q′ iff q′ = q.

CS 275 Sect. II.3.2. 25/ 80

II.3.2. Nondeterministic Finite State Automata (13.2)

The Extended Transition Relation

Again, more formally −→∗ is a subset of Q × T ∗ × Q and q w ∗−→ q′

means that (q,w , q′) ∈−→∗

CS 275 Sect. II.3.2. 26/ 80

II.3.2. Nondeterministic Finite State Automata (13.2)

Example

For the automaton

q0

q3

o

q4 q5

r t

p

s q1

t

q2

a

t

sq6 q7 q8 q9

we give some definitions of −→∗:

CS 275 Sect. II.3.2. 27/ 80

II.3.2. Nondeterministic Finite State Automata (13.2)

Example

For each of the words given in the following all possible transitions from q0will be given:

I q0ε ∗−→ q0,

I q0s ∗−→ q1, q0

s ∗−→ q6.

I q0st ∗−→ q2, q0

st ∗−→ q7.

I q0sta ∗−→ q3.

I q0star ∗−→ q4.

I q0start∗−→ q5.

I q0sto ∗−→ q8.

I q0stop ∗−→ q9.CS 275 Sect. II.3.2. 28/ 80

II.3.2. Nondeterministic Finite State Automata (13.2)

The Language accepted by an NFA

Definition

Let A = (Q, q0,F ,T ,−→) be an NFA. The::::::::::language

::::::::::accepted

:::by

::A is

defined as

L(A) := {w ∈ T ∗ | q0 w ∗−→ q for some q ∈ F}

CS 275 Sect. II.3.2. 29/ 80

II.3.2. Nondeterministic Finite State Automata (13.2)

Operational Understanding of NFAs

To an NFA A = (Q, q0,F ,T ,−→) corresponds a non-deterministicprogram for checking whether an input string is in L(A).The program will have

I a variable q ∈ Q,

I and a pointer to a position in the input string,

I a Boolean variable stopped , which will we true if the automaton hasstopped without having reached the end of the input string.

A run of the NFA on an input string s is as given by the pseudo-code onthe next slide:

CS 275 Sect. II.3.2. 30/ 80

II.3.2. Nondeterministic Finite State Automata (13.2)

Operational Understanding of NFAs

{− Initialisation −}q := q0;p := beginning of word s;stopped := false;while (p 6= end of word s) ∧ ¬stopped do{a := next symbol in s at position p;

if (¬∃q′.(q a−→ q′))then {stopped := true; }else {choose q′ s.t. q

a−→ q′;q := q′;p := position of next symbol in s from position p; }

}

CS 275 Sect. II.3.2. 31/ 80

II.3.2. Nondeterministic Finite State Automata (13.2)

Operational Understanding of NFAs

I If at the end of the execution stopped = true (therefore we have notreached the end of the string), the string is not accepted in this run.

I Otherwise the string is accepted if q ∈ F .

A string is accepted if there exist a sequence of non-deterministic choices,such that the string is accepted in the corresponding run.

CS 275 Sect. II.3.2. 32/ 80

II.3.2. Nondeterministic Finite State Automata (13.2)

Operational Understanding of NFAs

One can now see that a string s is accepted by some run of the programfor automaton A iff s ∈ L(A).

CS 275 Sect. II.3.2. 33/ 80

II.3.3. Examples of Automata (13.3)

II.3.1. String Recognition (13.1)

II.3.2. Nondeterministic Finite State Automata (13.2)

II.3.3. Examples of Automata (13.3)

II.3.4. Automata with Empty Move Transitions (13.4)

II.3.5. Deterministic Finite State Automata (13.6)

CS 275 Sect. II.3.3. 34/ 80

II.3.3. Examples of Automata (13.3)

Example 1: An Automaton accepting {1, 2, 3}

q0

q1

q2

q3

1

2

3

CS 275 Sect. II.3.3. 35/ 80

II.3.3. Examples of Automata (13.3)

Example 1: An Automaton accepting {1, 2, 3}

Display style:

automaton A1,2,3

states q0, q1, q2, q3

terminals 1, 2, 3

start q0

final q1, q2, q3

transitions q01−→ q1

q02−→ q2

q03−→ q3

CS 275 Sect. II.3.3. 36/ 80

II.3.3. Examples of Automata (13.3)

Simplified Version

2

3

1

q1q0

CS 275 Sect. II.3.3. 37/ 80

II.3.3. Examples of Automata (13.3)

Simplified Version

Shorter pictorial presentation of the same automaton:

q1q0

1,2,3

CS 275 Sect. II.3.3. 38/ 80

II.3.3. Examples of Automata (13.3)

Simplified Version

Display style:

automaton A1,2,3′

states q0, q1

terminals 1, 2, 3

start q0

final q1

transitions q00−→ q1

q01−→ q1

q02−→ q1

CS 275 Sect. II.3.3. 39/ 80

II.3.3. Examples of Automata (13.3)

Example 2: Automaton accepting ???

q0

0, 1 , . . ., 9

CS 275 Sect. II.3.3. 40/ 80

II.3.3. Examples of Automata (13.3)

Simplified Version

Display style:

automaton A

states q0

terminals 0, . . . , 9

start q0

final q0

transitions q0a−→ q0 (for a ∈ {0, 1, . . . , 9})

CS 275 Sect. II.3.3. 41/ 80

II.3.3. Examples of Automata (13.3)

Example 3: Automaton accepting ???

0, 1 , . . ., 9

1, 2, . . ., 9

q0 q1

q2

0

CS 275 Sect. II.3.3. 42/ 80

II.3.3. Examples of Automata (13.3)

Display Style

Display style:

automaton A

states q0, q1, q2

terminals 0, . . . , 9

start q0

final q1, q2

transitions q00−→ q2

q0a−→ q1, (a ∈ {1, . . . , 9})

q1a−→ q1, (a ∈ {0, . . . , 9})

CS 275 Sect. II.3.3. 43/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

II.3.1. String Recognition (13.1)

II.3.2. Nondeterministic Finite State Automata (13.2)

II.3.3. Examples of Automata (13.3)

II.3.4. Automata with Empty Move Transitions (13.4)

II.3.5. Deterministic Finite State Automata (13.6)

CS 275 Sect. II.3.4. 44/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

Example

Automaton with empty move transitions are like NFAs, but have thepossibility to make transitions without consuming a letter. Thesetransitions are labelled as ε.

q0 q1

ba

ε

CS 275 Sect. II.3.4. 45/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

Display style

automaton A

states q0, q1

terminals a, b

start q0

final q1

transitions q0a−→ q0,

q0ε−→ q1,

q1b−→ q1.

CS 275 Sect. II.3.4. 46/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

Definition NFA with Empty Moves

Definition

A::::::::::::::::::non-deterministic

::::::finite

::::::state

::::::::::::automaton

:::::with

:::::::empty

:::::::moves

(Q, q0,F ,T ,−→) is given by

I A finite set Q of::::::states.

I A single::::::initial

:::::state q0.

I A set F ⊆ Q of::::::::::accepting

:::::::states.

I A finite set of:::::::::terminal

:::::::::symbols T .

I A relation −→ between q ∈ Q, a ∈ T ∪ {ε} and q′ ∈ Q.We write q

a−→ q′ iff q, a, q′ are in this relation.

CS 275 Sect. II.3.4. 47/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

The Extended Transition Function

We extend the function −→∗ to NFA with empty moves as follows:

Definition

Let A = (Q, q0,F ,T ,−→) be an NFA with empty moves. Then we definea relation −→∗ between q ∈ Q, w ∈ T ∗, q′ ∈ Q.

We write q w ∗−→ q′ iff q,w , q′ are in this relation.

I q ε ∗−→ q′ iff

q = q0ε−→ q1

ε−→ q2ε−→ · · · ε−→ qn = q′

for some n ∈ N, qi ∈ Q.

The special case n = 0 is allowed, so we have always q ε ∗−→ q.

CS 275 Sect. II.3.4. 48/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

The Extended Transition Function

Definition (Cont)

I For w = a1 · · · qn ∈ T+ we define q w ∗−→ q′ iff

q = q0ε ∗−→ q′0

a1−→ q1

ε ∗−→ q′1a2−→ q2

ε ∗−→ q′2a3−→ q3

· · ·ε ∗−→ q′n−1

an−→ qnε ∗−→ q′n = q′

for some qi , q′i ∈ Q.

CS 275 Sect. II.3.4. 49/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

Informal Understanding

So q a1···an∗−→ q′ if we can reach from q state q′ using finitely many

ε-transitions, then one a1-transition, then again finitely many ε-transitions,an a2 transition, . . ., finitely many ε-transitions, and an an transition:

q0ε−→ ε−→ · · · ε−→ a1−→ q1

ε−→ ε−→ · · · ε−→ a2−→ q2ε−→ · · · ε−→an−1−→ qn−1

ε−→ ε−→ · · · ε−→ an−→ qnε−→ · · · ε−→ q′

CS 275 Sect. II.3.4. 50/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

Language Accepted

Definition

Let A = (Q, q0,F ,T ,−→) be an NFA with empty moves. Then

L(A) := {w ∈ T ∗ | ∃q′ ∈ F .q0w ∗−→ q′}.

CS 275 Sect. II.3.4. 51/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

NFA with Empty Moves are Equivalent to NFAs

Theorem (II.3.4.1.)

Let A be an NFA with empty moves.Then there exists an NFA A′ without empty moves s.t. L(A) = L(A′).A′ can be computed from A.

CS 275 Sect. II.3.4. 52/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

Proof Idea

I Let A = (Q, q0,F ,T ,−→) be an NFA with empty moves.I We define an equivalent ordinary NFA A′ = (Q ′, q′0,F

′,T ′,−→) asfollows:

I Q ′ = Q, q′0 = q0, T ′ = T .

I qa

−→′ q′ iff q a ∗−→ q′,

I F ′ := {q ∈ Q | ∃q′ ∈ F .q ε ∗−→ q′}.

I It is easy to see that L(A) = L(A′).See Additional Material for details.

CS 275 Sect. II.3.4. 53/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

Translating NFA with Empty Moves into NFA

I So from an NFA with empty moves we get an NFA without emptymoves by:

I Having the same states, initial state and terminals as the original NFAwith empty moves.

I Having a transition qa

−→′ q′ in the new NFA, if we can get from q byusing in the original automaton

I first arbitrary many ε-transitions (0-many is allowed)I one transition labelled with aI followed by arbitrary many ε-transitions (0-many is allowed).

I Have as new accepting states all states from which one can get byusing ε-transitions (including 0-many) to an accepting state of theoriginal automaton.

CS 275 Sect. II.3.4. 54/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

Example

Consider the NFA with empty moves from above:

q0 q1

ba

ε

CS 275 Sect. II.3.4. 55/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

Example

The transformed version without empty moves is as follows:

q0 q1

a b

a,b

CS 275 Sect. II.3.4. 56/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

Example 1 Using empty moves for Designing NFA

With empty move transitions it is easier to design an NFA:Step 1: Define an NFA with empty movesAs an example consider the language {an(ab)m | n,m ∈ N}

q0 q1

a

ε

q2

a b

CS 275 Sect. II.3.4. 57/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

Example 1 Using empty moves for Designing NFA

Step 2: Transform it into a NFA without empty moves:

q0 q1

a

q2

b

a

aa

CS 275 Sect. II.3.4. 58/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

Example 2 Using empty moves for Designing NFA

Consider L = {(ab)n | n ∈ N} ∪ {(ac)n | n ∈ N}.Step 1: NFA with empty moves:

q0 q1

ε

q2

a

b

q3 q4

a

CS 275 Sect. II.3.4. 59/ 80

II.3.4. Automata with Empty Move Transitions (13.4)

Example 2 Using empty moves for Designing NFA

Step 2: Transform it into a NFA without empty moves:

q0 q1 q2

a

b

q3 q4

a

c

a

a

CS 275 Sect. II.3.4. 60/ 80

II.3.5. Deterministic Finite State Automata (13.6)

II.3.1. String Recognition (13.1)

II.3.2. Nondeterministic Finite State Automata (13.2)

II.3.3. Examples of Automata (13.3)

II.3.4. Automata with Empty Move Transitions (13.4)

II.3.5. Deterministic Finite State Automata (13.6)

CS 275 Sect. II.3.5. 61/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Definition DFA

Definition

Let A = (Q, q0,F ,T ,−→) be an NFA. A is a::::::::::::::deterministic

:::::finite

::::::state

:::::::::::automaton, in short

::::DFA, if for all q ∈ Q and a ∈ T there exist at most

one q′ s.t.q

a−→ q′

So deterministic finite state automata are those automata correspondingto real programs: we have never to make a choice.

CS 275 Sect. II.3.5. 62/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Example (DFA)

The following NFA is a DFA:

s

q0 q1 q2

tq3

q6

a

o

q4 q5

q7

r t

p

Note that there is no transition from q0 labelled by letter 6= s.For a DFA from each state and element of the alphabet there need to beat most one transition – it is possible to have no transition.

CS 275 Sect. II.3.5. 63/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Example (not a DFA)

The following NFA is not a DFA:

q0

q3

o

q4 q5

r t

p

s q1

t

q2

a

t

sq6 q7 q8 q9

CS 275 Sect. II.3.5. 64/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Theorem II.3.5.1.

Theorem (II.3.5.1.)

Let A be an NFA.Then there exists a DFA A′ s.t. L(A) = L(A′).A′ can be computed from A.

Remark: Wikipedia describes the following argument and algorithm nicely:https://en.wikipedia.org/wiki/Powerset construction

CS 275 Sect. II.3.5. 65/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Example

Consider the NFA above which was not an DFA:

q0

q3

o

q4 q5

r t

p

s q1

t

q2

a

t

sq6 q7 q8 q9

CS 275 Sect. II.3.5. 66/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Definition of the DFA

I We define a DFA as follows:I The states Q ′ of the DFA are all possible sets of states Q of the NFA.

I Note that since the Q is finite, Q ′ is finite.

I The initial state is the set of initial states, here {q0}.

CS 275 Sect. II.3.5. 67/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Definition of the DFA

I From a set of states B we can make an a-transition to the set ofstates q, which can be reached from any state q ∈ B by ana-transition.

I For instance

I {q0}s

−→′ {q1, q6}since the set of states we can reach by an s-transition from q0 arestates q1, q6.

I {q1, q6}t

−→′ {q2, q7}since the set of states we can reach by a t-transition from any of thestates q1 or q6 are the states q2, q7.

I A set of states is an accepting state, if at least one of the states in itis in F .

I For instance {q9} or {q5} or {q4, q9} are accepting.

CS 275 Sect. II.3.5. 68/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Optimisation of the DFA

I We can as well omit all states which are unreachable from the initialstate.

I It is not necessary to add all subsets of Q to Q ′:I We can omit ∅:

I ∅ is not an accepting state, and ∅ a−→ ∅.I So ∅ is a

::::sink of the DFA.

It’s non-accepting and all transitions from it go back to it (why?). Sowe can’t escape from it (why?).

I We can omit it and all transitions into it.

I So we get a (usually much smaller) automaton, by starting from theinitial state {q0}, and successively determine all transitions of theDFA which don’t end up in ∅.

I We give two examples and then introduce an algorithm fortransforming an NFA into a DFA.

CS 275 Sect. II.3.5. 69/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Example above Repeated

q0

q3

o

q4 q5

r t

p

s q1

t

q2

a

t

sq6 q7 q8 q9

CS 275 Sect. II.3.5. 70/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Corresponding DFA

Using the technique above we obtain the following automaton:

st

a

o

r t

p

{q0}

{q4} {q5}

{q1, q6} {q2, q7}

{q3}

{q8} {q9}

Note that this was the original DFA above (up to renaming of states).

CS 275 Sect. II.3.5. 71/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Simplified Notation

For simplicity, we omit the {· · · }, and write e.g. q0, q1 instead of the state{q0, q1}.Note that q0, q1 is still one new state:

st

a

r t

p

q0 q2, q7

q3

q8

q1, q6q9

q4 q5

o

CS 275 Sect. II.3.5. 72/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Example 2

Consider the following NFA accepting L = . . .:

a

a,b

b

q1q0

CS 275 Sect. II.3.5. 73/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Corresponding DFA

a

b

b

a

b

q0

q1

q0, q1

CS 275 Sect. II.3.5. 74/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Algorithm for Obtaining from an NFA an Equivalent DFA

I Input: An NFA (Q, q0,F ,T ,−→).I Output: A DFA (Q ′, q′0,F

′,T ′,−→′).I T ′ := T , q′0 := {q0}.I We first determine iteratively

Q ′ (which consists of subsets of Q)−→′

Simultaneously we define iteratively the set of states

Treated(Q ′) ⊆ Q ′

These are the set of states for which we have defined −→′ originatingfrom them completely.

I Initially letQ ′ := {q′0}−→′ := ∅Treated(Q ′) := ∅

CS 275 Sect. II.3.5. 75/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Algorithm for Obtaining an Equivalent DFA from an NFA

I As long as there is a state q′ = {q1, . . . , qn} s.t.

q′ ∈ Q ′ \ Treated(Q ′)

select one and do the following:I If for some a ∈ T there is at least one i and at least one transition

qia−→ q′ for some a ∈ T do the following:

I For each such a let

q′′ := {q′1, . . . , q′m} := {q′ ∈ Q | ∃i ∈ {1, . . . , n}.qia−→ q′}

So q′′ = {q′1, . . . , q′m} are all states which are obtained by ana-transition from at least one state in {q1, . . . , qn}.

I Ifq′′ 6∈ Q ′ ,

then add q′′ to Q ′.I Then add to −→′

q′a

−→′ q′′

I Once this has been done for all a, then add q′ to Treated(Q ′).

CS 275 Sect. II.3.5. 76/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Algorithm for Obtaining an Equivalent DFA from an NFA

I If there was no such a then add q′ to Treated(Q ′) without adding anyelement to −→′.

I Once Q ′ = Treated(Q ′), so the loop above has been completed, wecomplete the definition of the DFA by defining

F ′ := {{q1, . . . , qn} ∈ Q ′ | {q1, . . . , qn} ∩ F 6= ∅}

So F ′ is the set of states in Q ′, which contain at least one state in F .

CS 275 Sect. II.3.5. 77/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Proof Idea of Equivalence between NFA and generatedDFA

I One can easily see that in the DFA we have that for all words w ∈ T ∗

{q0} w ∗−→′ {q1, . . . , qn} =: B

iff

B = {q | q0 w ∗−→ q}

so B is the set of states which we can reach from q0 by a w -transition.

I So that in the DFA after parsing w we reach state

q′ := {q0, . . . , qn}

means that q0, . . . , qn are all states we can reach in the NFAafter parsing w .

CS 275 Sect. II.3.5. 78/ 80

II.3.5. Deterministic Finite State Automata (13.6)

Proof Idea of Equivalence between NFA and generatedDFA

I Therefore in the DFA we reach an accepting state (one containing anelement of F ) by a w -transition,iff the NFA can reach an accepting state by a w -transition.

I Therefore the language accepted by this DFA is the same as thelanguage accepted by the original NFA.

I A formal proof can be found in the Additional Material.

CS 275 Sect. II.3.5. 79/ 80

II.3.5. Deterministic Finite State Automata (13.6)

DFA are special cases of NFA

I Note that every DFA is an NFA.I If you are

I asked to define an NFA and derive from it a DFA,I and your first idea is already deterministic, i.e. a DFA,

I then your solution can be used as an answer for both the NFA andthe DFA question.

I So you don’t need to make your initial solution non-deterministic.

CS 275 Sect. II.3.5. 80/ 80