0% found this document useful (0 votes)
27 views28 pages

CS501 TOC Lab File-Pages-Deleted

The document contains a list of 10 experiments related to automata theory and computation. The experiments involve tasks like designing programs for DFAs, PDAs, Turing machines and other computational models. Code snippets in C/C++ are provided for solving some of the problems.

Uploaded by

Deepak Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views28 pages

CS501 TOC Lab File-Pages-Deleted

The document contains a list of 10 experiments related to automata theory and computation. The experiments involve tasks like designing programs for DFAs, PDAs, Turing machines and other computational models. Code snippets in C/C++ are provided for solving some of the problems.

Uploaded by

Deepak Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

LIST OF EXPERIMENTS

S.
No. Name of Experiment Cos
CL501.1
01 Design a Program for creating DFA that starts and end with ‘a’ from input (a, b). CL501.2
CL501.5
CL501.1
Design a Program for creating DFA which will accept all the strings ending
02 CL501.2
with 00 over an alphabet {0, 1}
CL501.5
CL501.1
03 Design a Program for Mode 3 Machine. CL501.2
CL501.5
CL501.1,
Design a program for accepting decimal number divisible
04 CL501.3,
by 3.
CL501.5

Design a program for creating a machine which counts number of 1’s and 0’s in
05 CL501.4
a given string.

CL501.1,
06 Design a Program to find 2’s complement of a given binary number. CL501.2,
CL501.5
CL501.1,
07 Design a Program Generate Binary Numbers from 1 to n.
CL501.2
CL501.1,
08 Design a Program to convert NDFA to DFA.
CL501.2

Design a PDA to accept WCWR where w is any string and WR is reverse of that CL501.1,
09
string and C is a Special symbol. CL501.4

Design a Turing machine that’s accepts the following language a n b n c n where CL501.1,
10
n>=1 CL501.4
Experiment No. 1
Objective: Design a Program for creating DFA that starts and end with ‘a’ from input (a, b).

#include <iostream>
#include <time.h>
using namespace
std; int main()
{

// for producing different random


// numbers every time.
srand(time(0));
// random length of string from 1 - 16
// we are taking input from input stream,
// we can take delimiter to end the
string int max = 1 + rand() % 15;

// generating random string and processing it


inti = 0;
while (i< max) {

// producing random character over


// input alphabet (a, b)
char c = 'a' + rand() %
2; cout<< c << " ";
i++;
// first character is 'a'
if (c == 'a') {

// if there is only 1 character


// i.e. 'a'
if (i == max)

cout<< "YES\n";
while (i< max) {

c = 'a' + rand() % 2;

cout<< c << " "; i+

+;

// if character is 'a' and it

// is the last character

if (c == 'a' &&i == max) {

cout<< "\nYES\n";

}
// if character is 'b' and it
// is the last character
else if (i == max) {
cout<< "\nNO\n";

}
}
}
// first character is 'b' so no matter
// what the string is, it is not going
// to be
accepted else {
while (i< max) {

c = 'a' + rand() % 2;
cout<< c << " ";

i++;
}
cout<< "\nNO\n";
}
}

return 0;
}

OUTPUT
Experiment No. 2
Objective: Design a Program for creating DFA which will accept all the strings ending with 00 over an
alphabet {0, 1}

#define max 100

int main()

charstr[max],f='a';

inti;

printf("enter the string to be checked: ");

scanf("%s",str);

for(i=0;str[i]!='\0';i++)

switch(f)

case 'a': if(str[i]=='0') f='b';

else if(str[i]=='1') f='a';

break;

case 'b': if(str[i]=='0') f='c';

else if(str[i]=='1') f='d';

break;

case 'c': if(str[i]=='0') f='c';

else if(str[i]=='1') f='d';

break;

case 'd': if(str[i]=='0') f='b';

else if(str[i]=='1') f='d';

break;
}

if(f=='c') printf("\nString is accepted as it reached the final state %c at the end.",f);

elseprintf("\nString is not accepted as it reached %c which is not the final state.",f);

OUTPUT
Experiment No. 3
Objective: Design a Program for Mode 3 Machine.

#include <stdio.h>

int mode(int a[],int n) {

intmaxValue = 0, maxCount = 0, i,

j;

for (i = 0; i< n; ++i)

{ int count = 0;

for (j = 0; j < n; ++j)

{ if (a[j] == a[i])

++count;

if (count >maxCount)

{ maxCount = count;

maxValue = a[i];

returnmaxValue;

int main() {

int n = 5;
int a[] = {0,6,3,2,3};

printf("Mode = %d ", mode(a,n));

return 0;

OUTPUT
Experiment No. 4
Objective: Design a program for accepting decimal number divisible by 3.

#include <stdio.h>
#include <stdlib.h>
// Function to build DFA for divisor k
void preprocess(int k, int Table[][2])
{
int trans0, trans1;
// The following loop calculates the two transitions for each state,
// starting from state 0
for (int state=0; state<k; ++state)
{
// Calculate next state for bit
0 trans0 = state<<1;
Table[state][0] = (trans0 < k)? trans0: trans0-k;

// Calculate next state for bit


1 trans1 = (state<<1) + 1;
Table[state][1] = (trans1 < k)? trans1: trans1-k;
}
}
// A recursive utility function that takes a 'num' and DFA (transition
// table) as input and process 'num' bit by bit over
DFA voidisDivisibleUtil(intnum, int* state, int
Table[][2])
{
// process "num" bit by bit from MSB to LSB
if (num != 0)
{
isDivisibleUtil(num>>1, state, Table);
*state = Table[*state][num&1];
}
}
// The main function that divides 'num' by k and returns the
remainder intisDivisible (intnum, int k)
{
// Allocate memory for transition table. The table will have k*2
entries int (*Table)[2] = (int (*)[2])malloc(k*sizeof(*Table));

// Fill the transition table


preprocess(k, Table);
// Process ‘num’ over DFA and get the remainder
int state = 0;
isDivisibleUtil(num, &state, Table);

// Note that the final value of state is the


remainder return state;
}
// Driver program to test above functions
int main()
{
intnum = 47; // Number to be divided
int k = 5; // Divisor

int remainder = isDivisible (num, k);

if (remainder ==
printf("Divisible\n");
0) else
printf("Not Divisible: Remainder is %d\n", remainder);

return 0;
}

OUTPUT
Experiment No. 5
Objective: Design a program for creating a machine which count number of 1’s and 0’s in a given string.

#include <bits/stdc++.h>

using namespace std;

// Function to check

// if bit is 1 or not

boolisOne(inti)

if (i == 1)

return true;

else

return false;
}

// Driver Code

int main()

int a[] = { 1, 0, 0, 1, 0, 0, 1 };

int n = sizeof(a) / sizeof(a[0]);

intcount_of_one = count_if(a, a + n, isOne);

cout<< "1's: " <<count_of_one<<endl;

cout<< "0's: " << (n - count_of_one) <<endl;

return 0;
}
OUTPUT
Experiment No. 6
Objective: Design a Program to find 2’s complement of a given binary

number. #include <stdio.h>


#define SIZE 8

int main()
{
char binary[SIZE + 1], onesComp[SIZE + 1], twosComp[SIZE + 1];
inti, carry=1;

printf("Enter %d bit binary value: ", SIZE);

/* Input 8-bit binary string */


gets(binary);

/* Find ones complement of the binary number */


for(i=0; i<SIZE; i++)
{
if(binary[i] == '1')
{
onesComp[i] = '0';
}
else if(binary[i] == '0')
{
onesComp[i] = '1';
}
}
onesComp[SIZE] = '\0';

/*
* Add 1 to the ones complement
*/
for(i=SIZE-1; i>=0; i--)
{
if(onesComp[i] == '1' && carry == 1)
{
twosComp[i] = '0';
}
else if(onesComp[i] == '0' && carry == 1)
{
twosComp[i] = '1';
carry = 0;
}
else
{
twosComp[i] = onesComp[i];
}
}
twosComp[SIZE] = '\0';

printf("Original binary = %s\n", binary);


printf("Ones complement = %s\n", onesComp);
printf("Twos complement = %s\n", twosComp);

return 0;
}

OUTOUT
Experiment No. 7
Objective: Design a Program Generate Binary Numbers from 1 to n.

// C++ program to generate binary numbers from 1 to n


#include <bits/stdc++.h>
using namespace std;
// This function uses queue data structure to print binary
numbers voidgeneratePrintBinary(int n)
{
// Create an empty queue of strings
queue<string> q;

// Enqueue the first binary


number q.push("1");

// This loops is like BFS of a tree with 1 as root


// 0 as left child and 1 as right child and so on
while (n--)
{
// print the front of queue
string s1 = q.front();
q.pop();
cout<< s1 << "\n";
string s2 = s1; // Store s1 before changing it
// Append "0" to s1 and enqueue it
q.push(s1.append("0"));
// Append "1" to s2 and enqueue it. Note that s2
contains
// the previous front
q.push(s2.append("1"));
}
}

// Driver program to test above


function int main()
{
int n = 10;
generatePrintBinary(n);
return 0;
}
OUTPUT
Experiment No. 8

Objective: Design a Program to convert NDFA to DFA.

#include<stdio.h>

#include<string.h>

#include<math.h>

int ninputs;

int dfa[100][2][100] = {0};

int state[10000] = {0};

char ch[10], str[1000];

int go[10000][2] = {0};

int arr[10000] = {0};

int main()

int st, fin, in;

int f[10];

inti,j=3,s=0,final=0,flag=0,curr1,curr2,k,l;

int c;

printf("\nFollow the one based indexing\n");

printf("\nEnter the number of states::");

scanf("%d",&st);

printf("\nGive state numbers from 0 to %d",st-1);

for(i=0;i<st;i++)

state[(int)(pow(2,i))] = 1;

printf("\nEnter number of final states\t");


scanf("%d",&fin); printf("\

nEnter final states::");

for(i=0;i<fin;i++)

scanf("%d",&f[i]);

int p,q,r,rel;

printf("\nEnter the number of rules according to NFA::");

scanf("%d",&rel);

printf("\n\nDefine transition rule as \"initial state input symbol final state\"\n");

for(i=0; i<rel; i++)

scanf("%d%d%d",&p,&q,&r);

if (q==0)

dfa[p][0][r] = 1;

else

dfa[p][1][r] = 1;

printf("\nEnter initial state::");

scanf("%d",&in);

in = pow(2,in);

i=0;

printf("\nSolving according to DFA");

int x=0;

for(i=0;i<st;i++)

{
for(j=0;j<2;j++)

intstf=0;

for(k=0;k<st;k++)

if(dfa[i][j][k]==1)
stf = stf + pow(2,k);

go[(int)(pow(2,i))][j] = stf;

printf("%d-%d-->%d\n",(int)(pow(2,i)),j,stf);

if(state[stf]==0)

arr[x++] = stf;

state[stf] = 1;

//for new states

for(i=0;i<x;i++)

printf("for %d-----",arr[x]);

for(j=0;j<2;j++)

int new=0;

for(k=0;k<st;k++)

if(arr[i] & (1<<k))


{

int h = pow(2,k);

if(new==0)

new = go[h][j];

new = new | (go[h][j]);

if(state[new]==0)

arr[x++] = new;

state[new] = 1;

printf("\nThe total number of distinct states are::\n");

printf("STATE 0 1\n");

for(i=0;i<10000;i++)

if(state[i]==1)

//printf("%d**",i);

int y=0;

if(i==0)

printf("q0 ");
else

for(j=0;j<st;j++)

int x = 1<<j;

if(x&i)

printf("q%d ",j);

y = y+pow(2,j);

//printf("y=%d ",y);

//printf("%d",y);

printf(" %d %d",go[y][0],go[y][1]);

printf("\n");

j=3;

while(j--)

printf("\nEnter string");

scanf("%s",str);

l = strlen(str);

curr1 = in;

flag = 0;

printf("\nString takes the following path-->\n");

printf("%d-",curr1);
for(i=0;i<l;i++)

curr1 = go[curr1]

[str[i]-'0'];

printf("%d-",curr1);

printf("\nFinal state - %d\n",curr1);

for(i=0;i<fin;i++)

if(curr1 & (1<<f[i]))

flag = 1;

break;

if(flag)

printf("\nString Accepted");

else

printf("\nString Rejected");
return 0; }

}
OUTPUT
Experiment No. 8

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

Soln:-

Some string will come followed by one 'c', followed by reverse of the string before 'c'.
So we get to know that 'c' will work as an alarm to starting popping STACK.
So we will pop every 'a' with 'a' and every 'b' with 'b'.
For every two a's and b's push them into STACK
When 'c' comes do nothing.
Starting popping STACK: 'a' for 'a' and 'b' for
'b'. We have designed the PDA for the problem:

STACK Transition Function


δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, Z) = (q0, bZ)
δ(q0, b, b) = (q0, bb)
δ(q0, a, b) = (q0, ab)
δ(q0, b, a) = (q0, ba)
// this is decision step
δ(q0, c, a) = (q1, a)
δ(q0, c, b) = (q1, b)
δ(q1, b, b) = (q1, ε)
δ(q1, a, a) = (q1, ε)

δ(q1, ε, Z) = (qf, Z)
Note: qf is Final State

Explanation
Let’s see, how this DPDA is working:
We will take one input string: "abbcbba"

 Scan string from left to right


 First input is 'a' and follow the rule:
 on input 'a' and STACK alphabet Z, push the two 'a's into STACK as : (a,Z/aZ) and state
will be q0
 Second input is 'b' and so follow the rule:
 on input 'b' and STACK alphabet 'a', push the 'b' into STACK as : (b,a/ba) and state will be q0
 Third input is 'b' and so follow the rule:
 on input 'b' and STACK alphabet 'b', push the 'b' into STACK as : (b,b/bb) and state will be q0
 Fourth input is 'c' and so follow the rule:
 on input 'c' and STACK alphabet 'a' or 'b' and state q0, do nothing as : (c,b/b) and state will
be q1
 Fifth input is 'b' and so follow the rule:
 on input 'b' and STACK alphabet 'b' (state is q1), pop one 'b' from STACK as : (b,b/ε)
and state will be q1
 Sixth input is 'b' and so follow the rule:
 on input 'b' and STACK alphabet 'b' (state is q1), pop one 'b' from STACK as : (b,b/ε)
and state will be q1
 Seventh input is 'a' and so follow the rule:
 on input 'a' and STACK alphabet 'a' and state q1, pop one 'a' from STACK as : (a,a/ε)
and state will remain q1
 We reached end of the string, so follow the rule:
 on input ε and STACK alphabet Z, go to final state(qf) as : (ε, Z/Z)
Experiment No. 10

Objective: Design a Turing machine that’s accepts the following language a n b n c n where n>=1.

Soln:-
Approach used –
First replace a 0 from front by X, then keep moving right till you find a 1 and replace this 1 by Y.
Again, keep moving right till you find a 2, replace it by Z and move left. Now keep moving left
till you find a X. When you find it, move a right, then follow the same procedure as above.
A condition comes when you find a X immediately followed by a Y. At this point we keep moving
right and keep on checking that all 1’s and 2’s have been converted to Y and Z. If not then string
is not accepted. If we reach $ then string is accepted.
 Step-1:
Replace 0 by X and move right, Go to state Q1.
 Step-2:
Replace 0 by 0 and move right, Remain on same state
Replace Y by Y and move right, Remain on same state
Replace 1 by Y and move right, go to state Q2.
 Step-3:
Replace 1 by 1 and move right, Remain on same state
Replace Z by Z and move right, Remain on same
state Replace 2 by Z and move right, go to state Q3.
 Step-4:
Replace 1 by 1 and move left, Remain on same state
Replace 0 by 0 and move left, Remain on same state
Replace Z by Z and move left, Remain on same state
Replace Y by Y and move left, Remain on same
state Replace X by X and move right, go to state Q0.
 Step-5:
If symbol is Y replace it by Y and move right and Go to state
Q4 Else go to step 1
 Step-6:
Replace Z by Z and move right, Remain on same state
Replace Y by Y and move right, Remain on same state
If symbol is $ replace it by $ and move left, STRING IS ACCEPTED, GO TO FINAL
STATE Q5

You might also like