Updated the minimize algorithm to properly reduce itself.

master
mercury 2003-12-10 18:11:16 +00:00
parent c2b7ab58ca
commit 42d146c241
1 changed files with 40 additions and 30 deletions

View File

@ -110,9 +110,9 @@ public class DFA {
// find all pairs of states (p,q) such that final[p] != final[q] // find all pairs of states (p,q) such that final[p] != final[q]
for ( int statenum = 0; statenum < numstates; statenum++ ) { for ( int statenum = 0; statenum < numstates; statenum++ ) {
//if ( !reachable[statenum] ) continue; if ( !reachable[statenum] ) continue;
for ( int statenum2 = 0; statenum2 < numstates; statenum2++ ) { for ( int statenum2 = 0; statenum2 < numstates; statenum2++ ) {
//if ( !reachable[statenum2] ) continue; if ( !reachable[statenum2] ) continue;
if ( finalstates[statenum] != finalstates[statenum2] ) if ( finalstates[statenum] != finalstates[statenum2] )
mark[statenum][statenum2] = true; mark[statenum][statenum2] = true;
else else
@ -128,7 +128,7 @@ public class DFA {
while ( flag ) { while ( flag ) {
flag = false; flag = false;
for ( int x = 0; x < numstates; x++ ) { for ( int x = 0; x < numstates; x++ ) {
//if ( !reachable[x] ) continue; if ( !reachable[x] ) continue;
for ( int y = 0; y < numstates; y++ ) { for ( int y = 0; y < numstates; y++ ) {
if ( x == y ) continue; if ( x == y ) continue;
for ( int t = 0; t < numterminals; t++ ) { for ( int t = 0; t < numterminals; t++ ) {
@ -141,8 +141,6 @@ public class DFA {
}// endfor first state }// endfor first state
}// end while }// end while
int numminimized = numstates;
int numminfinal = numfinalstates;
int minstates[] = new int[numstates]; int minstates[] = new int[numstates];
for ( int state = 0; state < numstates; state++ ) { for ( int state = 0; state < numstates; state++ ) {
visited[state] = false; visited[state] = false;
@ -151,44 +149,56 @@ public class DFA {
else else
minstates[state] = state; minstates[state] = state;
} }
// extract the distinguishable states // extract the distinguishable states
for ( int state = 0; state < numstates; state++ ) { for ( int state = 0; state < numstates; state++ ) {
if ( !reachable[state] || visited[state] ) continue; if ( minstates[state] == -1 || visited[state] ) continue;
for ( int p = 0; p < numstates; p++ ) { for ( int p = 0; p < numstates; p++ ) {
if ( p == state ) continue; if ( p == state || minstates[p] == -1 ) continue;
if ( !mark[state][p] && !visited[p] ) { if ( !mark[state][p] && !visited[p] ) {
minstates[p] = state; minstates[p] = state;
visited[p] = true; visited[p] = true;
if ( finalstates[p] )
numminfinal--;
else
numminimized--;
} }
}// endfor p }// endfor p
}// endfor state }// endfor state
// update paths between states // set minstate to have all of the new minimized state numbers
for ( int u = 0; u < numstates; u++ ) { int unique = 0;
for ( int t = 0; t < numterminals; t++ ) { for ( int x = 0; x < minstates.length; x++ ) {
path[u][t] = minstates[path[u][t]]; if ( minstates[x] == x && minstates[x] != -1 ) {
}// endfor t for ( int y = 0; y < minstates.length; y++ ) {
}// endfor u if ( minstates[y] == x ) {
minstates[y] = unique;
// make new data variables and update them to the proper values }
int newpath[][] = new int[numminimized][numterminals]; }
boolean newfinal[] = new boolean[numminimized]; unique++;
int mrow = 0;
for ( int row = 0; row < numstates; row++ ) {
if ( visited[row] ) continue;
for ( int t = 0; t < numterminals; t++ ) {
newpath[mrow][t] = path[row][t];
} }
newfinal[mrow] = finalstates[row];
mrow++;
} }
// rearrange all the paths using the minstate array into a new path array
int newpath[][] = new int[numstates][numterminals];
boolean newfinal[] = new boolean[numstates];
int minstatecounter = 0, minfinalcounter = 0;
for ( int state = 0; state < numstates; state++ ) {
// find all accessible vertecies and vertecies that have not been counted already
if ( minstates[state] != -1 && minstatecounter <= minstates[state] ) {
for ( int t = 0; t < numterminals; t++ ) {
newpath[minstatecounter][t] = minstates[ path[state][t] ];
}
if ( finalstates[state] ) {
newfinal[minstatecounter] = true;
minfinalcounter++;
}
else {
newfinal[minstatecounter] = false;
}
minstatecounter++;
}
}
path = newpath; path = newpath;
numstates = numminimized; numstates = minstatecounter;
numfinalstates = numminfinal; numfinalstates = minfinalcounter;
finalstates = newfinal; finalstates = newfinal;
} }
} }