finite automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-dfas/finite automata...

185
Finite Automata Part One

Upload: others

Post on 12-Mar-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Finite AutomataPart One

Page 2: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Computability Theory

Page 3: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What problems can we solve with a computer?

Page 4: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What problems can we solve with a computer?

What is a “problem?”

Page 5: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

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 6: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Formal Language Theory

Page 7: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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 8: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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 9: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Answer at PollEv.com/cs103 ortext CS103 to 22333 once to join, then a number.

Answer at PollEv.com/cs103 ortext CS103 to 22333 once to join, then a number.

How many of the following statements are true?

● Alphabets are sequences of characters.● Languages are sets of strings. ● Strings are sets of characters.● Characters are individual symbols.● Languages are sequences of characters.

How many of the following statements are true?

● Alphabets are sequences of characters.● Languages are sets of strings. ● Strings are sets of characters.● Characters are individual symbols.● Languages are sequences of characters.

Page 10: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

To Recap

● Languages are sets of strings.● Strings are sequences of characters.● Characters are individual symbols.● Alphabets are sets of characters.

Languages

Strings

are sets of

Charactersare finite sequences of

Alphabets

are nonempty, finite sets of

Page 11: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Old MacDonald Had a Symbol, � Σ-eye-ε-ey∈, Oh! �

● You may have noticed that we have several letter-E-ish symbols in CS103, which can get confusing!

● Here’s a quick guide to remembering which is which:

● In language theory, Σ refers to an alphabet.● In language theory, ε is the empty string,

which is length 0.● In set theory, use ∈ to say “is an element of.”● In set theory, use ⊆ to say “is a subset of.”

Page 12: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What problems can we solve with a computer?

Page 13: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What problems can we solve with a computer?

What kind of computer?

Page 14: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Computers are Messy

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

Page 15: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What is a computer?

Bell Labs in Oakland, CA(Photo by Larry Luckham, 1969)

iWatch(2018)

Page 16: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Computers are Messy

That messiness makes it hard to rigorously say 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 17: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

We need a simpler way of discussing computing machines.

Page 18: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

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

Page 19: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Automata are Clean

q0 q1

q2q3

0

1

0

1

0

1 1

0

start

q2

Page 20: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

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

broadly applicable claims about huge classes of real computers by arguing that they're just special cases of our more general models.

Page 21: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Two Models of a Machine

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

● Finite automata (next two weeks) 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 Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What problems can we solve with a computer?

Page 23: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Finite Automata

Page 24: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

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

whether a string is contained within somelanguage.

Page 25: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

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

Page 26: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 27: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

Each circle represents a state of the automaton.

Each circle represents a state of the automaton.

Page 28: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 29: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

One special state is designated as the start

state.

One special state is designated as the start

state.

q1

q3 q2

Page 30: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 31: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 32: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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 0

The automaton is run on an input string

and answers “yes” or “no.”

The automaton is run on an input string

and answers “yes” or “no.”

0 1 0 1 1 0

q1

q3 q2

Page 33: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 34: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 35: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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 0

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

state.

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

state.

0 1 0 1 1 0

q1

q2

Page 36: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 37: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 38: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

The automaton now begins processing

characters in the order in which they appear.

The automaton now begins processing

characters in the order in which they appear.

q1

q2

Page 39: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 40: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 41: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

Each arrow in this diagram represents a

transition. The automaton always

follows the transition corresponding to the current symbol being

read.

Each arrow in this diagram represents a

transition. The automaton always

follows the transition corresponding to the current symbol being

read.

q1

q3

Page 42: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 43: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q3 q2

Page 44: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q3 q2

Page 45: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q3 q2

Page 46: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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 0

After transitioning, the automaton

considers the next symbol in the input.

After transitioning, the automaton

considers the next symbol in the input.

0 1 0 1 1 0

q3 q2

Page 47: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q3 q2

Page 48: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q3 q2

Page 49: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 50: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 51: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 52: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 53: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q2

Page 54: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q2

Page 55: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q2

Page 56: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q2

Page 57: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 58: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 59: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 60: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 61: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q2

Page 62: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q2

Page 63: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q2

Page 64: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q2

Page 65: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

0 1 0 1 1 00 1 0 1 1 0

q1

q3

Page 66: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

0 1 0 1 1 00 1 0 1 1 0

q1

q3

Page 67: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

0 1 0 1 1 00 1 0 1 1 0

q1

q3

Page 68: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

0 1 0 1 1 00 1 0 1 1 0

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

“yes” or “no.”

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

“yes” or “no.”

Page 69: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

0 1 0 1 1 0

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

“yes” or “no.”

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

The double circle indicates that this

state is an accepting state, so the automaton

outputs “yes.”

Page 70: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

0 1 0 1 1 0

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

“yes” or “no.”

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

The double circle indicates that this

state is an accepting state, so the automaton

outputs “yes.”

Page 71: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

0 1 0 1 1 0

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

“yes” or “no.”

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

The double circle indicates that this

state is an accepting state, so the automaton

outputs “yes.”

Page 72: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 73: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 74: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 75: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 76: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 77: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q2

Page 78: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q2

Page 79: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q2

Page 80: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q2

Page 81: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

1 0 1 0 0 0

q1

q3

Page 82: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

1 0 1 0 0 0

q1

q3

Page 83: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

1 0 1 0 0 0

Page 84: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

1 0 1 0 0 0

q1

q3

Page 85: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q3 q2

Page 86: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q3 q2

Page 87: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q3 q2

Page 88: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q3 q2

Page 89: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 90: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 91: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 92: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 93: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q3 q2

Page 94: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q3 q2

Page 95: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q3 q2

Page 96: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q3 q2

Page 97: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 98: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 99: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 100: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

1

A Simple Finite Automaton

q0 q1

q2q3

0

1

0

0

1 1

0

start

q2

1 0 1 0 0 0

This state is not an accepting state (it’s a rejecting state), so the automaton says

“no.”

This state is not an accepting state (it’s a rejecting state), so the automaton says

“no.”

q1

q3 q2

Page 101: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

This state is not an accepting state (it’s a rejecting state), so the automaton says

“no.”

This state is not an accepting state (it’s a rejecting state), so the automaton says

“no.”

q1

q3 q2

Page 102: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

This state is not an accepting state (it’s a rejecting state), so the automaton says

“no.”

This state is not an accepting state (it’s a rejecting state), so the automaton says

“no.”

q3

Page 103: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

q1

q3 q2

Page 104: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

A Simple Finite Automaton

q0 q1

q3

0

1

0

1

0

1 1

0

start

q2

1 1 0 1 1 1 0 0

Answer at PollEv.com/cs103 ortext CS103 to 22333 once to join, then A or R.

Answer at PollEv.com/cs103 ortext CS103 to 22333 once to join, then A or R.

Try it yourself!Does this automaton

accept (vote A) or reject (vote R)?

Try it yourself!Does this automaton

accept (vote A) or reject (vote R)?

Page 105: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

The Story So Far

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

● Some state is designated as the start state.

● Some 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 106: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Just Passing Through

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

Page 107: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Just Passing Through

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

1 1 0 1

q0

q1

Page 108: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Just Passing Through

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

1 1 0 1

q4q3

q1

Page 109: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Just Passing Through

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

1 1 0 1

q4q3

q1

Page 110: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Just Passing Through

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

1 1 0 1

q4q3

q0

Page 111: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Just Passing Through

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

1 1 0 1

q4q3

q0

Page 112: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Just Passing Through

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

1 1 0 1

q4q3q3

q0

q1

Page 113: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Just Passing Through

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

1 1 0 1

q4q3

q0

q1

Page 114: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Just Passing Through

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

1 1 0 1

q4q3

q0

q1

Page 115: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Just Passing Through

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

1 1 0 1

q4q3

q0

q1

Page 116: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Just Passing Through

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

1 1 0 1

q4q3

q0

Page 117: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Just Passing Through

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

1 1 0 1

q4q3

q0

Page 118: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Just Passing Through

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

1 1 0 1

q4q3

q0

Page 119: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Just Passing Through

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

1 1 0 1

q4q3

q0

Page 120: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

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

A finite automaton accepts if it ends inan accepting state.

Page 121: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

Page 122: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

Page 123: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

Page 124: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

Page 125: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

Page 126: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

Page 127: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

Page 128: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

No matter where we start in the automaton, after

seeing two 1's, we end up in accepting state q3.

No matter where we start in the automaton, after

seeing two 1's, we end up in accepting state q3.

Page 129: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

Page 130: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

Page 131: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

Page 132: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

Page 133: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

Page 134: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

Page 135: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

No matter where we start in the automaton, after

seeing two 0's, we end up in accepting state q5.

No matter where we start in the automaton, after

seeing two 0's, we end up in accepting state q5.

Page 136: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

Page 137: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

What Does This Accept?

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

This automaton accepts a string in {0, 1}* iff

the string ends in 00 or 11.

This automaton accepts a string in {0, 1}* iff

the string ends in 00 or 11.

Page 138: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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 that processes characters from the alphabet Σ, then ℒ(D)

is formally defined as

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

Page 139: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Answer at PollEv.com/cs103 ortext CS103 to 22333 once to join, then a number.

Answer at PollEv.com/cs103 ortext CS103 to 22333 once to join, then a number.

How many of the following statements are true?

- A language of an automaton can have an infinitelylong string (or many of them) in it.

- A language of an automaton can contain infinitelymany strings.

- A language of an automaton can contain no strings.

How many of the following statements are true?

- A language of an automaton can have an infinitelylong string (or many of them) in it.

- A language of an automaton can contain infinitelymany strings.

- A language of an automaton can contain no strings.

Page 140: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

A Small Problem

q0

q1

0

start

q2 1

0

Page 141: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

A Small Problem

q0

q1

0

start

q2 1

0 0 1 1 0

Page 142: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

A Small Problem

q0

q1

0

start

q2 1

0 0 1 1 0

Page 143: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

A Small Problem

q0

q1

0

start

q2 1

0 0 1 1 0

Page 144: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

A Small Problem

q0

q1

0

start

q2 1

0 0 1 1 0

Page 145: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

A Small Problem

q0

q1

0

start

q2 1

0 0 1 1 0

Page 146: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

A Small Problem

q0

q1

0

start

q2 1

0 0 1 1 0

Page 147: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

A Small Problem

q0

q1

0

start

q2 1

0 0 1 1 0

Page 148: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

A Small Problem

q0

q1

0

start

q2 1

0 0 1 1 0

Page 149: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Another Small Problem

q0

q2

0, 1startq1

0

0, 1

0, 1

Page 150: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Another Small Problem

q0

q2

0, 1start

0 0 0

q1

0

0, 1

0, 1

Page 151: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Another Small Problem

q0

q2

0, 1start

0 0 0

q1

0

0, 1

0, 1

Page 152: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Another Small Problem

q0

q2

0, 1start

0 0 0

q1

0

0, 1

0, 1

Page 153: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Another Small Problem

q0

q2

0, 1start

0 0 0

q1

0

0, 1

0, 1

Page 154: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Another Small Problem

q0

q2

0, 1start

0 0 0

q1

0

0, 1

0, 1

Page 155: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Another Small Problem

q0

q2

0, 1start

0 0 0

q1

0

0, 1

0, 1

Page 156: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Another Small Problem

q0

q2

0, 1start

0 0 0

q1

0

0, 1

0, 1

Page 157: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

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 multiple

transitions out of a state on some input?

Page 158: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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 159: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

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 160: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Answer at PollEv.com/cs103 ortext CS103 to 22333 once to join, then a number.

Answer at PollEv.com/cs103 ortext CS103 to 22333 once to join, then a number.

How many of these are valid DFAs over {0, 1}?How many of these are valid DFAs over {0, 1}?

q0q1

q3

0

1

0

1

0

1 1

0

start

q2

q0

q2

0

start

q4

0

0

q1

q3

1

1

1

0

1

0 1

q0

q1

0

start

q2 1

0

q0q1

0, 1start

q3

0, 10, 1

q20, 1

q0

q2

0, 1start q1

0

0, 1

0, 1

Page 161: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Is this a DFA?

Page 162: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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 means

only finitely many different things the machine can remember.

Page 163: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

Page 164: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

start

Page 165: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

start

a

Page 166: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

startq1

b

a

Page 167: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

startq1

b

a a

Page 168: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

startq1 q2

b b

a a

Page 169: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

startq1 q2

b b

a a a

Page 170: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

startq1 q2

b b

a a a

b

Page 171: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

startq1 q2

b b

a a a

b

Page 172: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

startq1 q2

b b

a a a

b

Each state remembers the remainder of the

number of bs seen so far modulo three.

Each state remembers the remainder of the

number of bs seen so far modulo three.

Page 173: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

Page 174: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

start

Page 175: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

start

b

Page 176: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

startq1

a

b

Page 177: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

startq1

a

b

b

Page 178: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

startq1 q2

a

a

b

b

Page 179: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

startq1 q2

a

a

b

b

Page 180: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

startq1 q2

a

a

b

b

a, b

Page 181: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

startq1 q2

a

a

b

b

a, b

Page 182: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

startq1 q2

a

a

b

b

Σ

Page 183: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · Strings An alphabet is a finite, nonempty set of symbols called characters

Recognizing Languages with DFAs

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

q0

startq1 q2

a

a

b

b

Σ

Page 184: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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 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:

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 185: Finite Automata - web.stanford.eduweb.stanford.edu/class/cs103/lectures/14-DFAs/Finite Automata I.pdf · 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

Σ

Σ