用JAVA ArrayList实现DFA最小化算法

用JAVA ArrayList实现DFA最小化算法,java,algorithm,arraylist,dfa,Java,Algorithm,Arraylist,Dfa,我已经用数组列表实现了,但是它没有返回正确的答案。如果有人能指出我遗漏了算法的哪一部分,我将不胜感激 程序应该从文件中读取数据,然后处理它。但是这个函数与这些数据无关。我已经把它硬编码好了 实现该算法的方法名为(Unreaccessates) DEBUG I:所以我仔细检查了代码,发现问题在于围绕表达式| temp.add(transitionTable[j][I])|的循环。这将用于前2次迭代,但之后它将不考虑所有的状态。现在的挑战是修复它 package dRegAut; import j

我已经用数组列表实现了,但是它没有返回正确的答案。如果有人能指出我遗漏了算法的哪一部分,我将不胜感激

程序应该从文件中读取数据,然后处理它。但是这个函数与这些数据无关。我已经把它硬编码好了

实现该算法的方法名为(Unreaccessates)

DEBUG I:所以我仔细检查了代码,发现问题在于围绕表达式| temp.add(transitionTable[j][I])|的循环。这将用于前2次迭代,但之后它将不考虑所有的状态。现在的挑战是修复它

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
}

package-dregout;
导入java.io.*;
导入java.util.ArrayList;
导入java.util.Iterator;
公共级dfamin{
//保存文件中数据的全局变量
纽姆州、纽马尔哈贝茨州、纽姆芬纳州的私营企业;
专用字符字母[];
私有布尔最终状态[];
私有int[][]可传递;
/**
*@param args
*@抛出异常
*@throws numberformatexception
*/
公共静态void main(字符串[]args)为MateException、IOException抛出numberf{
国际新州、新墨西哥州、新墨西哥州;
字符字母表[];
布尔最终状态[];
int[][]可传递;
//获取文件名并打开一个流来读取它
FileInputStream fileStream=newfileinputstream(“/path/to/file/trace”);
BufferedReader br=新的BufferedReader(新的InputStreamReader(fileStream));
//存储文件中的每一行
弦线;
//从文件中读取每一行
而((line=br.readLine())!=null){
//从每行读取单间隔数据
String[]splittedLine=line.split(“”);
//从这行读numStates,nummalphabets
numStates=Integer.parseInt(splittedLine[0]);
numAlphabets=Integer.parseInt(splittedLine[1]);

//对于(int a=0;a),调试了一个小时后,它开始工作。我只需更改我在调试I(有问题)中提到的问题。我将其更改如下:

transitionTable[newStates.get(j)][i]

这是一个太多的代码,有太多的粗枝大叶,没有什么用处。清理一下,还要注意,你首先要负责找出哪里出了问题:确保所有的算法步骤都是独立的方法,并测试它们,看看它们是否自己做了应该做的事情。这行得通吗?测试它们,看看是什么当它们按顺序运行时,它们会这样做,等等。然后,如果你仍然不能弄清楚,这里是询问它的地方,有关于你已经尝试过但没有帮助你解决问题的所有信息。@Mike'Pomax'Kamermans我实际上试着调试它,(这就是为什么代码中有这么多注释),但我无法理解。但我会再做一次,首先集中精力不要让剩下的代码。使用一些硬编码的输入,使
不可访问的
方法本身可以运行,然后逐步完成。