finite automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...computers are messy

263
Finite Automata Part One

Upload: others

Post on 31-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Finite AutomataPart One

Page 2: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Computability Theory

Page 3: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What problems can we solve with a computer?

Page 4: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What problems can we solve with a computer?

What kind of computer?

Page 5: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Computers are Messy

http://www.intel.com/design/intarch/prodbref/272713.htm

Page 6: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Computers are Messy

http://www.dharmanitech.com/

Page 7: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Computers are Messy

http://techon.nikkeibp.co.jp/article/HONSHI/20090119/164259/

Page 8: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Computers are Messy

http://en.wikipedia.org/wiki/File:Eniac.jpg

Page 9: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Computers are Messy

That messiness makes it hard to rigorouslysay what we intuitively know to be true:

that, on some fundamental level, different brands of computers or programming

languages are more or less equivalent in what they are capable of doing.

C vs C++

vs Java

vs Python

Page 10: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

We need a simpler way of

discussing computing machines.

Page 11: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

An automaton (plural: automata) is a mathematical model of a computing device.

Page 12: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Computers are Messy

http://www.intel.com/design/intarch/prodbref/272713.htm

Page 13: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Automata are Clean

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 14: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Computers are Messy

http://www.dharmanitech.com/

Page 15: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Automata are Clean

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 16: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Computers are Messy

http://techon.nikkeibp.co.jp/article/HONSHI/20090119/164259/

Page 17: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Automata are Clean

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 18: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Computers are Messy

http://en.wikipedia.org/wiki/File:Eniac.jpg

Page 19: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Automata are Clean

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 20: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Why Build Models?

Mathematical simplicity.

• It is significantly easier to manipulate our abstract models of computers than it is to manipulate actual computers.

Intellectual robustness.

• If we pick our models correctly, we can make broad, sweeping claims about huge classes of real computers by arguing that they're just special cases of our more general models.

Page 21: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Why Build Models?

The models of computation we will explore in this class correspond to different conceptions of what a computer could do.

Finite automata (this week) are an abstraction of computers with finite resource constraints.

Provide upper bounds for the computing machines that we can actually build.

Turing machines (later) are an abstraction of computers with unbounded resources.

Provide upper bounds for what we could ever hope to accomplish.

Page 22: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What problems can we solve with a computer?

Page 23: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What problems can we solve with a computer?

What is a “problem?”

Page 24: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Problems with Problems

Before we can talk about what problems we can solve, we need a formal definition of a “problem.”

We want a definition that

• corresponds to the problems we want to solve,

• captures a large class of problems, and

• is mathematically simple to reason about.

No one definition has all three properties.

Page 25: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Formal Language Theory

Page 26: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

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: Let Σ = {a, b}. Here are some 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 27: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

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 28: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

The Cast of Characters

Languages are sets of strings.

Strings are finite sequences of characters.

Characters are individual symbols.

Alphabets are sets of characters.

Languages

Strings Characters

Alphabets

Page 29: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Strings and Problems

Given a string w, determine whether w ∈ S.

Suppose that L is the language

L = { "a", "b", "c", …, "z" }

This is modeling the problem:

Given a string w, determine whetherw is a single lower-case English letter.

Page 30: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Strings and Problems

Given a string w, determine whether w ∈ S.

Suppose that L is the language

L = { p | p is a legal C++ program }

This is modeling the problem:

Given a string w, determine whetherw is a legal C++ program.

Page 31: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

The Model

Fundamental Question: For what languages L can you design an automaton that takes as input a string, then determines whether the string is in L?

The answer depends on the choice of L, the choice of automaton, and the definition of “determines.”

In answering this question, we’ll go through a whirlwind tour of models of computation and see how this seemingly abstract question has very real and powerful consequences.

Page 32: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

To Summarize

An automaton is an idealized mathematical computing machine.

A language is a set of strings, a string is a (finite) sequence of characters, and a character is an element of an alphabet.

Goal: Figure out in which cases we can build automata for particular languages.

Page 33: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What problems can we solve with a computer?

Page 34: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Finite Automata

Page 35: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A finite automaton is a simple type of mathematical machine for determining

whether a string is contained within some

language.

Page 36: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Each finite automaton consists of a setof states connected by transitions.

Page 37: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 38: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

Each circle represents a state of the automaton.

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 39: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 40: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

One special state is designated as the start state.

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 41: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 42: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 43: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 0

The automaton is run on an input string and

answers “yes” or “no.”

0 1 0 1 1 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 44: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

q2q3

0 1 0 1 1 00 1 0 1 1 0

q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 45: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 46: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

The automaton can be in one state at a time. It begins in

the start state.

Page 47: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

q2q3

0 1 0 1 1 00 1 0 1 1 0

q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 48: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

The automaton now begins processing characters in the order in which they appear.

Page 49: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 50: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 51: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q0 q1

q2q3 q2

Each arrow in this diagram represents a transition. The

automaton always follows the transition corresponding to the current symbol being

read.

0

0

0

0

1 1 1 1

start

Page 52: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

q2q3

0 1 0 1 1 00 1 0 1 1 0

q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 53: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 54: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 55: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 56: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 0

After transitioning, the automaton considers the next symbol in the input.

0 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 57: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 58: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 59: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 60: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 61: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 62: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 63: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 64: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 65: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 66: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 67: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 68: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 69: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 70: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 71: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 72: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 73: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 74: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 75: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 76: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 77: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 78: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Now that the automaton has looked at all this input, it can decide whether to say “yes”

or “no.”

Page 79: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

The double circle indicates that this state is an accepting state, so the automaton outputs

“yes.”

0

0

0

0

1 1 1 1

start

Now that the automaton has looked at all this input, it can decide whether to say “yes”

or “no.”

Page 80: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

0 1 0 1 1 00 1 0 1 1 0

q2q3q3

q0 q1

q2q3 q2

Now that the automaton has looked at all this input, it can decide whether to say “yes”

or “no.”

The double circle indicates that this state is an accepting state, so the automaton outputs

“yes.”

0

0

0

0

1 1 1 1

start

Page 81: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 82: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 83: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 84: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 85: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 86: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 87: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 88: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 89: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 90: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 91: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 92: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 93: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 94: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 95: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 96: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 97: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 98: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 99: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 100: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 101: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 102: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 103: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 104: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 105: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 106: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 107: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 108: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

This state is not an accepting state (it’s a

rejecting state), so the automaton says “no.”

Page 109: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

1 0 1 0 0 0

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

This state is not an accepting state (it’s a

rejecting state), so the automaton says “no.”

Page 110: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

q0 q1

q2q3 q2

0

0

0

0

1 1 1 1

start

Page 111: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple Finite Automaton

q0 q1

q2q3 q2

Try it yourself!Does this automaton

accept or reject this string?

1 1 0 1 1 1 0 0

0

0

0

0

1 1 1 1

start

Page 112: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

The Story So Far

A finite automaton is a collection of states joined by transitions.

Some state is designated as the start state.

Some number of states are designated as accepting states.

The automaton processes a string by beginning in the start state and following the indicated transitions.

If the automaton ends in an accepting state, it accepts the input.

Otherwise, the automaton rejects the input.

Page 113: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Time-Out For Announcements!

Page 114: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Midterm

• The midterm will become available at 9:30 AM PDT tomorrow (Thursday). All the details you need to know are on a pinned Campuswire post.

• There are no office hours tomorrow. They’ve all been moved to not happen during the midterm.

• You’ve got this!

Page 115: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Back to CS103!

Page 116: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Just Passing Through

q0

q2

q4

q1

q3

0 0

0

0

0

1

1 1

1

1

start

Page 117: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Just Passing Through

q0

q2q1 1 1 0 1

q0

q1

q4q3

0 0

0

0

0

1

1 1

1

1

start

Page 118: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Just Passing Through

q0

q2

q4

q1

q3

1 1 0 1

q4q3

q1

0 0

0

0

0

1

1 1

1

1

start

Page 119: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Just Passing Through

q0

q2

q4

q1

q3

1 1 0 1

q4q3

q1

0 0

0

0

0

1

1 1

1

1

start

Page 120: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Just Passing Through

q0

q2

q4

q1

q3

1 1 0 1

q4q3

q0

0 0

0

0

0

1

1 1

1

1

start

Page 121: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Just Passing Through

q0

q2

q4

q1

q3

1 1 0 1

q4q3

q0

0 0

0

0

0

1

1 1

1

1

start

Page 122: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Just Passing Through

q0

q2

q4

q1

q3

1 1 0 1

q4q3q3

q0

q1

0 0

0

0

0

1

1 1

1

1

start

Page 123: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Just Passing Through

q0

q2

q4

q1

q3

1 1 0 1

q4q3

q0

q1

0 0

0

0

0

1

1 1

1

1

start

Page 124: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Just Passing Through

q0

q2

q4

q1

q3

1 1 0 1

q4q3

q0

q1

0 0

0

0

0

1

1 1

1

1

start

Page 125: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Just Passing Through

q0

q2

q4

q1

q3

1 1 0 1

q4q3

q0

q1

0 0

0

0

0

1

1 1

1

1

start

Page 126: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Just Passing Through

q0

q2

q4

q1

q3

1 1 0 1

q4q3

q0

0 0

0

0

0

1

1 1

1

1

start

Page 127: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Just Passing Through

q0

q2

q4

q1

q3

1 1 0 1

q4q3

q0

0 0

0

0

0

1

1 1

1

1

start

Page 128: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Just Passing Through

q0

q2

q4

q1

q3

1 1 0 1

q4q3

q0

0 0

0

0

0

1

1 1

1

1

start

Page 129: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Just Passing Through

q0

q2

q4

q1

q3

1 1 0 1

q4q3

q0

0 0

0

0

0

1

1 1

1

1

start

Page 130: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A finite automaton does not accept assoon as it enters an accepting state.

A finite automaton accepts if it ends inan accepting state.

Page 131: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

0 0

0

0

0

1

1 1

1

1

start

Page 132: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

0 0

0

0

0

1

1 1

1

1

start

Page 133: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

0 0

0

0

0

1

1 1

1

1

start

Page 134: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

0 0

0

0

0

1

1 1

1

1

start

Page 135: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

0 0

0

0

0

1

1 1

1

1

start

Page 136: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

0 0

0

0

0

1

1 1

1

1

start

Page 137: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

0 0

0

0

0

1

1 1

1

1

start

Page 138: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

No matter where we start in the automaton, after seeing two 1's, we end up in accepting state q3.

0 0

0

0

0

1

1 1

1

1

start

Page 139: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

0 0

0

0

0

1

1 1

1

1

start

Page 140: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

0 0

0

0

0

1

1 1

1

1

start

Page 141: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

0 0

0

0

0

1

1 1

1

1

start

Page 142: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

0 0

0

0

0

1

1 1

1

1

start

Page 143: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

0 0

0

0

0

1

1 1

1

1

start

Page 144: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

0 0

0

0

0

1

1 1

1

1

start

Page 145: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

No matter where we start in the automaton, after seeing two 0's, we end up in accepting state q4.

0 0

0

0

0

1

1 1

1

1

start

Page 146: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

0 0

0

0

0

1

1 1

1

1

start

Page 147: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

What Does This Accept?

q0

q2

q4

q1

q3

This automaton accepts a string in {0, 1}* iff the string ends in 00 or 11.

0 0

0

0

0

1

1 1

1

1

start

Page 148: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

The language of an automaton is the

set of strings that it accepts.

If D is an automaton that processes characters from the alphabet Σ, then ℒ(D)

is formally defined as

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

Page 149: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Small Problem

q0

q1q2

0 0

1

start

Page 150: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Small Problem

q0

q1q2

0 1 1 00 0

1

start

Page 151: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Small Problem

q0

q1q2

0 1 1 00 0

1

start

Page 152: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Small Problem

q0

q1q2

0 1 1 00 0

1

start

Page 153: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Small Problem

q0

q1q2

0 1 1 00 0

1

start

Page 154: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Small Problem

q0

q1q2

0 1 1 00 0

1

start

Page 155: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Small Problem

q0

q1q2

0 1 1 00 0

1

start

Page 156: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Small Problem

q0

q1q2

0 1 1 00 0

1

start

Page 157: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Small Problem

q0

q1q2

0 1 1 00 0

1

start

Page 158: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Another Small Problem

q0

q2

q1

start

0, 1

0, 1

0, 1

0

Page 159: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Another Small Problem

q0

q2

0 0 0

q1

0, 1

0, 1

0, 1

0

Page 160: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Another Small Problem

q0

q2

0 0 0

q1

0, 1

0, 1

0, 1

0

Page 161: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Another Small Problem

q0

q2

0 0 0

q1

0, 1

0, 1

0, 1

0

Page 162: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Another Small Problem

q0

q2

0 0 0

q1

0, 1

0, 1

0, 1

0

Page 163: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Another Small Problem

q0

q2

0 0 0

q1

0, 1

0, 1

0, 1

0

Page 164: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Another Small Problem

q0

q2

0 0 0

q1

0, 1

0, 1

0, 1

0

Page 165: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Another Small Problem

q0

q2

0 0 0

q1

0, 1

0, 1

0, 1

0

Page 166: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

The Need for Formalism

In order to reason about the limits of what finite automata can and cannot do, we need to formally specify their behavior in all cases.

All of the following need to be defined or disallowed:

What happens if there is no transition out of a state on some input?

What happens if there are multipletransitions out of a state on some input?

Page 167: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

DFAs

A DFA is a

• Deterministic

• Finite

• Automaton

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

Page 168: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

DFAs

• 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 169: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Is this a DFA over {0, 1}?

Page 170: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Is this a DFA over {0, 1}?

q0 q1

q2q3 q2

0

0

0

1 1 1 1

start0

Page 171: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Is this a DFA over {0, 1}?

Page 172: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Is this a DFA over {0, 1}?

q0

q2

q4

q1

q3

0 0

0

0

0

1

1 1

1

1

start

Page 173: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Is this a DFA over {0, 1}?

Page 174: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Is this a DFA over {0, 1}?

q0

q1q2

0 0

1

start

Page 175: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Is this a DFA over {0, 1}?

q0

q1q2

Page 176: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Is this a DFA over {0, 1}?

Page 177: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Is this a DFA over {0, 1}?

q0 q1

q3 q2

start 0, 1

0, 1

0, 1

0, 1

Page 178: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Is this a DFA over {0, 1}?

Page 179: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Is this a DFA over {0, 1}?

q0

q2

q1

0, 1

0, 1

0, 1

0

start

Page 180: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Is this a DFA over {0, 1}?

q0

q2

q1

Page 181: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Is this a DFA?

Page 182: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Is this a DFA?

Page 183: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Is this a DFA?

Drinking Family of Aardvarks

Page 184: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

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 means only finitely many different things the machine can remember.

Page 185: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}*| the number of b's in w is congruentto two modulo three }

Page 186: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}*| the number of b's in w is congruentto two modulo three }

q0

start

Page 187: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}*| the number of b's in w is congruentto two modulo three }

q0

a

start

Page 188: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}*| the number of b's in w is congruentto two modulo three }

q0 q1

a

b

start

Page 189: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}*| the number of b's in w is congruentto two modulo three }

q0 q1

aa

b

start

Page 190: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}*| the number of b's in w is congruentto two modulo three }

q0 q1 q2

aa

bb

start

Page 191: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}*| the number of b's in w is congruentto two modulo three }

q0 q1 q2

aaa

bb

start

Page 192: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}*| the number of b's in w is congruentto two modulo three }

q0 q1 q2

b

aaa

bb

start

Page 193: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}*| the number of b's in w is congruentto two modulo three }

q0 q1 q2

b

aaa

bb

start

Page 194: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}*| the number of b's in w is congruentto two modulo three }

q0 q1 q2

Each state remembers the

remainder of the number of bs

seen so far modulo three.

b

aaa

bb

start

Page 195: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}* | w contains aa as a substring }

Page 196: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}* | w contains aa as a substring }

q0

start

Page 197: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}* | w contains aa as a substring }

q0

b

start

Page 198: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}* | w contains aa as a substring }

q0 q1

a

b

start

Page 199: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}* | w contains aa as a substring }

q0 q1

a

b

b

start

Page 200: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}* | w contains aa as a substring }

q0 q1 q2

a

b

a

b

start

Page 201: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}* | w contains aa as a substring }

q0 q1 q2

a

b

a

b

start

Page 202: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}* | w contains aa as a substring }

q0 q1 q2

a

ba, b

a

b

start

Page 203: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}* | w contains aa as a substring }

q0 q1 q2

a

ba, b

a

b

start

Page 204: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}* | w contains aa as a substring }

q0 q1 q2

a

a

b

start

Page 205: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Recognizing Languages with DFAs

L = { w ∈ {a, b}* | w contains aa as a substring }

q0 q1 q2

a

a

b

start

Page 206: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

More Elaborate DFAs

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

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

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

Accepted:

/*a*//**/

/***//*aaa*aaa*/

/*a/a*/

Rejected:

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

/*//**a/

//aaaa

Page 207: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

More Elaborate DFAs

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

q1 q2 q3 q4q0

q5

/

*

a

/, a

a, *

/ *

Σ

start

*a, /

Σ

Page 208: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

The Regular Languages

Page 209: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

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

If L is a language and ℒ(D) = L, we say that D recognizes the language L.

Page 210: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Revisiting a Problem

q0 q1 q2q2

q3

start

0, 1

0, 1

11

0, 1

0

Page 211: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Revisiting a Problem

q0 q1 q2q2

start 11

0, 1

Page 212: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

NFAs

An NFA is a

• Nondeterministic

• Finite

• Automaton

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

Page 213: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

(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.

(This sort of nondeterminism is technically called existential nondeterminism, the most philosophical-sounding term we’ll introduce all quarter.)

Page 214: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple NFA

q0 q1 q2q2

q3

start

0, 1

0, 1

11

0, 1

0

Page 215: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple NFA

q0 has two transitions defined on 1!

q0 q1 q2q2

q3

start

0, 1

0, 1

11

0, 1

0

Page 216: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

A Simple NFA

0 1 0 1 1

q0 q1 q2q2

q3

start

0, 1

11

0, 1

0

0, 1

Page 217: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 218: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 219: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 220: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 221: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 222: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 223: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 224: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 225: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 226: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 227: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q1q0q0 q1 q2q2

q3

A Simple NFA

q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 228: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q1q0q0 q1 q2q2

q3

A Simple NFA

q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 229: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q2q2q1q1q0q0 q2q2

q3

A Simple NFA

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 230: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q3

q2q2q2q2q1q1q0q0

q3

A Simple NFA

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 231: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q3

q2q2q2q2q1q1q0q0

q3

A Simple NFA

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 232: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q3

q2q2q2q2q1q1q0q0

q3

A Simple NFA

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 233: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 234: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 235: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 236: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 237: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

q3

A Simple NFA

q1 q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 238: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q1q0q0 q1 q2q2

q3

A Simple NFA

q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 239: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q1q0q0 q1 q2q2

q3

A Simple NFA

q2q2

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 240: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q2q2q1q1q0q0 q2q2

q3

A Simple NFA

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 241: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q2q2q1q1q0q0 q2q2

q3

A Simple NFA

q3

0 1 0 1 1

start

0, 1

11

0, 1

0

0, 1

Page 242: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

0 1 0 1 1

q2q2q1q1q0q0 q2q2

q3

A Simple NFA

q3

start

0, 1

11

0, 1

0

0, 1

Page 243: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

A More Complex NFA

q1 q2q2

1 1

0, 1

start

Page 244: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

A More Complex NFA

q1 q2q2

If a NFA needs to make a transition when no transition exists, the automaton dies and

that particular path does not accept.

1 1

0, 1

start

Page 245: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

A More Complex NFA

q1 q2q2

0 1 0 1 1

1 1

0, 1

start

Page 246: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

A More Complex NFA

q1 q2q2

0 1 0 1 1

1 1

0, 1

start

Page 247: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

A More Complex NFA

q1 q2q2

0 1 0 1 1

1 1

0, 1

start

Page 248: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

A More Complex NFA

q1 q2q2

0 1 0 1 1

1 1

0, 1

start

Page 249: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q1q0q0 q1 q2q2

A More Complex NFA

q2q2

0 1 0 1 1

1 1

0, 1

start

Page 250: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q1q0q0 q1 q2q2

A More Complex NFA

q2q2

0 1 0 1 1

1 1

0, 1

start

Page 251: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q1q0q0 q2q2

A More Complex NFA

q2q2

Oh no! There's notransition defined!

0 1 0 1 1

1 1

0, 1

start

Page 252: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q1q1q0q0 q2q2

A More Complex NFA

q2q2

0 1 0 1 1

1 1

0, 1

start

Page 253: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

A More Complex NFA

q1 q2q2

0 1 0 1 1

1 1

0, 1

start

Page 254: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

A More Complex NFA

q1 q2q2

0 1 0 1 1

1 1

0, 1

start

Page 255: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

A More Complex NFA

q1 q2q2

0 1 0 1 1

1 1

0, 1

start

Page 256: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

A More Complex NFA

q1 q2q2

0 1 0 1 1

1 1

0, 1

start

Page 257: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q0q0 q1 q2q2

A More Complex NFA

q1 q2q2

0 1 0 1 1

1 1

0, 1

start

Page 258: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q1q0q0 q1 q2q2

A More Complex NFA

q2q2

0 1 0 1 1

1 1

0, 1

start

Page 259: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q1q0q0 q1 q2q2

A More Complex NFA

q2q2

0 1 0 1 1

1 1

0, 1

start

Page 260: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q2q2q1q1q0q0 q2q2

A More Complex NFA

0 1 0 1 1

1 1

0, 1

start

Page 261: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

q2q2q1q1q0q0 q2q2

A More Complex NFA

0 1 0 1 1

1 1

0, 1

start

Page 262: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

0 1 0 1 1

q2q2q1q1q0q0 q2q2

A More Complex NFA

1 1

0, 1

start

Page 263: Finite Automataweb.stanford.edu/class/archive/cs/cs103/cs103.1208...Computers are Messy

Next Time

DFAs vs NFAs

How do these two models of computation relate?

Regular Languages

A first classification of “problems”.