• date post

31-Mar-2018
• Category

## Documents

• view

232

2

Embed Size (px)

### Transcript of Nondeterministic Finite Automata and Regular Deterministic Finite Automaton A DFA is a 5-tuple M =...

• Nondeterministic Finite Automata and Regular Expressions

CS 2800: Discrete Structures, Spring 2015

Sid Chaudhuri

• Recap: Deterministic Finite Automaton A DFA is a 5-tuple M = (Q, , , q0, F)

Q is a finite set of states is a finite input alphabet (e.g. {0, 1}) is a transition function : Q Q q0 Q is the start/initial state

F Q is the set of final/accepting states

Yes No1

100

• csunplugged.com, clubpenguin.wikia.com

• csunplugged.com, clubpenguin.wikia.com

P = NP!!!

• csunplugged.com, clubpenguin.wikia.com

P = NP!!!

• csunplugged.com, clubpenguin.wikia.com

P = NP!!!

A NON-deterministic finite automaton lets you try all possible choices in

parallel. If ANY choice leads you to the treasure, the pirate can't harm you!

• A non-deterministic finite automaton

q01 0,10,1 q1 0,1q2

• A non-deterministic finite automaton

q01 0,10,1 q1 0,1q2

Different transitionsfrom q0 on input 1

• A non-deterministic finite automaton

q01 0,10,1 q1 0,1q2

An NFA accepts a string x if it can getto an accepting state on input x

Different transitionsfrom q0 on input 1

• A non-deterministic finite automaton

q01 0,10,1 q1 0,1q2

What language does this automaton accept?

• A non-deterministic finite automaton

q01 0,10,1 q1 0,1q2

Answer: All strings ending with 1

• Another NFA

q01 0,10,1 q2 0,1q3

What language does this automaton accept?

q10,1

• Another NFA

q01 0,10,1 q2 0,1q3

Answer: All strings with 1 in the penultimate place

q10,1

• Non-deterministic Finite Automaton An NFA is a 5-tuple M = (Q, , , q0, F)

• Non-deterministic Finite Automaton An NFA is a 5-tuple M = (Q, , , q0, F)

Q is a finite set of states

q0 q1 q2 q3

• Non-deterministic Finite Automaton An NFA is a 5-tuple M = (Q, , , q0, F)

Q is a finite set of states is a finite input alphabet (e.g. {0, 1})

q0 q1 q2 q3

• 0,1

Non-deterministic Finite Automaton An NFA is a 5-tuple M = (Q, , , q0, F)

Q is a finite set of states is a finite input alphabet (e.g. {0, 1}) is a transition function : Q 2Q

q0 q11 0,10,1 q2 0,1q3

• 0,1

Non-deterministic Finite Automaton An NFA is a 5-tuple M = (Q, , , q0, F)

Q is a finite set of states is a finite input alphabet (e.g. {0, 1}) is a transition function : Q 2Q

q0 q11 0,10,1 q2 0,1q3

Only changefrom DFAs

• 0,1

Non-deterministic Finite Automaton An NFA is a 5-tuple M = (Q, , , q0, F)

Q is a finite set of states is a finite input alphabet (e.g. {0, 1}) is a transition function : Q 2Q

q0 Q is the start/initial state

q0 q11 0,10,1 q2 0,1q3

Only changefrom DFAs

• 0,1

Non-deterministic Finite Automaton An NFA is a 5-tuple M = (Q, , , q0, F)

Q is a finite set of states is a finite input alphabet (e.g. {0, 1}) is a transition function : Q 2Q

q0 Q is the start/initial state

F Q is the set of final/accepting states

q0 q11 0,10,1 q2 0,1q3

Only changefrom DFAs

• Non-deterministic Finite Automaton

An NFA accepts a string x if it can get to an accepting state on input x

• Non-deterministic Finite Automaton

An NFA accepts a string x if it can get to an accepting state on input x Think of it as trying many options in parallel, and

hoping one path gets lucky

• Non-deterministic Finite Automaton

An NFA accepts a string x if it can get to an accepting state on input x Think of it as trying many options in parallel, and

hoping one path gets lucky

Transition f (state, symbol) is possible

• Non-deterministic Finite Automaton

An NFA accepts a string x if it can get to an accepting state on input x Think of it as trying many options in parallel, and

hoping one path gets lucky

Transition f (state, symbol) is possible the NFA treats this as a rejecting path (the string

may still reach an accepting state by another path)

• Non-deterministic Finite Automaton

An NFA accepts a string x if it can get to an accepting state on input x Think of it as trying many options in parallel, and

hoping one path gets lucky

Transition f (state, symbol) is possible the NFA treats this as a rejecting path (the string

may still reach an accepting state by another path) A convenient shortcut for our hell/black-hole state

• Non-deterministic Finite Automaton

An NFA accepts a string x if it can get to an accepting state on input x Think of it as trying many options in parallel, and

hoping one path gets lucky

Transition f (state, symbol) is possible the NFA treats this as a rejecting path (the string

may still reach an accepting state by another path) A convenient shortcut for our hell/black-hole state Class convention: Draw all possible transitions for DFA.

Not required for NFA (missing transitions lead to hell).

• Non-deterministic Finite Automaton

Every DFA is an NFA

• Non-deterministic Finite Automaton

Every DFA is an NFA If we're strict with our notation, we need to replace the

transitionf (state1, symbol) state2 withf (state1, symbol) {state2}

• Non-deterministic Finite Automaton

Every DFA is an NFA If we're strict with our notation, we need to replace the

transitionf (state1, symbol) state2 withf (state1, symbol) {state2}

Every NFA can be simulated by a DFA

• Non-deterministic Finite Automaton

Every DFA is an NFA If we're strict with our notation, we need to replace the

transitionf (state1, symbol) state2 withf (state1, symbol) {state2}

Every NFA can be simulated by a DFA i.e. they accept exactly the same language

• Non-deterministic Finite Automaton

Every DFA is an NFA If we're strict with our notation, we need to replace the

transitionf (state1, symbol) state2 withf (state1, symbol) {state2}

Every NFA can be simulated by a DFA i.e. they accept exactly the same language Exponential blowup: if the NFA has n states, the DFA

can require up to 2n states

• Thought for the Day #1

Find an NFA with n states that can't be simulated by a DFA with less than 2n states

• Every NFA can be simulated by a DFA

• Every NFA can be simulated by a DFA

p

q

ra

a

NFA(fragment)

• Every NFA can be simulated by a DFA

p

q

ra

a

{p} {q, r}a

DFA (fragment)NFA(fragment)

• Every NFA can be simulated by a DFA

p

q

ra

a

{p} {q, r}a

NFA(fragment)

DFA (fragment)

(This is merely illustrative formal construction on following slides)

Subset constructionMain idea: ONE state of DFA tracks current states of ALL evolving paths of NFA

• Every NFA can be simulated by a DFA

NFA Equivalent DFA

Specification

States

Alphabet

Transition Function

Initial State

Accepting States

• Every NFA can be simulated by a DFA

NFA Equivalent DFA

Specification (Q, , , q0, F) (2Q, , ', q'0, F')

States

Alphabet

Transition Function

Initial State

Accepting States

• Every NFA can be simulated by a DFA

NFA Equivalent DFA

Specification (Q, , , q0, F) (2Q, , ', q'0, F')

States Q 2Q

Alphabet

Transition Function

Initial State

Accepting States

• Every NFA can be simulated by a DFA

NFA Equivalent DFA

Specification (Q, , , q0, F) (2Q, , ', q'0, F')

States Q 2Q

Alphabet

Transition Function

Initial State

Accepting States

• Every NFA can be simulated by a DFA

NFA Equivalent DFA

Specification (Q, , , q0, F) (2Q, , ', q'0, F')

States Q 2Q

Alphabet

Transition Function '(S, a) = s S (s, a)Initial State

Accepting States

• Every NFA can be simulated by a DFA

NFA Equivalent DFA

Specification (Q, , , q0, F) (2Q, , ', q'0, F')

States Q 2Q

Alphabet

Transition Function '(S, a) = s S (s, a)Initial State q0 q'0 = {q0}

Accepting States

• Every NFA can be simulated by a DFA

NFA Equivalent DFA

Specification (Q, , , q0, F) (2Q, , ', q'0, F')

States Q 2Q

Alphabet

Transition Function '(S, a) = s S (s, a)Initial State q0 q'0 = {q0}

Accepting States F F' = {S 2Q | S F }

• Why NFAs?

• Why NFAs?

They can be way more compact than DFAs

• Why NFAs?

They can be way more compact than DFAs

0,1

q0 q11

0,1

q3 q20,1

NFA

• Why NFAs?

They can be way more compact than DFAs

q000 q001 q010 q011

q100 q101 q110 q111

0 1 0

00

00

0

0

1

1

1

1

1

1

1

0,1

q0 q11

0,1

q3 q20,1

NFA Equivalent minimal DFA

• Why NFAs?

They can be way more compact than DFAs

It's easier to directly convert regular expressions (wildcard search on steroids) to NFAs

q000 q001 q010 q011

q100 q101 q110 q111

0 1 0

00

00

0

0

1

1

1

1

1

1

1

0,1

q0 q11

0,1

q3 q20,1

NFA Equivalent minimal DFA

• XKCD

• Playing with regexes

http://regex101.com