NFAs and DFAscpennycu/2019-2020/Spring...Deterministic vs. Nondeterministic Finite Automata...

Post on 20-Apr-2020

6 views 0 download

Transcript of NFAs and DFAscpennycu/2019-2020/Spring...Deterministic vs. Nondeterministic Finite Automata...

NFAs and DFAs

Reminder: Regular OperationsLet A and B be languages. We define the regular

operations union, concatenation, and star as

follows:

● Union: A ∪ B = {x | x ∈ A or x ∈ B }.

● Concatenation: A ⚬ B = {xy | x ∈ A and y ∈ B }.

● Star: A* = {x

1

x

2

...x

k

| k ≥ 0 and each x

i

∈ A }.

Proofs that Regular Languages Are Closed Under...● Union: A ∪ B = {x | x ∈ A or x ∈ B}.

○ Proof by Construction (last class)

But…

● Concatenation: A ⚬ B = {xy | x ∈ A and y ∈ B}.

● Star: A* = {x

1

x

2

...x

k

| k ≥ 0 and each x

i

∈ A}.

We Need Better Tools!

Deterministicvs.

NondeterministicFinite Automata

Everything discussed thus far was

deterministic, thus a DFA

Now, we will expand DFAs to allow

multiple potential paths.

Two types of FSMs

What is Determinism?

Are computers deterministic?

“If the current state is known, and the

current inputs are known, then the

future state is also known.”

● No Choices

● No Randomness

● No Oracles

● No Errors/Cheating

According to our previous definition,

FSM == DFA

What is Nondeterminism?

“If the current state is known, and the

current inputs are known, there may be

multiple possible future states.”

● Random choice?

● Parallel choice and simultaneous execution?

NFAs: Relaxing The Requirements

Multiple edges with the same label

ε (optional) edges

● ε is not in Σ● Does not consume input character

Only need one path to an accept state

What could go wrong?

How do we know which path to try?

● Try them all

● Always make the right choice

NFA Formal Definition

A Nondeterministic Finite

Automaton is a 5-tuple

N = (Q, Σ, δ, q

0

, F )

where:

Q Set of states (finite)

Σ Alphabet of symbols (finite)

δ The transition function

δ: Q x Σε ➝ 𝓟(Q)

q

0

The starting (initial) state

q

0

∈ Q

F The set of “Accept” states

F ⊆ Q

NFA Formal Definition Example

N = (Q, Σ, δ, q

0

, F )

Q {q

1

, q

2

, q

3

, q

4

}

Σ {0, 1}

δ

q

0

q

1

F {q

4

}N = ({q

1

, q

2

, q

3

, q

4

}, {0, 1}, δ, q

1

, {q

4

})

NFA Formal Definition Example

N = (Q, Σ, δ, q

0

, F )

Q {q

1

, q

2

, q

3

, q

4

}

Σ {0, 1}

δ

q

0

q

1

F {q

4

}N = ({q

1

, q

2

, q

3

, q

4

}, {0, 1}, δ, q

1

, {q

4

})

NFA Formal Definition of ComputationLet N = (Q, Σ, δ, q

0

, F )

Let y

1

y

2

...y

m

be a string w where y

i

∈ Σε

N accepts w if there is a sequence of

states r

0

, r

1

, r

2

...r

m

in Q such that:

1. r

0

= q

0

,

2. r

i+1

∈ δ(r

i

, y

i+1

) for 0 ≤ i < m, and

3. r

m

∈ F

N “recognizes”

language A if

A = {w | N accepts w}

We now have a

NONDETERMINISTIC tool that we

can use to understand Regular

Languages!

Wait… Can we say this?!?

Proof of Equivalency

NFAs and DFAs

Proof by Construction:

A method to convert

N = (Q, Σ, δ, q

0

, F )

into

M = (Q ’, Σ, δ’, q ’

0

, F ’)

What Were We Talking About Last Time???

http://cscrunch.com:8890

Convert NFA to DFAProof by Construction. (ignoring ε for now)

Let N = (Q, Σ, δ, q

0

, F ) be a NFA that recognizes

some language A.

Construct a DFA M = (Q ’, Σ, δ’, q ’

0

, F ’) that

recognizes the same language A.

For ε, define E(R) = {q | q can be reached from R

by traveling along 0 or more ε arrows} where

R ⊆ Q and q ∈ Q.

q’

0

= E({q

0

})

δ’(R, a) = {q ∈ Q | q ∈ E(δ(r, a)) for some r ∈ R}

Q’ = 𝓟(Q)

ex: {{p

0

}, {p

0

, p

1

}, {p

0

, p

2

}, {p

0

, p

1

, p

2

}, …}

q ’

0

= {q

0

}

F ’ = {R ∈ Q ’ | R contains an accept state of N }

or, F ’ = {R ∈ Q ′ | R ∩ F ≠ ∅}

δ’(R, a) = {q ∈ Q | q ∈ δ(r, a) for some r ∈ R }

where R ∈ Q’ and a ∈ Σ.

Practice: Convert this NFA to a DFA

Practice: Convert this NFA to a DFA

We defined DFAs.

We defined Computation using DFAs.

We defined Regular Languages using

DFAs.

We defined NFAs.

We defined Computation using NFAs.

We showed equivalence of NFAs to

DFAs.

Now What?

Regular OperationsThey eat their fiber.

Union, Concatenation, and Star:

3 Proofs by Construction

Regular Operations: UnionLet N

1

= (Q

1

, Σ, δ1

, q

1

, F

1

) recognize A

1

,

and N

2

= (Q

2

, Σ, δ2

, q

2

, F

2

) recognize A

2

.

Construct N = (Q, Σ, δ, q

0

, F ) to recognize A

1

∪A

2

.

Q = {q

0

} ∪ Q

1

∪ Q

2

F = F

1

∪ F

2

For q ∈ Q and any a ∈ Σε,

Regular Operations: ConcatenationLet N

1

= (Q

1

, Σ, δ1

, q

1

, F

1

) recognize A

1

,

and N

2

= (Q

2

, Σ, δ2

, q

2

, F

2

) recognize A

2

.

Construct N = (Q, Σ, δ, q

1

, F

2

) to recognize A

1

○ A

2

.

Q = Q

1

∪ Q

2

For q ∈ Q and any a ∈ Σε,

Regular Operations: StarLet N

1

= (Q

1

, Σ, δ1

, q

1

, F

1

) recognize A

1

.

Construct N = (Q, Σ, δ, q

0

, F ) to recognize A

1

*.

Q = {q

0

} ∪ Q

1

F = {q

0

} ∪ F

1

For q ∈ Q and any a ∈ Σε,