Thankmelater 3
Thankmelater 3
Program
#include<stdio.h>
#include<stdlib.h>
struct node
int st;
};
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO- 2108390109003
void findclosure(int,int);
int findalpha(char);
void findfinalstate(void);
void unionclosure(int);
void print_e_closure(int);
static int
set[20],nostate,noalpha,s,notransition,nofinal,start,finalstate[20],c,r,buf
fer[20];
char alphabet[20];
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO- 2108390109003
2108390109003
void main()
int i,j,k,m,t,n;
scanf("%d",&noalpha);
getchar();
printf("\nEnter alphabets?\n");
for(i=0;i<noalpha;i++) {
alphabet[i]=getchar();
getchar(); }
scanf("%d",&nostate);
scanf("%d",&start);
scanf("%d",&nofinal);
for(i=0;i<nofinal;i++)
scanf("%d",&finalstate[i]);
printf("Enter no of transition?\n");
scanf("%d",¬ransition);
printf("\nEnter transition?\n");
for(i=0;i<notransition;i++)
{ scanf("%d %c%d",&r,&c,&s);
insert_trantbl(r,c,s); }
printf("\n");
for(i=1;i<=nostate;i++) {
c=0;
2108390109003
for(j=0;j<20;j++) {
buffer[j]=0;
e_closure[i][j]=0; }
findclosure(i,i); }
printf("-----------------------------------\n");
for(i=1;i<=nostate;i++) {
printf("state_no.-%d ",i);
print_e_closure(i);
printf("\n"); }}
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO- 2108390109003
int i;
if(buffer[x])
return;
e_closure[sta][c++]=x;
buffer[x]=1;
temp=transition[x][noalpha-1];
while(temp!=NULL) {
findclosure(temp->st,sta);
temp=temp->link;
} } }
int j;
j=findalpha(c);
if(j==999) {
2108390109003
printf("error\n");
exit(0);
temp->st=s;
temp->link=transition[r][j];
transition[r][j]=temp;
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO- 2108390109003
int i;
for(i=0;i<noalpha;i++)
if(alphabet[i]==c)
return i;
return(999);}
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO- 2108390109003
void print_e_closure(int i)
int j;
printf("{");
for(j=0;e_closure[i][j]!=0;j++)
printf("q%d,",e_closure[i][j]);
printf("}\t");}
Output
Enter the number of alphabets?
2108390109003
Enter alphabets?
Enter no of transition?
Enter transition?
1 a 1
1 e 2
2 b 2
2 e 3
3 c 3
-----------------------------------
state_no.-1 {q1,q2,q3,}
state_no.-2 {q2,q3,}
state_no.-3 {q3,}
2108390109003
Program no 5
Step-1: Consider the two vertexes having the epsilon move. Here
in Fig.1 we have vertex v1 and vertex v2 having epsilon move from
v1 to v2.
Step-2: Now find all the moves to any other vertex that start
from vertex v2 (other than the epsilon move that is
considering). After finding the moves, duplicate all the moves that
start from vertex v2, with the same input to start from vertex v1
and remove the epsilon move from vertex v1 to vertex v2.
2108390109003
Program
#include<stdio.h>
#include<stdlib.h>
struct node
int st;
};
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO- 2108390109003
void findclosure(int,int);
int findalpha(char);
void findfinalstate(void);
void unionclosure(int);
void print_e_closure(int);
static int
set[20],nostate,noalpha,s,notransition,nofinal,start,finalstate[20],c,r,buf
fer[20];
char alphabet[20];
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO- 2108390109003
void main()
2108390109003
{
int i,j,k,m,t,n;
scanf("%d",&noalpha);
getchar();
printf("\nEnter alphabets?\n");
for(i=0;i<noalpha;i++)
alphabet[i]=getchar();
getchar();
scanf("%d",&nostate);
scanf("%d",&start);
scanf("%d",&nofinal);
for(i=0;i<nofinal;i++)
scanf("%d",&finalstate[i]);
printf("Enter no of transition?\n");
scanf("%d",¬ransition);
printf("\nEnter transition?\n");
for(i=0;i<notransition;i++)
2108390109003
scanf("%d %c%d",&r,&c,&s);
insert_trantbl(r,c,s);
printf("\n");
for(i=1;i<=nostate;i++)
c=0;
for(j=0;j<20;j++)
buffer[j]=0;
e_closure[i][j]=0;
findclosure(i,i);
printf("-----------------------------------\n");
printf("start state:");
print_e_closure(start);
printf("\nAlphabets:");
for(i=0;i<noalpha;i++)
printf("%c ",alphabet[i]);
for(i=1;i<=nostate;i++)
print_e_closure(i);
printf("\nTnransitions are...:\n");
//RAJ PATEL
//CSE 3 YEAR
2108390109003
//ROLL_NO- 2108390109003
for(i=1;i<=nostate;i++)
for(j=0;j<noalpha-1;j++)
for(m=1;m<=nostate;m++)
set[m]=0;
for(k=0;e_closure[i][k]!=0;k++)
t=e_closure[i][k];
temp=transition[t][j];
while(temp!=NULL)
unionclosure(temp->st);
temp=temp->link;
printf("\n");
print_e_closure(i);
printf("%c\t",alphabet[j] );
printf("{");
for(n=1;n<=nostate;n++)
if(set[n]!=0)
printf("q%d,",n);
printf("}");} }
findfinalstate();
//RAJ PATEL
2108390109003
//CSE 3 YEAR
//ROLL_NO- 2108390109003
int i;
if(buffer[x])
return;
e_closure[sta][c++]=x;
buffer[x]=1;
temp=transition[x][noalpha-1];
while(temp!=NULL)
findclosure(temp->st,sta);
temp=temp->link;
} }}
int j;
j=findalpha(c);
if(j==999)
printf("error\n");
exit(0);
temp->st=s;
temp->link=transition[r][j];
transition[r][j]=temp;
2108390109003
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO- 2108390109003
int findalpha(char c)
int i;
for(i=0;i<noalpha;i++)
if(alphabet[i]==c)
return i;
return(999);
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO- 2108390109003
void unionclosure(int i)
int j=0,k;
while(e_closure[i][j]!=0)
k=e_closure[i][j];
set[k]=1;
j++; }}
void findfinalstate()
int i,j,k,t;
for(i=0;i<nofinal;i++)
for(j=1;j<=nostate;j++)
for(k=0;e_closure[j][k]!=0;k++)
if(e_closure[j][k]==finalstate[i])
2108390109003
print_e_closure(j);
} }} }}
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO- 2108390109003
void print_e_closure(int i)
int j;
printf("{");
for(j=0;e_closure[i][j]!=0;j++)
printf("q%d,",e_closure[i][j]);
printf("}\t");
Output
Enter alphabets?
2108390109003
Enter the number of final states?
Enter no of transition?
Enter transition?
1 a 1
1 e 2
2 b 2
2 e 3
3 c 3
-----------------------------------
start state:{q1,q2,q3,}
Alphabets:a b c e
Tnransitions are...:
{q1,q2,q3,} a {q1,q2,q3,}
{q1,q2,q3,} b {q2,q3,}
{q1,q2,q3,} c {q3,}
{q2,q3,} a {}
{q2,q3,} b {q2,q3,}
{q2,q3,} c {q3,}
{q3,} a {}
{q3,} b {}
{q3,} c {q3,}
2108390109003
Program 6
Objective: Convert NFA to DFA
2108390109003
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)
2108390109003
Program
#include<stdio.h>
#include<stdlib.h>
struct node
{
int st;
struct node *link;
};
struct node1
{
int nst[20];
};
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO.-2108390109003
void insert(int ,char, int);
int findalpha(char);
void findfinalstate(void);
int insertdfastate(struct node1);
int compare(struct node1,struct node1);
void printnewstate(struct node1);
static int
set[20],nostate,noalpha,s,notransition,nofinal,start,finalstate[20],
c,r,buffer[20];
int complete=-1;
char alphabet[20];
static int eclosure[20][20]={0};
struct node1 hash[20];
struct node * transition[20][20]={NULL};
//RAJ PATEL
//CSE 3 YEAR
2108390109003
//ROLL_NO.-2108390109003
void main()
{
int i,j,k,m,t,n,l;
struct node *temp;
struct node1 newstate={0},tmpstate={0};
alphabet[i]=getchar();
getchar();
}
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO.-2108390109003
printf("Enter the number of states?\n");
scanf("%d",&nostate);
printf("Enter the start state?\n");
scanf("%d",&start);
printf("Enter the number of final states?\n");
scanf("%d",&nofinal);
printf("Enter the final states?\n");
for(i=0;i<nofinal;i++)
scanf("%d",&finalstate[i]);
printf("Enter no of transition?\n");
2108390109003
scanf("%d",¬ransition);
printf("NOTE:- [Transition is in the form–> qno alphabet qno]\
n",notransition);
printf("NOTE:- [States number must be greater than zero]\n");
printf("\nEnter transition?\n");
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO.-2108390109003
for(i=0;i<notransition;i++)
{
scanf("%d %c%d",&r,&c,&s);
insert(r,c,s);
}
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
hash[i].nst[j]=0;
}
complete=-1;
i=-1;
printf("\nEquivalent DFA.....\n");
printf(".......................\n");
printf("Trnsitions of DFA\n");
newstate.nst[start]=start;
insertdfastate(newstate);
while(i!=complete)
{ i++;
2108390109003
newstate=hash[i];
for(k=0;k<noalpha;k++)
{
c=0;
for(j=1;j<=nostate;j++)
set[j]=0;
for(j=1;j<=nostate;j++)
{
l=newstate.nst[j];
if(l!=0)
{
temp=transition[l][k];
while(temp!=NULL)
{
if(set[temp->st]==0)
{
c++;
set[temp->st]=temp->st;
}
temp=temp->link;
}
}
}
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO.-2108390109003
printf("\n");
if(c!=0)
{
for(m=1;m<=nostate;m++)
tmpstate.nst[m]=set[m];
2108390109003
insertdfastate(tmpstate);
printnewstate(newstate);
printf("%c\t",alphabet[k]);
printnewstate(tmpstate);
printf("\n");
}
else
{
printnewstate(newstate);
printf("%c\t", alphabet[k]);
printf("NULL\n");
}
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO.-2108390109003
}
}
printf("\nStates of DFA:\n");
for(i=0;i<=complete;i++)
printnewstate(hash[i]);
printf("\n Alphabets:\n");
for(i=0;i<noalpha;i++)
printf("%c\t",alphabet[i]);
printf("\n Start State:\n");
printf("q%d",start);
printf("\nFinal states:\n");
findfinalstate();
}
int insertdfastate(struct node1 newstate)
{
2108390109003
int i;
for(i=0;i<=complete;i++)
{
if(compare(hash[i],newstate))
return 0;
}
complete++;
hash[complete]=newstate;
return 1;
}
int compare(struct node1 a,struct node1 b)
{
int i;
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO.-2108390109003
for(i=1;i<=nostate;i++)
{
if(a.nst[i]!=b.nst[i])
return 0;
}
return 1;
2108390109003
if(j==999)
{
printf("error\n");
exit(0);
}
temp=(struct node *) malloc(sizeof(struct node));
temp->st=s;
temp->link=transition[r][j];
transition[r][j]=temp;
}
int findalpha(char c)
{
int i;
for(i=0;i<noalpha;i++)
if(alphabet[i]==c)
return i;
return(999);
}
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO.-2108390109003
void findfinalstate()
{
int i,j,k,t;
for(i=0;i<=complete;i++)
{
2108390109003
for(j=1;j<=nostate;j++)
{
for(k=0;k<nofinal;k++)
{
if(hash[i].nst[j]==finalstate[k])
{
printnewstate(hash[i]);
printf("\t");
j=nostate;
break;
} } }}}
void printnewstate(struct node1 state)
{
int j;
printf("{");
for(j=1;j<=nostate;j++)
{
if(state.nst[j]!=0)
printf("q%d,",state.nst[j]);
}
printf("}\t");
}
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO.-2108390109003
Output
2108390109003
Enter the number of states?
4
Enter the start state?
1
Enter the number of final states?
2
Enter the final states?
3
4
Enter no of transition?
8
NOTE:- [Transition is in the form–> qno alphabet qno]
NOTE:- [States number must be greater than zero]
Enter transition?
1 a 1
1 b 1
1 a 2
2 b 2
2 a 3
3 a 4
3 b 4
4 b 3
Equivalent DFA.....
.......................
Trnsitions of DFA
{q1,} a {q1,q2,}
{q1,} b {q1,}
2108390109003
{q1,q2,} a {q1,q2,q3,}
{q1,q2,} b {q1,q2,}
{q1,q2,q3,} a {q1,q2,q3,q4,}
{q1,q2,q3,} b {q1,q2,q4,}
{q1,q2,q3,q4,} a {q1,q2,q3,q4,}
{q1,q2,q3,q4,} b {q1,q2,q3,q4,}
{q1,q2,q4,} a {q1,q2,q3,}
{q1,q2,q4,} b {q1,q2,q3,}
States of DFA:
{q1,} {q1,q2,} {q1,q2,q3,} {q1,q2,q3,q4,} {q1,q2,q4,}
Alphabets:
a b
Start State:
q1
Final states:
{q1,q2,q3,} {q1,q2,q3,q4,} {q1,q2,q4,}
Program 7
Objective: Minimize any given DFA.
2108390109003
Minimization of DFA
Minimization of DFA means reducing the number of states from given
FA. Thus, we get the FSM(finite state machine) with redundant states
after minimizing the FSM.
We have to follow the various steps to minimize the DFA. These are
as follows:
Step 1: Remove all the states that are unreachable from the initial
state via any set of the transition of DFA.
Step 3: Now split the transition table into two tables T1 and T2. T1
contains all final states, and T2 contains non-final states.
1. 1. δ (q, a) = p
2. 2. δ (r, a) = p
That means, find the two states which have the same value of a and b
and remove one of them.
Program
//RAJ PATEL
//CSE 3 YEAR
2108390109003
//ROLL_NO-2108390109003
#include <stdio.h>
#include <stdlib.h>
struct state{
int input[100];
int isFinal;
int current_set;
int last_set;
int value;
}table[100];
char input[100];
int generate_current_set(int );
void current_to_last(int );
void change_name(int,int,int);
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO-2108390109003
int main()
int n,m,t=-1,set=2,i,j,key,q,flag,p,u;
scanf("%d",&n);
scanf("%d",&m);
fflush(stdin);
gets(input);
for(int i=0;i<n;i++)
for(j=0;j<m;j++)
2108390109003
{
printf("Input %c \t",input[j]);
scanf("%d",&table[i].input[j]);
scanf("%d",&table[i].last_set);
table[i].isFinal=table[i].last_set;
flag=0;
q=1;
p=3;
print(table,n-1,m);
u=0;
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO-2108390109003
while(p!=q)
//compute function calculates the set value of every state and then
adds all the set values in array aar1 and returns the size of array aar1
if(flag==1)
q=p;
compute_value(n,m);
p=generate_current_set(n);
current_to_last(n);
for(int i=0;i<n;i++)
printf("%d",table[i].current_set);
printf("\n");
flag=1;
change_name(n,p,m);
2108390109003
//printf("%d",p);
print(optimized,p,m);
int ans,c;
for(int i=0;i<n;i++)
ans=0;
c=1;
for(int j=0;j<m;j++)
ans=ans+table[table[i].input[j]].last_set*c;
c*=10;
table[i].value=ans;}}
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO-2108390109003
int generate_current_set(int n)
int c=-1,flag;
for(int i=0;i<n;i++)
flag=0;
if(table[i].last_set==table[j].last_set&&table[i].value==table[j].value)
table[i].current_set=table[j].current_set;
flag=1;
break; } }
if(flag!=1)
2108390109003
{ c++;
table[i].current_set=c; }}
return c;
void current_to_last(int n)
for(int i=0;i<n;i++)
table[i].last_set=table[i].current_set;
for(int i=0;i<=c;i++)
for(int j=0;j<n;j++)
if(table[j].current_set==i)
for(int k=0;k<m;k++)
optimized[i].input[k]=table[j].input[k];
optimized[i].isFinal=table[j].isFinal;
break;
}}}
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO-2108390109003
for(int i=0;i<=c;i++)
for(int j=0;j<m;j++){
optimized[i].input[j]=table[optimized[i].input[j]].current_set; }}}
int i,j;
2108390109003
printf("\n\n\n\ns/i");
for(j=0;j<m;j++)
printf("\t%c",input[j]);
printf("\n");
for(i=0;i<=n;i++)
printf("%d",i);
if(tabl[i].isFinal==1)
printf("*\t");
else
printf("\t");
for(j=0;j<m;j++)
printf("%d\t",tabl[i].input[j]);
printf("\n");
//RAJ PATEL
//CSE 3 YEAR
//ROLL_NO-2108390109003
Output
ab
Input a 1
Input b 2
2108390109003
Is 0 a final state0
Input a 1
Input b 3
Is 1 a final state0
Input a 1
Input b 2
Is 2 a final state0
Input a 1
Input b 4
Is 3 a final state0
Input a 1
Input b 2
Is 4 a final state1
s/i a b
0 1 2
1 1 3
2 1 2
3 1 4
4* 1 2
1 equivalent 00012
2 equivalent 01023
3 equivalent 01023
s/i a b
0 1 0
1 1 2
2 1 3
2108390109003
3* 1 0
2108390109003