Top Banner
Finite Automata Part Two
54

Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

Jun 20, 2018

Download

Documents

doankhue
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

Finite AutomataPart Two

Page 2: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

Recap from Last Time

Page 3: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

Recognizing Languages with DFAs

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

q1

startq2

* q3

*q4

/q0/

q5

a, / *a

a, *

/, a

Σ

Σ

Page 14: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

New Stuff!

Page 15: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 16: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 17: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 18: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

The Regular Languages

Page 19: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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

Page 20: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 21: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 22: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 23: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

More Elaborate DFAs

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

q1

startq2

* q3

*q4

/q0/

q5

a, / *a

a, *

/, a

Σ

Σ

Page 24: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 25: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 26: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

Time-Out For Announcements!

Page 27: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

Problem Sets

● As a reminder, PS5 is due this Friday.● Have questions?

● Ask them on Piazza!● Stop by office hours!

● We recommend that you work through at least one or two of the problems by the end of the evening.

Page 28: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

Midterms Graded

● We’ve graded the first midterm exam! We’ll hand back graded midterms at the end of class today.

● Check the solution sets for statistics, information on how to estimate your grade, common mistakes, and techniques for improving going forward.

Page 29: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

WiCS Board Applications 2017-2018

Interested in joining Stanford Women in Computer Science (WiCS) next year? Apply for the WiCS Board today!

Applications are live at https://goo.gl/forms/2PFLVbwfS6HkO2Dk1

Find a list of the teams here https://quip.com/j4DYAbnFeMTC

The Deadline is Friday, May 12th at 11:59 PM

Contact Anvita (avgupta) or Nancy (xnancy) with any questions!

Page 30: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

Your Questions

Page 31: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

“Why do you delete questions you don't want to answer?”

Typically, I only delete questions if they aren’t constructive or they’re shibboleth questions.

Please feel free to reach out to me via email if you’d like to chat about things!

Typically, I only delete questions if they aren’t constructive or they’re shibboleth questions.

Please feel free to reach out to me via email if you’d like to chat about things!

Page 32: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

“What do you think when people say ‘college should be the best 4 years of your

life?’”

Story time!

Story time!

Page 33: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

“Do you think that Stanford is too pre-professional?”

It’s complicated. On the one hand, I think it is important to think about career prospects post-

graduation. On the other hand, I’m concerned that people worry way too much about this way too early on.

I’ll speak a bit more candidly in class.

It’s complicated. On the one hand, I think it is important to think about career prospects post-

graduation. On the other hand, I’m concerned that people worry way too much about this way too early on.

I’ll speak a bit more candidly in class.

Page 34: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

Back to CS103!

Page 35: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

NFAs

Page 36: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 37: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

(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 38: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 39: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 40: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

q2q2q1q1q0q0 q2q2

A More Complex NFA

start 1 1

0, 1

Question to ponder:What does this NFA

accept?

Question to ponder:What does this NFA

accept?

Page 41: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 42: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 43: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

ε-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 44: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 45: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

Perfect Guessing

● We can view nondeterministic machines as having Magic Superpowers that enable them to guess choices that lead to an accepting state.● 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 46: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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).● Start off in the set of all states formed by taking the start state

and including each state that can be reached by zero or more ε-transitions.

● When you read a symbol a in a set of states S:– Form the set S’ of states that can be reached by following a single a

transition from some state in S.– Your new set of states is the set of states in S’, plus the states

reachable from S’ by following zero or more ε-transitions.

Page 47: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 48: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

Designing NFAs

Page 49: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 50: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 51: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

Guess-and-Check

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

0

1 0

1

0 1

start Σ

Nondeterministically guess when to leave the start state.

Deterministically check whether that was the right time to do so.

Nondeterministically guess when to leave the start state.

Deterministically check whether that was the right time to do so.

Page 52: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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 53: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

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

ε

ε

ε

Nondeterministically guess which character

is missing.

Deterministically check whether that

character is indeed missing.

Nondeterministically guess which character

is missing.

Deterministically check whether that

character is indeed missing.

Page 54: Finite Automata - web.stanford.eduweb.stanford.edu/class/archive/cs/cs103/cs103.1176/lectures/15/... · We’ll hand back graded midterms at the end of class today. ... design a machine.

Just how powerful are NFAs?