Last active
March 9, 2024 22:53
-
-
Save AmalJossy/d7e83c3fafb214920a1c0df2430e6502 to your computer and use it in GitHub Desktop.
Program to minimize a given DFA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/* input format | |
row 1: input symbols | |
row 2: non-final states | |
row 3: final states | |
rows>3: transition table | |
*/ | |
int table[10][10]; | |
int matrix[10][10]={0}; | |
int indexOf(char *array,char x,int max){ | |
int i=0; | |
for(;i<max;i++){ | |
if(array[i]==x) | |
return i; | |
} | |
return -1; | |
} | |
int movesToMarked(int i, int j, int l){ | |
int k,x,y; | |
for(k=0;k<l;k++){ | |
x=table[i][k]; // index of next state on kth input | |
y=table[j][k]; | |
if(x!=-1 && y!=-1 && matrix[x][y]==1){ | |
return 1; | |
} | |
} | |
return 0; | |
} | |
void printStates(char *states, int n, int f){ // prints new states | |
int i,j; | |
int store=0; //stores all encountered states | |
int bin, final; | |
for(i=0;i<n+f;i++){ | |
bin=1<<i; //bitmask of state to add | |
if((store&bin)!=0) | |
continue; | |
final=(i>n)? 1 : 0; // is final state | |
store=store|bin; | |
printf("%c",states[i]); | |
for(j=i+1;j<n+f;j++){ | |
if(matrix[j][i]==0){ | |
bin=1<<j; | |
store=store|bin; | |
if(j>n) final=1; | |
printf("%c",states[j]); | |
} | |
} | |
if(final==1) | |
printf(" (f) "); | |
printf("\n"); | |
} | |
} | |
int main(){ | |
char language[10], ch; | |
int l = 0; | |
while (1){ //read input symbols till new line encountered | |
language[l++] = getchar(); | |
if(getchar()=='\n') | |
break; | |
} | |
int n = 0, f = 0; // n: no of non-final states, f: no of final states | |
char states[10]; // array to store all states | |
while (1){ | |
states[n++] = getchar(); //read input symbols till new line encountered | |
if(getchar()=='\n') | |
break; | |
} | |
char *finalstates; | |
finalstates = states + n; // address of first final state (not really required) | |
while (1){ | |
finalstates[f++] = getchar(); | |
if(getchar()=='\n') | |
break; | |
} | |
int i,j; | |
for (i = 0; i < n + f; i++){ | |
for(j=0;j<l;j++){ | |
scanf(" %c",&ch); | |
table[i][j]=indexOf(states,ch,n+f); // get transition table | |
} | |
} | |
for(i=0;i<n+f;i++){ | |
matrix[i][i]=-1; // diagonal elements not required | |
} | |
for(i=n;i<n+f;i++){ | |
for(j=0;j<n;j++){ | |
matrix[i][j]=1; | |
matrix[j][i]=1; // makes matrix symmetric so (i,j)=(j,i) and order becomes irrelevent | |
} | |
} | |
int change=1; | |
while(change){ | |
change=0; | |
for(i=0;i<n+f;i++){ | |
for(j=0;j<n+f;j++){ | |
if(matrix[i][j]==0){ | |
if(movesToMarked(i,j,l)){ // mark i,j of i,j moves to a marked pair on some input | |
change=1; | |
matrix[i][j]=1; | |
matrix[j][i]=1; | |
} | |
} | |
} | |
} | |
} | |
printf(" "); // print matrix | |
for(i=0;i<n+f;i++){ | |
printf("%c ",states[i]); | |
} | |
printf("\n"); | |
for(i=0;i<n+f;i++){ | |
printf("%c ",states[i]); | |
for(j=0;j<i;j++){ | |
printf("%d ",matrix[i][j]); | |
} | |
printf("\n"); | |
} | |
printStates(states,n,f); // print new states | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment