Chapter 1-Introduction To Finite Automata

Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

Chapter 1: Introduction

1. Alphabets and string


2. Languages and Grammar
3. Automata
i. Finite automata,
ii. Deterministic finite automata
iii. Non-deterministic finite automata

1
Introduction

• Theory of automata is a theoretical branch of computer science and


mathematics which studies of abstract computing devices or machines.
• It is the study of abstract machines and the computation problems that
can be solved using these machines.
• The abstract machine is called the automata. An automaton with a
finite number of states is called a Finite automaton.

2
Cont...

• Automata are essential for the study of the limits of computation.


There are two important issues:
1. What can a computer do at all? This study is called “decidability” and the
problems that can be solved by computer are called “decidable”. The
problems that can’t be solved by computer are called “undecidable.
2. What can a computer do efficiently? This study is called “intractability”
and the problems that can be solved by a computer using no more time
than some slowly growing function of the size of the input are called
“tractable”. 3
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

accepted accepted rejected


5
Cont...

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

Convert into its binary equivalent

Check whether it’s valid or invalid

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

FSM(Finite State Machine): a machine that can perform


low-level computations. It has a very limited amount of
memory.
CFL(Context Free Language): the automaton called
pushdown automata. Language is a set of strings.
Turing machines: machines that can perform high-level
computations.
Undecidable: a problem that can’t be solved or computed
mechanically. 8
Finite State Machine
‣ A machine that can perform low-level computations.
‣ It’s the simplest model of computation.
‣ It has a very limited amount of memory.
Basic Elements of Automata:
Symbol: anything that is used to represent something. E.g. 0, 1, 2, 3, 1, b, c, …
Alphabet: denoted by sigma (Ʃ): it is a collection of symbols.
E.g. {a, b}, {d, e, f}, {0, 1, 2}, etc.

String: a sequence of symbols


9
Example 1: 1, 0, a, b, 0, 1 Example 2: a, aa, ab, 01, bb, 2,…..
Cont...
Language: a set of strings over some alphabets
E.g. Ʃ = {a, b} , alphabets over a and b.
Let’s define languages over alphabet {a, b}.
L1: set of all strings of length 2
{aa, ab, ba, bb} = 4 possibilities
L2: set of all strings of length 3
{aaa, aab, abb, bbb, bba, baa, aba, bab} = 8 possibilities
L3: set of all strings that begin with ‘a’
{a, aa, ab, aaa, aab, aba, abb, aaaa, abbb, abaa,…,} = infinite set
10
Cont...

• Automata consist of states and transitions. The State is represented by


circles, and the Transitions are represented by arrows.
• Automata takes some input strings as input and this input goes through
some states and may enter in the final state.

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

Finite State Machine (Finite Automata)

FA with output FA without output

DFA NFA ɛ-NFA


Moore Mealy
machine machine

DFA: Deterministic Finite Automata


NFA: Non-Deterministic Finite Automata

ɛ-NFA: Epsilon Non-Deterministic Finite Automata 13


Cont...
Deterministic Finite Automata
• It comes from the word “determinism”.
• It has only one unique next state.
• It has no choice or randomness (of the next state) in a given input string.
• In the given current state, we know what the next will be (means the next
state can be determined).
• It can’t move to the next/other states without any input character (null move
is not accepted).
• It may have multiple final states, but only single initial or start state.
14
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.
• When the input is 0, the next state will be q2
Take q1:
• When the input is 1, the next state will be q0.
• When the input is 0, the next state will be q3.
Take q2:
• When the input is 1, the next state will be q3.
• When the input is 0, the next state will be q0
q0, q1, q2, and q3 are states Take q3:
• When the input is 1, the next state will be q2.
• When the input is 0, the next state will be q1.
15
As shown, no multiple next states in a given input
Cont...

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

q0 = start or initial state (indicated by F = {q1}, set indicates there may


be multiple final states.
incoming arrow from nowhere)
0 1
Qx∑ →Q
F = set of final states (denoted by double q0 q2 q1
δ = q1 q2 Q0
q0 x 0 → q2
circles) q2 q0 Q3 q0 x 1 → q1
q3 q1 q2
δ = transition function Q x ∑ → Q q0 in input 1
17
Cont...
#1: Design a DFA that accepts a set of all strings over {0,1} that start with 0.
∑ = {0, 1}, L = {0, 00, 01, 000, 010, 011, 0000, …} → infinite set

Dead or Let’s check some input strings


trap state

Input strings = {0011, 101, 011}


We must always start from the initial/start state.

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:

Step 1: Construct a DFA that contains ‘aabb” in it.

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

ab = q0 a q1 b q0✓accepted , q0 is final state


aba = q0 a q1 b q0 a q1✓accepted , q1is final state
aabb = q0 a q1 a q2 b q3 b q4 ╳ rejected, q4 is no a final state

aabba = q0 a q1 a q2 b q3 b q4 b q4 ╳ rejected

aabba contains aabb in it. 23


Cont...
# 5: How to figure out what a DFA recognizes?

Let’s check input strings


• 10, accepted
• 110, 1110, 11110, accepted (q0→q1→q2)
• 11, 111, 1111, rejected (q0→q2)
How to generalize? • 01, the only accepted (from q0→q3→q4)
• Top indicates (q0→q1→q2) : any string that contains at least one ‘1’ followed by 0.
• Bottom indicates (q0→q3→q4) : only string ‘01’. ☺☺☺
L ={accepting the string 01 or a string that contains at least one ‘1’ followed by ‘0’}.
Is the DFA is complete? What if it gets {001, 011, or 101, 1100, …, }.? 24
Example:
• 001(q0→q3→?) : where to go after getting two 0’s.
• 1101(q0→q1→ q1→ q2→?) : where to go after getting 110 followed by any ‘0’ or ‘1’.
Let’s complete the DFA

✓ This DFA accepts any string and


checks whether accepted or not. It’s
a complete DFA.
Trap/dead state

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

#1 L: a set of all strings that end with ‘.0’ From Example 1:


Q = {q0, q1} = 2 states
∑ = {0, 1}
q0 = q0
F = {q1}, set indicates there may be
Possibilities of when two multiple next states multiple final states.
(e.g. state q0 in input 0): 0 1
o Move to first state (q0 itself in e.g.1) q0 q0, q1 q0 Q x ∑ → 2Q
o Move to second state (q1 in eg.1) ==
o Move to all states parallelly (q0, and q1) δ = q1 ∅ ∅
Q x ∑ → 22
o Move to nowhere (null move ∅)
Total of 4 possiblee moves = 22 = 2Q
Dead configuration 30
Cont...

Possibilities of when three multiple next states


o Move to the first state
o Move to the second state
o Move to the third state
o Move to the first and second states parallel
o Move to the first and third states parallel
o Move to the second and third states parallel
o Move to all states parallelly (first, second, and thirds
o Move to nowhere (null move ∅)
Totally there are 8 possibilities, which means 23 = 2Q

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

#2 Design an NFA that accepts all strings that start with 0.


∑ = {0, 1}
L = {0, 00, 01, 000, 001, 010, 011, 0000, …, } → infinite set

Let’s try some inputs

001 = A
0
B
0
B
0 B ✓accepted

101 = A
1 ∅ X

Try 0111, 0001, 10101


33
Cont...

#3 Design an NFA that accepts all strings over {0, 1) of length 2.


∑ = {0, 1}
L = {00, 01, 10, 11}

Let’s try some inputs

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

Let’s try some inputs

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

Let’s try some inputs


1 A
A
A0
001 = A 0 B
1 C
✓accepted
B 0 ∅ dead configuration

Try 1101, 0101, 1010 36


Cont...

#6 Design an NFA that accepts all strings over {0, 1) that ends with 11.
∑ = {0, 1}
L = {11, 011, 1011, 111, …, }→ infinite set

Let’s try some inputs

1 A
A
A1
111 = A 1 B
1 C
✓accepted
B 1 C1 ∅

Try 01011, 1011, 0011


37
NFA to DFA conversion

• Every DFA is NFA, but not the vice versa


• There is an equivalent DFA for every NFA
Proof for the 1st statement
• (Q x ∑ → Q) ∈ (Q x ∑ → 2Q), but not the reverse, Q x ∑ → Q is contained
in Q x ∑ → 2Q , the reverse is not true.
Example 1: Design an NFA that accepts all strings over {0, 1} that start with 0,
and then convert into its equivalent DFA.

• Whatever the end string, the first character


must be zero.
• When the machine gets 1 at the beginning, it
goes to dead configuration(∅). 38
Cont...

Let’s design s transition table for an NFA.


0 1
→A B ∅ →A, indicates start state.
δ = B B B B , indicates final state.

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

• Let’s design the DFA of the above transition table of example 1.

Try these input strings: 01011, 1011,


0011

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

• Whatever the beginning string, the last


characters must be 11.
• When the machine gets 0 or 1 after
reaching the final state, then it goes to
dead configuration(∅)., it goes to dead

The method we used in the above examples is called subset construction method,
42
Cont...

Let’s design s transition table for an NFA.


0 1
→A A {A, B}
Replace the ∅ by dead state.
δ = B ∅ C
C ∅ ∅
A U B at input 0 == A U ∅ = A from NFA transition
Subset construction?? table
0 1 A U B at input 1 == AB U C = ABC
→A A AB
We have to discuss state AB, and it’s a single state
AB A ABC We have to discuss state ABC, and it’s a single state
δ = ABC A ABC {A} U {B} U {C} at input 1 == {AB} U {C} U {∅} = {ABC}

{A} U {B} U {C} at input == {A} U {∅} U {∅} = {ABC} 43


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

Equivalent DFA for example 2 NFA


44
Cont...

#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

Transition table for DFA

46
Cont...

DFA equivalent

Try these input strings: 111, 1011, 0101,


1110

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

Let’s design a state diagram:

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

Transition table for DFA


50
Cont...

DFA equivalent

Figure out what this DFA


accepts?

51
Epsilon Non-Deterministic Finite Automata
Reading Assignment
link https://cstaleem.com/theory-of-automata

52

You might also like