0% found this document useful (0 votes)
990 views

TOC Practical File 18

The document contains a practical file on the theory of computations. It includes 12 experiments related to designing programs for finite automata. Each experiment contains the aim, diagram, code and output for programs that design DFAs and NDFAs to accept various strings and perform operations on binary numbers. The last experiment provides code to convert an NDFA to a DFA.

Uploaded by

Aditya Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
990 views

TOC Practical File 18

The document contains a practical file on the theory of computations. It includes 12 experiments related to designing programs for finite automata. Each experiment contains the aim, diagram, code and output for programs that design DFAs and NDFAs to accept various strings and perform operations on binary numbers. The last experiment provides code to convert an NDFA to a DFA.

Uploaded by

Aditya Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

PRACTICAL FILE

THEORY OF COMPUTATIONS

CS - 501

Submitted To -
Submitted By -
Dr. Deepak Gupta.
Aditya Jha
Assoc. Prof.
0905CS191018
Dept. Of CSE
CSE – 5th Sem
INDEX
S. No. EXPERIMENTS
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.

7 Design a program for creating a machine which accepts string having


even no. of 1’s and 0’s.
8 Design a program for creating a machine 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.


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

DIAGRAM:-

0905CS191018 Aditya Jha

PYTHON CODE:-
def q0(s,i):

if len(s) == i:

print("Rejected")

return;

elif (s[i] == 'a'):

q1(s,i+1);

else:

qd(s,i+1)

def qd(s,i):
if len(s) == i:
print("Rejected")

return;

elif (s[i] == 'a'):

qd(s,i+1);

else:

qd(s,i+1)

def q1(s,i):

if len(s) == i:

print("Accepted")

return;

elif (s[i] == 'a'):

q1(s,i+1);

else:

q1(s,i+1)

if _name_ == "_main_":

s = input("Enter a string : ")

q0(s,0);

OUTPUT:-
Experiment:2

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

0905CS191018 Aditya Jha

CODE:-
def q0(s,i):
if len(s) == i:
print("Rejected")
return;
elif (s[i] == '1'):
qd(s,i+1);
else:
q1(s,i+1)

def q1(s,i):
if len(s) == i:
print("Rejected")
return;
elif (s[i] == '1'):
q2(s,i+1);
else:
q1(s,i+1)
def qd(s,i):
if len(s) == i:
print("Rejected")
return;
elif (s[i] == '1'):
qd(s,i+1);
else:
qd(s,i+1)

def q2(s,i):
if len(s) == i:
print("Accepted")
return;
elif (s[i] == '1'):
q2(s,i+1);
else:
q1(s,i+1)

if name == " main ":


s = input("Enter a string : ")
q0(s,0);

OUTPUT:-
Experiment:3

Aim:-Design a Program for creating machine that accepts


three consecutive one.

Diagram:-

CODE:-
def q0(s,i):
if len(s) == i:
print("Rejected")
return;
elif (s[i] == '1'):
q1(s,i+1);
else:
q0(s,i+1)

def q1(s,i):
if len(s) == i:
print("Rejected")
return;
elif (s[i] == '1'):
q2(s,i+1);
else:
q0(s,i+1)

def q2(s,i):
if len(s) == i:
print("Rejected")
return;
elif (s[i] == '1'):
q3(s,i+1);
else:
q0(s,i+1)

def q3(s,i):
if len(s) == i:
print("Accepted")
return;
elif (s[i] == '1'):
q3(s,i+1);
else:
q3(s,i+1)

if name == " main ":


s = input("Enter a string : ")
q0(s,0);

OUTPUT:-
Experiment:4

Aim:-Design a Program for creating machine that accepts


the string always ending with 101.

Diagram:-
CODE:-
def q0(s,i):
if len(s) == i:
print("Rejected")
return;
elif (s[i] == '1'):
q1(s,i+1);
else:
q0(s,i+1)

def q1(s,i):
if len(s) == i:
print("Rejected")
return;
elif (s[i] == '1'):
q1(s,i+1);
else:
q2(s,i+1)

def q2(s,i):
if len(s) == i:
print("Rejected")
return;
elif (s[i] == '1'):
q3(s,i+1);
else:
q0(s,i+1)

def q3(s,i):
if len(s) == i:
print("Accepted")
return;
elif (s[i] == '1'):
q1(s,i+1);
else:
q2(s,i+1)

if name == " main ":


s = input("Enter a string : ")
q0(s,0);

OUTPUT:-
Experiment:5

Aim:-Design a Program for Mode 3 Machine.

Diagram
0905CS191018 Aditya Jha

CODE:-
def q0(s,i):

if len(s) == i:
print("Accepted")
return;
elif (s[i] == '1'):
q1(s,i+1);
else:
q0(s,i+1)

def q1(s,i):
if len(s) == i:
print("Rejected")
return;
elif (s[i] == '1'):
q0(s,i+1);
else:
q2(s,i+1)
def q2(s,i):
if len(s) == i:
print("Rejected")
return;
elif (s[i] == '1'):
q2(s,i+1);
else:
q1(s,i+1)

if name == " main ":


s = input("Enter a string : ")
q0(s,0);

OUTPUT:-
Experiment:6

Aim:-Design a program for accepting binary number


divisible by 2.

Diagram:-

0905CS191018 Aditya Jha

Code:-
def q0(s,i):
if len(s)==i:
print("Accepted")
return;
if (s[i]=='1'):
q1(s,i+1)
else:
q0(s,i+1)

def q1(s,i):
if len(s)==i:
print("rejected")
return;
if (s[i]=='1'):
q1(s,i+1)
else:
q0(s,i+1)
if _name_ == "_main_":
s = input("Enter a string")
q0(s,0);

Output:-
Experiment:7

Aim:-Design a program for creating a machine which


accepts string having even no. of 1’s and 0’s ?

Diagram:-

0905CS191018 Aditya Jha

Code:-
def q0(s,i):
if len(s) == i:

print("Accepted")

return;

elif (s[i] == '1'):

q2(s,i+1);

else:

q1(s,i+1)
def q1(s,i):

if len(s) == i:

print("Rejected")

return;

elif (s[i] == '1'):

q3(s,i+1);

else:

q0(s,i+1)

def q2(s,i):

if len(s) == i:

print("Rejected")

return;

elif (s[i] == '1'):

q0(s,i+1);

else:

q3(s,i+1)

def q3(s,i):

if len(s) == i:

print("Rejected")

return;

elif (s[i] == '1'):


q1(s,i+1);

else:

q2(s,i+1)

if _name_ == "_main_":

s = input("Enter a string : ")

q0(s,0);

Output:-
Experiment:8

Aim:-Design a program for creating a machine which count


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

CODE:-
def q0(s,i,c0,c1):

if len(s) == i:

print("Number of 0's = ",c0)

print("Number of 1's = ",c1)

return;

elif (s[i] == '1'):

c1 = c1 + 1

q1(s,i+1,c0,c1);

else:

c0 = c0 + 1

q0(s,i+1,c0,c1)

def q1(s,i,c0,c1):

if len(s) == i:

print("Number of 0's = ",c0)

print("Number of 1's = ",c1)

return;
elif (s[i] == '1'):

c1 = c1 + 1

q1(s,i+1,c0,c1);

else:

c0 = c0 + 1

q0(s,i+1,c0,c1)

if _name_ == "_main_":

s = input("Enter a string : ")

count_0 = 0

count_1 = 0

q0(s,0,count_0,count_1);

OUTPUT:-
Experiment:9

Aim:-Design a Program to find 2’s complement of a given


binary number.

Diagram:-

CODE:-
def q0(s, i):

if len(s) == 0 or len(s) == i:

return

elif s[i] == '1':

q1(s,i+1)

print('1',end="")

else:

q0(s,i+1)

print('0',end="")

def q1(s,i):
if len(s) == 0 or len(s) == i:

return

elif s[i] == '1':

q1(s,i+1)

print('0',end="")

else:

q1(s,i+1)

print('1',end="")

def my_function(x):

return x[::-1]

if _name_ == "_main_":

mystr = input("Enter a string : ")

s = my_function(mystr)

q0(s,0)

OUTPUT:-
Experiment:10

Aim:-Design a Program which will increment the given


binary number by 1.
DIAGRAM:-

0905CS191018 Aditya Jha

CODE:-
def q0(s, i):

if len(s) == 0 or len(s) == i:

return

elif s[i] == '1':

q2(s,i+1)

print('0',end="")

else:

q1(s,i+1)
print('1',end="")

def q1(s,i):

if len(s) == 0 or len(s) == i:

return

elif s[i] == '1':

q1(s,i+1)

print('1',end="")

else:

q1(s,i+1)

print('0',end="")

def q2(s,i):

if len(s) == 0 or len(s) == i:

return

elif s[i] == '1':

q2(s,i+1)

print('0',end="")

else:

q1(s,i+1)

print('1',end="")

def my_function(x):
return x[::-1]

if _name_ == "_main_":

mystr = input("Enter a string : ")

s = my_function(mystr)

q0(s + '0',0)

OUTPUT:-
Experiment:11

Aim:- Design a Program to convert NDFA to DFA.

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 eg:


a,b so input 2 for a,b,c input 3

for i in range(n):

state = input("state name : ") #Enter state name eg: A, B, C, q1, q2 ..etc

nfa[state] = {} #Creating a nested dictionary

for j in range(t):

path = input("path : ") #Enter path eg : a or b in {a,b} 0 or 1 in {0,1}

print("Enter end state from state {} travelling through path {} :


".format(state,path))

reaching_state = [x for x in input().split()] #Enter all the end states that

nfa[state][path] = reaching_state #Assigning the end states to the paths


in dictionary

print("\nNFA :- \n")

print(nfa) #Printing NFA


print("\nPrinting NFA table :- ")

nfa_table = pd.DataFrame(nfa)

print(nfa_table.transpose())

print("Enter final state of NFA : ")

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

###################################################

new_states_list = [] #holds all the new states created in dfa

dfa = {} #dfa dictionary/table or the output structure we


needed

keys_list = list(list(nfa.keys())[0]) #conatins all the states in nfa plus


the states created in dfa are also appended further

path_list = list(nfa[keys_list[0]].keys()) #list of all the paths eg: [a,b] or [0,1]

###################################################

# Computing first row of DFA transition table

dfa[keys_list[0]] = {} #creating a nested dictionary in dfa

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 which is a new state
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) #then append it to the


new_states_list

keys_list.append(var) #as well as to the keys_list which


contains all the states

###################################################

# Computing the other rows of DFA transition table

while len(new_states_list) != 0: #consition is true only if the


new_states_list is not empty

dfa[new_states_list[0]] = {} #taking the first element of the


new_states_list and examining it

for _ in range(len(new_states_list[0])):

for i in range(len(path_list)):

temp = [] #creating a temporay 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 = ""

s = 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) #then append it to the


new_states_list

keys_list.append(s) #as well as to the keys_list which


contains all the states

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

print("\nDFA :- \n")

print(dfa) #Printing the DFA created

print("\nPrinting DFA table :- ")

dfa_table = pd.DataFrame(dfa)

print(dfa_table.transpose())

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:-

You might also like