You are on page 1of 50

Chapter 1: Regular Languages

Finite Automata I
1. Regular Languages

• 1.1 Finite Automata


• Deterministic FA DFA
• Non-Deterministic FA NFA
• ε-NFA
• 1.2 Regular Expressions
• RE = FA
• 1.3 Properties of Regular Languages
• Pumping lemma
• Closure properties
• Decision properties
1.1 Finite Automata

• Deterministic FA
• Non-Deterministic FA
• NFA = DFA
• ε-NFA
• ε-NFA = NFA
Objectives

• This time, we will look at how to define


a very simple “computer” called
deterministic finite automaton (DFA)
• Show that DFA can solve some string
decision problem
• Then, we slightly change the definition
of DFA to give another computer
called non-deterministic finite
automaton (NFA)
Finite Automata
• The simplest computational model, with very
limited memory, but highly useful
• Example: a finite automaton modeling an
on/off switch

push

Start off on

push
Car Audio System (Homework)

On/Off

AM
Stop
mode

AM Hints:
Seek
mode • How to model modes (AM/ FM)?
• How to model the change
system states?
Types of Finite Automata

• Deterministic (DFA): At any point when the


machine is in a state with an input symbol,
there is a unique next state to go.
• Non-Deterministic (NFA): There can be more
than one next state for each state-input pair.
DFA
Accept
Input
or
String
Reject
DFA

• A machine with finite number of states,


some states are accepting states, others
are rejecting states
• At any time, it is in one of the states
• It reads an input string, one character at a
time
DFA
Accept
Input
or
String
Reject
DFA

• After reading each character, it moves to


another state depending on what is read
and what is the current state
• If reading all characters, the DFA is in an
accepting state, the input string is accepted.
• Otherwise, the input string is rejected.
Example of DFA
0,1

start q1 q2

0,1

• The circles indicates the states


• Its accepting state is marked with double circle
• The arrows pointing from a state q indicates how
to move on reading a character when current
state is q
State Diagram of FA
transitions states

M1: 0 1

1 0

q1 q2 q3

0, 1

start state accept state


Hints: try to answer the following questions when you see any
state diagram
• How many states? How many transitions? Where is the accept states?
• Where is the start state? What is the alphabet and string? …
M1 Cont’d
0 1

1 0

q1 q2 q3

0, 1

on input string “0110”, the machine goes:


q1q1q2q2q3 = “reject”
start
M1 Cont’d
0 1

1 0

q1 q2 q3

0, 1

on input string “101”, the machine goes:


q1q2q3q2 = “accept”
start
M1 Cont’d What is the
language accepted
0 1 by M1?
1 0

q1 q2 q3

0, 1
010: reject
11: accept
0110100: accept
010000010010: reject
Remember!

• A machine may accept several


strings, but it always accepts
(recognizes) only one language.
Formal Definition of DFA

• A DFA is a 5-tuple (Q, Σ, δ, qstart, F), where


• Q is a set consisting finite number of states
• Σ is an alphabet consisting finite number of characters
• δ: Q x Σ  Q is the transition function
• takes a state and input symbol as arguments, and returns a
state
• qstart ∈ Q is the start state
• F ⊆ Q is the set of accepting states

• Note: only 1 start state, and can have many


accepting states
M1’s Formal Definition

• M1 = (Q, Σ, δ, q0, F), where 0 1


• Q = {q1, q2, q3} 1 0
• Σ = {0, 1}
q1 q2 q3
• δ(q1,0)=q1, δ(q1,1)=q2,
δ(q2,0)=q3, δ(q2,1)=q2, 0, 1
δ(q3,0)=q2, δ(q3,1)=q2
0 1
• q1 is the start state
q1 q1 q2
• F = {q2}
q2 q3 q2
q3 q2 q2
Formal Definition of DFA
0,1

start q1 q2

0,1

Q = {q1, q2 }, Σ = { 0, 1 }, qstart = q1, F = { q2 }


δ(q1, 0) = q2, δ(q1, 1) = q2
δ(q2, 0) = q1, δ(q2, 1) = q1
Extension of δ to Strings
• Intuitively, an FA accepts a string w = a1a2…an if
there is a path in the state diagram that:
1. Begins at the start state,
2. Ends at an accept state, and
3. Has sequence of labels a1, a2, …, an.

• Formally, the transition function δ can be


extended to δ*(q, w), where w is any string of
input symbols.
• Basis: δ*(q, ε) = q
• Induction: δ*(q, wa) = δ (δ*(q, w), a)
Extension of δ to Strings

δ vs. δ* ?

δ : Transition function for a specific


string

δ* : an extended Transition
function for any possible string
formed in the language.
Formal Definition of Computation

• Let M = (Q, Σ, δ, qstart, F) be a DFA


• Let w = w1 w2 … wn be a string with each wi
a member of the alphabet Σ
• Then, M accepts w if a sequence of states
r0, r1, …, rn in Q exists with the three
conditions:
• r0 = qstart
• δ(ri, wi+1) = ri+1
• rn ϵ F
Some Terminology

Let M be a DFA
• Among all possible strings, M will
accept some of them, and M will
reject the remaining
• The set of strings which M accepts is
called the language recognized by M
• That is, M recognizes A if
A = { w | M accepts w }
Language of an FA

• An FA M = (Q, Σ, δ, q0, F) accepts a


string w if δ*(q0, w) ∈F.
• The language recognized by an FA
M= (Q, Σ, δ, q0, F) is
L(M) = {w | δ*(q0, w) ∈F}.
• A language is called a regular
language if some finite automaton
recognizes it.
Designing Finite Automata

• Given some language, design a FA


that recognizes it.
• Pretending to be a FA,
• You see the input symbols one by one
• You have finite memory, i.e. finite set
of states,
• So, remember only the important
information (finite set of possibilities)
about the string seen so far.
Example 1

• Design a machine the accept the following:


• Σ ={0,1}, L = {w | w has odd number of 1s},
design a FA to recognize L.

1. Ask Yourself: What is the necessary information to


remember?
2. Answer:
• Is the number of 1s seen so far even or odd?
• The number of 1s may have: Two possibilities.
Example 1 (Cont’d)

3. Assign a state to each possibility. qeven qodd


Example 1 (Cont’d)

0 0

1
4. Assign the transitions from one
possibility to another upon reading qeven qodd
a symbol.
1
Example 1 (Cont’d)

0 0

1
5. Set the start and accept states.
qeven qodd

1
Example 2

• How to design a DFA that accepts all


binary strings that represent an even
number? (e.g., accepts 110, 010, but
rejects 111, 010101)
• Let’s pretend ourselves as a DFA…
• After reading a character, we must
decide whether the string seen so far is
in the language (what is the language
we’re talking now??) because we don’t
know if it is the last character…
Example 2 (Cont’d)

• Before we design the DFA:


• Ask yourself: What does the
pattern of binary formats for
even numbers look like?
Example 2 (Cont’d)

• Answer: Even numbers


always end in 0.

Examples:
Decimal 2 4 6 8 10 12 14
Binary 10 100 110 1000 1010 1100 1110
Designing a DFA
Now, the designed DFA is:

1. Think which state is the accept state!


2. Think how to draw transitions!
0 1
1

start qeven qodd

0
Exercise 1

• Σ ={0,1}, L = {w | w has even


number of 0s and even number of
1s}, design a FA to recognize L.
• What to remember?
• How many possibilities?
Exercise 1 (Cont’d)

• Ask yourself: How many states


do we need to represent the
possible combinations of the
number of 0s and 1s seen so far?

• In another way: What are the


possible combinations of the
number of 0s and 1s
Exercise 1 (Cont’d)

• Answer:
• Even number of 0s and Even number of 1s.
• Even number of 0s and odd number of 1s.
• Odd number of 0s and even number of 1s.
• Odd number of 02 and odd number of 1s.

• Then:
• We need 4 state for each combination:
{q00, q10, q01, q11}
Exercise 1 (Cont’d)

1. Think which state is the accept state!


2. Think how to draw transitions!
1

q00 q01

0 1 0 0
0
1

q10 q11

1
Exercise 2

• Σ ={0,1}, L = {w | w contains 001


as a substring}, design a FA to
recognize L.
Exercise 2 (Cont’d)

• Ask yourself: How many states


do we need to represent any
string that contains 001 as a
substring?

• In another way: What are the


possible possibilities of seeing the
001 in the input string?
Exercise 2 (Cont’d)
• Answer: (4 Possibilities!!)
• Haven’t just seen any symbols of
001
• Have just seen a 0
• Have just seen a 00
• Have seen the entire pattern 001

• Then:
• We need 4 state for each possibility:
{q, q0, q00, q001}
Example 2

1. Think which state is the accept state!


2. Think how to draw transitions!
0
1 0, 1
0
0
1
q q0 q00 q001

1
1.1 Finite Automata

• Deterministic FA
• Non-Deterministic FA
• NFA = DFA
• ε-NFA
• ε-NFA = NFA
Nondeterministic Finite Automata

• Deterministic: At any point when the machine is in


a state with an input symbol, there is a unique next
state to go.
• Non-Deterministic: There can be more than one
next state for each state-input pair.
• Example: an automaton that accepts all strings
ending in 01.

0, 1
M2:
0 1
q0 q1 q2
How an NFA computes?
0, 1

0 1
q0 q1 q2

q0 q0 q0 q0 q0 q0

q1 q1 q1
(die)
q2 q2
(die)
Input: 0 0 1 0 1
Formal Definition of NFA

• An NFA can be in several states at


once, or viewed in another way, it
can “guess” which state to go next.
• Formally, an NFA is a 5-tuple N = (Q,
Σ, δ, q0, F), where all is as DFA, but
• δ: Q x ΣP (Q) is a transition function
from Q x Σ to the power set of Q.
Formal Definition of NFA

• An NFA is a 5-tuple (Q, Σ, δ, qstart, F), where


• Q is a set consisting finite number of states
• Σ is an alphabet consisting finite number of
characters
• δ: Q x Σε  2Q is the transition function
• qstart is the start state
• F is the set of accepting states

• Here, we let Σε = Σ [ {ε}


M2’s Formal Definition

• M2 = (Q, Σ, δ, q0, F), where


0 1
• Q = {q0, q1, q2}
q0 {q0,q1} {q0}
• Σ = {0, 1}
q1 ∅ {q2}
• δ(q0,0)={q0,q1}, δ(q0,1)={q0},
q2
δ(q1,1)={q2} ∅ ∅
• q0 is the start state 0, 1
• F = {q2}
0 1
q0 q1 q2
Formal Definition of NFA
0,1 0,1

start
1 0, ε 1
q1 q2 q3 q4

Q = {q1, q2, q3, q4}, Σ = { 0, 1 },


qstart = q1, F = { q4 },

δ(q1, 0) = {q1}, δ(q1, 1) = {q1,q2}, δ(q1, ε) = { }, …


Formal Definition of NFA’s Computation

• Let M = (Q, Σ, δ, qstart, F) be an NFA


• Let w be a string over the alphabet Σ
• Then, M accepts w if we can write
w = w1 w2 … wn such that each wi ϵ Σε and
a sequence of states r0, r1, …, rn in Q exists
with the three conditions:
• r0 = qstart
• ri+1 ϵ δ(ri, wi+1) compare this with DFA
• rn ϵ F
Regular Language

• The Union, Concatenation, and Star


operations are called regular
operations
• Languages that can be recognized by
DFA are called regular language
Language of an NFA

• Extension of δ to δ*(q, w):


• Basis: δ*(q, ε) = {q}
• Induction: δ*(q, wa) = ∪p∈ δ*(q, w)δ(p, a)
• Formally, the language recognized by
N = (Q, Σ, δ, q0, F) is
L(M) = {w | δ*(q0, w) ∩ F ≠ ∅}.
(i.e. if any path from the start state to an
accept state is labeled w.)

You might also like