Finite Automata - Stanford...

182
Finite Automata Part Two

Transcript of Finite Automata - Stanford...

Page 1: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Finite AutomataPart Two

Page 2: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Recap from Last Time

Page 3: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Strings

● An alphabet is a finite, nonempty set of symbols called characters.

● Typically, we use the symbol Σ to refer to an alphabet.

● A string over an alphabet Σ is a finite sequence of characters drawn from Σ.

● Example: If Σ = {a, b}, here are some valid strings over Σ:

a aabaaabbabaaabaaaabbb abbababba

● The empty string has no characters and is denoted ε.

● Calling attention to an earlier point: since all strings are finite sequences of characters from Σ, you cannot have a string of infinite length.

Page 4: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Languages

● A formal language is a set of strings.● We say that L is a language over Σ if it is a

set of strings over Σ.● Example: The language of palindromes over

Σ = {a, b, c} is the set● {ε, a, b, c, aa, bb, cc, aaa, aba, aca, bab, … }

● The set of all strings composed from letters in Σ is denoted Σ*.

● Formally, we say that L is a language over Σ if L ⊆ Σ*.

Page 5: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

A Simple Finite Automaton

q0 q1

q2q3

0

1

0

1

0

1 1

0

start

q2

0 1 0 1 1 00 1 0 1 1 0

Page 6: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

A Simple Finite Automaton

q0 q1

q2q3

0

1

0

1

0

1 1

0

start

q2

1 0 1 0 0 0

Page 7: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

The language of an automaton is the set of strings that it accepts.

If D is an automaton, we denote the language of D as ℒ(D).

ℒ(D) = { w ∈ Σ* | D accepts w }

Page 8: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

DFAs

● A DFA is a● Deterministic● Finite● Automaton

● DFAs are the simplest type of automaton that we will see in this course.

Page 9: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

DFAs, Informally

● A DFA is defined relative to some alphabet Σ.

● For each state in the DFA, there must be exactly one transition defined for each symbol in Σ.● This is the “deterministic” part of DFA.

● There is a unique start state.● There are zero or more accepting states.

Page 10: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Designing DFAs

● At each point in its execution, the DFA can only remember what state it is in.

● DFA Design Tip: Build each state to correspond to some piece of information you need to remember.● Each state acts as a “memento” of what

you're supposed to do next.● Only finitely many different states ≈ only

finitely many different things the machine can remember.

Page 11: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Recognizing Languages with DFAs

L = { w ∈ {0, 1}*| the number of 1's in w is congruent to two modulo three }

q0

startq1 q2

1 1

0 0 0

1

Page 12: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Recognizing Languages with DFAs

L = { w ∈ {0, 1}* | w contains 00 as a substring }

q0

startq1 q2

0 0

1

1

Σ

Page 13: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

New Stuff!

Page 14: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

More Elaborate DFAsL = { w ∈ {a, *, /}* | w represents a C-style comment }

Let's have the symbol be a placeholder meaning “some character that isn't a star or a slash.”

Try designing a DFA for comments! Here's some test cases to help you check your work.

ACCEPTED REJECTED /*a*/ /**

/**/ /**/a/*aa*/ /***/ aaa/**/aa /*aaa*aaa*/ /*/ /*a/a*/ /**a/ //aaaa

Let's have the symbol be a placeholder meaning “some character that isn't a star or a slash.”

Try designing a DFA for comments! Here's some test cases to help you check your work.

ACCEPTED REJECTED /*a*/ /**

/**/ /**/a/*aa*/ /***/ aaa/**/aa /*aaa*aaa*/ /*/ /*a/a*/ /**a/ //aaaa

Page 15: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

More Elaborate DFAsL = { w ∈ {a, *, /}* | w represents a C-style comment }

q1

startq2

* q3

*q4

/q0/

q5

a, / *a

a, *

/, a

Σ

Σ

Page 16: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

New Stuff!

Page 17: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Tabular DFAs

startq0q0

1

q1 q2 q3

0 1 0

0

Σ1

q3

Page 18: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Tabular DFAs

startq0q0

1

q1 q2 q3

0 1 0

0

Σ

q0

q1

q2

q3

0 1

1

q3

Page 19: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Tabular DFAs

startq0q0

1

q1 q2 q3

0 1 0

0

Σ

q0

q1

q2

q3

0 1

q0q1

q2q3

q3 q3

q0q3

1

q3

Page 20: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Tabular DFAs

startq0q0

1

q1 q2 q3

0 1 0

0

Σ

*q0

q1

q2

*q3

0 1

q0q1

q2q3

q3 q3

q0q3

1

q3

Page 21: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Tabular DFAs

startq0q0

1

q1 q2 q3

0 1 0

0

Σ

*q0

q1

q2

*q3

0 1

q0q1

q2q3

q3 q3

q0q3

1

These stars indicate accepting

states.

These stars indicate accepting

states.

q3

Page 22: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Tabular DFAs

startq0q0

1

q1 q2 q3

0 1 0

0

Σ

*q0

q1

q2

*q3

0 1

q0q1

q2q3

q3 q3

q0q3

1

Since this is the first row, it's the

start state.

Since this is the first row, it's the

start state.

q3

Page 23: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Code‽ In a Theory Course‽int kTransitionTable[kNumStates][kNumSymbols] = { {0, 0, 1, 3, 7, 1, …}, …};bool kAcceptTable[kNumStates] = { false, true, true, …};bool SimulateDFA(string input) { int state = 0; for (char ch: input) state = kTransitionTable[state][ch]; return kAcceptTable[state];}

Page 24: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

The Regular Languages

Page 25: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

A language L is called a regular language if there exists a DFA D such that (ℒ D) = L.

Page 26: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

The Complement of a Language

● Given a language L ⊆ Σ*, the complement of that language (denoted L) is the language of all strings in Σ* that aren't in L.

● Formally:

L = Σ* - L

Page 27: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

The Complement of a Language

● Given a language L ⊆ Σ*, the complement of that language (denoted L) is the language of all strings in Σ* that aren't in L.

● Formally:

L = Σ* - L

Σ*

Page 28: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

The Complement of a Language

● Given a language L ⊆ Σ*, the complement of that language (denoted L) is the language of all strings in Σ* that aren't in L.

● Formally:

L = Σ* - L

Σ*

L

Page 29: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

The Complement of a Language

● Given a language L ⊆ Σ*, the complement of that language (denoted L) is the language of all strings in Σ* that aren't in L.

● Formally:

L = Σ* - L

L L

Σ*

Page 30: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Complements of Regular Languages

● As we saw a few minutes ago, a regular language is a language accepted by some DFA.

● Question: If L is a regular language, is L necessarily a regular language?

● If the answer is “yes,” then if there is a way to construct a DFA for L, there must be some way to construct a DFA for L.

● If the answer is “no,” then some language L can be accepted by some DFA, but L cannot be accepted by any DFA.

Page 31: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Complementing Regular Languages

L = { w ∈ {0, 1}* | w contains 00 as a substring }

q0

startq1 q2

0 0

1

1

Σ

L = { w ∈ {0, 1}* | w does not contain 00 as a substring }

q0

startq1 q2

0 0

1

1

Σ

Page 32: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

More Elaborate DFAs

L = { w ∈ {a, *, /}* | w represents a C-style comment }

q1

startq2

* q3

*q4

/q0/

q5

a, / *a

a, *

/, a

Σ

Σ

Page 33: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

More Elaborate DFAs

L = { w ∈ {a, *, /}* | w doesn't represent a C-style comment }

q1

startq2

* q3

*q4

/q0/

q5

a, / *a

a, *

/, a

Σ

Σ

Page 34: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

More Elaborate DFAs

L = { w ∈ {a, *, /}* | w doesn't represent a C-style comment }

q1

startq2

* q3

*q4

/q0/

q5

a, / *a

a, *

/, a

Σ

Σ

q5

q3q2q1q0

Page 35: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Closure Properties

● Theorem: If L is a regular language, then L is also a regular language.

● As a result, we say that the regular languages are closed under complementation.

All languages

Regular languages

L

L

Page 36: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Time-Out For Announcements!

Page 37: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Problem Set Five

● PS5 is due on Friday.● Stop by office hours with questions!● Ask questions on Piazza!

● Recommendation: have the TAs look over some of your proofs to offer feedback on your proof structure and writing style.

Page 38: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Special Office Hours

● Our awesome TA Anna is holding extra office hours today from 4:30PM – 5:30PM in the Huang basement (right outside!) today for folks with questions on the midterm.

● Feel free to stop on by!

Page 39: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Your Questions

Page 40: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

There were a lot of great questions! If I didn't get to yours today, please feel free to

ask again for Friday!

Page 41: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

“Despite studying all weekend and going to office hours, I still did worse than expected on the midterm. Is there anything else I can do?”

Yes, absolutely! The whole point of having the midterm this early in the quarter is to give you a chance to react to the feedback that you received.

First, get a lot of practice. Work through the practice problems, give your solutions to the TAs, and ask them to politely but honestly give feedback. Repeat this process until you're comfortably getting the hang of things. This is a great way to improve.

Also, be strategic in your studying. While there are a good number of terms and definitions to remember, studying for this class should be an active process (try practice problems, get feedback, repeat) rather than a passive process (read things and try to absorb them).

More generally, treat the staff as a resource. That's what we're here for! You won't improve unless you get a lot of feedback, and we're happy to provide it!

Yes, absolutely! The whole point of having the midterm this early in the quarter is to give you a chance to react to the feedback that you received.

First, get a lot of practice. Work through the practice problems, give your solutions to the TAs, and ask them to politely but honestly give feedback. Repeat this process until you're comfortably getting the hang of things. This is a great way to improve.

Also, be strategic in your studying. While there are a good number of terms and definitions to remember, studying for this class should be an active process (try practice problems, get feedback, repeat) rather than a passive process (read things and try to absorb them).

More generally, treat the staff as a resource. That's what we're here for! You won't improve unless you get a lot of feedback, and we're happy to provide it!

Page 42: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

“It feels like this class is very intuition based... I'm not a math god I have no idea how to

improve...advice??”

I'd recommend thinking about it as experience rather than intuition. It's like programming – you learn how to be a good programmer by just having time to make a lot of mistakes and see better approaches.

In this class it can seem like everything is constantly a mystery. Part of that is because we specifically build the problem sets so that you need to have different insights and perspectives to solve each problem. This means that at the time you're working through things everything can seem mysterious, but when you turn around and look back at it things you'll be amazed at how much you've learned. (Think CS106A: it probably was pretty challenging at the time, but if you look back from a CS107 vantage point you'll be amazed how much you learned).

Whenever you solve a problem and have some new insight, think of it as expanding your bag of tricks. When you encounter a new problem, you can think back to what you've done in the past and see if something applies. A little experience goes a long way.

I'd recommend thinking about it as experience rather than intuition. It's like programming – you learn how to be a good programmer by just having time to make a lot of mistakes and see better approaches.

In this class it can seem like everything is constantly a mystery. Part of that is because we specifically build the problem sets so that you need to have different insights and perspectives to solve each problem. This means that at the time you're working through things everything can seem mysterious, but when you turn around and look back at it things you'll be amazed at how much you've learned. (Think CS106A: it probably was pretty challenging at the time, but if you look back from a CS107 vantage point you'll be amazed how much you learned).

Whenever you solve a problem and have some new insight, think of it as expanding your bag of tricks. When you encounter a new problem, you can think back to what you've done in the past and see if something applies. A little experience goes a long way.

Page 43: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

“I started coding with 106A/B unlike everyone else who started when they were in diapers. How

can I compete with them for grades or jobs or grad school admissions?”

Last time we checked, half of all CS majors started off in CS106A (including the legendary Mehran Sahami – that's life in the city) and some superstars like Marissa Mayer started off in CS105, so don't worry if you're just getting started. Most high schools don't offer any computer science and the majority of folks start coding when they get here.

For grades: most grades in coding classes are on an absolute scale rather than a relative one. We're aware that some people arrive with more experience and deliberately try not to penalize other people for that.

For grad school: for a coterm, you're not competing against other students – we love coterms and want more of them. No one is going to care whether you started coding early or late as long as you worked hard and did well in your classes. For a Ph.D, no one cares about coding at all – they care about your research experience – and you have total control over this.

For jobs – the biggest gap you'll experience is right when you start coding, but after a year or two that gap will close.

Last time we checked, half of all CS majors started off in CS106A (including the legendary Mehran Sahami – that's life in the city) and some superstars like Marissa Mayer started off in CS105, so don't worry if you're just getting started. Most high schools don't offer any computer science and the majority of folks start coding when they get here.

For grades: most grades in coding classes are on an absolute scale rather than a relative one. We're aware that some people arrive with more experience and deliberately try not to penalize other people for that.

For grad school: for a coterm, you're not competing against other students – we love coterms and want more of them. No one is going to care whether you started coding early or late as long as you worked hard and did well in your classes. For a Ph.D, no one cares about coding at all – they care about your research experience – and you have total control over this.

For jobs – the biggest gap you'll experience is right when you start coding, but after a year or two that gap will close.

Page 44: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

“If I'm planning to work in industry, is there any advantage to having a PhD vs. a Master's? If I can

get promoted to a PhD-entry-level position with just a Master's after a few years, then I'm in the same

position I would be having done a PhD, right?”

If you're planning on just doing industrial software, there's almost no reason to get a Ph.D other than personal interest. You're better served just jumping into industry and taking it from there.

Industrial jobs that require Ph.Ds are very different from regular software engineering jobs. A Ph.D is a research apprenticeship and certifies that you're capable of doing high-quality, unsupervised research in a field. The skills you pick up in the four to six years you'll spend in a Ph.D are quite different from what you'll learn as an engineer, and accordingly it is much harder to transition into those jobs by starting in industry.

So the best answer I can give is to figure out what it is that you want to do and work backwards from there. Want to go into research spots? Pursue a Ph.D! Want to work as an engineer? Get a BS or MS and jump into a job!

If you're planning on just doing industrial software, there's almost no reason to get a Ph.D other than personal interest. You're better served just jumping into industry and taking it from there.

Industrial jobs that require Ph.Ds are very different from regular software engineering jobs. A Ph.D is a research apprenticeship and certifies that you're capable of doing high-quality, unsupervised research in a field. The skills you pick up in the four to six years you'll spend in a Ph.D are quite different from what you'll learn as an engineer, and accordingly it is much harder to transition into those jobs by starting in industry.

So the best answer I can give is to figure out what it is that you want to do and work backwards from there. Want to go into research spots? Pursue a Ph.D! Want to work as an engineer? Get a BS or MS and jump into a job!

Page 45: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Back to CS103!

Page 46: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

NFAs

Page 47: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

NFAs

● An NFA is a● Nondeterministic● Finite● Automaton

● Structurally similar to a DFA, but represents a fundamental shift in how we'll think about computation.

Page 48: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

(Non)determinism

● A model of computation is deterministic if at every point in the computation, there is exactly one choice that can make.

● The machine accepts if that series of choices leads to an accepting state.

● A model of computation is nondeterministic if the computing machine may have multiple decisions that it can make at one point.

● The machine accepts if any series of choices leads to an accepting state.

Page 49: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

A Simple NFA

q0 q1 q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

Page 50: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

A Simple NFA

q0 q1 q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

q0 has two transitions defined on 1!

q0 has two transitions defined on 1!

Page 51: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

A Simple NFA

q0 q1 q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 52: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 53: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 54: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 55: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 56: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 57: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 58: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 59: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 60: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 61: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q1q0q0 q1 q2q2

q3

A Simple NFA

q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 62: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q1q0q0 q1 q2q2

q3

A Simple NFA

q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 63: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q2q2q1q1q0q0 q2q2

q3

A Simple NFA

start 1 1

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 64: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q3

q2q2q2q2q1q1q0q0

q3

A Simple NFA

start 1 1

0, 1

0 0, 1

0, 1

0 1 0 1 1

Page 65: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q3

q2q2q2q2q1q1q0q0

q3

A Simple NFA

start 1 1

0, 1

0 0, 1

0, 1

0 1 0 1 1

Page 66: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q3

q2q2q2q2q1q1q0q0

q3

A Simple NFA

start 1 1

0, 1

0 0, 1

0, 1

0 1 0 1 1

Page 67: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 68: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 69: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 70: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 71: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q1q0q0 q1 q2q2

q3

A Simple NFA

q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 72: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q1q0q0 q1 q2q2

q3

A Simple NFA

q2

start 1 1 q2

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 73: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q2q2q1q1q0q0 q2q2

q3

A Simple NFA

start 1 1

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 74: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q2q2q1q1q0q0 q2q2

q3

A Simple NFA

start 1 1

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 75: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q2q2q1q1q0q0 q2q2

q3

A Simple NFA

start 1 1

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 76: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q2q2q1q1q0q0 q2q2

q3

A Simple NFA

start 1 1

0, 1

q3

0 0, 1

0, 1

0 1 0 1 1

Page 77: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

A More Complex NFA

q1 q2

start 1 1 q2

0, 1

Page 78: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

A More Complex NFA

q1 q2

start 1 1 q2

0, 1

If a NFA needs to make a transition when no transition

exists, the automaton dies and that particular path rejects.

If a NFA needs to make a transition when no transition

exists, the automaton dies and that particular path rejects.

Page 79: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

A More Complex NFA

q1 q2

start 1 1 q2

0, 1

0 1 0 1 1

Page 80: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

A More Complex NFA

q1 q2

start 1 1 q2

0, 1

0 1 0 1 1

Page 81: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

A More Complex NFA

q1 q2

start 1 1 q2

0, 1

0 1 0 1 1

Page 82: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

A More Complex NFA

q1 q2

start 1 1 q2

0, 1

0 1 0 1 1

Page 83: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q1q0q0 q1 q2q2

A More Complex NFA

q2

start 1 1 q2

0, 1

0 1 0 1 1

Page 84: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q1q0q0 q1 q2q2

A More Complex NFA

q2

start 1 1 q2

0, 1

0 1 0 1 1

Page 85: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q1q0q0 q2q2

A More Complex NFA

q2

start 1 1 q2

0, 1

0 1 0 1 1

Oh no! There's notransition defined!

Page 86: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q1q1q0q0 q2q2

A More Complex NFA

q2

start 1 1 q2

0, 1

0 1 0 1 1

Page 87: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

A More Complex NFA

q1 q2

start 1 1 q2

0, 1

0 1 0 1 1

Page 88: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

A More Complex NFA

q1 q2

start 1 1 q2

0, 1

0 1 0 1 1

Page 89: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

A More Complex NFA

q1 q2

start 1 1 q2

0, 1

0 1 0 1 1

Page 90: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1 q2q2

A More Complex NFA

q1 q2

start 1 1 q2

0, 1

0 1 0 1 1

Page 91: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q1q0q0 q1 q2q2

A More Complex NFA

q2

start 1 1 q2

0, 1

0 1 0 1 1

Page 92: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q1q0q0 q1 q2q2

A More Complex NFA

q2

start 1 1 q2

0, 1

0 1 0 1 1

Page 93: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q2q2q1q1q0q0 q2q2

A More Complex NFA

start 1 1

0, 1

0 1 0 1 1

Page 94: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q2q2q1q1q0q0 q2q2

A More Complex NFA

start 1 1

0, 1

0 1 0 1 1

Page 95: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q2q2q1q1q0q0 q2q2

A More Complex NFA

start 1 1

0, 1

0 1 0 1 1

Page 96: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q2q2q1q1q0q0 q2q2

A More Complex NFA

start 1 1

0, 1

0 1 0 1 1

Page 97: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

NFA Acceptance

● An NFA N accepts a string w if there is some series of choices that lead to an accepting state.

● Consequently, an NFA N rejects a string w if no possible series of choices lead it into an accepting state.

● It's easier to show that an NFA does accept something than to show that it doesn't.

Page 98: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

Page 99: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0 q1

q4 q5

q2

q0q3

q0

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

startq1

q4 q5

q2

q0

a

ε

a

b

b, ε b

a

ε

q3

Page 100: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0 q1

q4 q5

q2

q0q3

q0

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b b

q1

q4 q5

q2

q0

a

ε

a

b

b, ε b

a

ε

q3

Page 101: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0 q1

q4 q5

q2

q0q3

q0

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b b

q1

q4 q5

q2

q0

a

ε

a

b

b, ε b

a

ε

q3

Page 102: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q0 q1

q4 q5

q2

q0q3

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b b

q1

q4 q5

q2

q0

a

ε

a

b

b, ε b

a

ε

q3

Page 103: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q3q0q3

q0q0 q1

q4 q5

q2

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b b

q1

q4 q5

q2a

ε

a

b

b, ε b

a

ε

Page 104: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q4q0q3q0q3

q0q0 q1

q4 q5

q2

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b b

q1

q5

q2a

ε

a

b

b, ε b

a

ε

Page 105: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q4q0q3q0q3

q0q0 q1

q4 q5

q2

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b b

q1

q5

q2a

ε

a

b

b, ε b

a

ε

Page 106: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q4

q1

q4q0q3q0q3

q0q0 q1

q5

q2

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b bq5

q2a

ε

a

b

b, ε b

a

ε

Page 107: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q1 q2

q4

q1

q4q0q3q0q3

q0q0

q5

q2

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b bq5

a

ε

a

b

b, ε b

a

ε

Page 108: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q1 q2

q4

q1

q4q0q3q0q3

q0q0

q5

q2

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b bq5

a

ε

a

b

b, ε b

a

ε

Page 109: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0 q2q1 q2

q4

q1

q4q0q3q0q3

q0

q5

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b bq5

a

ε

a

b

b, ε b

a

ε

Page 110: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0 q2q1 q2

q4

q1

q4q0q3q0q3

q0

q5

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b bq5

a

ε

a

b

b, ε b

a

ε

Page 111: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q3

q0q0 q2q1 q2

q4

q1

q4q0q3 q5

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b bq5

a

ε

a

b

b, ε b

a

ε

Page 112: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q4q0q3q0q3

q0q0 q2q1 q2

q4

q1

q5

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b bq5

a

ε

a

b

b, ε b

a

ε

Page 113: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q4 q5q4q0q3q0q3

q0q0 q2q1 q2q1

q5

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b b

a

ε

a

b

b, ε b

a

ε

Page 114: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q4 q5q4q0q3q0q3

q0q0 q2q1 q2q1

q5

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b b

a

ε

a

b

b, ε b

a

ε

Page 115: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q3 q5q4 q5q4q0q3

q0q0 q2q1 q2q1

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b b

a

ε

a

b

b, ε b

a

ε

Page 116: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q0q3 q5q4 q5q4q0q3

q0q0 q2q1 q2q1

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

start

b a a b b

a

ε

a

b

b, ε b

a

ε

Page 117: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

ε-Transitions

● NFAs have a special type of transition called the ε-transition.

● An NFA may follow any number of ε-transitions at any time without consuming any input.

● NFAs are not required to follow ε-transitions. It's simply another option at the machine's disposal.

Page 118: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Intuiting Nondeterminism

● Nondeterministic machines are a serious departure from physical computers. How can we build up an intuition for them?

● There are two particularly useful frameworks for interpreting nondeterminism:● Perfect guessing● Massive parallelism

Page 119: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₀ q₁ q₂ q₃

Perfect Guessing

startq₀ q₁ q₂a b

Σ

q₃a q₃

Page 120: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₀ q₁ q₂ q₃

Perfect Guessing

start

a

q₀ q₁ q₂a b

Σ

b a b

q₃a

a

q₃

Page 121: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₀q₀ q₁ q₂ q₃

Perfect Guessing

start

a

q₁ q₂a b

Σ

b a b

q₃a

a

q₃

Page 122: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₀q₀ q₁ q₂ q₃

Perfect Guessing

start

a

q₁ q₂a b

Σ

b a b

q₃a

a

q₃

Page 123: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₀q₀ q₁ q₂ q₃

Perfect Guessing

start

a

q₁ q₂a b

Σ

b a b

q₃a

a

q₃

Page 124: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₀ q₁

a b

q₀ q₁ q₂ q₃

Perfect Guessing

start

a

q₂a b

Σ

b

q₃a

a

q₃

Page 125: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₀ q₁

a b

q₀ q₁ q₂ q₃

Perfect Guessing

start

a

q₂a b

Σ

b

q₃a

a

q₃

Page 126: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₁ q₂q₀ q₁

a b

q₀ q₂ q₃

Perfect Guessing

start

a

a b

Σ

b

q₃a

a

q₃

Page 127: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₁ q₂q₀ q₁

a b

q₀ q₂ q₃

Perfect Guessing

start

a

a b

Σ

b

q₃a

a

q₃

Page 128: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₃q₂q₁ q₂q₀ q₁

a b

q₀

Perfect Guessing

start

a

a b

Σ

b

a

a

q₃

Page 129: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₃q₂q₁ q₂q₀ q₁

a b

q₀

Perfect Guessing

start

a

a b

Σ

b

a

a

q₃

Page 130: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Perfect Guessing

● We can view nondeterministic machines as having Magic Superpowers that enable them to guess the correct choice of moves to make.● If there is at least one choice that leads to an

accepting state, the machine will guess it.● If there are no choices, the machine guesses

any one of the wrong guesses.

● No known physical analog for this style of computation – this is totally new!

Page 131: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₃q₂q₁ q₂q₀ q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 132: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 133: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 134: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 135: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 136: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 137: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 138: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 139: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 140: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 141: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 142: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 143: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 144: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 145: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 146: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 147: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 148: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 149: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 150: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 151: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 152: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 153: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 154: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 155: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 156: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 157: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 158: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 159: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 160: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

Page 161: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

q₃q₂q₁q₀

Massive Parallelism

start

a

a b

Σ

b a b

a

a

q₃

We're in at least one accepting state, so there's some path that gets us to

an accepting state.

Therefore, we accept!

We're in at least one accepting state, so there's some path that gets us to

an accepting state.

Therefore, we accept!

Page 162: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Massive Parallelism

● An NFA can be thought of as a DFA that can be in many states at once.

● At each point in time, when the NFA needs to follow a transition, it tries all the options at the same time.

● (Here's a rigorous explanation about how this works; read this on your own time). If you're in an initial set of states S, to determine the next set of states S', do the following:● For each active state, find all the transitions leaving the state on

the current symbol.● Follow all those transitions and add the endpoints to S'.● Follow all ε-transitions out of S' and add them to S'.● Your new set of states is the resulting set S'.

Page 163: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

So What?

● Each intuition of nondeterminism is useful in a different setting:● Perfect guessing is a great way to think about how to

design a machine.● Massive parallelism is a great way to test machines – and

has nice theoretical implications.● Nondeterministic machines may not be feasible, but

they give a great basis for interesting questions:● Can any problem that can be solved by a nondeterministic

machine be solved by a deterministic machine?● Can any problem that can be solved by a nondeterministic

machine be solved efficiently by a deterministic machine?

● The answers vary from automaton to automaton.

Page 164: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Designing NFAs

Page 165: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Designing NFAs

● When designing NFAs, embrace the nondeterminism!

● Good model: Guess-and-check:● Is there some information that you'd really like to have?

Have the machine nondeterministically guess that information.

● Then, have the machine deterministically check that the choice was correct.

● The guess phase corresponds to trying lots of different options.

● The check phase corresponds to filtering out bad guesses or wrong options.

Page 166: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Guess-and-Check

L = { w ∈ {0, 1}* | w ends in 010 or 101 }

Page 167: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Guess-and-Check

L = { w ∈ {0, 1}* | w ends in 010 or 101 }

0

1 00

1

1

1

0

0 1

0

1

0

1

start

Page 168: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Guess-and-Check

L = { w ∈ {0, 1}* | w ends in 010 or 101 }

Page 169: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Guess-and-Check

L = { w ∈ {0, 1}* | w ends in 010 or 101 }

start

Page 170: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Guess-and-Check

L = { w ∈ {0, 1}* | w ends in 010 or 101 }

0

1 0

start

Page 171: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Guess-and-Check

L = { w ∈ {0, 1}* | w ends in 010 or 101 }

0

1 0

1

0 1

start

Page 172: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Guess-and-Check

L = { w ∈ {0, 1}* | w ends in 010 or 101 }

0

1 0

1

0 1

start Σ

Page 173: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Guess-and-Check

L = { w ∈ {a, b, c}* | at least one of a, b, or c is not in w }

Page 174: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Guess-and-Check

L = { w ∈ {a, b, c}* | at least one of a, b, or c is not in w }

a

b

c

a

b

a, b

c a

c

b

c

c

a b

a, c

b, c

b

a

Σstart

Page 175: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Guess-and-Check

L = { w ∈ {a, b, c}* | at least one of a, b, or c is not in w }

Page 176: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Guess-and-Check

L = { w ∈ {a, b, c}* | at least one of a, b, or c is not in w }a, b

Page 177: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Guess-and-Check

L = { w ∈ {a, b, c}* | at least one of a, b, or c is not in w }a, b

a, c

Page 178: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Guess-and-Check

L = { w ∈ {a, b, c}* | at least one of a, b, or c is not in w }a, b

a, c

b, c

Page 179: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Guess-and-Check

L = { w ∈ {a, b, c}* | at least one of a, b, or c is not in w }a, b

a, c

b, c

start

Page 180: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Guess-and-Check

L = { w ∈ {a, b, c}* | at least one of a, b, or c is not in w }a, b

a, c

b, c

start

ε

ε

ε

Page 181: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Just how powerful are NFAs?

Page 182: Finite Automata - Stanford Universityweb.stanford.edu/.../archive/cs/cs103/cs103.1172/lectures/15/Slides… · Strings An alphabet is a finite, nonempty set of symbols called characters.

Next Time

● The Power of NFAs● Relating determinism and nondeterminism.

● Closure Properties● Transforming languages by transforming

machines.