TOC (CS 501) Lab Manual Compressed
TOC (CS 501) Lab Manual Compressed
“Theory of Computation”
SEMESTER –V
TOC LAB [2022]
LABORATORY PLAN
Actual
Exp CO HOD
Experiment Date of Remarks
No. Verification
Completion
number.
LAB MANUAL
Theory of Computation
EXPERIMENT-1
AIM: Design a Program for creating machine that accepts the string always ending
with 101.
Discussion:
State Input
- 0 1
Q0 Q0 Q1
Q1 Q2 Q1
Q2 Q0 Q3
Q3 Q0 Q1
TOC LAB [2022]
Transition Diagram
Code in C++
#include<iostream>
using namespace std;
void q0(string,int);
void q1(string,int);
void q2(string,int);
void q3(string,int);
return;
}
if(s[i]=='0')
q2(s,i+1);
else
q1(s,i+1);
}
void q2(string s, int i)
{
cout<<"q2->";
if(i==s.length())
{
cout<<"\n Rejected";
return;
}
if(s[i]=='0')
q0(s,i+1);
else
q3(s,i+1);
}
void q3(string s, int i)
{
cout<<"q3->";
if(i==s.length())
{
cout<<"\n Given String is accepted";
return;
}
if(s[i]=='0')
q0(s,i+1);
else
q1(s,i+1);
}
int main()
{
string s;
cout<<"Enter the string (0 or 1)";
cin>>s;
cout<<"The state of transition are :";
q0(s,0);}
TOC LAB [2022]
EXPERIMENT 2
AIM: Design a Program for creating machine that accepts three consecutive one.
Discussion:
State Input
- 0 1
Q0 Q0 Q1
Q1 Q0 Q2
Q2 Q0 Q3
Q3 Q3 Q3
Transition Diagram
TOC LAB [2022]
#include<iostream>
using namespace std;
void q0(string,int);
void q1(string,int);
void q2(string,int);
void q3(string,int);
}
if(s[i]=='0')
q0(s,i+1);
else
q3(s,i+1);
}
void q3(string s, int i)
{
cout<<"q3->";
if(i==s.length())
{
cout<<"\n Given String is accepted";
return;
}
if(s[i]=='0')
q3(s,i+1);
else
q3(s,i+1);
}
int main()
{
string s;
cout<<"Enter the string (0 or 1)";
cin>>s;
cout<<"The state of transition are :";
q0(s,0);
}
TOC LAB [2022]
EXPERIMENT 3
AIM: Design a Program for Mode 3 Machine
Discussion:
State Input
- 0 1
Q0 Q0 Q1
Q1 Q2 Q0
Q2 Q1 Q2
Transition Diagram
TOC LAB [2022]
#include<iostream>
using namespace std;
void q0(string,int);
void q1(string,int);
void q2(string,int);
if(s[i]=='0')
q1(s,i+1);
else
q2(s,i+1);
}
int main()
{
string s;
cout<<"Enter the string (0 or 1)";
cin>>s;
cout<<"The state of transition are :";
q0(s,0);
}
TOC LAB [2022]
EXPERIMENT 4
AIM: Design a program for accepting decimal number divisible by 2.
Discussion:
As per the AIM, set of valid strings are represented by set A:
A = {0,1,2,3,4,5,6,7,8,9}.
means any binary string that when divide by two gives remainder zero. Let M be the
machine for above AIM, hence it can be define as M(Q, Σ, 𝛿, q0, F) where
Q: set of states: {q, q0}
Σ: set of input symbols: {0, 1}
q0: initial state {q0}
F: set of Final states: {q0}
𝛿: Transition Function: (Transition state diagram is shown in Figure 4.)
State Input
- 0,2,4,6,8 1,3,5,7,9
Q0 Q0 Q1
Q1 Q2 Q0
Transition Diagram
Figure 1: FSM - accepting binary string if divisible by 2
TOC LAB [2022]
#include<iostream>
using namespace std;
void q0(string,int);
void q1(string,int);
EXPERIMENT 5
AIM: Design a program for creating a machine which accepts string having equal no. of 1’s and
0’s.
Discussion:
Row State Input ⸹(transition function) Stack(leftmost symbol represents top of stack) Stack after move
1 q0 0 ⸹(0,Z/0Z) 0 q0
2 q0 0 ⸹(0,0/00) 0 q0
3 q0 0 ⸹(0,1/€) € q0
4 q0 1 ⸹(1,Z/1Z) 1 q0
5 q0 1 ⸹(1,1/11) 1 q0
6 q0 1 ⸹(1,0/€) € q0
7 q0 € ⸹(€,Z/Z) q1
Fig:1-Transition Diagram
Solution:-
TOC LAB [2022]
#include<iostream>
#include<string.h>
using namespace std;
int top;
char s[10];
class Stack
{
public:
void push(int x)
{
s[top++]=x;
}
void pop(int x)
{
s[top--]=x;
}
};
int main()
{
int i,n;
char a[10];
cout<<"\nProgram For PDA Which Accpets Strings Of (0^n)(1^n)\n";
cout<<"\nEnter String::";
cin>>a;
n=strlen(a);
Stack st;
top=-1;
for(i=0;i<n;i++)
{
if(a[i]=='0' || a[i]=='1')
{
if(a[i]=='0')
{
st.push(a[i]);
}
else
{
st.pop(a[i]);
}
}
else
TOC LAB [2022]
{
break;
}
}
if(top==-1)
{
cout<<"\nString Accepted.\n";
}
else
{
cout<<"\nString Rejected.\n";
}
return 0;
}
TOC LAB [2022]
EXPERIMENT 6
AIM: Design a program for creating a machine which counts number of 1’s and 0’s in a given
string.
Discussion:
Next state
→A A y B x
B B y B x
Figure 1: FSM – Moore Machine for counting no. of 0’s and 1’s
TOC LAB [2022]
Solution:-
#include<iostream>
using namespace std;
void A(string,int);
void B(string,int);
int z=0;
int o=0;
if(i==s.length())
{
cout<<"\n no of zeros are"<<z;
cout<<"\n no. of ones are"<<o;
cout<<"\n thanku ";
return;
}
cout<<"\n A->";
if(s[i]=='0')
{
z++;
cout<<"\n out put is y";
A (s,i+1);
}
else
{
o++;
cout<<"\n output is x";
B(s,i+1);
}
}
void B(string s, int i)
{
if(i==s.length())
{
TOC LAB [2022]
}
int main()
{
string s;
cout<<"Enter the string (0 or 1)";
cin>>s;
cout<<"The state of transition are :";
A(s,0);
}
TOC LAB [2022]
EXPERIMENT 7
Discussion:
Next state
→ q1 q1 0 q2 1
q2 q2 1 q3 0
q3 q2 1 q3 0
Transition Table
Solution:-
#include<iostream>
using namespace std;
void q1(string,int);
void q2(string,int);
void q3(string,int);
int a[10], j;
void q1(string s, int i)
{
if(i==s.length())
{
cout<<"\n 2's complement of the given stringis : ";
for(j=s.length()-1;j>=0;j--)
cout<<a[j];
return;
}
cout<<"\n q1->";
if(s[i]=='0')
{
cout<<"\n out put is 0";
a[j]=0;
j++;
q1(s,i+1);
}
else
{
cout<<"\n output is 1";
a[j]=1;
j++;
q2(s,i+1);
}
}
void q2(string s, int i)
{
if(i==s.length())
TOC LAB [2022]
}
void q3(string s, int i)
{
if(i==s.length())
{
cout<<"\n 2's complement of the given stringis : ";
for(j=s.length()-1;j>=0;j--)
cout<<a[j];
return;
}
cout<<"\n q2->";
if(s[i]=='0')
{
cout<<"\n output is 1";
a[j]=1;
j++;
q2(s,i+1);
TOC LAB [2022]
}
else
{
cout<<"\n output is 0";
a[j]=0;
j++;
q3(s,i+1);
}
}
int main()
{
string s;
cout<<"Enter the string (0 or 1)";
cin>>s;
string rev = string(s.rbegin(),s.rend());
EXPERIMENT 8
AIM: Design a Program which will increment the given binary number by 1.
Discussion:
Next state
→ q0 q1 1 q0 0
q1 q0 0 q1 1
Transition Table
Solution:-
#include<iostream>
using namespace std;
void q0(string,int);
void q1(string,int);
void q2(string,int);
int a[10], j;
void q0(string s, int i)
{
if(i==s.length())
{
cout<<"\n binary string after incremented by 1 : ";
for(j=s.length()-1;j>=0;j--)
cout<<a[j];
return;
}
cout<<"\n q1->";
if(s[i]=='0')
{
cout<<"\n out put is 1";
a[j]=1;
j++;
q1(s,i+1);
}
else
{
cout<<"\n output is 0";
a[j]=0;
j++;
q0(s,i+1);
}
}
void q1(string s, int i)
{
if(i==s.length())
TOC LAB [2022]
}
void q2(string s, int i)
{
if(i==s.length())
{
cout<<"\n binary string after incremented by 1 : ";
for(j=s.length()-1;j>=0;j--)
cout<<a[j];
return;
}
cout<<"\n q2->";
if(s[i]=='0')
{
cout<<"\n out put is 0";
a[j]=0;
j++;
q2(s,i+1);
}
TOC LAB [2022]
else
{
cout<<"\n output is 1";
a[j]=1;
j++;
q1(s,i+1);
}
}
int main()
{
string s;
cout<<"Enter the string (0 or 1)";
cin>>s;
string rev = string(s.rbegin(),s.rend());
cout<<"The state of transition are :";
q0(rev,0);
}
TOC LAB [2022]
EXPERIMENT 9
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
void print_dfa() {
cout << "\n DFA Table:\n";
cout << "================\n";
cout << "Q\t";
for(int j=0; j<m; j++) {
cout << j << "\t";
}
cout << endl;
for(int i=0; i<tot; i++) {
cout << "[";
for(int k=0; k < ds[i].size(); k++) cout << ds[i][k]; cout << "]\t";
for(int j=0; j<m; j++) {
cout << "[";
for(int k=0; k<ds[dt[i][j]].size(); k++) {
cout << ds[dt[i][j]][k];
}
cout << "]\t";
}
cout << endl;
}
cout << endl;
}
int main() {
TOC LAB [2022]
/*
Input:
21
11
11
01
010
21
33
2
*/
queue<int> q;
while(!q.empty()) {
sort(cur.begin(), cur.end());
cur.resize(unique(cur.begin(), cur.end()) - cur.begin());
}
}
print_dfa();
return 0;
}
TOC LAB [2022]
EXPERIMENT 10
AIM: Design a Program to create PDA machine that accept the well-formed parenthesis
Discussion:
Row State Input ⸹(transition function) Stack(leftmost symbol represents top of stack) Stack after move
1 q0 ( ⸹((,Z/0Z) ( q0
2 q0 ( ⸹((,(/(() ( q0
3 q0 ( ⸹((,)/€) € q0
4 q0 1 ⸹(),)/))) ) q0
5 q0 1 ⸹(),(/€) € q0
6 q0 € ⸹(€,Z/Z) q1
Fig:1-Transition Diagram
TOC LAB [2022]
Solution:-
#include<bits/stdc++.h>
using namespace std;
bool areParanthesisBalance(string expr)
{
stack<char>s;
char x;
for(int i=0;i<expr.length();i++)
{
if(expr[i]=='('||expr[i]=='['||expr[i]=='{')
{
s.push(expr[i]);
continue;
}
if(s.empty())
return false;
switch(expr[i])
{
case')':
x=s.top();
s.pop();
if(x=='{'||x=='[')
return false;
break;
case'}':
x=s.top();
s.pop();
if(x=='('||x=='[')
return false;
break;
case']':
x=s.top();
s.pop();
if(x=='('||x=='{')
return false;
break;
TOC LAB [2022]
}
}
return(s.empty());
}
int main()
{
string expr;
cout<<"enter the paranthesis";
cin>>expr;
if(areParanthesisBalance(expr))
cout<<"balanced";
else
cout<<"Not Balanced";
return 0;
}
TOC LAB [2022]
EXPERIMENT 11
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.
Discussion:
As per the AIM, set of valid strings that can be generated by given language is represented in set A:
A = {0C0, 1C1, 011000110C011000110, 101011C110101, ....}
means string must have some binary string followed by special character 'C' followed reverse of
binary string that appears before 'C'. 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, q2}
Σ: set of input symbols: {0, 1, C}
Г: Set of stack symbols: {A, B, 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 ofstring is
based on stack whether it is empty or not. If empty means valid else invalid.]
δ: Transition Function: (Transition state diagram is shown in Figure 2.)
TOC LAB [2022]
δ(q0, 0, Z) → (q0, AZ)
δ(q0, C, A) → (q1, A)
δ(q0, C, B) → (q1, B)
δ(q1, 0, A) → (q1, ε)
δ(q1, 1, B) → (q1, ε)
δ(q1, ε, Z) → (q2, ε)
1. If the input symbol of string is '0' and stack top is Z then push symbol 'A' into stack and read
the next character in input string.
2. If the input symbol of string is '1' and stack top is Z then push symbol 'B' into stack and read
the next character in input string.
3. If the input symbol of string is '0' and stack top is A or B then push symbol 'A' into stack and
read the next character in input string.
4. If the input symbol of string is '1' and stack top is A or B then push symbol 'B' into stack and
read the next character in input string.
5. If the input symbol of string is 'C' and stack top is A or B then change state from q0 to q1
and read the next character in input string.
6. If the input symbol of string is '0', machine state is q1 and stack top is A then pop stack top
and read the next character in input string.
7. If the input symbol of string is '1', machine state is q1 and stack top is B then pop stack top
and read the next character in input string.
8. If all the characters of input string are parsed, stack top is Z and machine state is q1, it
means string is valid, pop Z from stack and change the state from q1 to q2.
TOC LAB [2022]
Transition Diagram
Figure 2: PDA that accept language WCWR where w is any binary string and WR is reverse of that
string and C is a special symbol.
Code in C++
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
void main()
{
char Input[100];
char stack[100];
int Top = -1;
clrscr();
cout<<"Enter string to be validate\n";
gets(Input);
stack[++Top] = 'Z';//Taking 'Z'as an initial stack symbol.
int i=-1;
q0:
i++;
if(Input[i]=='0' && stack[Top]== 'Z')
{
stack[++Top]= 'A';
TOC LAB [2022]
goto q0;
}
else if(Input[i]=='1' && stack[Top]== 'Z')
{
stack[++Top]= 'B';
goto q0;
}
else if(Input[i]=='0' && (stack[Top]== 'A'|| stack[Top]== 'B'))
{
stack[++Top]= 'A';
goto q0;
}
else if(Input[i]=='1' && (stack[Top]== 'A'|| stack[Top]== 'B'))
{
stack[++Top]= 'B';
goto q0;
}
else if(Input[i]=='C' &&(stack[Top]== 'A'||stack[Top]== 'B'))
{
goto q1;
}
else
{
goto Invalid;
}
q1:
i++;
if(Input[i]=='0' && stack[Top]== 'A')
{
Top--;
goto q1;
}
else if(Input[i]=='1' && stack[Top]== 'B')
Top--;
goto q1;
}
TOC LAB [2022]
else if(Input[i]=='\0' && stack[Top]== 'Z')
{
goto Valid;
}
else
goto Invalid;
Valid:
cout<<"\n Output: Valid String";
goto exit;
Invalid:
cout<<"\n Output: Invalid String";
goto exit;
exit:
getch();
}
TOC LAB [2022]
EXPERIMENT 12
AIM: Design a Turing machine that’s accepts the following language an b n c n where n>0
Mark 'a' then move right.
Repeat above steps till all 'a', 'b' and 'c' are marked
turing_machine_tape_for_a_to_power_n_and_b_to_power_n_and_c_to_power_n
Step 1-2
Reach unmarked 'b' and pass every 'a' and 'Y'(in case) on the way to 'b'
Step 3-4
Reach unmarked 'c' and pass every 'b' and 'Z'(in case) on the way to 'c'
Mark 'c' as 'Z' and move one step left cause now we have to repeat process
Reach unmarked 'X' and pass every 'Z', 'b', 'Y', 'a' on the way to 'X'
Step 5-9
Step 10
When TAPE header reaches 'X' and next symbol is 'Y' that means 'a's are finished
Check for 'b's and 'c's by going till BLANK(in right) and passing 'Y' and 'Z' on the way
TOC LAB [2022]
If no 'b' and 'c' are not found that means string is accepted
Following Steps:
Following Steps:
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.
TOC LAB [2022]
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