Introduction to Theory of Computation
Introduction to Theory of Computation
Module 1
1️⃣ Introduction to Theory of Computation (ToC)
🧐 What is this course about?
Theory of Computation is one of the most fundamental and abstract courses in Computer Science.
Fundamental and abstract courses → Courses that teach core concepts, theories, and
foundations, not just practical skills.
It doesn't teach you how to write code or build a physical computer. Instead, it teaches you how
computer scientists have defined "computation" over the past 50 years.
Key Factors:
2. We feed it an Input.
3. The machine processes the input and gives one of two answers:
✅ YES (Accept)
❌ NO (Reject)
💡 Examples of Computability
To understand what machines can and cannot do, let's look at three levels of problems.
Machine Logic: Scan the digits one by one. If the last one scanned is 0, say YES. If it is 1, say
NO.
Module 1 1
Result: This is possible to design. This uses a Finite State Machine (FSM).
(Here, q0 is the start state, and q1 is the final accepting state)
Explanation of Diagram:
Start at q0 : This is the "reject" state (because we haven't seen a 0 at the end yet).
Real-life Example: Compilers. If you miss a semicolon, the compiler rejects your code (Syntax
Error). If the code is valid, it compiles.
Capabilities: Very limited memory. Can only perform low-level calculations (like Example 1
above).
Module 1 2
2. Context-Free Language (CFL)
What is it? More powerful than FSM.
Key Note: Here, "Language" does not mean C++ or Java. In ToC, Language = A Set of Strings.
Capabilities: Can handle nested structures (like checking if brackets (( )) are balanced).
Capabilities: Very powerful. It models how modern computers work. It can perform high-level
computations.
4. Undecidable
What is it? The "Outer Space" of computation.
Meaning: These are problems that cannot be solved mechanically by any machine (like
Example 3, the infinite loop checker).
Here are your comprehensive and simplified study notes for the second lecture. 🎓📘
2️⃣ Prerequisites for Theory of Computation
Before we dive into building machines (like Finite State Machines), we need to learn the "language"
of this subject. Just like you learn ABCs before writing sentences, we need to learn about Symbols,
Alphabets, and Strings first.
A. Symbol
Definition: The most basic unit. It can be anything used to represent information.
Examples:
Letters: a , b , c
Digits: 0 , 1 , 2
Special characters: # , $
B. Alphabet (Σ)
Definition: A finite collection of symbols. We denote it using the Greek letter Sigma (Σ).
Examples:
Module 1 3
C. String
Definition: A sequence of symbols picked from the alphabet.
ab (A string of length 2)
2. What is a Language? 🗣️
Definition: A Language is essentially a Set of Strings.
How it works: You take an alphabet, make some strings, and group them together based on a
rule.
L2 Strings of length 3 {000, 001, 010, 011, … , 111} Finite Set (Ends)
L3 Strings that start with 0 {0, 00, 01, 000, 001, … } Infinite Set (Never ends)
Key Takeaway: A language can be Finite (has a limited number of strings) or Infinite (goes on
forever).
Result: {ϵ}
Module 1 4
4. Cardinality 🔢
Definition: Cardinality simply means the number of elements in a set.
Formula: If our alphabet has 2 symbols (like 0 and 1), the cardinality of Σn is 2n .
Example Check:
Definition: The set of ALL possible strings of ALL possible lengths over an alphabet.
Σ∗ = Σ0 ∪ Σ1 ∪ Σ2 ∪ Σ3 …
Expanded View:
5. Σ∗ : The set of everything possible you can make with that alphabet.
Module 1 5
Finite Automata (or Finite State Machines) are broadly divided into two categories based on
whether they produce an output or not.
Module 1 6
States (Circles): These represent the conditions or "memory" of the machine. Labeled as
A, B, C, Dor q0 , q1 , etc.
Transitions (Edges/Arrows): The lines connecting the circles. They show movement from one
state to another.
Inputs (Labels on Edges): The numbers or letters (like 0 , 1 ) written on the arrows. They tell the
machine when to move.
Example: An arrow from $A$ to $B$ labeled 1 means: "If you are in State A and see input 1,
go to State B."
Start State (Initial State): The state with an arrow coming from nowhere pointing to it. This is
where the machine begins.
Final State (Terminating State): The state with a Double Circle. If the machine stops here after
reading the input, the string is Accepted.
DF A = (Q, Σ, q0 , F , δ)
Example from
Symbol Name Explanation
Lecture
Q Set of States The complete list of all circles in the diagram. {A, B, C, D}
Σ Inputs (Alphabet) The symbols the machine can read. {0, 1}
The state where the machine begins (Initial
q0 Start State $A$
State).
$F$ Set of Final States The set of accepting states (Double Circles). {D}
Transition The rule book that maps (Current State + Input)
δ See Table Below
Function $\to$ Next State.
Formula for δ :
δ : Q × Σ → Q
Module 1 7
(Meaning: Take a State from $Q$ and an Input from Σl, and you get a resulting State in $Q$)
Start State: A
Input 0 goes
Current State Input 1 goes to...
to...
A (Start) C B
B D A
C A D
D (Final) B C
1. Problem Statement 🎯
We need to design a Deterministic Finite Automata (DFA) for a specific language L1 .
0 (Starts with 0)
00 , 01
Key Observation: As long as the first digit is 0, the rest doesn't matter.
1 (Starts with 1)
10 , 11
Module 1 8
100 ...
State B must be a Final State (Double Circle) because the string is now valid.
Since the string already started with 0, any character that comes after (whether 0 or 1)
doesn't change the fact that it started with 0.
So, we create a Self Loop on State B for inputs 0, 1 . It stays accepted forever.
Module 1 9
We create a Self Loop on State C for inputs 0, 1 . The machine is stuck here.
Definition: A state from which the machine can never reach the Final State.
Why use it? If the input becomes invalid (like starting with 1), we trap the machine in this state
so it can never say "Yes" (Accept).
B (Final) 0 Loop on B. B
B (Final) 1 Loop on B. B
Module 1 10
A (Start) 1 Wrong start! Move to Trap. C
00 , 01 , 10 , 11
Reason: These are the only strings with exactly two characters.
Length 1: 0 , 1
3. Step-by-Step Construction 🛠️
We will build the DFA based on the length of the string processed so far.
Module 1 11
Is this the final length? Yes!
From State C, if we read any more inputs ( 0 or 1 ), the length becomes 3. This is invalid.
5. Result: ACCEPTED ✅
Test 2: String 001 (Length 3 - Should
Reject)
1. Start at A.
2. Input 0: Move to B.
Module 1 12
4. Input 1: Move to D (Dead State). Test 3: String 1 (Length 1 - Should
5. End of string. We are at D (Not Final).
Reject)
2. Input 1: Move to B.
a. Result: REJECTED ❌
Here are your study notes for the advanced DFA construction example. 🎓📘
6️⃣ DFA Design: Strings NOT Containing a Substring
1. Problem Statement 🎯
Task: Construct a Deterministic Finite Automata (DFA) that accepts any string over the alphabet
{a, b}that does not contain the sequence "aabb".
Alphabet (Σ): {a, b}
Condition: If the sequence aabb appears anywhere in the string, Reject it. If it never appears,
Accept it.
The Process:
1. Simplify: First, design a DFA for the opposite problem: Accepts strings that DO contain "aabb".
2. Invert: Once that is done, we simply flip the states (Final ↔Non-Final).
Module 1 13
Step-by-Step Construction:
State A (Start):
If input is a : We have aaa . The last two are still aa , so we are still in the correct position.
Stay at C.
If input is a : Sequence broken ( aaba ). The last character is a . This is the start of a new
potential pattern. Go back to State B (Has seen a ).
Since we are designing the "Contains" machine, once we find aabb , we are happy.
Module 1 14
4. Part 2: The "Negative" DFA (Does NOT Contain aabb )
Now, we perform the Complement Operation to solve the original problem.
The Rule:
The Transformation:
State Original Role (Contains aabb) New Role (No aabb)
In the new machine, we accept everything as long as we don't reach the end. If the input completes
the pattern aabb, it falls into State E, which is now a reject state (Dead State), and the string is
rejected.
State E: This is the Reject State (Dead State). If the machine ever reaches here, it means
"aabb" was found, so we must reject the string.
Module 1 15
(i.e., what pattern of strings it accepts).
Step 2: At State B:
Pattern Recognized: Any string that starts with at least one 1 and ends with a 0 .
2. Strings starting with at least one '1' followed by any number of '1's, and ending with a '0'.
Module 1 16
Formal Definition:
Example: String 00 .
0
A C
Now at C, if we get another 0, where do we go? Nowhere! The diagram has no arrow for 0
from C.
00 (Stuck at C)
010 (Stuck at E)
1101 (Stuck at D)
In a strict DFA definition, every state must have a transition for every input. If the diagram doesn't
show them, the machine is technically incomplete.
We create a new state X (Dead State) and send all "missing" arrows there.
C 0 Go to X (Dead State)
2. Invalid Flows: If the input violates the pattern (like getting a 0 after 0 ), it goes to State X.
Module 1 17
3. State X: Loops forever. The string is rejected because X is not a double circle.
Concept: If you can build a DFA (Deterministic Finite Automata) or NFA (Non-Deterministic
Finite Automata) that accepts the valid strings of a language and rejects the invalid ones, that
language is Regular.
History: In previous lectures, we designed DFAs for specific patterns (like "strings starting with
0"). Those were all Regular Languages.
The Root Cause: FSMs fail for these languages because they require Memory.
1. Limited Memory: An FSM's "memory" is strictly limited to its current state. It knows "where
I am now," but it cannot remember "what exactly happened 100 steps ago."
3. Cannot Count: An FSM cannot count indefinitely. It can count small, fixed numbers (like "3
$a$'s"), but it cannot count to an unknown number $n$ (like "$n$ number of $a$'s").
Module 1 18
Example 1: The "Copy-Paste" Problem (Pattern Repetition)
Language Structure: w ⋅ w
Meaning: A string followed by the exact same string repeated.
Part 1: ababb
To verify this, the machine needs to memorize the entire first part ( ababb ) so it can check if
the second part matches it character by character.
Since an FSM has no storage (like a hard drive or RAM) to save the first string, it cannot
perform this check.
Sample Strings:
The machine sees a stream of 'a's. It must count them (e.g., "I have seen 452 $a$'s").
Then, when the '$b$'s start, it must verify that there are exactly 452 'b's.
Since $n$ can be any number (even infinity), the machine would need infinite states to keep
count. An FSM has Finite states.
4. Summary Table 📊
Feature Regular Language Non-Regular Language
Module 1 19
9️⃣Operations on Regular Languages
In the Theory of Computation, we often manipulate languages (sets of strings) to create new ones.
The three fundamental operations used to build Regular Languages are Union, Concatenation, and
Star.
B. Concatenation (∘)
Symbol: A ∘ B (or simply $AB$)
Simple Explanation: You can pick any number of strings from set A (including zero) and stick
them together in any order.
Important Note: The Star operation always includes ϵ(Epsilon/Empty string) because you can
choose to pick "zero" strings ($k=0$).
2. Practical Example 🧮
Let's define two small languages to see how these operations work in practice.
A ∪ B = {pq, r, t, uv}
Module 1 20
Take every item from A and attach every item from B to its end.
Length 1: pq, r
(Note: Regular Languages are also closed under the Star operation, though the lecture specifically
highlighted Union and Concatenation first.)
Module 1 21
One Input = One Path: In a DFA, if you are in a specific state and receive a specific input, there
is only one unique next state you can go to.
Example: If you are at State A and input is 1, you MUST go to State B. You cannot go
anywhere else.
Decision Making:
Module 1 22
Parallel Processing: Ideally, you can imagine the machine splitting into multiple copies and
taking ALL valid paths simultaneously.
Meaning: The machine can move from one state to another without reading any input (on an
empty string).
Rule: This feature is exclusive to NFA. You will NEVER see an ϵ-transition in a standard DFA.
Exactly one unique state for every Can have zero, one, or multiple next states for an
Next State
input. input.
Structure:
Input 1: Stays in A.
In a DFA, we must strictly define exactly one path for every input. In an NFA, we can just say "If it's
a 0, maybe go to the end?" and leave other parts undefined. This makes designing NFAs much
Module 1 23
simpler and more flexible.
2. Incomplete Transitions:
DFA Formula: δ : Q × Σ → Q
(One state + One input →One Result)
NFA Formula: δ : Q × Σ → 2Q
(One state + One input →A Set of Possible Results)
1. Go to {A}
2. Go to {B}
Module 1 24
4. Go to ∅(Nowhere/Dead State)
Total possibilities = 4.
Mathematical formula: 2number of states = 22 = 4.
If we had 3 States (A, B, C):
Possibilities: {A}, {B}, {C}, {A, B}, {B, C}, {A, C}, {A, B, C}, ∅.
Total = 23 = 8.
Conclusion:
Since an NFA allows us to go to any combination of states (or none), the transition function maps to
the Power Set (2Q ).
Transitions:
0
A {A, B}(Split)
1
A {A}
B : has no transitions (Dead end).
Step-by-Step Execution:
Module 1 25
2. Input '1':
Current States: { A }
3. Input '0':
4. Input '0':
Final States: { A, B }
Conclusion:
At the end of the string, the active states are {A, B}.
Result: Since at least one active state is Final, the string is ACCEPTED.
Step-by-Step Execution:
2. Input '0':
Current States: { A, B }
3. Input '1':
Module 1 26
Final States: { A }
Conclusion:
At the end of the string, the only active state is {A}.
Result: None of the active states are Final. The string is REJECTED.
Formal Rule:
🌟An NFA accepts a string if, after reading the entire input, the set of active states contains at
least one Final State.
Logic:
If any one of those paths succeeds (reaches a Final State), the whole string is considered
valid.
We don't care if other paths reached a dead end (ϕ) or a non-final state.
Summary Formula:
Let S be the set of states the machine is in after reading the string.
If S ∩F =
∅(Intersection with Final States is not empty) →ACCEPT
Otherwise →REJECT
Module 1 27
1. Analyzing the Logic
Valid Strings: 0 , 00 , 01 , 000 ... (Must start with 0).
2. Construction Steps
1. Start State (A):
Comparison:
1. Construction Steps
1. Start State (A): Represents Length 0.
Module 1 28
Input 0 or 1 : Move to State B.
Mark as Final.
Then it reads 1 .
Result: The machine enters a Dead Configuration and rejects the string.
Advantage: In a DFA, we would have needed an extra "Trap State" for length 3+. In NFA, we just
stop drawing! ✨
📝 Summary: DFA vs. NFA Design
Feature DFA Design NFA Design
Mandatory. Every state must have an arrow for Optional. You only draw the arrows
Completeness
every input (0 and 1). you need.
Complexity More complex, more states needed. Simpler, cleaner, fewer states.
🌟[Link]
watch the trick, and after making the NFA with this trick check it on few diverse examples.
Module 1 29
Logic:
Start State (A): We don't care what happens at the beginning. We can have any number of
0 s or 1 s. So, we loop 0, 1 on the start state.
The Magic Guess: When we see a 1 , the machine has two choices:
Final State (B): Once we reach B, we stop. If the string ends there, it's accepted.
Why NFA is cool here: In a DFA, we would need to handle what happens if a 0 comes after the 1
(go back to start). In an NFA, if a 0 comes after we moved to B, that path just "dies" (Dead
Configuration), and the machine relies on the path that stayed in A.
Logic:
Start State (A): The 0 can appear later, so we loop 0, 1 initially to skip any prefix.
The Trigger: When we see the specific symbol 0 , we can choose to move to State B.
Final State (B): Once we have found our 0 , the condition is met! We don't care what comes
after. So, we loop 0, 1 on the final state forever.
Module 1 30
Goal: The string must begin with 1 followed by 0 .
Logic:
Final State (C): Once we reach C, the condition ("Starts with 10") is satisfied. We don't care
about the rest of the string, so we loop 0, 1 on C forever.
Logic:
Start State (A): The pattern 01 can be anywhere. Loop 0, 1 to wait for it.
Detection:
Logic:
The Transition:
Module 1 31
Why? Because if we get more input after reaching C, then 11 was not the end.
By leaving C with no transitions, any extra input causes the branch to die (Dead
Configuration), ensuring we only accept if the string truly ends at 11 .
📝 Homework Assignment
The lecturer posed a challenge for you:
2. Question: What is the minimum number of states required for each DFA?
Hint: Try to trace the states logically. For example, "Ends with 11" usually requires 3 states in
NFA but might need the same or more in DFA to track overlaps (like 111 ).
Module 1 32