Nondeterministic Finite Automata CSCI 3130 Formal Languages and - - PowerPoint PPT Presentation

nondeterministic finite automata
SMART_READER_LITE
LIVE PREVIEW

Nondeterministic Finite Automata CSCI 3130 Formal Languages and - - PowerPoint PPT Presentation

1/23 Nondeterministic Finite Automata CSCI 3130 Formal Languages and Automata Theory Siu On CHAN Chinese University of Hong Kong Fall 2017 2/23 Example from last lecture with a simpler solution 1 0 0 1 1 0 or 0 1 0 1 1 0 1 0 0


slide-1
SLIDE 1

1/23

Nondeterministic Finite Automata

CSCI 3130 Formal Languages and Automata Theory Siu On CHAN

Chinese University of Hong Kong

Fall 2017

slide-2
SLIDE 2

2/23

Example from last lecture with a simpler solution

Construct a DFA over alphabet {0, 1} that accepts all strings ending in 01

qε q0 q1 q00 q01 q10 q11

1 1 1 1 1 1 1

  • r

q0 q1 q2

1 1 1

Three weeks later: DFA minimization

slide-3
SLIDE 3

3/23

Another example from last lecture

Construct a DFA over alphabet {0, 1} that accepts all strings ending in 101

  • r

q0 q1 q2 q2

1 1 1 1

slide-4
SLIDE 4

4/23

String matching DFAs

Ending in 01

q0 q1 q2

1 1 1 Ending in 101

q0 q1 q2 q2

1 1 1 1 Fast string matching algorithms to turn a pattern into a string matching DFA and execute the DFA: Boyer–Moore (BM) and Knuth–Morris–Pratt (KMP) (won’t cover in class)

slide-5
SLIDE 5

5/23

Nondeterminism

slide-6
SLIDE 6

6/23

In a few lectures

q0 q1 q2 q2

1 1 1 1 What problems can finite state machines solve? We’ll answer this question in the next few lectures Useful to consider hypothetical machines that are nondeterministic

slide-7
SLIDE 7

7/23

Even easier with guesses

Suppose we could guess when the input string has only 3 symbols lefu Accept strings ending in 101: 3 symbols lefu 1 1 1 This is not a DFA!

slide-8
SLIDE 8

8/23

Nondeterministic finite automata

A machine that allows us to make guesses

q0 q1 q2 q3

0,1 1 1 Each state can have zero, one, or more outgoing transitions labeled by the same symbol

slide-9
SLIDE 9

9/23

Choosing where to go

q0 q1 q2 q3

0,1 1 1 State q0 has two transitions labeled 1 Upon reading 1, we have the choice of staying at q0 or moving to q1

slide-10
SLIDE 10

10/23

Ability to choose

q0 q1 q2 q3

0,1 1 1 State q1 has no transition labeled 1 Upon reading 1 at q1, die; upon reading 0, continue to q2

slide-11
SLIDE 11

11/23

Ability to choose

q0 q1 q2 q3

0,1 1 1 State q1 has no transition going out Upon reading 0 or 1 at q3, die

slide-12
SLIDE 12

12/23

Meaning of NFA

q0 q1 q2 q3

Guess if we are 3 symbols away from end of input If so, guess we will see the pattern 101 Check that we are at the end of input 0,1 1 1

slide-13
SLIDE 13

13/23

How to run an NFA

q0 q1 q2 q3

0,1 1 1 input: 01101 The NFA can have several active states at the same time NFA accepts if at the end, one of its active states is accepting

slide-14
SLIDE 14

14/23

Example

Construct an NFA over alphabet {0, 1} that accepts all strings containing the pattern 001 somewhere 11001010, 001001, 111001 should be accepted

ε, 000, 010101

should not

slide-15
SLIDE 15

15/23

Example

Construct an NFA over alphabet {0, 1} that accepts all strings containing the pattern 001 somewhere

q0 q1 q2 q3

0,1 1 0,1

slide-16
SLIDE 16

16/23

Definition

A nondeterministic finite automaton (NFA) is a 5-tuple (Q, Σ, δ, q0, F) where

◮ Q is a finite set of states ◮ Σ is an alphabet ◮ δ : Q × (Σ ∪ {ε}) → subsets of Q is a transition function ◮ q0 ∈ Q is the initial state ◮ F ⊆ Q is a set of accepting states

Difgerences from DFA:

◮ transition function δ can go into several states ◮ allows ε-transitions

slide-17
SLIDE 17

17/23

Language of an NFA

The NFA accepts string x if there is some path that, starting from q0, ends at an accepting state as x is read from lefu to right The language of an NFA is the set of all strings accepted by the NFA

slide-18
SLIDE 18

18/23

ε-transitions

ε-transitions can be taken for free: q0 q1 q2 ε

a b a accepts a, b, aab, bab, aabab, … rejects

ε, aa, ba, bb, …

slide-19
SLIDE 19

19/23

Example

q0 q1 q2 ε,1 ε

alphabet Σ = {0, 1} states Q = {q0, q1, q2} initial state q0 accepting states F = {q2} table of transition function δ

inputs

1

ε

states

q0 ∅ {q1} {q1} q1 {q0, q1} ∅ {q2} q2 ∅ ∅ ∅

slide-20
SLIDE 20

20/23

Running NFA

ε

q0 q1 q2 ε,1

1

ε

2

00

q0 q1 q2 ε,1

1 2 3

ε

4

q0 q1 q2 ε,1

1 4 3 2

ε

5

q0 q1 q2 ε,1

1 3 2 4

ε

5

slide-21
SLIDE 21

21/23

Running NFA

001

q0 q1 q2 ε

1

, 1

4 3 2

ε

5

101

q0 q1 q2 ε,1

1 3 2

ε

4

11

q0 q1 q2 ε,1

1

ε q0 q1 q2 ε,1

1

ε

2

slide-22
SLIDE 22

22/23

Language of this NFA

q0 q1 q2 ε,1 ε

What is the language of this NFA?

slide-23
SLIDE 23

23/23

Example of ε-transitions

Construct an NFA that accepts all strings with an even number of 0s or an

  • dd number of 1s

q r r s s

even number of 0s

  • dd number of 1s

1 1 1 1

slide-24
SLIDE 24

23/23

Example of ε-transitions

Construct an NFA that accepts all strings with an even number of 0s or an

  • dd number of 1s

q0 r0 r1 s0 s1

even number of 0s

  • dd number of 1s

ε ε

1 1 1 1