finite automata and non-determinismrlc/courses/theory/classnotes/finiteautomata.pdf · designing...

60
Finite Automata and Non-Determinism Foundations of Computer Science Theory

Upload: others

Post on 05-Sep-2019

8 views

Category:

Documents


0 download

TRANSCRIPT

Finite Automata and Non-Determinism Foundations of Computer

Science Theory

Finite Automata

• What is a computer? • Maybe it is better to discuss an idealized

computer called a computational model • We will discuss several different

computational models, depending on the features that we want to focus on

• Let’s begin our discussion with the simplest model, called the discrete finite automaton (DFA)

What is a Finite Automaton?

• A finite automaton is a formal system of computation that remembers only a finite amount of information

• The information is represented by a state • The state changes in response to inputs • Rules tell us how the state changes in

response to inputs; these rules are called transitions

Why Study Finite Automata?

• Discrete finite automata (DFAs) are used for both design and verification of circuits and in communication protocols

• They are also used for many text-processing applications

• DFAs are an important component of compilers

• They can describe simple patterns of events

Finite Automata Predate Computers

The abacus

The Jacquard loom, 1801

The Prague Orloj, originally built in 1410.

Discrete Finite Automata (DFA)

• We describe the behavior of discrete finite automata abstractly (without reference to any specific application) using state machines

• The discrete finite automaton is represented as a state machine like the one shown below, which has three states: q1, q2, and q3

Discrete Finite Automata (DFA)

• Given a sequence of inputs (an input string), start in the start state and follow the transition from each symbol in turn

• The input is accepted if you wind up in a final (accepting) state after all inputs have been read

Discrete Finite Automata (DFA)

• For example, when we feed the input string 1101 to this machine, the processing proceeds as follows:

1. Start in state q1. 2. Read 1, follow transition from q1 to q2

3. Read 1, follow transition from q2 to q2

4. Read 0, follow transition from q2 to q3

5. Read 1, follow transition from q3 to q2

6. Accept because the machine is in an accept state q2 at the end of the input

Examples of DFAs

Drawing from Dr. Steve Goddard, http://www.cse.unl.edu/~goddard/Courses/CSCE310J

A vending machine: A sequences of coin deposits to purchase a 20₵ soda:

A security system: A sequences of events such that an alarm should sound:

Examples of DFAs A digital watch: A sequence of button-pushes to set an alarm.

Floating-point numbers: A sequence of characters that represent a floating-point number:

Example strings accepted by this DFA: +3.0, 3.0, 0.3E1, 0.3E+1, -0.3E+1, -3E8

Example DFA: Communications Protocol

Language of an Automaton

• The set of strings accepted by an automaton A is called the language of A, denoted L(A) – Different DFAs correspond to different languages – From our vending machine example: L(Vending) =

strings that determine if a soda is dispensed from the vending machine

• For example, the string NND is accepted, where N represents “nickel” an D represents “dime”

• DFAs accept exactly the Regular Languages

Formal Definition of Finite Automata

• More formally, a finite automaton is a 5-tuple (Q, Σ, δ, q0, F) where: – 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 accepting (or final) states

Formal Definition of Finite Automata

• We can describe a specific automaton, M, as M = (Q, Σ, δ, q0, F) where: – Q = {q1, q2, q3} – Σ = {0, 1} – The transition function δ is described as

– q1 is the start state – F = {q2}

Formal Definition of Finite Automata

• If L is the set of all strings that machine M accepts, we say that L is the language of machine M and write L(M) = L – This means “M recognizes L” – Distinction: a machine may accept several strings,

but it always recognizes only one language – If a machine accepts no strings, it still recognizes

one language – the empty language ∅

Formal Definition of Finite Automata

• For our example finite automaton,

• Thus, L(M) = L, or equivalently, M recognizes L

L = {w : w ends with a 1, or w has an even number of 0s following the last 1}

Example of a Finite State Machine

Formal description of this machine: M = ({q1, q2}, {0,1}, δ, q1, {q2}) where δ =

Try strings 1101 and 110. What is the language of this machine?

Example of a Finite State Machine

Formal description of this machine: M = ({q1, q2}, {0,1}, δ, q1, {q1}) where δ =

Try strings 1101 and 110. What is the language of this machine?

Example of a Finite State Machine

Formal description of this machine: M = ({q0, q1, q2}, {0,1}, δ, q0, {q1})

where δ =

Try strings 101, 0111, 100, 11001, and 1100. What is the language of this machine?

Example of a Finite State Machine

Try strings ab, ba, a, aa, b, bb, bbba, and bab. What is the language of this machine?

Designing Finite State Machines

• Question: Design a finite state machine to recognize the language from ∑ = {0,1} consisting of all strings with an odd number of 1s

1. Create a state for each of the possibilities

Designing Finite State Machines

2. Next, assign transitions by seeing how to go from one possibility to another upon reading a symbol

3. Add the start and accept states

Designing Finite State Machines

• Question: Design a finite state machine to recognize the language of all strings from ∑ = {0,1} that contain the string 001 as a substring

• What are the possibilities? – Have not seen any symbols of the pattern – Have seen a 0 – Have seen 00 – Have seen the entire pattern 001

Designing Finite State Machines

• Assign states q, q0, q00, q001, to the possibilities • Assign the transitions to each state:

– If in q and 1 is read, stay in q; if 0 is read, move to q0 – If in q0 and 1 is read, return to q; if 0 is read, move to q00 – If in q00 and 1 is read, move to q001 and accept; if 0 is read,

stay in q00 – If in q001 and 1 or 0 is read, stay in q001

Designing Finite State Machines

• Question: Design a finite state machine to recognize the language of all strings from ∑ = {0,1} except those that contain the string 001 as a substring – Just reverse the accepting and the non-accepting

states from the previous DFA

Designing Finite State Machines

• Question: Design a finite state machine to recognize the language of all strings from ∑ = {a,b} that start with the prefix ab – Notice that after the first two symbols in the string have

been read, the machine will either accept or reject; no further decisions need to be made, but the automaton still needs to process the entire string

Designing Finite State Machines

• We can therefore solve this problem with an automaton that has four states: an initial state, two states for recognizing ab (one of which is the final accept state), and one non-final “trap” state to indicate a rejected string

• Sometimes we can partition strings into equivalence classes of those that all share the same “future”

• For example, Let L = {w ∈ {a, b}* : w contains an even number of a’s and an odd number of b’s}

Designing Finite State Machines

Non-Deterministic DFAs

• A non-deterministic finite automaton (NFA) has the ability to be in several states at once – Transitions from a state on an input symbol can be

to any set of states, or to no state at all

• NFAs, just like DFAs, accept exactly the Regular Languages

Why Study Non-Determinism?

• Digital computers are completely deterministic – The state of a computer at any time is uniquely

predictable from the input and the initial state

• Since we are trying to model real systems, why should we include such non-mechanical features such as choice?

Why Study Non-Determinism?

• Non-determinism is an effective mechanism for describing some complicated languages concisely

• Non-determinism is sometimes helpful in solving problems easily – Many deterministic algorithms require that we

make a choice at some stage – For example, non-determinism can simulate

choice in search-and-backtrack algorithms

Non-Determinism

• As with a DFA, start in the (single) start state • A string is accepted if any path leads to a final state • A string is rejected only if there is no way to reach a

final accept state • Intuitively, the NFA always “guesses” the correct path

Formal Definition of an NFA

• A finite set of states, typically Q • An input alphabet, typically Σ • A transition function, typically δ • A start state in Q, typically q0 • A set of final states F ⊆ Q

Transition Function of an NFA

• For the NFA, the transition function δ is to a set of states (possibly empty) instead of a single state

• The range of δ is the power set 2Q; thus the transition is not always to a single element of Q, but to a subset of 2Q – This subset defines the set of all possible states that can be

reached by the transition – For example, if the current state is q1 when the symbol a is

read, and δ(q1, a) = {q0, q2}, then the transition could be to either q0 or q2

• In addition, δ(qi, a) may be empty, meaning that there is no transition defined for this specific situation; δ(qi, a) = ∅

• Also, epsilon can be the second argument of δ (a transition can be made without consuming an input symbol); δ(qi, ε) = qi

Example: Moves on a Chessboard

• States = squares on a 3x3 chessboard • The start state is 1 and the final state is 9 • An input character can be r (move to an

adjacent red square – 2, 4, 6, or 8) or b (move to an adjacent black square – 1, 3, 5, 7, or 9)

1 2

5

7 9

3

4

8

6

Can a given string consisting of r and b characters get from the start state to the final state? The answer is “yes” if any sequence of available moves will lead to the final state. Note that it is not necessary that all such choices do, and in general, there will always be some choices that will not.

1 2

5

7 9

3

4

8

6

1

r b b

4

2 1

5

3

7

5

1

3

9

7 Accept, since final state reached

Example: Moves on a Chessboard Assume the input string is rbb. Is there any way for this string to get us from the start state 1 to the final state 9? r b

1 {2,4} {5} 2 {4,6} {1,3,5} 3 {2,6} {5} 4 {2,8} {1,5,7} 5 {2,4,6,8} {1,3,7,9} 6 {2,8} {3,5,9} 7 {4,8} {5} 8 {4,6} {5,7,9} 9 {6,8} {5} *

Start state

Final state

δ

ε

NFAs with ε-Transitions (ε-NFA)

• Transitions on ε input – An ε transition can be done spontaneously,

without considering an input character – A convenience at times, but still only the regular

languages are accepted

L = {w : w ∈ (10)n, n ≥ 0}

NFAs with ε-Transitions (ε-NFA)

ε

ε

Example of an ε-NFA

C

E F

A

B D 1 1 1

0

0

0

ε

ε ε

0 1 ε A {E} {B} ∅ B ∅ {C} {D} C ∅ {D} ∅ D ∅ ∅ ∅ E {F} ∅ {B, C} F {D} ∅ ∅

Note that ε is “invisible” as far as input strings are concerned. Notice also that ε has a separate column in the transition table, but it is not an input symbol.

*

Closure of States

• The closure of a state q, CL(q) = the state q union with the set of states that can be reached from state q following only edges labeled ε

• CL(A) = {A}, CL(B) = {B, D} CL(C) = {C}, CL(D) = {D} CL(E) = {B, C, D, E} • The closure of a set of states = union of the

closure of each state

C

E F

A

B D 1 1 1

0 0

0

ε

ε ε

Equivalence of NFA and ε-NFA

• The NFA and the ε-NFA recognize the same types of languages (the regular languages)

• Every NFA is an ε-NFA – It just has no ε-transitions

• Every ε-NFA is an NFA – We can get rid of the ε-transitions by combining

them with the next transition on a real input

• Proof that every ε-NFA is an NFA: – Given an ε-NFA, start at the start state and follow

all the ε-transitions using the closure operator – Then follow all of the transitions from any of these

states on a real input symbol – The NFA is able to get to any of the states that can

be reached using these transitions on the ε-NFA on the real input symbol

Equivalence of NFA and ε-NFA

0 1 ε A {E} {B} ∅ B ∅ {C} {D} C ∅ {D} ∅ D ∅ ∅ ∅ E {F} ∅ {B, C} F {D} ∅ ∅

ε-NFA

0 1 A {E} {B} B ∅ {C} C ∅ {D} D ∅ ∅ E {F} {C, D} F {D} ∅

Example of ε-NFA to NFA

C

E F

A

B D 1 1 1

0 0

0

ε

ε ε 0

NFA

A

B

F E

C D

0

1 1 1

1 1

0

The new final states, B and E, are those whose closures include the original final state, D.

Recall the closure of E = {B, C, D, E}

• When we compare different classes of automata, the question arises whether one class is more powerful than another – By “more powerful” we mean that an automaton

of one kind can achieve something that cannot be done by any automaton of the other kind

Equivalence of DFAs and NFAs

• What about finite acceptors? • Since a DFA is in essence a restricted kind of

NFA, it is clear that any language that is accepted by a DFA is also accepted by some NFA

• The converse is not so obvious, because when we add non-determinism it is conceivable that there is a language accepted by some NFA for which we cannot find a DFA

Equivalence of DFAs and NFAs

• But it turns out that classes of DFAs and NFAs are equally powerful – For every language accepted by some NFA there is a

DFA that accepts the same language

• The idea is to have a procedure that will convert any NFA into an equivalent DFA – This procedure is called “Subset Construction”

Equivalence of DFAs and NFAs

• The rationale is as follows: after an NFA has read a string w, we might not know exactly what state it will be in, but we can say that it must be in one state of a set of possible states, say {qi, qj, …, qk}

• But an equivalent DFA must be in some definite state

• How can we make these two situations correspond?

Equivalence of DFAs and NFAs

• This is how: label each state of the DFA with a set of states in such a way that, after reading string w, the equivalent DFA will be in a single state labeled {qi, qj, …, qk}

• Since for a set of |Q| states there are exactly 2|Q| subsets, the corresponding DFA will have a finite (but perhaps exponential) number of states

Equivalence of DFAs and NFAs

Subset Construction

• Given an NFA with states Q, input alphabet Σ, transition function δN, start state q0, and final states F, construct an equivalent DFA with: – 2|Q| states (subsets of original set of states Q) – Input alphabet Σ – Start state {q0} – Final states = all those with a member of F

• Each DFA state will have a name that “looks like” a set of states (the states of the NFA)

• For a DFA state, an expression such as {p,q} is understood to be the name of a single state, not a set of states

• The DFA state is like a collection of objects whose values are sets of objects of another collection

• As an example, we’ll construct the DFA that is equivalent to the “chessboard” NFA we saw earlier

Subset Construction

r b {1} {2,4} {5}

{2,4} {5}

Subset Construction

1 2

5

7 9

3

4

8

6

DFA states r b 1 {2,4} {5} 2 {4,6} {1,3,5} 3 {2,6} {5} 4 {2,8} {1,5,7} 5 {2,4,6,8} {1,3,7,9} 6 {2,8} {3,5,9} 7 {4,8} {5} 8 {4,6} {5,7,9} 9 {6,8} {5} *

δ

r b {1}

{2,4,6,8} {5}

{2,4} {2,4,6,8} {1,3,5,7}

{1,3,5,7}

{2,4} {5}

Subset Construction

1 2

5

7 9

3

4

8

6

DFA states r b 1 {2,4} {5} 2 {4,6} {1,3,5} 3 {2,6} {5} 4 {2,8} {1,5,7} 5 {2,4,6,8} {1,3,7,9} 6 {2,8} {3,5,9} 7 {4,8} {5} 8 {4,6} {5,7,9} 9 {6,8} {5} *

δ

r b {1}

* {1,3,7,9}

{2,4,6,8} {2,4,6,8} {1,3,7,9} {5}

{2,4} {2,4,6,8} {1,3,5,7}

{1,3,5,7}

{2,4} {5}

Subset Construction

1 2

5

7 9

3

4

8

6

DFA states r b 1 {2,4} {5} 2 {4,6} {1,3,5} 3 {2,6} {5} 4 {2,8} {1,5,7} 5 {2,4,6,8} {1,3,7,9} 6 {2,8} {3,5,9} 7 {4,8} {5} 8 {4,6} {5,7,9} 9 {6,8} {5} *

δ

r b {1}

* {1,3,5,7,9} * {1,3,7,9}

{2,4,6,8} {1,3,5,7,9} {2,4,6,8} {2,4,6,8} {1,3,7,9} {5}

{2,4} {2,4,6,8} {1,3,5,7}

{1,3,5,7}

{2,4} {5}

Subset Construction

1 2

5

7 9

3

4

8

6

DFA states r b 1 {2,4} {5} 2 {4,6} {1,3,5} 3 {2,6} {5} 4 {2,8} {1,5,7} 5 {2,4,6,8} {1,3,7,9} 6 {2,8} {3,5,9} 7 {4,8} {5} 8 {4,6} {5,7,9} 9 {6,8} {5} *

δ

r b {1}

* {1,3,5,7,9} * {1,3,7,9}

{2,4,6,8} {1,3,5,7,9} {2,4,6,8} {2,4,6,8} {1,3,7,9} {5}

{2,4} {2,4,6,8} {1,3,5,7}

{1,3,5,7}

{2,4} {5}

{2,4,6,8} {1,3,5,7,9}

Subset Construction

1 2

5

7 9

3

4

8

6

DFA states r b 1 {2,4} {5} 2 {4,6} {1,3,5} 3 {2,6} {5} 4 {2,8} {1,5,7} 5 {2,4,6,8} {1,3,7,9} 6 {2,8} {3,5,9} 7 {4,8} {5} 8 {4,6} {5,7,9} 9 {6,8} {5} *

δ

r b {1}

* {1,3,5,7,9} * {1,3,7,9} {2,4,6,8} {5}

{2,4,6,8} {1,3,5,7,9} {2,4,6,8} {2,4,6,8} {1,3,7,9} {5}

{2,4} {2,4,6,8} {1,3,5,7}

{1,3,5,7}

{2,4} {5}

{2,4,6,8} {1,3,5,7,9}

Subset Construction

1 2

5

7 9

3

4

8

6

DFA states r b 1 {2,4} {5} 2 {4,6} {1,3,5} 3 {2,6} {5} 4 {2,8} {1,5,7} 5 {2,4,6,8} {1,3,7,9} 6 {2,8} {3,5,9} 7 {4,8} {5} 8 {4,6} {5,7,9} 9 {6,8} {5} *

δ

r b {1}

* {1,3,5,7,9} {2,4,6,8} {1,3,5,7,9} * {1,3,7,9} {2,4,6,8} {5}

{2,4,6,8} {1,3,5,7,9} {2,4,6,8} {2,4,6,8} {1,3,7,9} {5}

{2,4} {2,4,6,8} {1,3,5,7}

{1,3,5,7}

{2,4} {5}

{2,4,6,8} {1,3,5,7,9}

Subset Construction

1 2

5

7 9

3

4

8

6

DFA states r b 1 {2,4} {5} 2 {4,6} {1,3,5} 3 {2,6} {5} 4 {2,8} {1,5,7} 5 {2,4,6,8} {1,3,7,9} 6 {2,8} {3,5,9} 7 {4,8} {5} 8 {4,6} {5,7,9} 9 {6,8} {5} *

δ

Subset Construction

Question: Convert the ε-NFA shown below to an equivalent DFA.

ε

Subset Construction

ε

Question: Convert the ε-NFA shown below to an equivalent DFA.

Summary

• DFAs, NFAs, and ε-NFAs all recognize exactly the same set of languages: the regular languages

• NFAs are easier to design and may have exponentially fewer states than the corresponding DFA

• But only a DFA can be implemented in a real computer!