0% found this document useful (0 votes)
38 views49 pages

Suyog Toc Lab File

The document outlines a Continuous Assessment Sheet for a Theory of Computation course at the Institute of Technology & Management, Gwalior, detailing various experiments and programming tasks related to automata theory. Each experiment includes aims, theoretical explanations, diagrams, and code implementations for designing different types of finite automata and Turing machines. The document serves as a practical guide for students to understand and implement concepts in computation theory.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views49 pages

Suyog Toc Lab File

The document outlines a Continuous Assessment Sheet for a Theory of Computation course at the Institute of Technology & Management, Gwalior, detailing various experiments and programming tasks related to automata theory. Each experiment includes aims, theoretical explanations, diagrams, and code implementations for designing different types of finite automata and Turing machines. The document serves as a practical guide for students to understand and implement concepts in computation theory.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Institute of Technology & Management, Gwalior

Department of Computer Science & Engineering


Continuous Assessment Sheet (CAS)
Academic Year: 2024-25

Course Name: Theory of Computation

Subject Code:- CS 501

Name of Student:SuyogBundiwale Class & Batch: CSE (D) 5th Semester Roll No.: 0905CS221212

Exp Date Concept Progra Oral and Total Date of Signatur


No. of Underat m Report (15) Submissi e of
Exp. standing Execut Writing on Staff
Title of Experiment (5) ion and (5)
(5)

1 Design a DFA over the input set {a,b} accept all


the string starting with symbol a.

2 Design a DFA over the input set {0,1}.


Accept all the Strings Starting with 0 and
ending with 1.
3 Design a Program for creating machine that
accepts three consecutive one.
4 Design a Program for creating machine that
accepts the string always ending with 101.

5 Design a Program for Mode 3 Machine.

6 Design a program for accepting binary number


divisible by 2.

Design a program for creating a machine


7 which accepts string having even no. of 1’sand
0’s.

Design a program for creating a machine


8 which count number of 1’s and 0’s in a given
string.

9 Design a Program to find 2’s complement of a


given binary number.

10 Design a Program which will increment the


given binary number by 1.
11 Design a Program to convert NDFA to DFA.

12 Design a Program to create PDA machine that


accept the well-formed parenthesis.

Institute of Technology & Management, Gwalior

Design a PDA to accept WCWR where w is


13 any string and WR is reverse of that string
and C is a Special symbol.
14 Design a Turing machine that’s accepts the
following language a n b n c n where n>0.

Signature Signature Signature


Head of Department Course Teacher Student
Experiment: 1

Aim: Design a DFA over the input set {a,b} accept all the string starting with
symbol a.

Explanation: DFA or Deterministic Finite Automata is a finite state machine which accepts a
string(under some specific condition) if it reaches a final state, otherwise rejects
it. In DFA, there is no concept of memory, therefore we have to check the string character by character,
beginning with the 0th character. The input set of characters for the problem is {a, b}. For a DFA to be
valid, there must a transition rule defined for each symbol of the input set at every state to a valid state.

DFA Machine that accepts all strings that all the string starting with symbol a. For
the above problem statement, we must first build a DFA machine. DFA machine is similar to a
flowchart with various states and transitions. DFA machine corresponding to the above problem is
shown below:

Diagram:

Code:
#include <iostream>
#include <string>
using namespace std;

void q1(const string& s, int i); void


qd(const string& s, int i);

void q0(const string& s, int i) { if


(s.length() == i) { cout <<
"Rejected" << endl;
return;
} else if (s[i] == 'a') { q1(s,
i + 1);

1
} else { qd(s, i
+ 1);
}
}

void qd(const string& s, int i) { if


(s.length() == i) { cout <<
"Rejected" << endl;
return;
} else if (s[i] == 'a') { qd(s,
i + 1);
} else { qd(s, i
+ 1);
}
}

void q1(const string& s, int i) { if


(s.length() == i) { cout <<
"Accepted" << endl; return;
} else if (s[i] == 'a') { q1(s,
i + 1);
} else { q1(s, i
+ 1);
}
}

int main() { string


s;
cout << "Enter a string: ";
cin >> s; q0(s, 0); return
0;
}
Output:

3
Experiment: 2

Aim: Design a DFA over the input set {0,1}. accept all the Strings Starting with 0
and ending with 1.

Theory:
• Step 1 − q0 is the initial state on input ‘0’ it goes to q2, and input 1 at q2 goes to q1 which is the final
state, and ‘01’ string is accepted.
• Step 2 − q0 on ‘1’ goes to q3 which is dead state because for q3 there is no path to reach to the final
state.
• Step 3 – q2 on input ‘0’ will remain in q2.

Diagram:

Code:
#include <iostream>
#include <string>
using namespace std;

void q1(const string& s, int i);


void qd(const string& s, int i);
void q2(const string& s, int i);

void q0(const string& s, int i) { if


(s.length() == i) { cout <<
"Rejected" << endl; return;
5
} else if (s[i] == '1') { qd(s,
i + 1);
} else { q1(s, i
+ 1);
}
}

void q1(const string& s, int i) { if


(s.length() == i) { cout <<
"Rejected" << endl;
return;
} else if (s[i] == '1') { q2(s,
i + 1);
} else { q1(s, i
+ 1);
}
}

void qd(const string& s, int i) { if


(s.length() == i) { cout <<
"Rejected" << endl;
return;
} else if (s[i] == '1') { qd(s,
i + 1);
} else { qd(s, i
+ 1);
}
}

void q2(const string& s, int i) { if


(s.length() == i) { cout <<
"Accepted" << endl; return;
} else if (s[i] == '1') { q2(s,
i + 1);
} else { q1(s, i
+ 1);
}
}

int main() { string s; cout <<


"Enter a string: "; cin >>
s; q0(s, 0); return 0;
}

7
Output:
9
Experiment: 3

Aim: Design a Program for creating machine that accepts three consecutive
ones.

Diagram:

Code:
#include <iostream>
#include <string>
using namespace std;

void q1(const string& s, int i);


void q2(const string& s, int i);
void q3(const string& s, int i);

void q0(const string& s, int i) { if


(s.length() == i) { cout <<
"Rejected" << endl;
return;
} else if (s[i] == '1') { q1(s,
i + 1);
} else { q0(s, i
+ 1);
}
}

void q1(const string& s, int i) { if


(s.length() == i) { cout <<
"Rejected" << endl;
return;
} else if (s[i] == '1') { q2(s,
i + 1);

10
} else { q0(s, i
+ 1);
}
}

void q2(const string& s, int i) { if


(s.length() == i) { cout <<
"Rejected" << endl;
return;
} else if (s[i] == '1') {
q3(s, i + 1);
} else { q0(s, i
+ 1);
}
}

void q3(const string& s, int i) { if


(s.length() == i) { cout <<
"Accepted" << endl; return;
} else if (s[i] == '1') {
q3(s, i + 1);
} else { q3(s, i
+ 1);
}
}

int main() { string


s;
cout << "Enter a string: ";
cin >> s; q0(s, 0); return
0;
}
Output:

12
Experiment: 4
Aim: Design a program for creating a machine that accepts the string
always ending with 101.

Diagram:

Code:
#include <iostream>
#include <string>
using namespace std;

void q1(const string& s, int i);


void q2(const string& s, int i);
void q3(const string& s, int i);

void q0(const string& s, int i) { if


(s.length() == i) { cout <<
"Rejected" << endl;
return;
} else if (s[i] == '1') { q1(s,
i + 1);
} else { q0(s, i
+ 1);
}
}

14
void q1(const string& s, int i) {
if (s.length() == i) { cout <<
"Rejected" << endl;
return;
} else if (s[i] == '1') { q1(s,
i + 1);
} else { q2(s, i
+ 1);
}
}

void q2(const string& s, int i) { if


(s.length() == i) { cout <<
"Rejected" << endl;
return;
} else if (s[i] == '1') { q3(s,
i + 1);
} else { q0(s, i
+ 1);
}
}

void q3(const string& s, int i) { if


(s.length() == i) { cout <<
"Accepted" << endl; return;
} else if (s[i] == '1') { q1(s,
i + 1);
} else { q2(s, i
+ 1);
}
}

int main() { string


s;
cout << "Enter a string: ";
cin >> s; q0(s, 0); return
0;
}
Output:

16
Experiment: 5

Aim: Design a Program for Mode 3 Machine.

Diagram:

Code:
#include <iostream>
#include <string>
using namespace std;

void q1(const string& s, int i); void


q2(const string& s, int i);

void q0(const string& s, int i) { if


(s.length() == i) {
cout << "Accepted" << endl;
return;
} else if (s[i] == '1') { q1(s,
i + 1);
} else { q0(s, i
+ 1);
}
}

void q1(const string& s, int i) { if


(s.length() == i) {
cout << "Rejected" << endl;
return;
} else if (s[i] == '1') { q0(s,
i + 1);

18
} else { q2(s, i
+ 1);
}
}
void q2(const string& s, int i) { if
(s.length() == i) {
cout << "Rejected" << endl; return;
} else if (s[i] == '1') { q2(s,
i + 1);
} else { q1(s, i
+ 1);
}
}

int main() {
string s; cout << "Enter a
string: "; cin >> s; q0(s, 0);
return 0; }
Output:

20
Experiment: 6

Aim: Design a program for accepting binary numbers divisible by 2.


Diagram:

Code:

#include <iostream>
#include <string>

using namespace std; void

q1(const string& s, int i);

void q0(const string& s, int i) { if


(s.length() == i) { cout <<
"Accepted" << endl;
return; }
if (s[i] == '1') { q1(s,
i + 1);
} else { q0(s, i
+ 1);
}
}

void q1(const string& s, int i) { if


(s.length() == i) { cout <<
"Rejected" << endl;
return; }
if (s[i] == '1') { q1(s,
i + 1);
} else { q0(s, i
+ 1);
}

21
}

int main() {
string s; cout << "Enter a
string: "; cin >> s; q0(s,
0); return 0;
}
Output:

22
7

a program for creating a machine which accepts string having even


number of 1’s and 0’s. Diagram:

Code:
#include <iostream>
#include <string>
using namespace std;

void q1(const string& s, int i);


void q2(const string& s, int i);
void q3(const string& s, int i);

void q0(const string& s, int i) { if


(s.length() == i) { cout <<
"Accepted" << endl;
return;
} else if (s[i] == '1') { q2(s,
i + 1);
} else { q1(s, i
+ 1);
}
}
Experiment:

Aim: Design
void q1(const string& s, int i) {
if (s.length() == i) { cout <<
"Rejected" << endl;

24
return;
} else if (s[i] == '1') { q3(s,
i + 1);
} else { q0(s, i
+ 1);
}
}

void q2(const string& s, int i) { if


(s.length() == i) { cout <<
"Rejected" << endl;
return;
} else if (s[i] == '1') { q0(s,
i + 1);
} else { q3(s, i
+ 1);
}
}

void q3(const string& s, int i) { if


(s.length() == i) { cout <<
"Rejected" << endl;
return;
} else if (s[i] == '1') { q1(s,
i + 1);
} else { q2(s, i
+ 1);
}
}

int main() { string


s;
cout << "Enter a string: ";
cin >> s; q0(s, 0); return
0;
}
Output:

15
Experiment:

Aim: Design

26
8

a program for creating a machine which counts number of 1’s and


0’s in a given string Code:
#include <iostream>
#include <string>

using namespace std; void q1(const string&

s, int i, int c0, int c1);

void q0(const string& s, int i, int c0, int c1) { if


(s.length() == i) { cout << "Number of 0's =
" << c0 << endl; cout << "Number of 1's = "
<< c1 << endl; return;
} else if (s[i] == '1') {
c1 = c1 + 1; q1(s, i
+ 1, c0, c1);
} else { c0 = c0 + 1;
q0(s, i + 1, c0, c1);
}
}

void q1(const string& s, int i, int c0, int c1) { if


(s.length() == i) { cout << "Number of 0's = "
<< c0 << endl; cout << "Number of 1's = " <<
c1 << endl; return;
} else if (s[i] == '1') {
c1 = c1 + 1; q1(s, i
+ 1, c0, c1);
} else { c0 = c0 + 1;
q0(s, i + 1, c0, c1);
}
}

int main() { string


s;
cout << "Enter a string: ";
cin >> s; int count_0 = 0;
int count_1 = 0; q0(s, 0,
count_0, count_1);
return 0;
}
Experiment:

Aim: Design
Output:

28
9

a program to find 2’s complement of a given binary number.

Diagram:

Code:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

void q1(const string& s, int i); void


q0(const string& s, int i);

void q0(const string& s, int i) { if (s.length()


== 0 || s.length() == i) return; if (s[i] == '1')
{ q1(s, i + 1); cout << '1';
} else { q0(s, i
+ 1); cout
<< '0';
}
}

void q1(const string& s, int i) { if (s.length()


== 0 || s.length() == i) return; if (s[i] == '1')
{ q1(s, i + 1); cout << '0';
} else { q1(s, i
+ 1); cout
<< '1';
}

29
Experiment:

Aim: Design
}

string my_function(const string& x) {


string reversed = x;
reverse(reversed.begin(), reversed.end());
return reversed;
}

int main() {
string mystr; cout <<
"Enter a string: "; cin >>
mystr;

string s = my_function(mystr);
q0(s, 0); cout
<< endl;
return 0;
}
Output:

30
Experiment: 10

Aim: Design a program which will increment the given binary number by
1. Diagram:

Code:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

void q2(const string& s, int i);


void q1(const string& s, int i);
void q0(const string& s, int i);

void q0(const string& s, int i) { if (s.length()


== 0 || s.length() == i) return; if (s[i] == '1')
{ q2(s, i + 1); cout << '0';
} else { q1(s, i
+ 1); cout
<< '1';
}
}

void q1(const string& s, int i) { if (s.length()


== 0 || s.length() == i) return; if (s[i] == '1')
{ q1(s, i + 1); cout << '1';

31
} else { q1(s, i
+ 1); cout
<< '0';
}
}

void q2(const string& s, int i) { if (s.length()


== 0 || s.length() == i) return; if (s[i] == '1')
{ q2(s, i + 1); cout << '0';
} else { q1(s, i
+ 1); cout
<< '1';
}
}

string my_function(const string& x) { string


reversed = x;
reverse(reversed.begin(), reversed.end()); return
reversed;
}

int main() { string


mystr;
cout << "Enter a string: "; cin
>> mystr;

string s = my_function(mystr);
q0(s + '0', 0);
cout << endl;
return 0;
}
Output:

21
Experiment:11
Aim:- Design a Program to convert NDFA to DFA.

Theory:- Conversion from NFA to DFA

In NFA, when a specific input is given to the current state, the machine goes to multiple states. It can have zero,
one or more than one move on a given input symbol. On the other hand, in DFA, when a specific input is given
to the current state, the machine goes to only one state. DFA has only one move on a given input symbol.

Let, M = (Q, ∑, δ, q0, F) is an NFA which accepts the language L(M). There should be equivalent DFA denoted by
M' = (Q', ∑', q0', δ', F') such that L(M) = L(M').

Steps for converting NFA to DFA:


Step 1: Initially Q' = ϕ

Step 2: Add q0 of NFA to Q'. Then find the transitions from this start state.

Step 3: In Q', find the possible set of states for each input symbol. If this set of states is not in Q', then add it to
Q'.

Step 4: In DFA, the final state will be all the states which contain F(final states of NFA).

CODE:-
import pandas as pd

# Taking NFA input from User


nfa = {}
n = int(input("No. of states: ")) # Enter total no. of states t =
int(input("No. of transitions: ")) # Enter total no. of transitions/paths

# Input states and transitions for i in range(n): state = input("State


name: ") # Enter state name (e.g., A, B, C) nfa[state] = {} for j in
range(t): path = input("Path: ") # Enter path (e.g., a or b)
print(f"Enter end state from state {state} travelling through path {path}: ") reaching_state
= [x for x in input().split()] # Enter all the end states nfa[state][path] = reaching_state #
Assigning the end states to the paths in dictionary

# Printing NFA print("\nNFA:\n")


print(nfa)
# Creating NFA table
print("\nPrinting NFA table:")
nfa_table = pd.DataFrame(nfa)
print(nfa_table.transpose())

# Input final states of NFA print("Enter final state of NFA: ") nfa_final_state =
[x for x in input().split()] # Enter final state/states of NFA

# Preparing for DFA conversion


new_states_list = [] # Holds all the new states created in DFA dfa
= {} # DFA dictionary/table keys_list = list(nfa.keys()) #
Contains all the states in NFA path_list =
list(nfa[keys_list[0]].keys()) # List of all the paths

# Computing first row of DFA transition table dfa[keys_list[0]] = {} for y in range(t): var =
"".join(nfa[keys_list[0]][path_list[y]]) # Creating a single string from all the elements of the list
dfa[keys_list[0]][path_list[y]] = var # Assigning the state in DFA table
if var not in keys_list: # If the state is newly created
new_states_list.append(var) # Append it to the new_states_list
keys_list.append(var) # Append to keys_list

# Computing the other rows of DFA transition table while new_states_list:


dfa[new_states_list[0]] = {} # Taking the first element of the new_states_list for i in
range(len(path_list)): temp = [] # Creating a temporary list for j in
range(len(new_states_list[0])): temp += nfa[new_states_list[0][j]][path_list[i]] # Taking
the union of the states
s = "".join(temp) # Creating a single string (new state) from all the elements of the list if s
not in keys_list: # If the state is newly created new_states_list.append(s) # Append it to
the new_states_list keys_list.append(s) # Append to keys_list
dfa[new_states_list[0]][path_list[i]] = s # Assigning the new state in the DFA table

new_states_list.remove(new_states_list[0]) # Removing the first element in the new_states_list

# Printing DFA
print("\nDFA:\n") print(dfa)

# Creating DFA table


print("\nPrinting DFA table:")
dfa_table = pd.DataFrame(dfa)
print(dfa_table.transpose())

# Finding final states of the DFA


dfa_states_list = list(dfa.keys())
dfa_final_states = [] for x in
dfa_states_list:
for i in x:
if i in nfa_final_state:
dfa_final_states.append(x) break

print("\nFinal states of the DFA are:", dfa_final_states) # Printing Final states of DFA
OUTPUT: -
12

Aim: Design a Program to create PDA machine that accept the well-formed parenthesis.
Theory: As per the AIM, set of valid strings that can be generated by given language is represented
in set A:
A = {(),(()), ()(), (()), ((()(()))) ...}
means all the parenthesis that are open must closed or combination of all legal parenthesis
formation. Here, opening par is '(' and closing parenthesis is ')'. Block diagram of push down
automata is shown in Figure 1.

Input string can be valid or invalid, valid if the input string follow set A (define above). PDA has to
determine whether the input string is according to the language or not.

Let M be the PDA machine for above AIM, hence it can be define as M(Q, Σ, Г, δ, q0, Z0, F) where
Q: set of states: {q0, q1}
Experiment:

Σ: set of input symbols: {(, )}


Г: Set of stack symbols: {(, Z}
q0: initial state (q0)
Z0: initial stack symbol (Z)
F: set of Final states: { } [Note: Here, set of final states is null as decision of validity of string is
based on stack whether it is empty or not. If empty means valid else invalid.] δ: Transition
Function:
δ(q0, (, Z) → (q0, (Z)
δ(q0, (, () → (q0, (()
δ(q0, ), () → (q0, ε)
δ(q0, ε, Z) → (q1, ε)

Rules for implementing PDA for a given language


Initial Setup: Load the input string in input buffer, push Z as an initial stack symbol and
consider the machine in at initial state q0. Rules:

1. It is must that the first symbol should be '('.


2. If the input symbol of string is '(' and stack top is Z then push symbol '(' into stack and read
the next character in input string.
3. If next character is again '(', then push '(' again in stack and repeat the same process for all
'(' in input string.
4. If character is ')' and stack top is '(', then pop '(' from stack.
5. If all the characters of input string are parsed and stack top is Z, it means string is valid, pop
Z from stack and change the state from q0 to q1.
13

Aim: Design a PDA to accept WCWR where w is any string and WR is reverse of that string and C is a
Special symbol.

Theory: Approach used in this PDA –


Keep on pushing 0’s and 1’s no matter whatever is on the top of stack until reach the middle element. When
middle element ‘c’ is scanned then process it without making any changes in stack. Now if scanned symbol is ‘1’
and top of stack also contain ‘1’ then pop the element from top of stack or if scanned symbol is ‘0’ and top of
stack also contain ‘0’ then pop the element from top of stack. If string becomes empty or scanned symbol is ‘$’
and stack becomes empty, then reach to final state else move to dead state.

• Step 1: On receiving 0 or 1, keep on pushing it on top of stack without going to next state.

• Step 2: On receiving an element ‘c’, move to next state without making any change in stack.

• Step 3: On receiving an element, check if symbol scanned is ‘1’ and top of stack also contain ‘1’ or if symbol
scanned is ‘0’ and top of stack also contain ‘0’ then pop the element from top of stack else move to dead state.
Keep on repeating step 3 until string becomes empty.

• Step 4: Check if symbol scanned is ‘$’ and stack does not contain any element then move to final state else
move to dead state.

Examples:
Input : 1 0 1 0 1 0 1 0 1
Output :ACCEPTED
Experiment:

Input : 1 0 1 0 1 1 1 1 0
Output :NOT ACCEPTED
14

Aim: Design a Turing machine that’s accepts the following language a n b n c n where n>0.

Steps:

1. Mark 'a' then move right.


2. Mark 'b' then move right
3. Mark 'c' then move left
4. Come to far left till we get 'X'
5. Repeat above steps till all 'a', 'b' and 'c' are marked
6. At last if everything is marked that means string is accepted.

State Transition Diagram:

We have designed state transition diagram for anbncn | n ≥ 1 as follows:

Following Steps:

a. Mark 'a' with 'X' and move towards unmarked 'b'


b. Move towards unmarked 'b' by passing all 'a's
c. To move towards unmarked 'b' also pass all 'Y's if exist

Following Steps:

a. Mark 'b' with 'Y' and move towards unmarked 'c'


b. Move towards unmarked 'c' by passing all 'b's
c. To move towards unmarked 'c' also pass all 'Z's if exist
Experiment:

Following Steps:

a. Mark 'c' with 'Z' and move towards first 'X' (in left)
b. Move towards first 'X' by passing all 'Z's, 'b's, 'Y's and 'a's
c. When 'X' is reached just move one step right by doing nothing.

To check all the 'a's, 'b's and 'c's are over add loops for checking 'Y' and 'Z' after "we get 'X' followed by
'Y'"
To reach final state(qf) just replace BLANK with BLANK and move either direction

You might also like