Chapter 1-Introduction To Finite Automata
Chapter 1-Introduction To Finite Automata
Chapter 1-Introduction To Finite Automata
1
Introduction
2
Cont...
• Some problems take so much time that computers are useless for all but
very small instances of the problem. This class of problems is called
“intractable” or “NP-hard”.
• Often we take all polynomial functions to be slowly growing (take so much
time)while functions that grow faster than any polynomial are deemed to
grow too fast (do take not much time, and can be solved).
• The main motivation behind developing the automata theory was to develop
methods to describe and analyze the dynamic behavior of discrete systems.
4
Generally:
• It’s mainly about what kind of things can you really compute mechanically.
• How fast and how much space does it take to do so?
Example #1: if you want to design a machine that accepts any binary string that
ends with ‘0’. Is it possible to design this kind of machine?
Let’s take some inputs:
So that it’s possible to design
110010100 01001110 10101011 that means it can be
computed mechanically
start end start end start end
Example #2: if you want to design a machine accepts all valid java codes. Is it
possible to design this kind of machine?
Let’s look some examples:
Compilers: takes java codes and checks whether the code is correct or incorrect.
And then if it’s correct, it compiles the code.
Java codes
So that, it’s possible to design this kind of machine that means it can be computed mechanically.
6
Cont...
Example #3: if you want to design a machine accepts all valid java codes and
never goes into infinite loop. Is it possible to design this kind of machine?
• As we have seen in the example 2, it’s possible to design a machine that
accepts all valid java codes.
• But, is it possible to design a machine that never goes into infinite loop? It’s
impossible (even if we design this kind of machine we will get a wrong output
or no output at all).
So that, it’s impossible to design this kind of machine that means it can’t be computed
mechanically.
7
Layers of automata
11
Cont...
Grammar in automata
• Rules or mathematical models used to write languages (sets of strings)
correctly or in the right way.
• It’s a standard way of representing the language.
• It contains a set of production rules which makes the string of language.
• The set of possible strings derived from grammar is known as the language of
that grammar.
We’ll see in detail in chapter two
12
Categories of finite state machine
Example 2:
Let’s take some states with inputs:
There are two different inputs (0 &1).
Take q0:
• When the input is 1, the next state will be q2.
• When the input is 0, the next state will be q1.
Take q1:
• No next states.
Take q2:
• When the input is 1, the next state will be q2 itself.
• When the input is 0, the next state will be q2 itself
q0, q1, and q2 are states
As shown above, no multiple next states in a given input
16
Cont...
• Every DFA can be defined in five tuples.
From Example 1:
{Q, ∑, q0, F, δ} Q = {q0, q1, q2, q3}
Q = set of all states ∑ = {0, 1}
∑ = Inputs q0 = q0
0 0
it is accepted b/c it
00 = q0 q1 q1
reaches to the final
state(q1).
Initial state Final state
q0 is the start state
18
Cont...
0 0 1 1
0011 = q0 q1 q1 q1 q1 it is accepted b/c it reaches to
the final state(q1).
1 0
101 = q0 q2 q2 0 q2 it is rejected b/c it doesn’t reach to the
final state(it reaches to trap/dead state
q2).
0 1 1
011 = q0 q1 q1 q1 it is accepted b/c it reaches to the final
state(q1).
19
Cont...
#2: Construct a DFA that accepts a set of all strings over {0,1} of length 2.
∑ = {0, 1}, L = {00, 01, 10, 11}
Let’s check some input strings
Input strings = {00, 111, 11}
0
00 = q0 0 q1 q2 it is accepted b/c it reaches
to the final state(q2).
1
111 = q0 q1 1 q2 1 q3 it is rejected b/c it doesn’t
reaches to the final state(it
reaches to dead state q3).
11 = q0
1 q1 1 q2 it is accepted b/c it reaches to the final
state(q2).
20
Cont...
#3: Construct a DFA that accepts a set of all strings over {0, 1} with a substring of 01.
∑ = {0, 1}, L = {01 ,010, 011, 0101, …} → infinite set
substring of 01 means any string that contains 01
Let’s check some input strings
Input strings = {0011, 000, 111, 1010, 1110}
0011 = q0
0 q1 0 q1 1 q2 1 q1✓accepted
000 = q0
0 q1 0 q1 0 q1 ╳ rejected
1010 = q0 1 q3 0 q4 1 q2 1 q2
✓accepted
1 1 q3 1 q3 ╳ rejected
111 = q0 q3
1 1
1110 = q0 1 q3 1 q3 q1 q4 ╳ rejected
21
Cont...
#4: Construct a DFA that accepts a set of all strings over {a, b} that doesn't
contain ‘aabb’ in it.
∑ = {a, b}, L = {a, aa, aab, abb, b, bb, …} → infinite set without ‘aabb’
Simplification:
22
Cont...
Step2 : flip the states (change the final states to non-final states and non-final states to
final states).
Let’s check some input strings
Input strings = {a, ab, aba, aabb, aabba}
a = q0
0 q1 ✓accepted , q1 is a final state
aabba = q0 a q1 a q2 b q3 b q4 b q4 ╳ rejected
25
Exercise
Design a DFA that contains the following language.
L = {Set of strings that has an even number of ‘1’s and an even number of ‘0’s}.
26
Non-Deterministic Finite Automata
• It comes from the word “non-determinism”.
• There could be multiple next states from the given current state.
• There is a randomness (of the next state) in a given input string.
• If there are multiple next states, the next state may be chosen randomly or
chosen parallelly.
• Empty string(epsilon) transition is possible (null move is accepted).
• It may have multiple final states.
27
Cont...
Example 1:
Let’s take some states with inputs:
There are two different inputs (0 &1).
Take q0:
• When the input is 1, the next state will be q1 and q3.
• When the input is 0, the next state will be q0 itself and q2.
• When empty string, the next state will be q4.
N.B:
• NFA supports null (empty string ) move, but DFA doesn’t
support.
• DFA doesn’t change the sate without getting any input
character.
28
Cont...
• Every NFA can be defined in five tuples.
{Q, ∑, q0, F, δ}
Q = set of all states
∑ = Inputs
q0 = start or initial state (indicated by the incoming arrow from
nowhere)
F = set of final states (denoted by double circles)
δ = transition function Q x ∑ → 2Q
29
Cont...
31
Cont...
Let’s try some input strings in example 1: {100, 01, 111}
Whatever the first (beginning) string, the last input must be 0. q0
0
q0
1 0
100 = q0 q0 q1✓accepted
q1 0 ∅ dead configuration
q0 1 q0 X rejected
0
01 = q0
q1 ∅ dead configuration
Try 111???
N.B: the NFA accepts if there is any way to run a machine that ends in any state out of which
at least one state is a final state. Look at the example above in string 100.(q0→q0→q0→q1)
The dead configuration is equivalent to the dead state in DFA. 32
Cont...
001 = A
0
B
0
B
0 B ✓accepted
101 = A
1 ∅ X
C ✓accepted
0 0
00 = A B
001 = A
0
B
0
C
0 ∅ X rejected
Try 0, 01, 11
34
Cont...
#4 Design an NFA that accepts all strings over {0, 1) that start with 10.
∑ = {0, 1}
L = {10, 100, 101, 1010, 110, …,}→infinite set
100 = A
1
B
0
C
0 C ✓accepted
101 = A
1
B
0
C
1 C ✓accepted
11 = A
1 B
1 ∅ X
Try 110, 1011, 1110 35
Cont...
#5 Design an NFA that accepts all strings over {0, 1) that contain 01.
∑ = {0, 1}
L = {01, 001, 101, 1011,…}→infinite set
#6 Design an NFA that accepts all strings over {0, 1) that ends with 11.
∑ = {0, 1}
L = {11, 011, 1011, 111, …, }→ infinite set
1 A
A
A1
111 = A 1 B
1 C
✓accepted
B 1 C1 ∅
To convert into its equivalent DFA, we have to replace the dead configuration
by dead state. 0 1
→A B C
We have to discuss state B and state C
δ = B B B
C C C
C is a dead state, replaced by ∅
39
Cont...
40
Cont...
Exercise:
Design an NFA for the following languages and then convert into its DFA
equivalent.
1. L1 = {set of all strings over {0, 1} that ends with 01}
2. L2 = {set of all strings over {0, 1} that ends with 11}
3. L3 = {set of all strings over {0, 1} that contain 01}
41
Cont...
#2: Design an NFA that accepts all strings over {0, 1} that end with 11, and
then convert into its equivalent DFA
The method we used in the above examples is called subset construction method,
42
Cont...
• N.B: the final state will be the state(s) which contain(s) the final state in
NFA (in this case C).
• So that state {ABC} will be the only state which contains state C .
0 1
→A A AB
AB A ABC
δ =
ABC A ABC
#3: Design an NFA that accepts all strings over {0, 1} in which the second last
string is always 1, and then convert into its equivalent DFA
45
Cont...
0 1
→A A {A, B}
Transition table for NFA
δ = B C C
C ∅ ∅
0 1
→A A AB
The final States are states that contain C in it, and C(if found).
δ = AB AC ABC
AC A AB
ABC AC ABC
46
Cont...
DFA equivalent
47
Cont...
#4: Design the equivalent DFA for the NFA given by:
M = [{A, B, C}, (a, b), δ, A, {C}]
Where δ is given by: • {A, B, C}, indicates states
a b • (a, b) indicates inputs
→A {A, B} C
δ = B A B • A indicates it is a start state
∅ {A, B}
C • {C} indicates it is a final state
48
Cont...
a b
→A {A, B} C
δ = B A B
∅ {A, B}
C
49
Cont...
a b
→A {A, B} C
δ = B A B Transition table for NFA
∅ {A, B}
C
a b
→A AB C Each state must be discussed
δ = AB A BC
BC A AB
C D AB
D D D
DFA equivalent
51
Epsilon Non-Deterministic Finite Automata
Reading Assignment
link https://cstaleem.com/theory-of-automata
52