Theory of computation2
Theory of computation2
Automata
An automaton is a computational model with the following components:
Abstract Machine
• An abstract machine is a theoretical design used to simulate the logic and
behavior of computational processes.
• It does not have physical components.
• It consists of defined states, transitions, and rules that represent and solve
problems by processing inputs step by step.
• Examples:
o Finite Automaton: Used for pattern recognition.
o Turing Machine: Used for performing complex computations.
Theory of Automata
• The Theory of Automata is a branch of computer science that deals with the
design and analysis of abstract machines and the problems they can solve.
• It is foundational for understanding how computers process information,
recognize patterns, and perform computations.
Formal and Informal Languages
In the theory of computation, languages define sets of rules for creating valid strings of symbols.
Here’s the distinction between formal and informal languages:
1. Formal Languages
• A formal language is a set of strings formed from a specific alphabet that follow
well-defined rules.
• Characteristics:
o Created using a specific set of symbols.
o Follows structured and precise rules.
o Not used for human communication but rather as a "game of symbols"
with formal rules.
• Applications:
o Programming languages (e.g., Python, Java).
o Mathematical formulas.
2. Informal Languages
• Informal languages, also called natural languages, are used for everyday
communication (e.g., English, Urdu, Spanish).
• Characteristics:
o Lack strict rules and can be flexible in meaning.
o Open to interpretation, making them unsuitable for precise computations
and programming.
• Summary: Informal languages are flexible and widely used by humans for general
communication.
2. Machine Communication:
• Automata and formal languages enable effective communication between
machines and humans.
• This concept is fundamental for the functioning of computing applications.
3. Compiler Design:
• Compilers, which convert high-level programming code into machine code,
rely heavily on automata theory principles.
• Automata assist in:
o Breaking code into smaller parts.
o Checking if the code follows the correct syntax.
o Identifying and highlighting errors in the code.
7. System Verification
• Automata theory helps prove the correctness of programs by providing a
structured way to verify program behavior.
• This is especially important for reliable systems, such as those used in
aviation or medical devices.
• Type 3:
o Pattern matching, text search algorithms, and lexical analysis.
• Type 2:
o Syntax analysis in compilers, XML, HTML, and artificial intelligence.
• Type 1:
o Natural language processing, formal verification, and programming
languages.
• Type 0:
o Theoretical computing and problem solvability analysis.
1. Alphabets (Σ)
2. Strings (ω)
Length of String:
• Definition: An operation that generates a set of all possible strings (including the
empty string) by concatenating zero or more symbols from an alphabet.
• Notation: Denoted by Σ*.
• Examples:
o For Σ = {a, b}, Σ* includes {ε, a, b, aa, bb, ab, …}.
• Definition: Similar to the Kleene star, but it does not include the empty string.
• Notation: Denoted by Σ+.
• Examples:
o For Σ = {a, b}, Σ+ includes {a, b, aa, bb, ab, …}.
5. Language
Finite Automata
• Definition:
A finite automaton is a theoretical computational model used to recognize and process
strings of symbols.
• Working Principle:
o A finite automaton processes strings of symbols from an alphabet, transitioning
between states according to a transition function.
o A string is accepted if, after processing all symbols, the automaton ends in an
accept state.
• Applications:
o Pattern matching.
o Network protocols.
o Lexical analysis (the first phase of a compiler that processes source code).
• Finite automata may either:
o Produce an output.
o Accept or reject a string.
1. Without output:
2. With output:
• Mealy Machine:
o Produces output based on the current state and input.
• Moore Machine:
o Produces output based only on the current state.
Deterministic Finite Automaton (DFA)
• Definition:
A deterministic finite automaton (DFA) is a finite state machine that either accepts
or rejects strings of symbols.
Alternatively, a DFA can be defined as a finite state machine that recognizes patterns
in strings.
Characteristics of DFA
1. Deterministic:
o Each state has exactly one transition for each symbol in the alphabet,
ensuring a unique path for any input string.
2. Finite State:
o A DFA has a finite number of states, including:
▪ One start state.
▪ One or more final (accepting) states.
Input Output
100 Reject
01000 Accept
10101 Reject
0101110 Accept
0000 Accept
iii. DFA for Strings Starting with '1' and ending with '0' (∑ = {0, 1}):
Input Output
1010 Accept
0001 Reject
10110 Accept
0101 Reject
0111 Reject
v. DFA Accepting Strings Starting and Ending with Different Symbols (∑ = {0, 1}):
Input Output
aaa Reject
aaababbab Accept
abababa Reject
bababa Accept
baaaab Reject
vi. DFA Accepting Even Number of 0’s and Even Number of 1’s (∑ = {0, 1}):
Input Output
0011 Accept
0111 Reject
11001100 Accept
101010 Reject
10000 Reject
Input Output
10000 Accept
100101 Reject
1100 Accept
111110 Reject
011100 Accept
viii. DFA Accepting Strings with Total Number of ‘a’s Divisible by 3 (∑ = {a, b}):
Input Output
baba Reject
aaa Accept
bbbba Reject
bbbbbb Accept
aabab Reject
ix. DFA Accepting Exactly One 'a' Over ∑ = {a, b}:
Input Output
aab Reject
a Accept
bbbba Reject
abab Reject
abaa Reject
Input Output
a Accept
abbbba Reject
baba Reject
b Accept
baaaa Reject
xi. DFA for Strings with 'b' as the Second Letter (∑ = {a, b}):
Input Output
abaaba Accept
aaabaa Reject
abbbbb Accept
aaaaa Reject
xii. DFA Accepting Strings with Exactly Four '1’s (∑ = {0, 1}):
Input Output
01111001 Reject
1111 Accept
001111 Accept
01010101 Accept
10001 Reject
xiii. DFA Accepting Only One 'a' Per String (∑ = {a, b}):
Input Output
abbb Accept
abbbaa Reject
bbbba Accept
bbb Reject
abab Reject
Input Output
ab Accept
aaba Reject
ababaa Reject
aaaabbab Accept
bbaa Reject
Input Output
00 Accept
10 Accept
11 Reject
100 Accept
xvii. DFA for Strings Starting with "aba" (∑ = {a, b}):
Input Output
aba Accept
abba Reject
baab Reject
abab Accept
baba Reject
Input Output
0111 Reject
11 Accept
00 Accept
01101 Reject
010 Reject
xix. DFA for Strings Without "00" or "11" as Substrings (∑ = {0, 1}):
Input Output
11 Reject
00 Reject
1010 Accept
1100 Reject
010111 Reject
xx. DFA for Strings Starting and Ending with 0 (∑ = {0, 1}):
Input Output
0011 Reject
01110 Accept
1000 Reject
011101 Reject
011110 Accept
xxi. DFA for Strings Where Each '0' Is Surrounded by '1's (∑ = {0, 1}):
Input Output
1 Accept
101 Accept
111 Accept
100 Reject
10 Reject
Input Output
100 Reject
11 Accept
10 Accept
11101 Reject
0 Accept
Regular Expressions
Definition:
• Matching strings
• Validating input
• Searching/manipulating text based on specific rules
1. Literals:
A literal matches exact characters in a string.
Example: The literal "a" matches the character "a" wherever it appears in a
string.
2. Concatenation:
Concatenation represents a sequence of characters that must appear in order.
Example: The expression "abc" matches the substring "abc" in the string.
3. Alternation:
Alternation is represented by the pipe symbol (|), which denotes a choice
between alternatives.
Example: "a|b" matches either "a" or "b" in a string.
4. Kleene Star:
The Kleene star operator (*) matches zero or more occurrences of the preceding
character or group.
Example: "a*" matches "" (empty string), "a", "aa", and so on.
5. Kleene Plus:
The Kleene plus operator (+) matches one or more occurrences of the preceding
character or group.
Example: "a+" match "a", "aa", "aaa", but not "".
6. Optional:
The question mark (?) makes the preceding character or group optional, meaning
it can occur zero or one time.
Example: "a?" matches "" (empty string) or "a".
7. Character Class:
A character class allows matching any one character listed inside square brackets
([]).
Example: [abc] matches "a", "b", or "c".
8. Range:
A range specifies a span of characters inside square brackets ([]).
Example: [a-z] matches any lowercase alphabetic character from "a" to "z".
9. Anchors:
Anchors are special characters used to match positions in a string, rather than
actual characters.
Example:
o ^a matches any string that starts with "a".
o a$ matches any string that ends with "a".
10. Groups:
Parentheses (()) group expressions, treating them as a single unit. They are also
used to apply operators like * or + to the entire group.
Example: (ab)+ matches "ab", "abab", "ababab", and so on.
Examples:
1. Design a regular expression for the language accepting all strings of a’s over ∑ =
[a].
Solution: R.E = (a)*
2. Design a regular expression for the language accepting all strings of a’s except
null string over ∑ = [a].
Solution: R.E = (𝑎+ )
3. Design a regular expression for language accepting all strings of a’s and b’s over
∑ = [a, b].
Solution: R.E = (a+b) *
4. Write regular expression for language accepting all strings of a’s and b’s except
null string over ∑ = [a, b].
Solution: R.E = (𝑎 + 𝑏)+ OR R.E = (a+b). (a+b) *
5. Design regular expression for language accepting all string ending with 00 over ∑
= [0,1].
Solution: R.E = (0+1) *.00
6. Design regular expression for language which accepts string starting from 1 and
ending with 0, over ∑ = [0,1].
Solution: R.E = 1. (0+1) *.0
7. Design regular expression for language accepting strings start and end with a over
∑ = [a, b].
Solution: R.E = a.(a+b) *. a
8. Design regular expression for language in which any number of a’s is followed by
any number of b’s is followed by any number of c’s where ∑ = [a, b, c].
Solution: R.E = abc*
9. Design regular expression for language accepting strings start with a and end with
any symbol where ∑ = [a, b].
Solution: R.E = a.(a+b) *
10. Design regular expression for language accepting strings that start and end with
any combination of a’s and b’s but the 2nd symbol will be “b” where ∑ = [a, b].
Solution: R.E = (a+b). b.(a+b) *
11. Design regular expression which accepts strings in which there will be 4 “1’s”
and any of “0’s” where ∑ = [0,1].
Solution: R.E = 10101010
12. Design regular expression for language accepting strings starting and ending with
a and any combination of ‘b’ in between where ∑ = [a, b].
Solution: R.E = a b*a
13. Design regular expression for language accepting string having only one “a”
where ∑ = [a].
Solution: R.E = a
14. Design regular expression for language accepting string having only one ‘a’ or ‘b’
where ∑ = [a, b].
Solution: R.E = (a+b)
15. Design R.E accepting strings having only one ‘a’ where ∑ = [a, b].
Solution: R.E = bab
Examples:
1. Design NFA that accepts all strings ending with ‘a’, where ∑ = [a, b].
Solution: R.E = (a+b) *. a
Input Output
ab Reject
aaa Accept
aba Accept
bbaab Reject
2. Design NFA which accepts all strings ending with “00”, where ∑ = [0,1].
Solution:
Input Output
11100 Accept
101001 Reject
1000 Accept
00011 Reject
3. Design NFA which accepts all strings containing substring “aa”, where ∑ =
[a, b].
Solution: R.E = (a+b) * aa(a+b) *
Input Output
abbabb Reject
abaab Accept
abbbaab Accept
ababa Reject
4. Design the NFA transition diagram from the transition table given below.
Transition Table:
States 0 1
q₀ {q₀, q₁} {q₀, q₂}
q₁ {q₃} ε
q₂ {q₂, q₃} {q₃}
*q₃ {q₃} {q₃}
Solution:
Transition diagram can be created with states q₀, q₁, q₂, and q₃, showing the
transitions as per the table above.
5. Construct an NFA for a language L that accepts all strings in which the third
symbol from the right end is always “a”, over ∑ = {a, b}.
Solution:
The NFA will track the last three symbols to ensure the third symbol from the
right end is "a."
Characteristics of NFA:
• Multiple Transition:
From any state, an NFA can have multiple transitions for a given input
symbol.
• Epsilon (ε) Transition:
An NFA can include transitions that do not consume input symbols, known as
epsilon (ε) transitions.
• Acceptance of States:
An input string is accepted by an NFA if there exists at least one path from the
start state to an accept state.
• Non-determinism:
NFA does not follow a unique path; it can take multiple possible paths or
make multiple choices for a given input.
States 0 1
→q₀ {q₀} {q₁}
q₁ {q₁, q₂} {q₁}
*q₂ {q₂} {q₁, q₂}
Solution:
To convert the given NFA to DFA, define the tuple of DFA as:
M' = (θ', ∑', q₀', F', δ')
Transition Functions:
States 0 1
→[q₀] [q₀] [q₁]
[q₁] [q₁, q₂] [q₁]
*[q₂] [q₂] [q₁, q₂]
[q₁, q₂] [q₁, q₂] [q₁, q₂]
2. Given NFA:
States a b
→q₀ {q₀, q₁} {q₁}
*q₁ {q₂} {q₂}
q₂ {∅} {q₂}
1. [q₀, q₁]:
o δ'([q₀, q₁], a) = δ (q₀, a) ∪ δ (q₁, a)
= {q₀, q₁} ∪ {q₂}
= [q₀, q₁, q₂]
o δ'([q₀, q₁], b) = δ (q₀, b) ∪ δ (q₁, b)
= {q₁} ∪ {q₂}
= [q₁, q₂]
2. [q₀, q₁, q₂]:
o δ'([q₀, q₁, q₂], a) = δ (q₀, a) ∪ δ (q₁, a) ∪ δ (q₂, a)
= {q₀, q₁} ∪ {q₂} ∪ {∅}
= [q₀, q₁, q₂]
o δ'([q₀, q₁, q₂], b) = δ (q₀, b) ∪ δ (q₁, b) ∪ δ (q₂, b)
= {q₁} ∪ {q₂} ∪ {q₂}
= [q₁, q₂]
3. [q₁, q₂]:
o δ'([q₁, q₂], a) = δ (q₁, a) ∪ δ (q₂, a)
= {q₂} ∪ {∅}
= [q₂]
o δ'([q₁, q₂], b) = δ (q₁, b) ∪ δ (q₂, b)
= {q₂} ∪ {q₂}
= [q₂]
States a b
→[q₀] [q₀, q₁] [q₁]
*[q₁] [q₂] [q₂]
[q₂] [∅] [q₂]
*[q₀, q₁] [q₀, q₁, q₂] [q₁, q₂]
*[q₀, q₁, q₂] [q₀, q₁, q₂] [q₁, q₂]
*[q₁, q₂] [q₂] [q₂]
𝑞2 {𝜙} { 𝑞2 }
▪ Let M’ = = (θ′, ∑’, q 0 ′, F’, δ′) be DFA.
▪ We will calculate the 𝛿′-transition for DFA.
NFA DFA
• The new generated state is final state because it contains a final state 𝑞1 ,so
the transition table for DFA will be:
States a b
→[𝑞0 ] [𝑞0 , 𝑞1 ] [𝑞1 ]
*[𝑞1 ] [ 𝑞2 ] [𝑞2 ]
[𝑞2 ] [𝜙] [ 𝑞2 ]
*[ 𝑞0 , 𝑞1 ] [𝑞0 , 𝑞1 , 𝑞2 ] [𝑞1 , 𝑞2 ]
*[𝑞0 , 𝑞1 , 𝑞2 ] [𝑞0 , 𝑞1 , 𝑞2 ] [𝑞1 , 𝑞2 ]
*[𝑞1 , 𝑞2 ] [𝑞2 ] [𝑞2 ]
Characteristics of E-NFA:
States 0 1 ε
→q₀ {q₁} ∅ ∅
q₁ ∅ ∅ {q₂}
*q₂ ∅ {q₂} ∅
• ε-closure(q₀) = {q₀}
• ε-closure(q₁) = {q₁, q₂}
• ε-closure(q₂) = {q₂}
1. δ'(q₀,0) = ε-closure({δ(q₀,0)})
= ε-closure({q₁})
= {q₁, q₂}
2. δ'(q₀,1) = ε-closure({δ(q₀,1)})
= ε-closure (∅)
=∅
3. δ'(q₁,0) = ε-closure({δ(q₁,0)})
= ε-closure (∅)
=∅
4. δ'(q₁,1) = ε-closure({δ(q₁,1)})
= ε-closure({q₂})
= {q₂}
5. δ'(q₂,0) = ε-closure({δ(q₂,0)})
= ε-closure (∅)
=∅
6. δ'(q₂,1) = ε-closure({δ(q₂,1)})
= ε-closure({q₂})
= {q₂}
States 0 1
→q₀ {q₁, q₂} ∅
q₁ ∅ {q₂}
*q₂ ∅ {q₂}
Example 2
Solution:
Transition table for the given E-NFA:
States 0 1 2 ε
→q₀ {q₀} ∅ ∅ {q₁}
q₁ ∅ {q₁} ∅ {q₂}
*q₂ ∅ ∅ {q₂} ∅
1. δ'(q₀,0) = ε-closure({δ(q₀,0)})
= ε-closure({q₀})
= {q₀, q₁, q₂}
2. δ'(q₀,1) = ε-closure({δ(q₀,1)})
= ε-closure({q₁})
= {q₁, q₂}
3. δ'(q₀,2) = ε-closure({δ(q₀,2)})
= ε-closure({q₂})
= {q₂}
4. δ'(q₁,0) = ε-closure({δ(q₁,0)})
= ε-closure (∅)
=∅
5. δ'(q₁,1) = ε-closure({δ(q₁,1)})
= ε-closure({q₁})
= {q₁, q₂}
6. δ'(q₁,2) = ε-closure({δ(q₁,2)})
= ε-closure({q₂})
= {q₂}
7. δ'(q₂,0) = ε-closure({δ(q₂,0)})
= ε-closure (∅)
=∅
8. δ'(q₂,1) = ε-closure({δ(q₂,1)})
= ε-closure (∅)
=∅
9. δ'(q₂,2) = ε-closure({δ(q₂,2)})
= ε-closure({q₂})
= {q₂}
States 0 1 2
→q₀ {q₀, q₁, q₂} {q₁, q₂} {q₂}
q₁ ∅ {q₁, q₂} {q₂}
*q₂ ∅ ∅ {q₂}
Transition Diagrams
Minimization of DFA
Definition:
1. Higher Efficiency: Fewer states mean less complexity and faster execution.
2. Simplified Representation: Easier to visualize and work with.
3. Resource Optimization: Reduced storage and computation overhead.
Example 1:
Given DFA Transition Table
States 0 1
→A B F
B G C
*C A C
D C G
E B F
F C G
G G E
H G C
B ×
C × ×
D × × ×
E ✓ × × ×
F × × × ✓
G × × × × ×
H × ✓ × × × × ×
A B C D E F G
• (A, E)
• (B, H)
• (D, F)
Example 2:
Given DFA Transition Table
States 0 1
→q₀ q₁ q₂
q₁ q₁ q₃
q₂ q₁ q₂
q₃ q₁ q₄
*q₄ q₁ q₂
Solution:
First, draw the transition table.
States 0 1
A B C
B B D
C B C
D B E
*E B C
States 0 1
AC B C
B B D
D B E
E B C
Moore Machine
Definition:
A Moore machine is a finite state machine in which the next state is decided by
the current input symbol. The output symbol at a given time depends only on the
present state of the machine.
Solution:
The transition diagram and transition table are as follows:
States next state output
0 1
q0 q1 q2 1
q1 q2 q1 1
q2 q2 q0 0
Solution:
To generate the 1's complement of a binary number, the logic is simple: if the
input is 0, the output is 1, and if the input is 1, the output is 0.
1. Start state.
2. State that takes 0 and produces an output of 1.
3. State that takes 1 and produces an output of 0.
Input 1 0 1 1
States q0 q2 q1 q2 q2
output 0 0 1 0 0
Solution:
For designing such a machine, we need to check two conditions: the substrings "101"
and "110."
Solution:
We need a Moore machine to determine if an input string contains an odd or even
number of 1's. If there is an even number of 1's in the string, the output will be 1;
otherwise, it will be 0.
Example 5: design a Moore machine with the input alphabet {0,1} and
output alphabet contain {Y/N}, if the sequence contains 1010 the output will
be Y otherwise it produces N as output.
Solution:
Mealy Machine
Definition:
A Mealy machine is a machine in which the output depends upon the present input
symbol and the present state of the machine. The Mealy machine is represented by the 6-
tuple (Q, q0, ∑, Δ, δ, λ), where:
In a Mealy machine, the output is represented with each input symbol for each state
separated by "/".
Solution:
For this Mealy machine, the input alphabet is ∑ = {0, 1} and the output alphabet is
Δ = {a, b}.
Solution:
This Mealy machine scans a sequence of input 0 and 1 and generates:
For this, the input alphabet is ∑ = {0, 1} and the output alphabet is Δ = {A, B, C}.
Arden's Method
Arden's Theorem is a fundamental concept in the theory of computation, used to find a
regular expression that represents the language accepted by a given DFA (Deterministic
Finite Automaton) or NFA (Nondeterministic Finite Automaton). It is applied to
construct regular expressions from a DFA or NFA.
R=Q+RP
where Q and P are regular expressions and P does not contain the empty string
(ε), the solution is:
R = QP*
Examples:
a) Construct regular expression from the given DFA.
• Solution:
• First, we make equation from given DFA.
𝑞1 = 𝑞1 0 + 𝜀 → 1
𝑞2 = 𝑞1 1 + 𝑞2 1 → 2
𝑞3 = 𝑞2 0 + 𝑞3 (0+1) → 3
• Now considered equ →1
1→ 𝑞1 = 𝑞1 0 +𝜀
𝑞1 = 𝜀 + 𝑞1 0
𝑞1 =𝜀 + 0*
𝑞1 =0*→3
• Now put the value of 𝑞1 in equ 2
2→𝑞2 = 0*.1 +𝑞2 .1
𝑞2 = 0*1. 1
𝑞2 = 0*.1+ →4
• Combine the values of 𝑞1 and 𝑞2
R.E = 0* +0*.1+
Pumping lemma: -
Purpose:
• The pumping lemma is used to prove that a language is not regular.
However, it cannot prove that a language is regular.
• If A is a regular language, then A has a pumping length P, such that
any string s , where ∣s∣≥P, can be divided into three parts s=xyz, such
that the following conditions are satisfied:
i. x𝑦 𝑖 z 𝜀 A for every i >=0
ii. |y| > 0
iii. |xy| <=p
Examples:
1. Using pumping lemma prove that the language A= {𝑎𝑛 𝑏 𝑛 |n>=0}
is not regular.
• Sol: Assume that A is regular.
then pumping length= n
• now take string S from A i.e S = 𝑎𝑛 𝑏 𝑛 let n= 4
then S = 𝑎4 𝑏 4 = aaaabbbb
• now divide the above string into x, y and z.
aaa = x
abb =y
bb = z
• now check the condition (x𝑦 𝑖 z)
let i = 2
x 𝑦 2 z = aaa abbabb bb
= 𝑎4 𝑏 6 is not belong to A
“If you come across any errors, unclear explanations or area that
require further clarification these notes, please do not hesitate to
reach out to me via email [[email protected]]. I would be
happy to assist and address my concerns.”