java - 用JAVA ArrayList实现DFA最小化算法

标签 java algorithm arraylist dfa

我已经实现了 DFA minimization algorithm与数组列表,但它不返回正确的答案。如果有人能指出我遗漏了算法的哪一部分,我将不胜感激。(并纠正我是否使用了有效的方法来实现它)。

该程序应该从文件中读取数据,然后对其进行处理。但是这个函数与那些数据无关。我已经对其进行了硬编码以使其工作。

实现算法的方法名为(unreachableStates)

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

enter image description here

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
}

最佳答案

经过一个小时的调试,它开始工作了。我只需要更改我在 DEBUG I(有问题)中提到的问题。我将其更改如下:

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

关于java - 用JAVA ArrayList实现DFA最小化算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26725229/

相关文章:

java - 如何使用java从数组中删除/添加项目

algorithm - 如何确定地球上的点和线之间的最短路径?

java - IndexOutOfBoundsException : Index: 0, 大小:0 2D Arraylist

Java算法制作直角金字塔

java - 为什么我的方法打印一个空列表?

java - 尝试获取 hashmap 键内的 arraylist 值

java - java.io.BufferedWriter.min(int a, int b) 有什么意义?

java迭代器/可迭代子接口(interface)

java - 正则表达式分组混淆简单示例

algorithm - 我如何通过以最小步长增加每个数字来使整数数组都相对质数?