簡體   English   中英

使用JAVA ArrayList實現DFA最小化算法

[英]Implementing DFA minmization algorithm with JAVA ArrayList

我已經使用數組列表實現了DFA最小化算法 ,但是它沒有返回正確的答案。 如果有人指出我缺少算法的哪一部分,我將不勝感激(並更正我是否使用了有效的方法來實現它)。

該程序應該從文件中讀取數據,然后對其進行處理。 但是此功能與這些數據無關。 我已經對其進行了硬編碼。

實現該算法的方法稱為(unreachableStates)

調試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
}

經過一個小時的調試,使其正常運行。 我只需要更改在DEBUG I(有問題)中提到的問題。 我將其更改如下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM