I have implemented the DFA minimization algorithm with array lists, but it doesn't return the correct answer. If someone could point out which part of the algorithm I am missing, I would appreciate it.(And correction on whether I have used an efficient way to implement it).
The program is supposed to read data from a file and then work on it. But this function has nothing to do with those data. I have hard coded it to work.
The method that implements the algorithm is named (unreachableStates)
DEBUG I: So I went thorough the code, and found out that the problem is the loop that surround the expression | temp.add(transitionTable[j][i]) |. This will work for the first 2 iterations, but after that it will not consider all the states. Now the challenge is to fix it.
package dRegAut;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
public class dfamin {
// Global variables to hold data from the file
private int numStates,numAlphabets,numFinalStates;
private char alphabets[];
private boolean finalStates[];
private int [][] transitionTable;
/**
* @param args
* @throws IOException
* @throws Numberfor matException
*/
public static void main(String[] args) throws Numberfor matException, IOException {
int numStates,numAlphabets,numFinalStates;
char alphabets[];
boolean finalStates[];
int [][] transitionTable;
// Take file name and open a stream to read it
FileInputStream fileStream = new FileInputStream("/path/to/file/trace");
BufferedReader br = new BufferedReader(new InputStreamReader(fileStream));
// Store each line from the file
String line;
// Read each line from file
while((line = br.readLine()) != null){
// Read single spaced data from each line
String [] splittedLine = line.split(" ");
// Read numStates,numAlphabets from the line
numStates = Integer.parseInt(splittedLine[0]);
numAlphabets = Integer.parseInt(splittedLine[1]);
//for (int a=0;a<numAlphabets;a++){
//alphabets[a] = '0';
//}
transitionTable = new int[numStates][numAlphabets];
int tt= 2;
// Loop thorough the line and read transition table
for (int row=0;row<numStates;row++){
for (int col=0;col<numAlphabets;col++){
transitionTable[row][col] = Integer.parseInt(splittedLine[tt]);
tt++;
} // End of for -loop to go thorough alphabets
} // End of for -loop to go thorough states
// Read number of final states
numFinalStates = Integer.parseInt(splittedLine[2+numStates*numAlphabets]);
//System.out.println(numFinalStates);
// Read final states
int z=0;
finalStates = new boolean[numStates];
int start = 3+numStates*numAlphabets ;
int end = (3+(numStates*numAlphabets))+numFinalStates;
for (int fs=start;fs<end;fs++){
finalStates[ Integer.parseInt(splittedLine[fs])] = true;
//System.out.println(finalStates[z]);
z++;
} // End of for -loop to read all final states
dfamin x = new dfamin(numStates,numAlphabets,numFinalStates,finalStates,transitionTable);
//x.minimizer();
//System.out.println(x);
//System.out.println("======================");
int [][] ttt = {{1,2},{0,2},{1,0},{1,2}};
x.unreachableStates(4,2,ttt);
} // End of while-loop to read file
// Close the stream
br.close();
}
dfamin(int nS,int nA,int nFS,boolean fS[], int [][] tT){
numStates = nS;
numAlphabets = nA;
numFinalStates = nFS;
//alphabets = a;
finalStates = fS;
transitionTable = tT;
} // End of DFAMinimizer constructor
/*
* A method to minmize the dfa
*/
public void minimizer(){
} // End of minimizer method
/*
* A method to find unreachable states
*
*/
public void unreachableStates(int numStates, int numAlphabets, int [][] transitionTable){
// Initialize a list to hold temporary list of states in it
ArrayList<Integer> reachableStates =new ArrayList();
ArrayList<Integer> newStates = new ArrayList();
// Start from the state zero
reachableStates.add(0);
newStates.add(0);
// Temporary array to hold reachable states
ArrayList<Integer> temp = new ArrayList();
// Loop until there is data in newStates
do {
// Empty temp array
temp.clear();
for (int j=0;j<newStates.size();j++){
for (int i=0; i<numAlphabets;i++){
//System.out.printf("Alphabets:%d State:%ds ",i,j);
//System.out.printf("State:%d newStates:%d \n",j, newStates.get(j));
//System.out.printf("transitionTable: %d\n",transitionTable[j][i]);
temp.add(transitionTable[j][i]);
//System.out.printf("Temp[%d] = %d",i,temp.get(i));
//System.out.printf("Alphabets: %d", i);
} // End of for -loop to go thorough all characters
//System.out.printf("newStates: %d\n",newStates.get(j));
} // End of for -loop to go thorough all elements of the newStates array list
//System.out.printf("newStateSize: %d",newStates.size());
// Clear newStates list
newStates.clear();
//System.out.printf("Temp Size: %d", temp.size());
// Add the elements that are in temp, but are not in reachableStates to newStates
for (int z=0;z<temp.size();z++){
for (int z1=0; z1<reachableStates.size();z1++){
// if the state was already present, don't add
if (temp.get(z) == reachableStates.get(z1)){
break;
}
if (temp.get(z) != reachableStates.get(z1) && z1 == reachableStates.size()-1){
//System.out.printf("Temp:%d reachableStates:%d z:%d z1:%d \n",temp.get(z),reachableStates.get(z1),z,z1);
newStates.add(temp.get(z));
}
//System.out.printf("ReachableStates: %d ", reachableStates.get(z1));
} // End of for -loop to go thorough all reachableStates elements and check if a match
} // End of for -loop thorugh all temp states
//System.out.printf("NewStates Size after loop:%d \n",newStates.size());
if (!newStates.isEmpty()){
// Add the newStates elements to reachable states
for (int y=0;y<newStates.size();y++){
//System.out.printf("newStates:%d newStatesSize:%d in %d",newStates.get(y),newStates.size(),y);
reachableStates.add(newStates.get(y));
}
}
/*
//System.out.println();
System.out.printf("reachable states:");
for (int y=0;y<reachableStates.size();y++){
System.out.printf("%d",reachableStates.get(y));
}
System.out.printf("End!\n");
*/
} while(!newStates.isEmpty());
System.out.printf("Reachable sadfStates: ");
for (int w = 0;w<reachableStates.size()-1;w++){
System.out.printf(" %d ",reachableStates.get(w));
}
System.out.println();
} // End of unreachableStates method
}
After an hour of debugging, got it working. I just had to change the problem that I mentioned in DEBUG I (in question). I changed it as following:
transitionTable[newStates.get(j)][i]