Professional Documents
Culture Documents
FA Ch1 PDF
FA Ch1 PDF
Theory
Chapter 1:Introduction
Theory of Computation
• It is a branch of computer science that deals with how efficiently a problem can be solved
on a model of computation, using an algorithm.
• Theory of computation is divided into 3 fields:
1. Automata Theory and Formal Languages
2. Computability Theory
3. Complexity Theory
• The goal is to ascertain the power and limits of computation.
• In order to study these aspects, it is necessary to define precisely what constitutes a model of
computation as well as what constitutes a computational problem.
• This is the purpose of automata theory.
• The computational models are automata, while the computational problems are formulated as
formal languages. 2
Cont’d...
1. Automata Theory and Formal
Languages
• It deals with the definition and properties of various mathematical model of computation.
• Examples:
i. Finite Automata(FA)
ii. Context Free Grammar (CFG)
iii. Turing Machine (TM)
2. Computability Theory
• It deals with what can/cannot be computed by the model.
3. Complexity Theory
• It groups the computable problems based on their hardness.
Main purpose of Theory of Computation
• Develop formal mathematical model of computation that reflect real world computers.
3
Cont'd….
2. Computability
Theory
•It deals with what can/cannot be computed by the model.
3. Complexity Theory
• It groups the computable problems based on their hardness.
4
Alphabets and Strings
1. Symbol
• Symbol(often also called character) is the smallest building block,
which can be any alphabet, letter, or picture.
• Example
• a, b, c, …, z
• 0,1,…, 9
• +,*, #, … special characters
2. Alphabet
• An alphabet is a finite, non-empty set of symbols.
• It is denoted by ∑.
• Examples: ∑ = {0,1} set of binary alphabets. 5
Cont'd….
• Examples: ∑ = {a, b, c, …, z} set of all lowercase letters.
∑ = {A, B, C, …Z} is an alphabet of uppercase letters.
∑ = {+, #, &,$, …} set of all special characters
3. String or Word
• It is a finite collection of symbols from the alphabet.
• The string is denoted by w.
• Example: w = 01110 is string from binary alphabet ∑ = {0,1}
w = aabbaacaa is string from alphabet ∑ = {a, b, c}
11
Formal Definition of Grammar
Definition: set of rules used to describe the string of the language.
Grammar G is a collection of four tuples.
G = {V, T, P, S }
V is finite set of variables
T is finite set terminal symbols
P is finite number of production rules
S is starting symbol
S→
Example 1: S → aS Example 2: AB
S → A→e
bS S B→ 12
Cont’d …
From Example 1 above: From Example 2 above:
S is starting symbol S is starting symbol
{a,b} are terminal symbols A,B are set of variables
Number of variables {-} e,b are set of terminals
Number of production =3 Number of production
=3
Language: set of strings
generated by any grammar.
Example 1: S → Example 2: S → aS|bS|a
AB • This grammar can be also
A→a written as:
S → aS
L=
B→ S →
{ab} bS S
b 13
→a
Cont’d …
• From Example 2 above: S → aS|bS|a
S
S S S
aS
a aS bS
a
{a} bS
aS a
aS
a {ba}
• L= (a+b)*a a
{aaa}
• Strings of the language are: {abaa}
{a, aaa, aba, aaba} = ending with a.
14
Cont’d …
• Problems: Identify languages generated by grammars
• Example 1: S → aS|bS|ε
• Minimum string is ε.
• The grammar can generate any combination of a and b with including empty string.
• Strings generated by the above grammar are:
• Strings are: {ε, a, b, aa, bb, ab, ba, abb, aab, aba, abaa, …}
• Language L = (a+b)*
• Example 2: S → aS|bS|a|b
• Minimum string is a and b without including ε.
• String generated by the above grammar are:
• {a, b, aa, bb, ab, ba, abb, aab, …} Language L
=(a+b)+ 15
Automata
• Theory of automata is a theoretical branch of computer science and mathematical.
• 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.
• The main motivation behind developing the automata theory was to
develop methods to describe and analyze the dynamic behavior of discrete systems.
• This automaton consists of states and transitions.
• The State is represented by circles, and the Transitions is represented by arrows.
• Automata is the kind of machine which takes some string as input and this input
goes through a finite number of states and may enter in the final state.
16
Finite Automata
• Finite automata are used to recognize patterns.
• It takes the string of symbol as input and changes its state accordingly.
• When the desired symbol is found, then the transition occurs.
• At the time of transition, the automata can either move to the next state or stay in
the same state.
• Finite automata have two states:
• Accept state or
• Reject state.
• When the input string is processed successfully, and the automata reached
its final state, then it will accept, otherwise it will reject the input.
17
Cont’d …
18
Formal Definition of FA
19
Transition Diagrams
• Transition diagram is a directed graph associated with vertices of the
graph corresponds to the state of finite automata.
1
0 1
• Example:
1 q2
q0 q1
Final or
Start or Accept State
Initial State
• {0, 1} are inputs
• qo initial state
• q1 intermediate state
• q2 is final state 20
Transition Table
• Transition Table is basically a tabular representation, of the transition function that
takes two arguments( a state and a symbol ) and returns a value (next state).
• Example:
0, 1
0
0 1
1 q1
q0 →qo q0 q1
* q1 q1 q1
Transition diagram
22
Transition Function
• The mapping function or transition function denoted by δ.
Two parameters are passed to this transition function
1. current state
2. input symbols
• Transition function always returns a state which can be called as next state.
δ(current state, current input symbol) = next state
Qx∑→Q
• Example: δ(q0, a) = q1
δ(q0, 1) =
23
Types of Automata
Finite Automata
a, b
q0 b q1
a
qo is initial state
q2
q1 is final state
{a, b} input
symbols
26
Formal Definition of DFA
27
Cont’d …
• DFA to accept
'a'
q0 a q1
29
Cont’d …
30
Cont’d …
• Example 2: Design DFA with given ∑ = {0,1} that accepts those string which starts
with 1 and ends with 0.
• Solution: minimum length of string is 2.
L = { 10, 100, 1010, 11001100, …. }
Number of states = |minimum length| + 1
DFA 0
1
Diagram:
q0 1 q1 0 q2
1
31
Cont’d …
• Example 3: Design DFA with given ∑ = {0,1} that accepts even number of 0’s and
even number of 1’s.
• Solution: minimum length of string is 3.
L = { 001, 110, 100, …. }
DFA Diagram:
32
Cont’d …
• Example 4: Construct DFA that accepting all strings over ∑ = {a,b} ending with
ab.
• Solution: minimum length of string is 2.
number of states = 2+1 =3
L = {ab, aab, bab,
bbab…. } b
DFA b a
Diagram:
q0 a q1 b q2
a
b 33
Non Deterministic Finite Automata (DFA)
• The finite automata are called NFA when there exist many paths for specific input
from the current state to the next state.
• It is easy to construct an NFA than DFA for a given regular language.
• Every NFA is not DFA, but each NFA can be translated into DFA.
• NFA is defined in the same way as DFA but with the following two exceptions:
1. it contains multiple next states, and
2. it contains ε transition.
34
Cont’d …
35
Cont’d …
Formal definition of NFA
• NFA also has five tuples same as DFA, but with different transition function,
as shown follows:
δ: Q x ∑ →2Q
• where,
• Q finite set of States
• ∑ finite set of input symbol
• q0 initial state
• F final state
•δ transition function
36
Cont’d …
Graphical Representation of an NFA
• An NFA can be represented by digraphs called state diagram. In which:
The state is represented by vertices.
The arc labeled with an input character show the transitions.
The initial state is marked with an arrow.
The final state is denoted by the double circle.
• Example 1: Construct transition diagram of NFA from the below given
information.
• Transition Table:
• Transition Table:
Present State Next state for Input 0 Next State of Input 1
→q0 q1 q1
q1 q2 q2
*q2 ε ε
40
Cont’d …
Present State 0 1
→q0 q0, q1 q0, q2
q1 q3 ε
q2 q2, q3 q3
*q3 q3 q3
41
Cont’d …
• The transition diagram can be drawn by using the mapping function
as:
42
Cont’d …
• Example 5: Design an NFA with ∑ = {0, 1} accepts all string ending with 01.
• Solution:
43
Cont’d …
• Example 6: Construct NFA for set of strings of all that starts with 0 over {0,
1}
Solution:
0,1
q0 0 q1
44
NFA with ε
• ε is empty symbol( no character to accept)
• Regular NFA with 5 tuples {Q, ∑, q0, F,
δ}
where δ : Q x ∑ → 2Q
• ε -NFA with 5 tuples {Q, ∑, q0, F,
δ} where δ : Q x ∑ U ε → 2 Q
1 0
• Example: 0 ε
A B C
DFA: δ: Q x ∑ →Q a, b
NFA: δ: Q x ∑
→2Q
Example 1: b b q2
q0 q1
47
Cont’d …
Step 1: Construct NFA transition a b
table a b q0 q0 [q0,q1]
NF DFA
A q0 q0 q0,q1 : [q0,q1] [q0] [q0q1q2]
q1 - q2 [q0q1q2] [q0] q0q1q2]
q2 - - q2 - -
• [q0,q1] new single state
a b
b b q0q1q2
q0 q0q1
a
a 48
Cont’d …
Example 2: Convert the given NFA to
DFA
a, b b
a a q2 a, b q3
q0 q1
Step 1:construct NFA transition b
table
δ a b
→q0 {q0,q1} q0
q1 q2 q1
*q2 q3 q3
*q3 - q2
49
Cont’d …
Step 2: Construct DFA transition table and create new states
NFA DF
δ a b δ a A b
→q0 {q0,q1} q0 →q0 [q0,q1] q0
q1 q2 q1 [q0q1] [q0q1q2] q0q1
*q2 q3 q3 *[q0q1q2] [q0q1q2q3] [q0q1q3]
*q3 - q2 *[q0q1q2q3] [q0q1q2q3] [q0q1q3q2]
*[q0q1q3] [q0q1q2] [q0q1q2]
b b
a a a, b a, b
q0 q0,q1 q0,q1,q2 q0,q1,q2,q3
b a, b
q0,q1,q3 50
Cont’d …
Problem:
Convert the following Non-Deterministic Finite Automata (NFA) to
Deterministic Finite Automata (DFA):
51
Equivalence of Two Finite Automata
• For any pair of states(qi, qj) the transition for input a ε ∑ is defined by
{qa, qb} where δ{qi,a}=qa and δ{qj,a}=qb.
• The two automata are not equivalent if for a pair {qa, qb} one
is intermediate state and the other is final state.
• Ifinitial state is final state of one automata, then second
automata also initial state must be final state for them to be
equivalent.
52
Cont’d …
Example1: find out whether the following two finite automata are equivalent or not.
c
c d
d
A q1 q2 q3
c
d
c
d
q7 • Pair of states both are
q4
B
c c final state or initial state
d
d
are equivalent.
q5 c q6 d
53
Cont’d …
State/ Input c d
{q1, q4} {q1, q4} {q2, q5}
FS, FS IS, IS
{q2, q5} {q3, q6} {q1, q4}
IS, IS FS, FS
{q3, q6} {q2, q7} {q3, q6}
IS, IS IS, IS
{q2, q7} {q3, q6} {q1, q4}
IS, IS FS, FS
• Based on the above table , the given finite automata A and B are
equivalent.
54
Cont’d …
Example2: find out whether the following two finite automata are equivalent or not.
c
c d
d
A q1 q2 q3
c
d
c
c
q7 • Pair of states both are
q4
B c
c final state or initial state
d c
or intermediate state are
q5 q6 d equivalent.
d 55
Cont’d …
State/ Input c d
{q1, q4} {q1, q4} {q2, q5}
FS, FS IS, IS
{q2, q5} {q3, q7} {q1, q6}
IS, IS FS, IS
• Based on the above table , the given finite automata A and B are not
equivalent.
56
Minimization of DFA
•The minimization of DFA means reducing the number of states from a given FA.
Step1: Remove all the states that are unreachable from initial state via any set
of transition of DFA.
Step2: Draw the transition table for all pair of states.
Step3: Now split the transition table into two tables T1 and T2.
T1 contains all final states
T2 contains non-final states
• Step4: Find similar rows from T1,
such that:
δ(q, a) =p • Find the two states which have same value of a and b and
q0 0 q1 0
q2 1
0 1 q4
1 0, 1
q3
0, 1
q5 0, 1
58
Cont’d …
Step1: Remove q2 and q4 in FA because they are unreachable
states.
0 q1
q0
0 1
1
0, 1
q3 q5 0, 1
Step2: Draw transition table for the rest of the states.
States 0 1
→q0 q1 q3
T1
q1 q0 q3
*q3 q5 q5
*q5 q5 q5 59
Cont’d …
Step3: (i) tables which starts from non final states.
States 0 1
→q0 q1 q3
q1 q0 q3
T1
0 q1
• The minimized DFA q0
0
1
1
q3 0, 1
61
Application of Finite Automata
• Finite Automata (FA) used:
1. For the designing of lexical analysis of a compiler.
2. For recognizing the pattern using regular expressions.
3. For the designing of the combination and sequential
circuits using Mealy and Moore Machines.
4. Used in text editors.
5. For the implementation of spell checkers.
62
Activity1
1. Draw a DFA for the language accepting strings ending with '01'
over input alphabets ∑ = {0, 1}.
2. Draw a DFA for the language accepting strings ending with 'abba'
over input alphabets ∑ = {a, b.
3. Draw a DFA for the language accepting strings ending with '0011' over
input alphabets ∑ = {0, 1}.
4. Draw a DFA for the language accepting strings ending with ‘0011’ over
input alphabets ∑ = {0, 1}.
63
Activity2
1. Design an NFA in which all the string contain a substring 1110.
2. Design an NFA with ∑ = {0, 1} accepts all string in which the
third symbol from the right end is always 0.
3. Design an NFA with ∑ = {0, 1} in which double '1' is followed by double
'0’.
4. Design an NFA with ∑ = {a, b,c} in which a is followed by b
and followed by c.
64
Activity3
1. Find out whether the following two finite automata are equivalent or not.
A.
B.
65
Activity4
1. Minimize the given DFA:
66
End!!!
THANK
YOU!!!
3/28/2022 Fikru Tafesse(MSc. in Computer Science) 67