This repository contains practical implementations for the Theory of Computation (TOC) course, part of the B.Sc. (Hons.) Computer Science curriculum, 5th Semester, Delhi University.
This collection includes C++ implementations of various computational models including:
- Finite Automata (FA)
- Pushdown Automata (PDA)
- Turing Machines (TM)
Each practical demonstrates fundamental concepts in automata theory and formal languages through working simulations.
File: P1.cpp
Design a Finite Automata that accepts all strings over Ξ£={0, 1} having three consecutive 1's as a substring.
Example:
- Input:
111β Accepted - Input:
0111β Accepted - Input:
1101β Rejected
File: P2.cpp
Design a Finite Automata that accepts strings over Ξ£={0, 1} having either exactly two 1's or exactly three 1's, not more nor less.
Example:
- Input:
110β Accepted (two 1's) - Input:
1011β Rejected (three 1's but needs to be exactly 2 or 3)
File: P3.cpp
Design a Finite Automata that accepts language L1 over Ξ£={a, b}, comprising of all strings (of length 4 or more) having first two characters same as the last two.
Example:
- Input:
ababβ Accepted - Input:
aabbaaβ Accepted - Input:
abbaβ Rejected
File: P4.cpp
Design a Finite Automata that accepts language L2 over Ξ£={a, b} where L2 = a(a+b)*b.
Example:
- Input:
abβ Accepted - Input:
aabβ Accepted - Input:
baβ Rejected
File: P5.cpp
Design a Finite Automata that accepts the EVEN-EVEN language over Ξ£={a, b} (strings with even number of a's and even number of b's).
Example:
- Input: `` (empty string) β Accepted
- Input:
aabbβ Accepted - Input:
abaβ Rejected
File: P6.cpp
Write a program to simulate an FA that performs:
- a. Union of languages L1 and L2
- b. Intersection of languages L1 and L2
- c. Concatenation L1L2
Example:
- L1 = {a, ab}
- L2 = {b, ab}
- Union: {a, ab, b}
- Intersection: {ab}
- Concatenation: {ab, aab, abb, abab}
File: P7.cpp
Design a Pushdown Automata that accepts the language {a^n b^n | n > 0, Ξ£={a, b}}.
Example:
- Input:
abβ Accepted - Input:
aabbβ Accepted - Input:
aaabβ Rejected
File: P8.cpp
Design a Pushdown Automata that accepts the language {wXw^r | w is any string over Ξ£={a, b} and w^r is reverse of that string and X is a special symbol}.
Example:
- Input:
aXaβ Accepted - Input:
abXbaβ Accepted - Input:
abXabβ Rejected
File: P9.cpp
Design and simulate a Turing Machine that accepts the language a^n b^n c^n where n > 0.
Example:
- Input:
abcβ Accepted - Input:
aabbccβ Accepted - Input:
aabbcβ Rejected
File: P10.cpp
Design and simulate a Turing Machine which increments a given binary number by 1.
Example:
- Input:
1010β Output:1011 - Input:
1111β Output:10000 - Input:
0β Output:1
- C++ Compiler (g++, clang, or any C++11 compatible compiler)
- Basic understanding of:
- Automata theory
- Finite state machines
- Regular languages
- Context-free languages
Using g++:
g++ P1.cpp -o P1Using clang++:
clang++ P1.cpp -o P1On Linux/macOS:
./P1On Windows:
P1.exeYou can combine compilation and execution:
g++ P1.cpp && ./a.out$ g++ P1.cpp -o P1
$ ./P1
Enter string: 0111
Read 0 -->String 1
Read 1---> String 2
Read 1---> String 3
String accepted$ g++ P5.cpp -o P5
$ ./P5
Enter string: aabb
Read a ---> q1
Read a ---> q0
Read b ---> q2
Read b ---> q0
String accepted (even a's, even b's)$ g++ P7.cpp -o P7
$ ./P7
Enter a string of a's followed by b's (e.g., aabb): aabb
String is accepted by the PDA (in the language a^n b^n, n>0).$ g++ P10.cpp -o P10
$ ./P10
Enter binary number: 1111
Result: 10000.
βββ P1.cpp # FA: Three consecutive 1's
βββ P2.cpp # FA: Exactly two or three 1's
βββ P3.cpp # FA: First two same as last two
βββ P4.cpp # FA: Language a(a+b)*b
βββ P5.cpp # FA: EVEN-EVEN language
βββ P6.cpp # FA: Set operations (union, intersection, concatenation)
βββ P7.cpp # PDA: a^n b^n language
βββ P8.cpp # PDA: Palindrome with middle marker
βββ P9.cpp # TM: a^n b^n c^n language
βββ P10.cpp # TM: Binary increment
βββ LICENSE # Apache License 2.0
βββ README.md # This file
By working through these practicals, students will:
- Understand the design and implementation of Finite Automata
- Learn to simulate DFA and NFA
- Implement Pushdown Automata for context-free languages
- Build basic Turing Machine simulations
- Gain practical experience with formal language theory
- Develop skills in computational problem-solving
- All programs are written in C++ and follow standard conventions
- Programs use interactive input/output for testing
- Each file contains comments describing the problem statement
- State transitions are implemented using recursive function calls
- Some programs provide detailed trace output showing state transitions
This is an educational repository for Delhi University students. If you find any bugs or have suggestions for improvements:
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Ananya Srivastava
Created for B.Sc. (Hons.) Computer Science students, Delhi University
Course: Theory of Computation
Semester: 5th
University: Delhi University
Program: B.Sc. (Hons.) Computer Science