Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Designing Finite State Machines and Pushdown Automata, Study notes of Theory of Computation

The design of various finite state machines and pushdown automata to accept different types of strings and perform operations on binary numbers. The programs include designing a machine that accepts strings ending with '101', a machine that accepts strings with equal number of '1's and '0's, a machine that accepts decimal numbers divisible by 2, a machine that counts the number of '1's and '0's in a given string, a program to increment a binary number by 1, and a program to create a pushdown automaton that accepts well-formed parentheses. The problem statements, solutions, and explanations for these experiments, making it a valuable resource for students studying automata theory, formal languages, and programming concepts related to string processing and numerical operations.

Typology: Study notes

2023/2024

Uploaded on 05/03/2024

chandni-sikarwar
chandni-sikarwar 🇮🇳

3 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
.
INDEX
S. Expt. Name of the
Experiment
Date of
Exp. Date of
Perform
Sign
No. No.
01. 1Design a Program for creating machine that accepts three consecutive one.
02. 2Design a Program for creating machine that accepts the string always
ending with 101.
03 3Design a Program for Mode 3 Machine.
04 4Design a program for accepting decimal number divisible by 2.
05 5Design a program for creating a machine which accepts string having equal
no. of 1’s and 0’s.
06 6Design a program for creating a machine which count number of 1’s and
0’s in a given string.
07 7Design a Program to find 2’s complement of a given binary number.
08 8Design a Program which will increment the given binary number by 1.
09 09 Design a Program to create PDA machine that accept the well-formed
parenthesis.
10 10 Design a PDA to accept 0^n1^n where n is the number of occurrence.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download Designing Finite State Machines and Pushdown Automata and more Study notes Theory of Computation in PDF only on Docsity!

INDEX

S. Expt. Name of the Experiment Date of Exp. Date of Perform Sign No. No.

  1. 1 Design a Program for creating machine that accepts three consecutive one.
  2. 2 Design a Program for creating machine that accepts the string always ending with 101. 03 3 Design a Program for Mode 3 Machine. 04 4 Design a program for accepting decimal number divisible by 2. 05 5 Design a program for creating a machine which accepts string having equal no. of 1’s and 0’s. 06 6 Design a program for creating a machine which count number of 1’s and 0’s in a given string. 07 7 Design^ a^ Program^ to^ find^ 2’s^ complement^ of^ a^ given^ binary^ number. 08 8 Design^ a^ Program^ which^ will^ increment^ the^ given^ binary^ number by 1. 09 09 Design^ a^ Program^ to^ create^ PDA^ machine^ that^ accept^ the^ well-formed parenthesis. 10 10 Design a PDA to accept 0^n1^n where n is the number of occurrence.

EXPERIMENT No. – 1 PROBLEM DEFINATION :Design a Program for creating machine that accepts three consecutive one? Theory: Given an array of n non-negative numbers, the task is to find the minimum sum of elements (picked from the array) such that at least one element is picked out of every 3 consecutive elements in the array. Examples : Input : arr[] = {1, 2, 3} Output : 1 Input : arr[] = {1, 2, 3, 6, 7, 1} Output : 4 We pick 3 and 1 (3 + 1 = 4) Note that there are following subarrays of three consecutive elements {1, 2, 3}, {2, 3, 6}, {3, 6, 7} and {6, 7, 1} We have picked one element from every subarray. Input : arr[] = {1, 2, 3, 6, 7, 1, 8, 6, 2, 7, 7, 1} Output : 7 The result is obtained as sum of 3 + 1 + 2 + 1 Viva Questions:

  1. Define Automata?
  2. What is the application of Finite Automata?
  3. What does it mean that string end with 3 consecutive 1?
  4. What are the characteristics of Automata? Solution: Sum(i) be the minimum possible sum when arr[i] is part of a solution sum (not necessarily result) and is last picked element. Then our result is minimum of sum(n-1), sum(n-2) and sum(n-3) [We must pick at least one of the last three elements].We can recursively compute sum(i) as sum of arr[i] and minimum(sum(i-1), sum(i-2), sum(i-3)). Since there are overlapping sub problems in recursive structure of problem, we can use Dynamic Programming to solve this problem. Solution: String should contain three consecutive 1's.

EXPERIMENT No. – 2 PROBLEM DEFINATION : Design a Program for creating machine that accepts the string always ending with

Theory: An NFA is typically described using a directed graph. Each edge and vertex is labelled either 0 or 1 representing possible transitions. Vertices represent the states of the NFA. Those labelled 0 are non accepting states, and those labelled 1 are accepting states.  It takes an input string of finite length. Typically, the input string is a binary sequence of 0’s and 1’s.  We begin with the vertex 1 which is always the start state and follow the edges sequentially given by the bits of input string until the input string has no further bit.  The term ‘non deterministic’ means that at any state V we have more than one choice of following an edge. The NFA consumes the input string and set of states Q represents the possible moves of NFA.  If Q contains at least one state where the last vertex is accepting then we say that the NFA has accepted the input string otherwise the NFA has rejected the input string.  String should end with 101 always. Viva Questions:

  1. Define string created by using alphabet?
  2. How to design the automata for accepting the string?
  3. How the string is accepted by the Automata?
  4. Difference between DFA and NDFA?

Solution: #include #include<string.h> using namespace std; int main() { char s[100]; int i,n; cout<<"enter string = "; cin>>s; n = strlen(s) - 1; if(s[n] == '1' && s[n-1] == '0' && s[n-2] == '1') { cout<<"String accepted"; } else { cout<<"String rejected"; } return 0 ; } Output: Enter the string to be checked: 11001 String is accepted as it reached the final state c at the end

Solution: C++ program to find remainder without using #include #include<string.h> using namespace std; int main() { string str; char state='A'; int i; printf("Enter the string to be checked in fom of {1:0}"); cin>>str; int len=str.length(); cout<<"--->"<<state; for(i=0;str[i]!='\0';i++) { cout<<"---->"<<str[i]<<"-->"; switch(state) { case 'A': if(str[i]=='1') state='B'; else if(str[i]=='0') if(i==len-1)

break; case 'B': else state='A'; state='A'; if(str[i]=='0') state='C'; else if(str[i]=='1') state='A'; break; case 'C': if(str[i]=='1') state='C'; else if(str[i]=='0') state='B'; break; } cout<<state; } if(state=='A') cout<<"\n string is accepeted as it reached the final state ....."<<state<<"......at the end"; else cout<<"\n string is not accepted as it reached.... "<<state<<"......which is not the final state"; }

Solution: #include #include<string.h> using namespace std; // Function to convert binary to decimal int binaryToDecimal(string n) { string num = n; int dec_value = 0; // Initializing base value to 1, i.e 2^ int base = 1; int len = num.length(); for (int i = len - 1; i >= 0; i--) { if (num[i] == '1') dec_value += base; base = base * 2;

return dec_value; } int main(){ string inputString; int number; cout<<"Enter Input string containing {0,1}:"; cin>>inputString; int length = inputString.length(); cout<<"\n Number in Decimal Format:"; number = binaryToDecimal(inputString); cout<<number; if(inputString[length-1] == '0'){ cout<<"
n"<<number<<" is divisible by 2"; } else { cout<<"\n"<<number<<" is not divisible by 2"; } /* if(number%2 == 0){ cout<<"
n"<<number<<" is divisible by 2"; } else { cout<<"\n"<<number<<" is not divisible by 2"; } */ return 0; }

EXPERIMENT No. – 5 PROBLEM DEFINATION : Design a program for creating a machine which accepts string having equal no. of 1’s and 0’s. Problem Statement: This is the program to check whether the string having equal no. of 1’s and 0’s or Not.  Enter Binary string.  Use Logic to manipulate.  Declare if-else Condition.  Displaying output on the Display. Viva Questions:

  1. Difference between PDA and FA?
  2. What is PDA?
  3. Limitations of FA.

Solution: #include<iostream.h> #include<conio.h> #include<string.h> int main() { clrscr(); char *ar; int i,count=0,c=0,n; cout<<"Enter string in the form of 0 and 1 "; cin>>ar; n=strlen(ar); for(i=0;i<n;i+ +) { if(ar[i]=='0') count++; else } if( count == c ) cout<<”\n Accepted (Equal 0s & 1s)”; cout<<”\n Rejected”; return 0; } c++;

cout<<"No of 0's="<<count<<endl; cout"no of 1's="<<c; return 0; }

EXPERIMENT No. – 7 PROBLEM DEFINATION : Design a Program to find2’scomplement of a given binary number.. Problem Statement: Twos complement of an N-bit number is defined as the complement with respect to 2N. It is the result of subtracting the number from 2N, which in binary is one followed by N zeroes. In simple words twos complement is defined as sum of 1’s complement of a binary number and 1. Logic: Step by step descriptive logic to find twos complement of a binary string.

  1. Input a binary string from user. Store it in a variable say binary.
  2. Find ones complement of the binary string. Store the result in some variable say onesComp.
  3. Declare and initialize another binary string variable to store twos complement, say twosComp = "".
  4. Initialize a variable to store carry bit during addition, say carry = 1.
  5. Run a loop from length of binary string to 1, decrement 1 in each iteration. The loop structure should look like for(i=SIZE-1; i>=1; i--) (where SIZE is length of the binary string).
  6. Inside the loop there can be three cases.  If both current binary bit and carry bit are 1 then, put 0 to twos complement. Which is if(onesComp[i]=='1' && carry==1) then, twosComp[i] = '0', carry is still preserved to 1.  If current binary bit is 0 and carry bit is 1 then, put 1 to twos complement and set carry bit to 0. Which is if(onesComp[i]=='0' && carry==1) then, twosComp[i] = '1' and carry = 0.  If carry bit is 0, then assign the value of onesComp to twosComp.

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; }

EXPERIMENT No. – 8 PROBLEM DEFINATION : Design a Program which will increment the given binary number by 1. Problem Statement: This is the program to count whether the no. of 1’s and 0’s in given string.  Enter Binary string.  Convert the binary string to Decimal  Increment the value  Convert the value into binary number Viva Questions:

  1. Explain the Increment Operator?
  2. What are Grammar , CNF and GNF?
  3. Types of Grammar?