Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Having trouble figuring out how to recursively do this.

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Having trouble figuring out how to recursively do this.

    It's me. I had posted here but that issue of the readin was done so I marked the thead as solved, or I'm going to soon. However, now I'm wondering how to recursively calcualate something. I'm going to post the whole program but the chief concern right now is with the getEpsilon() method and also you'd need to know about the ms ArrayList.

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Iterator;
     
     
    public class fsm
    {
     
    private static class FileParser
    {
     
     
     
    }
     
    private static class MySet
    {
     
    private  ArrayList<Integer> intList;
     
    public MySet()
    {
    intList = new ArrayList<Integer>();
     
    }
     
    public  void add(Integer data)
    {
    intList.add(data);
     
    }
     
    public void remove()
    {
    intList.remove(0);
     
    }
     
    public int size()
    {
    return intList.size();
    }
     
    public Integer get(int index) throws NullPointerException
    {
    return intList.get(index);
     
    }
     
    public ArrayList<Integer> getList()
    {
    return intList;
    }
     
     
     
    }
     
     static MySet[][] ms;
     static int characters;
     static HashSet<HashSet<Integer>> sos;
        public static void main(String[] args)
        {
    	Scanner s = null;
    	Scanner s2 = null;
    	ArrayList<Integer> stateList = new ArrayList<Integer>();
    	ArrayList<Character> alphabet = new ArrayList<Character>();
     
    	ArrayList<String> transitionLines = new ArrayList<String>();
    	ArrayList<String[]> splitArray = new ArrayList<String[]>();
     
     
     
     
     
     
     
     
    	int numOfStates = 0;
     
    try
        {
       s  = new Scanner(new File(args[0]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
    numOfStates = s.nextInt();
     
        System.out.println(numOfStates);
     
        String[][] splits = new String[numOfStates][2];
     
     
     
     
        String line = s.nextLine();
        if (line.equals(""))
        line = s.nextLine();
     
     
     
     
        System.out.println(line);
     
        char[] chars = line.toCharArray();
     
     
     
     
        char empty = ' ';
     
     
        String tab = "\t";
        char tab2 = tab.charAt(0);
     
        for (int i =0; i < chars.length; i++)
        {
        if (chars[i]!= empty && chars[i]!=tab2)
        {
        alphabet.add(chars[i]);
        }
        }
     
        alphabet.add('¸');
     
         System.out.println(alphabet.toString());
     
         characters = alphabet.size();
     
     
         for (int i=0; i < numOfStates; i++)
         {
         transitionLines.add(s.nextLine());
     
         }
     
         System.out.println(transitionLines.toString());
     
     
         for (int i =0; i < splits.length; i++)
         {
         splits[i] = transitionLines.get(i).split(":");
     
         }
     
     
         for (int i =0; i < splits.length; i++)
         {
         for (int j=0; j < splits[i].length; j++)
         {
         System.out.println(splits[i][j]);
     
         }
     
     
         }
     
       //  String[][] test = 
         ArrayList<String[]> fredBowling = new ArrayList<String[]>();
     
         for (int i =0; i < splits.length; i++)
         {
         fredBowling.add(splits[i][1].split("}"));
         }
         splits[0][1].split("}");
     
         ArrayList<Integer> state = new ArrayList<Integer>();
     
         for (int i=0; i < splits.length; i++)
         {
         state.add(Integer.parseInt(splits[i][0]));
     
         }
     
     
       //  System.out.println(java.util.Arrays.toString(test));
     
     
        // String smaller[] = new String[test.length];
     
        ArrayList<ArrayList<String>> smaller = new ArrayList<ArrayList<String>>();
     
         for (int i =0; i < fredBowling.size(); i++)
         {
         smaller.add(new ArrayList<String>());
         for (int j = 0; j < fredBowling.get(i).length; j++)
         {
         int temp = fredBowling.get(i)[j].indexOf("{");
     
         smaller.get(i).add(fredBowling.get(i)[j].substring(temp + 1));
         }
     
         }
     
       //  System.out.println(java.util.Arrays.toString(smaller));
     
        //  ArrayList<String[]> finale = new ArrayList<String[]>();
         ArrayList<ArrayList<String[]>> finale = new ArrayList<ArrayList<String[]>>();
     
         // for (int i =0; i < smaller.length; i++)
      //    {
       //   finale.add(smaller[i].split(","));
     
     //    }
     
         for (int i =0; i < smaller.size(); i++)
         {
         finale.add(new ArrayList<String[]>());
         for (int j =0; j < smaller.get(i).size(); j++)
         {
         finale.get(i).add(smaller.get(i).get(j).split(","));
     
         }
     
     
         }
     
         for (int i =0; i < finale.size(); i++)
         {
         for (int j =0; j < finale.get(i).size(); j++)
         {
         for (int k =0; k < finale.get(i).get(j).length; k++)
         {
         System.out.println("MySet[" + i + "]["+j +"].get(" + k + ") = " +finale.get(i).get(j)[k]);
         }
     
     
         }
     
         }
     
          ms = new MySet[finale.size()][finale.get(0).size()];
     
         for (int i =0; i < finale.size(); i++)
         { // begin for
     
         for (int j =0; j < finale.get(i).size(); j++)
         { // begin for 
         try{
     
         ms[i][j] = new MySet();
     
     
     
         for (int k =0; k < finale.get(i).get(j).length; k++)
         { // begin for
         if (finale.get(i).get(j)[k] != null && !finale.get(i).get(j)[k].equals(""))
         { // begin if 
         ms[i][j].add(Integer.parseInt(finale.get(i).get(j)[k]));
     
         } // end if
     
         } // end for
     
         } // end try
           catch(ArrayIndexOutOfBoundsException aioobe)
         {
         System.err.println("Index " + i + "," + j + " is out of bounds.");
         }
     
         } // end for
     
     
         } // end for
     
         for (int i =0; i < ms.length; i++)
         {
         for (int j =0; j < ms[i].length; j++)
         {
         for (int k =0; k < ms[i][j].size(); k++)
         {
         System.out.println("ms[" + i + "]["+j +"].get(" + k + ") = "+ ms[i][j].get(k));
     
         }
         }
     
         }
     
         int startingState = s.nextInt();
         System.out.println("s:" + startingState);
     
         String fs = s.nextLine();
     
         if (fs.equals("") || fs == null)
         fs = s.nextLine();
     
         String[] firstHalf = fs.split("}");
     
     
          int temp = firstHalf[0].indexOf("{");
     
          String secondHalf = firstHalf[0].substring(temp+1);
     
          String[] finalHalf = secondHalf.split(",");
     
     
         ArrayList<Integer> finalState = new ArrayList<Integer>();
     
         for (int i =0; i < finalHalf.length; i++)
    {
    if (finalHalf[i] != null && !finalHalf[i].equals(""))
    {
    finalState.add(Integer.parseInt(finalHalf[i]));
    }
     
    }
     
    System.out.println("Accepting state: " + finalState.toString());
     
    System.out.println(getFinalStateFrom(0, 0).toString());
     
    System.out.println(recursion1(0,0).toString());
     
    System.out.println(getEpsilon().toString());
     
    try
        {
       s2  = new Scanner(new File(args[1]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
     
     
     
     
     
        }
     
        public static HashSet<Integer> getEpsilon()
        {
        HashSet<Integer> hs = new HashSet<Integer>();
        ArrayList<Integer> temp = new ArrayList<Integer>(); // this is only here to store stuff and access it easier without an evil iterator
     
        hs.add(0);
        temp.add(0);
     
      //  int i = 0; // used for the while loop below
        Integer temp2 = 0; // used to check if the next item in epsilon is null.
        MySet ms3 = new MySet();
     
        ms3 = ms[0][ms[0].length -1];
     
        if (ms3 == null)
        return hs;
     
        else
        {
     
        for (int i =0; i < ms3.size(); i++)
        hs.add(ms3.get(i));
     
     
        }
     
     
        for (int i =0; i < ms3.size(); i++)
        {
     
        while (ms[ms3.get(i)][ms[0].length-1] !=null)
        {
     
     
     
     
        }
     
     
        }
     
     
     
     
     
        return hs;
        }
     
     
        public static HashSet<Integer> getLambdas(Integer state)
        {
     
        HashSet<Integer> hs = new HashSet<Integer>();
     
       if (ms[state][characters - 1] != null)
        {
        for (int i =0; i < ms[state][characters - 1].size(); i++)
        {
        if (ms[state][characters - 1].get(i) != null)
        {
        hs.add(ms[state][characters -1].get(i));
        }
        }
        }
     
        return hs;
        }
     
     
        public static HashSet<Integer> getFinalStateFrom(Integer state, Integer character)
        {
     
        ArrayList<Integer> al = new ArrayList<Integer>();
     
        HashSet<Integer> hs = new HashSet<Integer>();
     
     
        if (ms[state][character] != null)
        {
        for (int i =0; i < ms[state][character].size(); i++)
        {
        if (ms[state][character].get(i) != null)
        {
        hs.add(ms[state][character].get(i));
        }
        }
        }
     
     
      //  hs.addAll(getFinalStateFrom(state, characters - 1);
     
      System.out.println("Added: " + hs.toString());
     
        hs.addAll(getLambdas(state));
     
        System.out.println("Added: " + hs.toString());
     
        return hs;
     
     
     
     
     
        }
     
        public static HashSet<Integer> recursion1(Integer state, Integer character)
        {
        HashSet<Integer> hs = new HashSet<Integer>();
        for (Iterator<Integer> it = getFinalStateFrom(state,character).iterator(); it.hasNext();)
        {
        try
        {
        hs.addAll(getFinalStateFrom(getFinalStateFrom(it.next(), characters -1).iterator().next(), character));
        }
     
        catch(java.util.NoSuchElementException nsee)
        {
        System.out.println("This element doesn't exist!");
        }
     
        }
        return hs;
        }
     
     
    }

    In the method

    public static HashSet<Integer> getEpsilon()
    {
    HashSet<Integer> hs = new HashSet<Integer>();
    ArrayList<Integer> temp = new ArrayList<Integer>(); // this is only here to store stuff and access it easier without an evil iterator

    hs.add(0);
    temp.add(0);

    // int i = 0; // used for the while loop below
    Integer temp2 = 0; // used to check if the next item in epsilon is null.
    MySet ms3 = new MySet();

    ms3 = ms[0][ms[0].length -1];

    if (ms3 == null)
    return hs;

    else
    {

    for (int i =0; i < ms3.size(); i++)
    hs.add(ms3.get(i));


    }


    for (int i =0; i < ms3.size(); i++)
    {

    while (ms[ms3.get(i)][ms[0].length-1] !=null)
    {




    }


    }





    return hs;
    }

    What I was going for is if there is an epsilon transition, basically if ms[0][ms[0].length-1] isn't empty to add the states in there. You always add 0 to start I think even if ms[0][ms[0].length-1] = null. Anyway, you add those states. Then for each of those states, you further loop to check each of the epsilon things in them. So if state 2 (a.k.a. ms[1]) and the epsilon has say 3, 5 as its closures, a.k.a. the values in the MySet my[1][ms[0].length-1] have more closures, say 4 and 1, then you add 2, 3, 5, 4, 1 and all the ones for ms[3][ms[0].length-1] and the closures of those, etc, until you either run out of closures, or you encounter a part where the only sets being added are already there, i.e. if one of them had a loop to 0 for instance.

    To kind of summarize what I said,

    ms[0][ms[0].length-1] = {1,2,3,4,5.....}
    ms[1][ms[0].length-1] = {10, 11, 12, 13, 14}
    ms[10][ms[0].length-1] = {15} // had to end it somewhere, though it could in theory go on much longer
    ms[15][ms[0].length-1] = {};
    ms[2][ms[0].length-1] = {8,9};
    ms[8][ms[0].length-1] = {};
    ms[9][ms[0].length-1] = {}; // I'm making it brief here
    ms[3][ms[0].length -1] = {.......}

    add (0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15, 8, 9, .......);

    This thread has been cross posted here:

    http://www.javaprogrammingforums.com/whats-wrong-my-code/15042-not-sure-how-work-multiple-delimiters-also-there-random-tabbing.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    As I said, I've dealt with the read in issue. Now it's the recursive parts that are giving me confusion.

    WARNING: Compiling that as is may possibly cause an infinite loop as there's nothing to change ms3. But how to change ms3 without changing the actual thing. Do I need a deep copy every time in the loop? ARGH!

    Also, this isn't the only recursive issue I've got, only the start.



  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    Ok, I've made a new method. Some of the old ones can be later removed. This one won't compile because it's missing a return statement in the if part.

    What I want is the return to return it recursivly but also, to not boot out if just merely ONE or so hit null but only if all of them hit null or reach a loop where they're just adding the same things over and over. Any idea how to do that?

       public static HashSet<Integer> getEpsilons(Integer state)
        {
     
        HashSet<Integer> hs = new HashSet<Integer>();
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
        hs.add(ms[state][ms[0].length-1].get(i));
     
        hs.addAll(getEpsilons(ms[state][ms[0].length-1].get(i)));
     
        }
     
        }
        else
     
        return hs;
     
        }

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    Ok, I got a method to do that, though is there any possibility of that return statement causing it to end too early when only one of them hits null but others could keep going?

    It doesn't appear so as I've tested it a bit but I was just wondering. Also, now I need it to once more get the epsilon closures but with some twists. Now I need it to either find a next state, if it's there, and add that, in addition to adding 0 as always, and then add only epsilon clsoures. One the other hand, if it doesn't find it right away, then it shouldn't add anything until it finds it. Once it finds it, it should add the spot where it found it, note it adds the spot where it points to, not where it was found at I think. Also, if it DOES find it right away though, or even later, it can go to where it's at and also follow the lambdas till they run out or along other characters of the same type.

    For instance

    if a is valid and there is so lambda then

    a -> (state where a leads to) -> lambda->lambda->etc is ok

    Also, assuming this is valid,

    lambda->a -> lambda -> lambda -> etc

    and

    lambda -> lambda -> a -> lambda -> lambda -> etc is valid. It doesn't matter if you can reach the letter, you can still add it as well by going through lambda and going to the letter that way and also all lambdas after that letter. However, you can't follow all the lambdas to a dead end where the letter isn't there and there's no more lambdas. (I already kind of got that, at least starting from 0.)

    Other than starting from 0 where it goes along lambdas till it can no more, as described in this method below that I got to work, I think at least it works fine, you can't

    0 -> lambda -> lambda -> lambda , etc and end with lambda without an a's in between. (I already kind of got that as I said.)

    However,

    0 -> lambda -> lambda -> ..... a -> lambda -> lambda -> .... lambda is ok as it already has an a.

    As I said, ti's kind of tricky.

    To make things more difficult they wanted me to also, once I get the states from a closure starting at 0 to once more perform the closure on that set that I got for each of the elements under that set for each character in the alphabet, minus lambda I think, and then union them together.

    For instance, say that in an alphabet with a, b under state 0, I got

    for a: {0,1,2,3,4}
    for b: {0, 1, 2, 3, 4, 5}

    then I'd take the lambda method (I can keep using the old methods I used to get the previous parts again to make this easier so I don't have to keep recoding each new part, and

    {1,a} where a actually is some integer actually in the coding, 0 I think by default unless something ever happened to be before a, which isn't in the test ones anyway, but anyway,

    Anyway,

    if (1,a} yields {2} and {0,a} = {3,4} and, for simplicity sake, {2,a}, {3,a} and {4,a} all yield an empty set, then it would create the new set {0, 1, 3, 4}

    which would further be operated on like as above. Also the {0,1,2,3,4} will be done the same way for the other characters of the alphabet as will the {0,1,3,4} and also the {0,1,2,3,4,5} and whatever derivates of {0,1,2,3,4,5} there are. In the end, it should stop when it's only adding repeats and has no more new sets (or if, in the very worst case, there simply are no sets at all, not encountered in any of the test cases really, there are none) but anyway, it should stop. It should add each of these sets produced, in addition to the one I already got for the lambdas starting at 0, and put them all in a Set of Sets. I should then print out the toString of each set in the set of sets.

    I might need an Iterator for that part.

    For finding the closures other than the lambdas (that one was the getEpsilons2()), I found I think that there are three possibilities for this one

    1.) There is no closures at all for either the desired character or lambda, in which case it would be a good idea to stop
    2.) There is a lambda closure but not a closure for the desired character
    {
    a.) Until it finds it, it only should add 0. When it says for instance that to go somewhere, I think it should add that spot that it pointed to, but not the spot itself where the letter was first found, and all lambdas after that.)
    }

    3. It finds both a lambda and the desired character
    {
    a.) Assuming it was the first time it found the character, it can go through the lambda part above, but also go through and add the character spot that it points to and all epsilons if any, after that.
    b.) If it's not the first time it finds the character, then it can only follow the lambdas.
    }

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Iterator;
     
     
    public class fsm
    {
     
    private static class FileParser
    {
     
     
     
    }
     
    private static class MySet
    {
     
    private  ArrayList<Integer> intList;
     
    public MySet()
    {
    intList = new ArrayList<Integer>();
     
    }
     
    public  void add(Integer data)
    {
    intList.add(data);
     
    }
     
    public void remove()
    {
    intList.remove(0);
     
    }
     
    public int size()
    {
    return intList.size();
    }
     
    public Integer get(int index) throws NullPointerException
    {
    return intList.get(index);
     
    }
     
    public ArrayList<Integer> getList()
    {
    return intList;
    }
     
     
     
    }
     
     static MySet[][] ms;
     static int characters;
     static HashSet<HashSet<Integer>> sos;
     static HashSet<Integer> epsilons;
        public static void main(String[] args)
        {
    	Scanner s = null;
    	Scanner s2 = null;
    	ArrayList<Integer> stateList = new ArrayList<Integer>();
    	ArrayList<Character> alphabet = new ArrayList<Character>();
     
    	ArrayList<String> transitionLines = new ArrayList<String>();
    	ArrayList<String[]> splitArray = new ArrayList<String[]>();
     
    	epsilons = new HashSet<Integer>();
     
     
     
     
     
     
    	int numOfStates = 0;
     
    try
        {
       s  = new Scanner(new File(args[0]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
    numOfStates = s.nextInt();
     
        System.out.println(numOfStates);
     
        String[][] splits = new String[numOfStates][2];
     
     
     
     
        String line = s.nextLine();
        if (line.equals(""))
        line = s.nextLine();
     
     
     
     
        System.out.println(line);
     
        char[] chars = line.toCharArray();
     
     
     
     
        char empty = ' ';
     
     
        String tab = "\t";
        char tab2 = tab.charAt(0);
     
        for (int i =0; i < chars.length; i++)
        {
        if (chars[i]!= empty && chars[i]!=tab2)
        {
        alphabet.add(chars[i]);
        }
        }
     
        alphabet.add('¸');
     
         System.out.println(alphabet.toString());
     
         characters = alphabet.size();
     
     
         for (int i=0; i < numOfStates; i++)
         {
         transitionLines.add(s.nextLine());
     
         }
     
         System.out.println(transitionLines.toString());
     
     
         for (int i =0; i < splits.length; i++)
         {
         splits[i] = transitionLines.get(i).split(":");
     
         }
     
     
         for (int i =0; i < splits.length; i++)
         {
         for (int j=0; j < splits[i].length; j++)
         {
         System.out.println(splits[i][j]);
     
         }
     
     
         }
     
       //  String[][] test = 
         ArrayList<String[]> fredBowling = new ArrayList<String[]>();
     
         for (int i =0; i < splits.length; i++)
         {
         fredBowling.add(splits[i][1].split("}"));
         }
         splits[0][1].split("}");
     
         ArrayList<Integer> state = new ArrayList<Integer>();
     
         for (int i=0; i < splits.length; i++)
         {
         state.add(Integer.parseInt(splits[i][0]));
     
         }
     
     
       //  System.out.println(java.util.Arrays.toString(test));
     
     
        // String smaller[] = new String[test.length];
     
        ArrayList<ArrayList<String>> smaller = new ArrayList<ArrayList<String>>();
     
         for (int i =0; i < fredBowling.size(); i++)
         {
         smaller.add(new ArrayList<String>());
         for (int j = 0; j < fredBowling.get(i).length; j++)
         {
         int temp = fredBowling.get(i)[j].indexOf("{");
     
         smaller.get(i).add(fredBowling.get(i)[j].substring(temp + 1));
         }
     
         }
     
       //  System.out.println(java.util.Arrays.toString(smaller));
     
        //  ArrayList<String[]> finale = new ArrayList<String[]>();
         ArrayList<ArrayList<String[]>> finale = new ArrayList<ArrayList<String[]>>();
     
         // for (int i =0; i < smaller.length; i++)
      //    {
       //   finale.add(smaller[i].split(","));
     
     //    }
     
         for (int i =0; i < smaller.size(); i++)
         {
         finale.add(new ArrayList<String[]>());
         for (int j =0; j < smaller.get(i).size(); j++)
         {
         finale.get(i).add(smaller.get(i).get(j).split(","));
     
         }
     
     
         }
     
         for (int i =0; i < finale.size(); i++)
         {
         for (int j =0; j < finale.get(i).size(); j++)
         {
         for (int k =0; k < finale.get(i).get(j).length; k++)
         {
         System.out.println("MySet[" + i + "]["+j +"].get(" + k + ") = " +finale.get(i).get(j)[k]);
         }
     
     
         }
     
         }
     
          ms = new MySet[finale.size()][finale.get(0).size()];
     
         for (int i =0; i < finale.size(); i++)
         { // begin for
     
         for (int j =0; j < finale.get(i).size(); j++)
         { // begin for 
         try{
     
         ms[i][j] = new MySet();
     
     
     
         for (int k =0; k < finale.get(i).get(j).length; k++)
         { // begin for
         if (finale.get(i).get(j)[k] != null && !finale.get(i).get(j)[k].equals(""))
         { // begin if 
         ms[i][j].add(Integer.parseInt(finale.get(i).get(j)[k]));
     
         } // end if
     
         } // end for
     
         } // end try
           catch(ArrayIndexOutOfBoundsException aioobe)
         {
         System.err.println("Index " + i + "," + j + " is out of bounds.");
         }
     
         } // end for
     
     
         } // end for
     
         for (int i =0; i < ms.length; i++)
         {
         for (int j =0; j < ms[i].length; j++)
         {
         for (int k =0; k < ms[i][j].size(); k++)
         {
         System.out.println("ms[" + i + "]["+j +"].get(" + k + ") = "+ ms[i][j].get(k));
     
         }
         }
     
         }
     
         int startingState = s.nextInt();
         System.out.println("s:" + startingState);
     
         String fs = s.nextLine();
     
         if (fs.equals("") || fs == null)
         fs = s.nextLine();
     
         String[] firstHalf = fs.split("}");
     
     
          int temp = firstHalf[0].indexOf("{");
     
          String secondHalf = firstHalf[0].substring(temp+1);
     
          String[] finalHalf = secondHalf.split(",");
     
     
         ArrayList<Integer> finalState = new ArrayList<Integer>();
     
         for (int i =0; i < finalHalf.length; i++)
    {
    if (finalHalf[i] != null && !finalHalf[i].equals(""))
    {
    finalState.add(Integer.parseInt(finalHalf[i]));
    }
     
    }
     
    System.out.println("Accepting state: " + finalState.toString());
     
    System.out.println(getFinalStateFrom(0, 0).toString());
     
    System.out.println(recursion1(0,0).toString());
     
    System.out.println(getEpsilon().toString());
         System.out.println(getEpsilons(0).toString());
     
         getEpsilons2(0);
     
         System.out.println("To DFA: ");
         System.out.println(epsilons.toString());
    try
        {
       s2  = new Scanner(new File(args[1]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
     
     
     
     
     
        }
     
        public static void getClosures(Integer state, Integer character)
        {
     
        if (ms[state][character] == null && ms[state][ms[0].length-1] == null)
        {
        // if there is no closures at all, then it's a good idea to stop
     
     
     
        }
     
     
     
        else
        {
     
     
        }
     
     
        }
     
        public static void getEpsilons2(Integer state)
        {
     
         epsilons.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
        epsilons.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons2(ms[state][ms[0].length-1].get(i));
     
        }
     
        }
        else
     
        return;
     
     
        }
     
        public static HashSet<Integer> getEpsilons(Integer state)
        {
     
        HashSet<Integer> hs = new HashSet<Integer>();
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
        hs.add(ms[state][ms[0].length-1].get(i));
     
        hs.addAll(getEpsilons(ms[state][ms[0].length-1].get(i)));
     
        }
        return hs;
        }
        else
     
        return hs;
     
        }
     
        public static HashSet<Integer> getEpsilon()
        {
        HashSet<Integer> hs = new HashSet<Integer>();
        ArrayList<Integer> temp = new ArrayList<Integer>(); // this is only here to store stuff and access it easier without an evil iterator
     
        hs.add(0);
        temp.add(0);
     
      //  int i = 0; // used for the while loop below
        Integer temp2 = 0; // used to check if the next item in epsilon is null.
        MySet ms3 = new MySet();
     
        ms3 = ms[0][ms[0].length -1];
     
        if (ms3 == null)
        return hs;
     
        else
        {
     
        for (int i =0; i < ms3.size(); i++)
        hs.add(ms3.get(i));
     
     
        }
     
     
        for (int i =0; i < ms3.size(); i++)
        {
     
     
        try
        {
     
       MySet ms4 = new MySet();
     
       hs.addAll(ms[ms3.get(i)][ms[0].length-1].getList());
       }
     
       catch(NullPointerException npe)
       {
     
     
       }
     
       catch(ArrayIndexOutOfBoundsException aioobe)
       {
     
     
       }
     
       catch(Exception e)
       {
     
     
       }
     
     
     
     
     
     
     
     
        }
     
     
     
     
     
        return hs;
        }
     
     
     
     
        public static HashSet<Integer> getLambdas(Integer state)
        {
     
        HashSet<Integer> hs = new HashSet<Integer>();
     
       if (ms[state][characters - 1] != null)
        {
        for (int i =0; i < ms[state][characters - 1].size(); i++)
        {
        if (ms[state][characters - 1].get(i) != null)
        {
        hs.add(ms[state][characters -1].get(i));
        }
        }
        }
     
        return hs;
        }
     
     
        public static HashSet<Integer> getFinalStateFrom(Integer state, Integer character)
        {
     
        ArrayList<Integer> al = new ArrayList<Integer>();
     
        HashSet<Integer> hs = new HashSet<Integer>();
     
     
        if (ms[state][character] != null)
        {
        for (int i =0; i < ms[state][character].size(); i++)
        {
        if (ms[state][character].get(i) != null)
        {
        hs.add(ms[state][character].get(i));
        }
        }
        }
     
     
      //  hs.addAll(getFinalStateFrom(state, characters - 1);
     
      System.out.println("Added: " + hs.toString());
     
        hs.addAll(getLambdas(state));
     
        System.out.println("Added: " + hs.toString());
     
        return hs;
     
     
     
     
     
        }
     
        public static HashSet<Integer> recursion1(Integer state, Integer character)
        {
        HashSet<Integer> hs = new HashSet<Integer>();
        for (Iterator<Integer> it = getFinalStateFrom(state,character).iterator(); it.hasNext();)
        {
        try
        {
        hs.addAll(getFinalStateFrom(getFinalStateFrom(it.next(), characters -1).iterator().next(), character));
        }
     
        catch(java.util.NoSuchElementException nsee)
        {
        System.out.println("This element doesn't exist!");
        }
     
        }
        return hs;
        }
     
     
    }

    Another issue was how to add a HsshSet, the same HashSet to the HashSet of HashSets when I make the recursive calls.

    I can't know in advance how many there will be. I could always have a list of HashSets, but how would I know when to stop adding the local hash sets that I'd create to the current index of the list and to add to a new one and to start adding to that one?

    This is more of a scoping issue.
    Last edited by javapenguin; April 18th, 2012 at 06:56 PM.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    My biggest issue is how to make sure that I know if I've already found the character, hence that I shouldn't keep trying to follow it in the recursive call.

    I thought of passing a boolean as a parameter, but that might not work either as the recursive calls, as java is by reference, not value, could go haywire if it changed it for ALL the times, hence that cancelling it possibly too soon.

    Could somebody please help me?




    public static void getClosures2(Integer state, Integer character, boolean hit)
        {
     
        if (
     
        }
        public static void getClosures(Integer state, Integer character)
        {
     
     
        if (ms[state][character] != null && ms[state][ms[0].length-1] != null)
        {
     
        for (int i =0; i < ms[state][character].size(); i++)
        {
        getClosures2(ms[state][character].get(i), character, true);
     
        }
     
     
     
        }
     
     
        else if (ms[state][character] == null && ms[state][ms[0].length-1]!=null)
        {
     
     
        }
     
        else
        {
        // if there is no closures at all, then it's a good idea to stop
     
        return;
     
        }
     
     
        }
     
        public static void getEpsilons2(Integer state)
        {
     
         epsilons.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
        epsilons.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons2(ms[state][ms[0].length-1].get(i));
     
        }
     
        }
        else
     
        return;
     
     
        }

    I added that second getClosures method as I figured that it could be used with a boolean to stop it, hopefully, from continuing on. I'd simply call getEpsilons for that one as it'd work, but that would add to a different HashSet that it shouldn't be. I could duplicate some things I guess. It seems I'm doing this very inefficiently. The entire code is:

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Iterator;
     
     
    public class fsm
    {
     
    private static class FileParser
    {
     
     
     
    }
     
    private static class MySet
    {
     
    private  ArrayList<Integer> intList;
     
    public MySet()
    {
    intList = new ArrayList<Integer>();
     
    }
     
    public  void add(Integer data)
    {
    intList.add(data);
     
    }
     
    public void remove()
    {
    intList.remove(0);
     
    }
     
    public int size()
    {
    return intList.size();
    }
     
    public Integer get(int index) throws NullPointerException
    {
    return intList.get(index);
     
    }
     
    public ArrayList<Integer> getList()
    {
    return intList;
    }
     
     
     
    }
     
     static MySet[][] ms;
     static int characters;
     static HashSet<HashSet<Integer>> sos;
     static HashSet<Integer> epsilons;
        public static void main(String[] args)
        {
    	Scanner s = null;
    	Scanner s2 = null;
    	ArrayList<Integer> stateList = new ArrayList<Integer>();
    	ArrayList<Character> alphabet = new ArrayList<Character>();
     
    	ArrayList<String> transitionLines = new ArrayList<String>();
    	ArrayList<String[]> splitArray = new ArrayList<String[]>();
     
    	epsilons = new HashSet<Integer>();
     
     
     
     
     
     
    	int numOfStates = 0;
     
    try
        {
       s  = new Scanner(new File(args[0]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
    numOfStates = s.nextInt();
     
        System.out.println(numOfStates);
     
        String[][] splits = new String[numOfStates][2];
     
     
     
     
        String line = s.nextLine();
        if (line.equals(""))
        line = s.nextLine();
     
     
     
     
        System.out.println(line);
     
        char[] chars = line.toCharArray();
     
     
     
     
        char empty = ' ';
     
     
        String tab = "\t";
        char tab2 = tab.charAt(0);
     
        for (int i =0; i < chars.length; i++)
        {
        if (chars[i]!= empty && chars[i]!=tab2)
        {
        alphabet.add(chars[i]);
        }
        }
     
        alphabet.add('¸');
     
         System.out.println(alphabet.toString());
     
         characters = alphabet.size();
     
     
         for (int i=0; i < numOfStates; i++)
         {
         transitionLines.add(s.nextLine());
     
         }
     
         System.out.println(transitionLines.toString());
     
     
         for (int i =0; i < splits.length; i++)
         {
         splits[i] = transitionLines.get(i).split(":");
     
         }
     
     
         for (int i =0; i < splits.length; i++)
         {
         for (int j=0; j < splits[i].length; j++)
         {
         System.out.println(splits[i][j]);
     
         }
     
     
         }
     
       //  String[][] test = 
         ArrayList<String[]> fredBowling = new ArrayList<String[]>();
     
         for (int i =0; i < splits.length; i++)
         {
         fredBowling.add(splits[i][1].split("}"));
         }
         splits[0][1].split("}");
     
         ArrayList<Integer> state = new ArrayList<Integer>();
     
         for (int i=0; i < splits.length; i++)
         {
         state.add(Integer.parseInt(splits[i][0]));
     
         }
     
     
       //  System.out.println(java.util.Arrays.toString(test));
     
     
        // String smaller[] = new String[test.length];
     
        ArrayList<ArrayList<String>> smaller = new ArrayList<ArrayList<String>>();
     
         for (int i =0; i < fredBowling.size(); i++)
         {
         smaller.add(new ArrayList<String>());
         for (int j = 0; j < fredBowling.get(i).length; j++)
         {
         int temp = fredBowling.get(i)[j].indexOf("{");
     
         smaller.get(i).add(fredBowling.get(i)[j].substring(temp + 1));
         }
     
         }
     
       //  System.out.println(java.util.Arrays.toString(smaller));
     
        //  ArrayList<String[]> finale = new ArrayList<String[]>();
         ArrayList<ArrayList<String[]>> finale = new ArrayList<ArrayList<String[]>>();
     
         // for (int i =0; i < smaller.length; i++)
      //    {
       //   finale.add(smaller[i].split(","));
     
     //    }
     
         for (int i =0; i < smaller.size(); i++)
         {
         finale.add(new ArrayList<String[]>());
         for (int j =0; j < smaller.get(i).size(); j++)
         {
         finale.get(i).add(smaller.get(i).get(j).split(","));
     
         }
     
     
         }
     
         for (int i =0; i < finale.size(); i++)
         {
         for (int j =0; j < finale.get(i).size(); j++)
         {
         for (int k =0; k < finale.get(i).get(j).length; k++)
         {
         System.out.println("MySet[" + i + "]["+j +"].get(" + k + ") = " +finale.get(i).get(j)[k]);
         }
     
     
         }
     
         }
     
          ms = new MySet[finale.size()][finale.get(0).size()];
     
         for (int i =0; i < finale.size(); i++)
         { // begin for
     
         for (int j =0; j < finale.get(i).size(); j++)
         { // begin for 
         try{
     
         ms[i][j] = new MySet();
     
     
     
         for (int k =0; k < finale.get(i).get(j).length; k++)
         { // begin for
         if (finale.get(i).get(j)[k] != null && !finale.get(i).get(j)[k].equals(""))
         { // begin if 
         ms[i][j].add(Integer.parseInt(finale.get(i).get(j)[k]));
     
         } // end if
     
         } // end for
     
         } // end try
           catch(ArrayIndexOutOfBoundsException aioobe)
         {
         System.err.println("Index " + i + "," + j + " is out of bounds.");
         }
     
         } // end for
     
     
         } // end for
     
         for (int i =0; i < ms.length; i++)
         {
         for (int j =0; j < ms[i].length; j++)
         {
         for (int k =0; k < ms[i][j].size(); k++)
         {
         System.out.println("ms[" + i + "]["+j +"].get(" + k + ") = "+ ms[i][j].get(k));
     
         }
         }
     
         }
     
         int startingState = s.nextInt();
         System.out.println("s:" + startingState);
     
         String fs = s.nextLine();
     
         if (fs.equals("") || fs == null)
         fs = s.nextLine();
     
         String[] firstHalf = fs.split("}");
     
     
          int temp = firstHalf[0].indexOf("{");
     
          String secondHalf = firstHalf[0].substring(temp+1);
     
          String[] finalHalf = secondHalf.split(",");
     
     
         ArrayList<Integer> finalState = new ArrayList<Integer>();
     
         for (int i =0; i < finalHalf.length; i++)
    {
    if (finalHalf[i] != null && !finalHalf[i].equals(""))
    {
    finalState.add(Integer.parseInt(finalHalf[i]));
    }
     
    }
     
    System.out.println("Accepting state: " + finalState.toString());
     
    System.out.println(getFinalStateFrom(0, 0).toString());
     
    System.out.println(recursion1(0,0).toString());
     
    System.out.println(getEpsilon().toString());
         System.out.println(getEpsilons(0).toString());
     
         getEpsilons2(0);
     
         System.out.println("To DFA: ");
         System.out.println(epsilons.toString());
    try
        {
       s2  = new Scanner(new File(args[1]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
     
     
     
     
     
        }
     
     
        public static void getClosures2(Integer state, Integer character, boolean hit)
        {
     
        if (
     
        }
        public static void getClosures(Integer state, Integer character)
        {
     
     
        if (ms[state][character] != null && ms[state][ms[0].length-1] != null)
        {
     
        for (int i =0; i < ms[state][character].size(); i++)
        {
        getClosures2(ms[state][character].get(i), character, true);
     
        }
     
     
     
        }
     
     
        else if (ms[state][character] == null && ms[state][ms[0].length-1]!=null)
        {
     
     
        }
     
        else
        {
        // if there is no closures at all, then it's a good idea to stop
     
        return;
     
        }
     
     
        }
     
        public static void getEpsilons2(Integer state)
        {
     
         epsilons.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
        epsilons.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons2(ms[state][ms[0].length-1].get(i));
     
        }
     
        }
        else
     
        return;
     
     
        }
     
        public static HashSet<Integer> getEpsilons(Integer state)
        {
     
        HashSet<Integer> hs = new HashSet<Integer>();
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
        hs.add(ms[state][ms[0].length-1].get(i));
     
        hs.addAll(getEpsilons(ms[state][ms[0].length-1].get(i)));
     
        }
        return hs;
        }
        else
     
        return hs;
     
        }
     
        public static HashSet<Integer> getEpsilon()
        {
        HashSet<Integer> hs = new HashSet<Integer>();
        ArrayList<Integer> temp = new ArrayList<Integer>(); // this is only here to store stuff and access it easier without an evil iterator
     
        hs.add(0);
        temp.add(0);
     
      //  int i = 0; // used for the while loop below
        Integer temp2 = 0; // used to check if the next item in epsilon is null.
        MySet ms3 = new MySet();
     
        ms3 = ms[0][ms[0].length -1];
     
        if (ms3 == null)
        return hs;
     
        else
        {
     
        for (int i =0; i < ms3.size(); i++)
        hs.add(ms3.get(i));
     
     
        }
     
     
        for (int i =0; i < ms3.size(); i++)
        {
     
     
        try
        {
     
       MySet ms4 = new MySet();
     
       hs.addAll(ms[ms3.get(i)][ms[0].length-1].getList());
       }
     
       catch(NullPointerException npe)
       {
     
     
       }
     
       catch(ArrayIndexOutOfBoundsException aioobe)
       {
     
     
       }
     
       catch(Exception e)
       {
     
     
       }
     
     
     
     
     
     
     
     
        }
     
     
     
     
     
        return hs;
        }
     
     
     
     
        public static HashSet<Integer> getLambdas(Integer state)
        {
     
        HashSet<Integer> hs = new HashSet<Integer>();
     
       if (ms[state][characters - 1] != null)
        {
        for (int i =0; i < ms[state][characters - 1].size(); i++)
        {
        if (ms[state][characters - 1].get(i) != null)
        {
        hs.add(ms[state][characters -1].get(i));
        }
        }
        }
     
        return hs;
        }
     
     
        public static HashSet<Integer> getFinalStateFrom(Integer state, Integer character)
        {
     
        ArrayList<Integer> al = new ArrayList<Integer>();
     
        HashSet<Integer> hs = new HashSet<Integer>();
     
     
        if (ms[state][character] != null)
        {
        for (int i =0; i < ms[state][character].size(); i++)
        {
        if (ms[state][character].get(i) != null)
        {
        hs.add(ms[state][character].get(i));
        }
        }
        }
     
     
      //  hs.addAll(getFinalStateFrom(state, characters - 1);
     
      System.out.println("Added: " + hs.toString());
     
        hs.addAll(getLambdas(state));
     
        System.out.println("Added: " + hs.toString());
     
        return hs;
     
     
     
     
     
        }
     
        public static HashSet<Integer> recursion1(Integer state, Integer character)
        {
        HashSet<Integer> hs = new HashSet<Integer>();
        for (Iterator<Integer> it = getFinalStateFrom(state,character).iterator(); it.hasNext();)
        {
        try
        {
        hs.addAll(getFinalStateFrom(getFinalStateFrom(it.next(), characters -1).iterator().next(), character));
        }
     
        catch(java.util.NoSuchElementException nsee)
        {
        System.out.println("This element doesn't exist!");
        }
     
        }
        return hs;
        }
     
     
    }

    No it doesn't compile as is. I could easily make it compile. However, I'm trying to figure out what to put.

    I'll call getEpsilons3, which is the same as get Epsilons except it contains a HashSet as a param. How to know which HashSet to use, as I'm using a HashSet of HashSets is tricky in the coding.

    I can't predetermine the amount I'm going to get.
    Last edited by javapenguin; April 18th, 2012 at 08:53 PM.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    Now it will compile but I'm still working on some of the kinks. Now I was trying to repetitively apply the methods already used, the getClosure(), for the newly created HashSets in the HashSet ArrayList. However, I don't think it's going as planned. While it wasn't originally going to, for my first attempt to get some of the logic set up, it wasn't going to add it all, I thought the for loop, being set to the ArrayList size would keep going till it starting adding repetitive sets and that would hopefully stop. Well it doesn't. However, I then changed it so that it would only apply it once on the newly created array lists. However, it's still going on forever and throwing that Exception, I handled it with a catch block so it won't crash but it prints out the error message in there, maybe not constantly, but infinitely for sure.

     
     
    private static class FileParser
    {
     
     
     
    }
     
    private static class MySet
    {
     
    private  ArrayList<Integer> intList;
     
    public MySet()
    {
    intList = new ArrayList<Integer>();
     
    }
     
    public  void add(Integer data)
    {
    intList.add(data);
     
    }
     
    public void remove()
    {
    intList.remove(0);
     
    }
     
    public int size()
    {
    return intList.size();
    }
     
    public Integer get(int index) throws NullPointerException
    {
    return intList.get(index);
     
    }
     
    public ArrayList<Integer> getList()
    {
    return intList;
    }
     
     
     
    }
     
     static MySet[][] ms;
     static int characters;
     static HashSet<HashSet<Integer>> sos;
     static HashSet<Integer> epsilons;
     
     static ArrayList<HashSet<Integer>> ah;
        public static void main(String[] args)
        {
    	Scanner s = null;
    	Scanner s2 = null;
    	ArrayList<Integer> stateList = new ArrayList<Integer>();
    	ArrayList<Character> alphabet = new ArrayList<Character>();
     
    	ArrayList<String> transitionLines = new ArrayList<String>();
    	ArrayList<String[]> splitArray = new ArrayList<String[]>();
     
    	epsilons = new HashSet<Integer>();
     
    	ah = new ArrayList<HashSet<Integer>>();
     
     
     
     
    	int numOfStates = 0;
     
    try
        {
       s  = new Scanner(new File(args[0]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
    numOfStates = s.nextInt();
     
        System.out.println(numOfStates);
     
        String[][] splits = new String[numOfStates][2];
     
     
     
     
        String line = s.nextLine();
        if (line.equals(""))
        line = s.nextLine();
     
     
     
     
        System.out.println(line);
     
        char[] chars = line.toCharArray();
     
     
     
     
        char empty = ' ';
     
     
        String tab = "\t";
        char tab2 = tab.charAt(0);
     
        for (int i =0; i < chars.length; i++)
        {
        if (chars[i]!= empty && chars[i]!=tab2)
        {
        alphabet.add(chars[i]);
        }
        }
     
        alphabet.add('¸');
     
         System.out.println(alphabet.toString());
     
         characters = alphabet.size();
     
     
         for (int i=0; i < numOfStates; i++)
         {
         transitionLines.add(s.nextLine());
     
         }
     
         System.out.println(transitionLines.toString());
     
     
         for (int i =0; i < splits.length; i++)
         {
         splits[i] = transitionLines.get(i).split(":");
     
         }
     
     
         for (int i =0; i < splits.length; i++)
         {
         for (int j=0; j < splits[i].length; j++)
         {
         System.out.println(splits[i][j]);
     
         }
     
     
         }
     
       //  String[][] test = 
         ArrayList<String[]> fredBowling = new ArrayList<String[]>();
     
         for (int i =0; i < splits.length; i++)
         {
         fredBowling.add(splits[i][1].split("}"));
         }
         splits[0][1].split("}");
     
         ArrayList<Integer> state = new ArrayList<Integer>();
     
         for (int i=0; i < splits.length; i++)
         {
         state.add(Integer.parseInt(splits[i][0]));
     
         }
     
     
       //  System.out.println(java.util.Arrays.toString(test));
     
     
        // String smaller[] = new String[test.length];
     
        ArrayList<ArrayList<String>> smaller = new ArrayList<ArrayList<String>>();
     
         for (int i =0; i < fredBowling.size(); i++)
         {
         smaller.add(new ArrayList<String>());
         for (int j = 0; j < fredBowling.get(i).length; j++)
         {
         int temp = fredBowling.get(i)[j].indexOf("{");
     
         smaller.get(i).add(fredBowling.get(i)[j].substring(temp + 1));
         }
     
         }
     
       //  System.out.println(java.util.Arrays.toString(smaller));
     
        //  ArrayList<String[]> finale = new ArrayList<String[]>();
         ArrayList<ArrayList<String[]>> finale = new ArrayList<ArrayList<String[]>>();
     
         // for (int i =0; i < smaller.length; i++)
      //    {
       //   finale.add(smaller[i].split(","));
     
     //    }
     
         for (int i =0; i < smaller.size(); i++)
         {
         finale.add(new ArrayList<String[]>());
         for (int j =0; j < smaller.get(i).size(); j++)
         {
         finale.get(i).add(smaller.get(i).get(j).split(","));
     
         }
     
     
         }
     
         for (int i =0; i < finale.size(); i++)
         {
         for (int j =0; j < finale.get(i).size(); j++)
         {
         for (int k =0; k < finale.get(i).get(j).length; k++)
         {
         System.out.println("MySet[" + i + "]["+j +"].get(" + k + ") = " +finale.get(i).get(j)[k]);
         }
     
     
         }
     
         }
     
          ms = new MySet[finale.size()][finale.get(0).size()];
     
         for (int i =0; i < finale.size(); i++)
         { // begin for
     
         for (int j =0; j < finale.get(i).size(); j++)
         { // begin for 
         try{
     
         ms[i][j] = new MySet();
     
     
     
         for (int k =0; k < finale.get(i).get(j).length; k++)
         { // begin for
         if (finale.get(i).get(j)[k] != null && !finale.get(i).get(j)[k].equals(""))
         { // begin if 
         ms[i][j].add(Integer.parseInt(finale.get(i).get(j)[k]));
     
         } // end if
     
         } // end for
     
         } // end try
           catch(ArrayIndexOutOfBoundsException aioobe)
         {
         System.err.println("Index " + i + "," + j + " is out of bounds.");
         }
     
         } // end for
     
     
         } // end for
     
         for (int i =0; i < ms.length; i++)
         {
         for (int j =0; j < ms[i].length; j++)
         {
         for (int k =0; k < ms[i][j].size(); k++)
         {
         System.out.println("ms[" + i + "]["+j +"].get(" + k + ") = "+ ms[i][j].get(k));
     
         }
         }
     
         }
     
         int startingState = s.nextInt();
         System.out.println("s:" + startingState);
     
         String fs = s.nextLine();
     
         if (fs.equals("") || fs == null)
         fs = s.nextLine();
     
         String[] firstHalf = fs.split("}");
     
     
          int temp = firstHalf[0].indexOf("{");
     
          String secondHalf = firstHalf[0].substring(temp+1);
     
          String[] finalHalf = secondHalf.split(",");
     
     
         ArrayList<Integer> finalState = new ArrayList<Integer>();
     
         for (int i =0; i < finalHalf.length; i++)
    {
    if (finalHalf[i] != null && !finalHalf[i].equals(""))
    {
    finalState.add(Integer.parseInt(finalHalf[i]));
    }
     
    }
     
    System.out.println("Accepting state: " + finalState.toString());
     
    System.out.println(getFinalStateFrom(0, 0).toString());
     
    System.out.println(recursion1(0,0).toString());
     
    System.out.println(getEpsilon().toString());
         System.out.println(getEpsilons(0).toString());
     
         getEpsilons2(0);
     
     
         for (int i =0; i < characters - 1; i++)
         {
         ah.add(new HashSet<Integer>());
     
         }
     
         for (int i =0; i < characters -1; i++)
         {
         getClosures(0,i, ah.get(i));
     
         }
     
         System.out.println("To DFA: ");
     
     
         for (int i =0; i < characters -1; i++)
         {
         System.out.println(ah.get(i).toString());
     
         }
         System.out.println(epsilons.toString());
     
         int oldSize = ah.size() -1;
         for (int i =0; i < oldSize + 1; i++)
         {
         ah.add(new HashSet<Integer>());
         for (Iterator<Integer> it = ah.get(i+oldSize).iterator(); it.hasNext();)
         {
         for (int j =0; j < characters -1; j++)
         {
         try
         {
         getClosures(it.next(), j, ah.get(oldSize+i));
         }
     
         catch(java.util.ConcurrentModificationException cme)
         {
         System.out.println("Some elements are being modified concurrently.");
         }
         }
         }
         }
     
    try
        {
       s2  = new Scanner(new File(args[1]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
     
     
     
     
     
        }
     
     
        public static void getClosures2(Integer state, Integer character, boolean hit, HashSet<Integer> h)
        {
     
        if (hit == true)
        {
        getEpsilons3(state, h);
     
        }
     
        else
        {
     
     
        }
     
        }
        public static void getClosures(Integer state, Integer character, HashSet<Integer> h)
        {
     
     
        h.add(0);
        if (ms[state][character] != null && ms[state][ms[0].length-1] != null)
        {
     
        for (int i =0; i < ms[state][character].size(); i++)
        {
     
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true,h);
     
        }
     
        for (int j =0; j < ms[state][characters-1].size(); j++)
        {
        getClosures(ms[state][characters -1].get(j), character, h);
        }
     
        }
     
     
        else if (ms[state][character] == null && ms[state][ms[0].length-1]!=null)
        {
        for (int i = 0; i < ms[state][characters-1].size(); i++)
        {
        getClosures(ms[state][characters-1].get(i), character, h);
     
        }
     
        }
     
        else if (ms[state][character]!=null && ms[state][ms[0].length-1] == null)
        {
        for (int i =0; i < ms[state][character].size(); i++)
        {
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true, h);
     
        }
        }
     
        else
        {
        // if there is no closures at all, then it's a good idea to stop
     
        return;
     
        }
     
     
        }
     
     
          public static void getEpsilons3(Integer state, HashSet<Integer> h)
        {
     
         h.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
     
     
       h.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons3(ms[state][ms[0].length-1].get(i), h);
     
        }
     
        }
        else
     
        return;
     
     
        }
        public static void getEpsilons2(Integer state)
        {
     
         epsilons.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
        epsilons.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons2(ms[state][ms[0].length-1].get(i));
     
        }
     
        }
        else
     
        return;
     
     
        }
     
        public static HashSet<Integer> getEpsilons(Integer state)
        {
     
        HashSet<Integer> hs = new HashSet<Integer>();
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
        hs.add(ms[state][ms[0].length-1].get(i));
     
        hs.addAll(getEpsilons(ms[state][ms[0].length-1].get(i)));
     
        }
        return hs;
        }
        else
     
        return hs;
     
        }
     
        public static HashSet<Integer> getEpsilon()
        {
        HashSet<Integer> hs = new HashSet<Integer>();
        ArrayList<Integer> temp = new ArrayList<Integer>(); // this is only here to store stuff and access it easier without an evil iterator
     
        hs.add(0);
        temp.add(0);
     
      //  int i = 0; // used for the while loop below
        Integer temp2 = 0; // used to check if the next item in epsilon is null.
        MySet ms3 = new MySet();
     
        ms3 = ms[0][ms[0].length -1];
     
        if (ms3 == null)
        return hs;
     
        else
        {
     
        for (int i =0; i < ms3.size(); i++)
        hs.add(ms3.get(i));
     
     
        }
     
     
        for (int i =0; i < ms3.size(); i++)
        {
     
     
        try
        {
     
       MySet ms4 = new MySet();
     
       hs.addAll(ms[ms3.get(i)][ms[0].length-1].getList());
       }
     
       catch(NullPointerException npe)
       {
     
     
       }
     
       catch(ArrayIndexOutOfBoundsException aioobe)
       {
     
     
       }
     
       catch(Exception e)
       {
     
     
       }
     
     
     
     
     
     
     
     
        }
     
     
     
     
     
        return hs;
        }
     
     
     
     
        public static HashSet<Integer> getLambdas(Integer state)
        {
     
        HashSet<Integer> hs = new HashSet<Integer>();
     
       if (ms[state][characters - 1] != null)
        {
        for (int i =0; i < ms[state][characters - 1].size(); i++)
        {
        if (ms[state][characters - 1].get(i) != null)
        {
        hs.add(ms[state][characters -1].get(i));
        }
        }
        }
     
        return hs;
        }
     
     
        public static HashSet<Integer> getFinalStateFrom(Integer state, Integer character)
        {
     
        ArrayList<Integer> al = new ArrayList<Integer>();
     
        HashSet<Integer> hs = new HashSet<Integer>();
     
     
        if (ms[state][character] != null)
        {
        for (int i =0; i < ms[state][character].size(); i++)
        {
        if (ms[state][character].get(i) != null)
        {
        hs.add(ms[state][character].get(i));
        }
        }
        }
     
     
      //  hs.addAll(getFinalStateFrom(state, characters - 1);
     
      System.out.println("Added: " + hs.toString());
     
        hs.addAll(getLambdas(state));
     
        System.out.println("Added: " + hs.toString());
     
        return hs;
     
     
     
     
     
        }
     
        public static HashSet<Integer> recursion1(Integer state, Integer character)
        {
        HashSet<Integer> hs = new HashSet<Integer>();
        for (Iterator<Integer> it = getFinalStateFrom(state,character).iterator(); it.hasNext();)
        {
        try
        {
        hs.addAll(getFinalStateFrom(getFinalStateFrom(it.next(), characters -1).iterator().next(), character));
        }
     
        catch(java.util.NoSuchElementException nsee)
        {
        System.out.println("This element doesn't exist!");
        }
     
        }
        return hs;
        }
     
     
    }

    It has an error of a sort. It's called a Concurrent something or other exception. What exactly is causing that?

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    What is causing that error? Why doesn't anyone usually reply? .

    What's a concurrent modification exception
    Last edited by javapenguin; April 21st, 2012 at 01:45 PM.

  7. #7
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    A ConcurrentModificationException is thrown when more than one attempt to mutate or access an Object occurs at once ("concurrent" literally means "at the same time"). This can be done by the same Thread (under certain circumstances) or by different Threads. One way to fix this may be to make the method in which the exception was thrown synchronized; this would allow it to only be running once at any given time. Or, if you don't want to synchronize an entire method, you could create a synchronized code block to encase the problematic lines of code.

    Where exactly is this exception being thrown?

    Also, it would be much easier for everyone to help if you only post the code you need help with and more specifically state what you're trying to do recursively. I'd like to help, but the first post with those x-hundred lines of code scared me off... D:
    Last edited by snowguy13; April 21st, 2012 at 02:30 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    I found the problem. I, probably due to lack of sleep, somehow mistakenly didn't realize that I was passing the same HashSet to the methods that I already had and then possibly modifiying them somehow at the same time as iterating through them.

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Iterator;
     
     
    public class fsm
    {
     
    private static class FileParser
    {
     
     
     
    }
     
    private static class MySet
    {
     
    private  ArrayList<Integer> intList;
     
    public MySet()
    {
    intList = new ArrayList<Integer>();
     
    }
     
    public  void add(Integer data)
    {
    intList.add(data);
     
    }
     
    public void remove()
    {
    intList.remove(0);
     
    }
     
    public int size()
    {
    return intList.size();
    }
     
    public Integer get(int index) throws NullPointerException
    {
    return intList.get(index);
     
    }
     
    public ArrayList<Integer> getList()
    {
    return intList;
    }
     
     
     
    }
     
     static MySet[][] ms;
     static int characters;
     static HashSet<HashSet<Integer>> sos;
     static HashSet<Integer> epsilons;
     
     static ArrayList<HashSet<Integer>> ah;
        public static void main(String[] args)
        {
    	Scanner s = null;
    	Scanner s2 = null;
    	ArrayList<Integer> stateList = new ArrayList<Integer>();
    	ArrayList<Character> alphabet = new ArrayList<Character>();
     
    	ArrayList<String> transitionLines = new ArrayList<String>();
    	ArrayList<String[]> splitArray = new ArrayList<String[]>();
     
    	epsilons = new HashSet<Integer>();
     
    	ah = new ArrayList<HashSet<Integer>>();
     
     
     
     
    	int numOfStates = 0;
     
    try
        {
       s  = new Scanner(new File(args[0]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
    numOfStates = s.nextInt();
     
        System.out.println(numOfStates);
     
        String[][] splits = new String[numOfStates][2];
     
     
     
     
        String line = s.nextLine();
        if (line.equals(""))
        line = s.nextLine();
     
     
     
     
        System.out.println(line);
     
        char[] chars = line.toCharArray();
     
     
     
     
        char empty = ' ';
     
     
        String tab = "\t";
        char tab2 = tab.charAt(0);
     
        for (int i =0; i < chars.length; i++)
        {
        if (chars[i]!= empty && chars[i]!=tab2)
        {
        alphabet.add(chars[i]);
        }
        }
     
        alphabet.add('¸');
     
         System.out.println(alphabet.toString());
     
         characters = alphabet.size();
     
     
         for (int i=0; i < numOfStates; i++)
         {
         transitionLines.add(s.nextLine());
     
         }
     
         System.out.println(transitionLines.toString());
     
     
         for (int i =0; i < splits.length; i++)
         {
         splits[i] = transitionLines.get(i).split(":");
     
         }
     
     
         for (int i =0; i < splits.length; i++)
         {
         for (int j=0; j < splits[i].length; j++)
         {
         System.out.println(splits[i][j]);
     
         }
     
     
         }
     
       //  String[][] test = 
         ArrayList<String[]> fredBowling = new ArrayList<String[]>();
     
         for (int i =0; i < splits.length; i++)
         {
         fredBowling.add(splits[i][1].split("}"));
         }
         splits[0][1].split("}");
     
         ArrayList<Integer> state = new ArrayList<Integer>();
     
         for (int i=0; i < splits.length; i++)
         {
         state.add(Integer.parseInt(splits[i][0]));
     
         }
     
     
       //  System.out.println(java.util.Arrays.toString(test));
     
     
        // String smaller[] = new String[test.length];
     
        ArrayList<ArrayList<String>> smaller = new ArrayList<ArrayList<String>>();
     
         for (int i =0; i < fredBowling.size(); i++)
         {
         smaller.add(new ArrayList<String>());
         for (int j = 0; j < fredBowling.get(i).length; j++)
         {
         int temp = fredBowling.get(i)[j].indexOf("{");
     
         smaller.get(i).add(fredBowling.get(i)[j].substring(temp + 1));
         }
     
         }
     
       //  System.out.println(java.util.Arrays.toString(smaller));
     
        //  ArrayList<String[]> finale = new ArrayList<String[]>();
         ArrayList<ArrayList<String[]>> finale = new ArrayList<ArrayList<String[]>>();
     
         // for (int i =0; i < smaller.length; i++)
      //    {
       //   finale.add(smaller[i].split(","));
     
     //    }
     
         for (int i =0; i < smaller.size(); i++)
         {
         finale.add(new ArrayList<String[]>());
         for (int j =0; j < smaller.get(i).size(); j++)
         {
         finale.get(i).add(smaller.get(i).get(j).split(","));
     
         }
     
     
         }
     
         for (int i =0; i < finale.size(); i++)
         {
         for (int j =0; j < finale.get(i).size(); j++)
         {
         for (int k =0; k < finale.get(i).get(j).length; k++)
         {
         System.out.println("MySet[" + i + "]["+j +"].get(" + k + ") = " +finale.get(i).get(j)[k]);
         }
     
     
         }
     
         }
     
          ms = new MySet[finale.size()][finale.get(0).size()];
     
         for (int i =0; i < finale.size(); i++)
         { // begin for
     
         for (int j =0; j < finale.get(i).size(); j++)
         { // begin for 
         try{
     
         ms[i][j] = new MySet();
     
     
     
         for (int k =0; k < finale.get(i).get(j).length; k++)
         { // begin for
         if (finale.get(i).get(j)[k] != null && !finale.get(i).get(j)[k].equals(""))
         { // begin if 
         ms[i][j].add(Integer.parseInt(finale.get(i).get(j)[k]));
     
         } // end if
     
         } // end for
     
         } // end try
           catch(ArrayIndexOutOfBoundsException aioobe)
         {
         System.err.println("Index " + i + "," + j + " is out of bounds.");
         }
     
         } // end for
     
     
         } // end for
     
         for (int i =0; i < ms.length; i++)
         {
         for (int j =0; j < ms[i].length; j++)
         {
         for (int k =0; k < ms[i][j].size(); k++)
         {
         System.out.println("ms[" + i + "]["+j +"].get(" + k + ") = "+ ms[i][j].get(k));
     
         }
         }
     
         }
     
         int startingState = s.nextInt();
         System.out.println("s:" + startingState);
     
         String fs = s.nextLine();
     
         if (fs.equals("") || fs == null)
         fs = s.nextLine();
     
         String[] firstHalf = fs.split("}");
     
     
          int temp = firstHalf[0].indexOf("{");
     
          String secondHalf = firstHalf[0].substring(temp+1);
     
          String[] finalHalf = secondHalf.split(",");
     
     
         ArrayList<Integer> finalState = new ArrayList<Integer>();
     
         for (int i =0; i < finalHalf.length; i++)
    {
    if (finalHalf[i] != null && !finalHalf[i].equals(""))
    {
    finalState.add(Integer.parseInt(finalHalf[i]));
    }
     
    }
     
    System.out.println("Accepting state: " + finalState.toString());
     
     
         getEpsilons2(0);
     
     
         for (int i =0; i < characters - 1; i++)
         {
         ah.add(new HashSet<Integer>());
     
         }
     
         for (int i =0; i < characters -1; i++)
         {
         getClosures(0,i, ah.get(i));
     
         }
     
         System.out.println("To DFA: ");
     
     
     
         System.out.println(epsilons.toString());
     
         int oldSize = ah.size() -1;
     
         ah.add(new HashSet<Integer>());
         ah.add(new HashSet<Integer>());
     
     
     
          for (int i =0; i < characters -1; i++)
         {
         System.out.println(ah.get(i).toString());
     
         }
     
    try
        {
       s2  = new Scanner(new File(args[1]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
     
     
     
     
     
        }
     
     
        public static void getClosures2(Integer state, Integer character, boolean hit, HashSet<Integer> h)
        {
     
        if (hit == true)
        {
        getEpsilons3(state, h);
     
        }
     
        else
        {
     
     
        }
     
        }
        public static void getClosures(Integer state, Integer character, HashSet<Integer> h)
        {
     
     
        h.add(0);
        if (ms[state][character] != null && ms[state][ms[0].length-1] != null)
        {
     
        for (int i =0; i < ms[state][character].size(); i++)
        {
     
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true,h);
     
        }
     
        for (int j =0; j < ms[state][characters-1].size(); j++)
        {
        getClosures(ms[state][characters -1].get(j), character, h);
        }
     
        }
     
     
        else if (ms[state][character] == null && ms[state][ms[0].length-1]!=null)
        {
        for (int i = 0; i < ms[state][characters-1].size(); i++)
        {
        getClosures(ms[state][characters-1].get(i), character, h);
     
        }
     
        }
     
        else if (ms[state][character]!=null && ms[state][ms[0].length-1] == null)
        {
        for (int i =0; i < ms[state][character].size(); i++)
        {
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true, h);
     
        }
        }
     
        else
        {
        // if there is no closures at all, then it's a good idea to stop
     
        return;
     
        }
     
     
        }
     
     
     
     
        public static void getEpsilons2(Integer state)
        {
     
         epsilons.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
        epsilons.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons2(ms[state][ms[0].length-1].get(i));
     
        }
     
        }
        else
     
        return;
     
     
        }
     
     
    }

    I got the exception to go away, but now it'll modify my first two spots on the HashSet ArrayList, even though it shouldn't be.

    What I was going for was to have it apply the getClosures() for each element in the first item in the ArrayList and union the result of getClosures(thatElement....bla bla bla) together. I was to do that for each character other than lambda. In fact, I'm to do that procedure above for all of the ones in the ArrayList. However, I should probably have used a HashSet of HashSet at some point so I don't keep adding duplicates. It needs to stop when it is just only adding duplicates. Also, if it gets a new HashSet that isn't already there, it's to apply that procedure above on it again until there is only repeats left.

    So if I have
    {0,1,2,3,4}

    and after I apply it on it, it gets {1,2,3} then I apply it on {1,2,3} and if {1,2,3} produces {1,2,3,4,5} then I apply it on {1,2,3,4,5}

    I think I stop if it just keeps repeating with the same ones, hence why I'll probably need a HashSet of HashSet, though the iterators would be a royal pain.

    I'd need help with that.

    These lines of code below, which would go after the two new HashSets are added to the ArrayList, somehow are modifying my original HashSets.

      for (Iterator<Integer> it = ah.get(0).iterator(); it.hasNext();)
         {
         for (int j =0; j < characters -1; j++)
         {
         try
         {
         getClosures(it.next(), j, ah.get(0+oldSize));
         }
     
         catch(java.util.ConcurrentModificationException cme)
         {
         System.out.println("Some elements are being modified concurrently.");
     
         }
         }
         }
     
         for (Iterator<Integer> it = ah.get(1).iterator(); it.hasNext();)
         {
         for (int j =0; j < characters -1; j++)
         {
         try
         {
         getClosures(it.next(), j, ah.get(1+oldSize));
         }
     
         catch(java.util.ConcurrentModificationException cme)
         {
         System.out.println("Some elements are being modified concurrently.");
     
         }
         }
         }
    Last edited by javapenguin; April 21st, 2012 at 04:20 PM.

  9. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    It's so weird. I performed the toString on it for just the elements that were originally there and it did fine, I think it did anyway.

    I did it in the for loop for all of the elements in the ArrayList and now it's somehow changed the original ones!!!

    I've checked my code and it shouldn't be modifying the old ones.

    Is the error due to the oldSize thing?

  10. #10
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    I really want to help, but I'm utterly confused right now...

    Could you try explaining to me what you want to do (not the physical coding problem, but actually what the program is to achieve)? What are epsilons (I tried looking it up and epsilon has about seventy-nine quadrillion uses <-- angry hyperbole)? What are closures? Where are errors occurring?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  11. #11
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    Ok, now it's a bit better. However, in addition to me possibly not supposed to be autoadding the 0's other than for the Epsilon, I need help trying to make it keep going. So far all it will do is make the new sets for the first two generated ones and a new set for the new set of the first generated one.

    That FileParser thing is there, might be able to incorporate it into the main class, for the final step which I have to wait a while to get to as this is going slower than planned. I'm going to send the files to here, in a separate post, or at least an ammend to this one. Actually, the FileParser part is being removed till it's needed. The code is shorter and a little bit nicer too.

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Iterator;
     
     
    public class fsm
    {
     
     
    private static class MySet
    {
     
    private  ArrayList<Integer> intList;
     
    public MySet()
    {
    intList = new ArrayList<Integer>();
     
    }
     
    public  void add(Integer data)
    {
    intList.add(data);
     
    }
     
    public void remove()
    {
    intList.remove(0);
     
    }
     
    public int size()
    {
    return intList.size();
    }
     
    public Integer get(int index) throws NullPointerException
    {
    return intList.get(index);
     
    }
     
    public ArrayList<Integer> getList()
    {
    return intList;
    }
     
     
     
    }
     
     static MySet[][] ms;
     static int characters;
     static HashSet<HashSet<Integer>> sos;
     static HashSet<Integer> epsilons;
     
     static ArrayList<HashSet<Integer>> ah;
        public static void main(String[] args)
        {
    	Scanner s = null;
    	Scanner s2 = null;
    	ArrayList<Integer> stateList = new ArrayList<Integer>();
    	ArrayList<Character> alphabet = new ArrayList<Character>();
     
    	ArrayList<String> transitionLines = new ArrayList<String>();
    	ArrayList<String[]> splitArray = new ArrayList<String[]>();
     
    	epsilons = new HashSet<Integer>();
     
    	ah = new ArrayList<HashSet<Integer>>();
     
     
     
     
    	int numOfStates = 0;
     
    try
        {
       s  = new Scanner(new File(args[0]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
    numOfStates = s.nextInt();
     
        System.out.println(numOfStates);
     
        String[][] splits = new String[numOfStates][2];
     
     
     
     
        String line = s.nextLine();
        if (line.equals(""))
        line = s.nextLine();
     
     
     
     
        System.out.println(line);
     
        char[] chars = line.toCharArray();
     
     
     
     
        char empty = ' ';
     
     
        String tab = "\t";
        char tab2 = tab.charAt(0);
     
        for (int i =0; i < chars.length; i++)
        {
        if (chars[i]!= empty && chars[i]!=tab2)
        {
        alphabet.add(chars[i]);
        }
        }
     
        alphabet.add('¸');
     
         System.out.println(alphabet.toString());
     
         characters = alphabet.size();
     
     
         for (int i=0; i < numOfStates; i++)
         {
         transitionLines.add(s.nextLine());
     
         }
     
         System.out.println(transitionLines.toString());
     
     
         for (int i =0; i < splits.length; i++)
         {
         splits[i] = transitionLines.get(i).split(":");
     
         }
     
     
         for (int i =0; i < splits.length; i++)
         {
         for (int j=0; j < splits[i].length; j++)
         {
         System.out.println(splits[i][j]);
     
         }
     
     
         }
     
       //  String[][] test = 
         ArrayList<String[]> fredBowling = new ArrayList<String[]>();
     
         for (int i =0; i < splits.length; i++)
         {
         fredBowling.add(splits[i][1].split("}"));
         }
         splits[0][1].split("}");
     
         ArrayList<Integer> state = new ArrayList<Integer>();
     
         for (int i=0; i < splits.length; i++)
         {
         state.add(Integer.parseInt(splits[i][0]));
     
         }
     
     
       //  System.out.println(java.util.Arrays.toString(test));
     
     
        // String smaller[] = new String[test.length];
     
        ArrayList<ArrayList<String>> smaller = new ArrayList<ArrayList<String>>();
     
         for (int i =0; i < fredBowling.size(); i++)
         {
         smaller.add(new ArrayList<String>());
         for (int j = 0; j < fredBowling.get(i).length; j++)
         {
         int temp = fredBowling.get(i)[j].indexOf("{");
     
         smaller.get(i).add(fredBowling.get(i)[j].substring(temp + 1));
         }
     
         }
     
       //  System.out.println(java.util.Arrays.toString(smaller));
     
        //  ArrayList<String[]> finale = new ArrayList<String[]>();
         ArrayList<ArrayList<String[]>> finale = new ArrayList<ArrayList<String[]>>();
     
         // for (int i =0; i < smaller.length; i++)
      //    {
       //   finale.add(smaller[i].split(","));
     
     //    }
     
         for (int i =0; i < smaller.size(); i++)
         {
         finale.add(new ArrayList<String[]>());
         for (int j =0; j < smaller.get(i).size(); j++)
         {
         finale.get(i).add(smaller.get(i).get(j).split(","));
     
         }
     
     
         }
     
         for (int i =0; i < finale.size(); i++)
         {
         for (int j =0; j < finale.get(i).size(); j++)
         {
         for (int k =0; k < finale.get(i).get(j).length; k++)
         {
         System.out.println("MySet[" + i + "]["+j +"].get(" + k + ") = " +finale.get(i).get(j)[k]);
         }
     
     
         }
     
         }
     
          ms = new MySet[finale.size()][finale.get(0).size()];
     
         for (int i =0; i < finale.size(); i++)
         { // begin for
     
         for (int j =0; j < finale.get(i).size(); j++)
         { // begin for 
         try{
     
         ms[i][j] = new MySet();
     
     
     
         for (int k =0; k < finale.get(i).get(j).length; k++)
         { // begin for
         if (finale.get(i).get(j)[k] != null && !finale.get(i).get(j)[k].equals(""))
         { // begin if 
         ms[i][j].add(Integer.parseInt(finale.get(i).get(j)[k]));
     
         } // end if
     
         } // end for
     
         } // end try
           catch(ArrayIndexOutOfBoundsException aioobe)
         {
         System.err.println("Index " + i + "," + j + " is out of bounds.");
         }
     
         } // end for
     
     
         } // end for
     
         for (int i =0; i < ms.length; i++)
         {
         for (int j =0; j < ms[i].length; j++)
         {
         for (int k =0; k < ms[i][j].size(); k++)
         {
         System.out.println("ms[" + i + "]["+j +"].get(" + k + ") = "+ ms[i][j].get(k));
     
         }
         }
     
         }
     
         int startingState = s.nextInt();
         System.out.println("s:" + startingState);
     
         String fs = s.nextLine();
     
         if (fs.equals("") || fs == null)
         fs = s.nextLine();
     
         String[] firstHalf = fs.split("}");
     
     
          int temp = firstHalf[0].indexOf("{");
     
          String secondHalf = firstHalf[0].substring(temp+1);
     
          String[] finalHalf = secondHalf.split(",");
     
     
         ArrayList<Integer> finalState = new ArrayList<Integer>();
     
         for (int i =0; i < finalHalf.length; i++)
    {
    if (finalHalf[i] != null && !finalHalf[i].equals(""))
    {
    finalState.add(Integer.parseInt(finalHalf[i]));
    }
     
    }
     
    System.out.println("Accepting state: " + finalState.toString());
     
     
     
         getEpsilons2(0);
     
     
         for (int i =0; i < characters - 1; i++)
         {
         ah.add(new HashSet<Integer>());
     
         }
     
         for (int i =0; i < characters -1; i++)
         {
         getClosures(0,i, ah.get(i));
     
         }
     
         System.out.println("To DFA: ");
     
     
     
         System.out.println(epsilons.toString());
     
     
     
          for (int i =0; i < characters -1; i++)
         {
         System.out.println(ah.get(i).toString());
     
         }
     
     
         ah.add(getNewSet(ah.get(0), 0));
         ah.add(getNewSet(ah.get(1), 0));
         ah.add(getNewSet(ah.get(0), 1));
         ah.add(getNewSet(ah.get(characters+1), 0));
     
         System.out.println(ah.get(characters-1).toString());
         System.out.println(ah.get(characters).toString());
         System.out.println(ah.get(characters+1).toString());
         System.out.println(ah.get(characters+2).toString());
     
         HashSet<HashSet<Integer>> hshs = new HashSet<HashSet<Integer>>();
     
         for (int i =0; i < ah.size(); i++)
         {
         hshs.add(ah.get(i));
     
         }
     
         hshs.add(epsilons);
     
         System.out.println(hshs.toString());
    try
        {
       s2  = new Scanner(new File(args[1]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
     
     
     
     
     
        }
     
        public static void getClosures(Integer state, Integer character, HashSet<Integer> h)
        {
     
     
        h.add(0);
        if (ms[state][character] != null && ms[state][ms[0].length-1] != null)
        {
     
        for (int i =0; i < ms[state][character].size(); i++)
        {
     
        h.add(ms[state][character].get(i));
     
     
       getEpsilons3(state,h);
     
        }
     
        for (int j =0; j < ms[state][characters-1].size(); j++)
        {
        getClosures(ms[state][characters -1].get(j), character, h);
        }
     
        }
     
     
        else if (ms[state][character] == null && ms[state][ms[0].length-1]!=null)
        {
        for (int i = 0; i < ms[state][characters-1].size(); i++)
        {
        getClosures(ms[state][characters-1].get(i), character, h);
     
        }
     
        }
     
        else if (ms[state][character]!=null && ms[state][ms[0].length-1] == null)
        {
        for (int i =0; i < ms[state][character].size(); i++)
        {
        h.add(ms[state][character].get(i));
     
       getEpsilons3(state,h);
     
        }
        }
     
        else
        {
        // if there is no closures at all, then it's a good idea to stop
     
        return;
     
        }
     
     
        }
     
        public static HashSet<Integer> getNewSet(HashSet<Integer> oldSet, Integer character)
        {
     
        HashSet<Integer> newSet = new HashSet<Integer>();
     
        Iterator<Integer> itr = oldSet.iterator();
     
        while (itr.hasNext())
        {
     
        getClosures(itr.next(), character, newSet);
     
     
        }
     
        return newSet;
     
        }
     
     
          public static void getEpsilons3(Integer state, HashSet<Integer> h)
        {
     
         h.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
     
     
       h.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons3(ms[state][ms[0].length-1].get(i), h);
     
        }
     
        }
        else
     
        return;
     
     
        }
        public static void getEpsilons2(Integer state)
        {
     
         epsilons.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
        epsilons.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons2(ms[state][ms[0].length-1].get(i));
     
        }
     
        }
        else
     
        return;
     
     
        }
     
     
     
    }

    The files will be here shortly. I hope EOD lets me go to this forum. It uses Firefox 3, to maintain better compatibility with some stuff, so there's no guarantees.
    Attached Files Attached Files
    Last edited by javapenguin; April 21st, 2012 at 07:54 PM.

  12. #12
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    Please read my previous post on the first page (should be the last post) and answer those questions. I will be able to more effectively help if I know what's going on, and to do that I need those questions answered.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  13. #13
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    Never mind the epsilon thing. That part has been taken care of.

    This is very hard to explain.

    Suffice it to say that once I get the first group of HashSets, I then take each individual element and, for similar conditions, i.e. characters, on each of the result and continue to do so until there are no new results.

    To make things worse though, something went wrong just in the past hour or two that had been working for a bit.

    As for the stuff, that's actually sort of logic. A Nondeterministic finite state machine to a deterministic finite state machine. It's very hard to understand as they often use symbols to describe it all, and I was trying to explain it in laymen's terms the best I could, hence the 100's of lines in the first post.

    I got it to work a little bit better now.

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Iterator;
     
     
    public class fsm
    {
     
    private static class FileParser
    {
     
     
     
    }
     
    private static class MySet
    {
     
    private  ArrayList<Integer> intList;
     
    public MySet()
    {
    intList = new ArrayList<Integer>();
     
    }
     
    public  void add(Integer data)
    {
    intList.add(data);
     
    }
     
    public void remove()
    {
    intList.remove(0);
     
    }
     
    public int size()
    {
    return intList.size();
    }
     
    public Integer get(int index) throws NullPointerException
    {
    return intList.get(index);
     
    }
     
    public ArrayList<Integer> getList()
    {
    return intList;
    }
     
     
     
    }
     
     static MySet[][] ms;
     static int characters;
     static HashSet<HashSet<Integer>> sos;
     static HashSet<Integer> epsilons;
     
     static ArrayList<HashSet<Integer>> ah;
        public static void main(String[] args)
        {
    	Scanner s = null;
    	Scanner s2 = null;
    	ArrayList<Integer> stateList = new ArrayList<Integer>();
    	ArrayList<Character> alphabet = new ArrayList<Character>();
     
    	ArrayList<String> transitionLines = new ArrayList<String>();
    	ArrayList<String[]> splitArray = new ArrayList<String[]>();
     
    	epsilons = new HashSet<Integer>();
     
    	ah = new ArrayList<HashSet<Integer>>();
     
     
     
     
    	int numOfStates = 0;
     
    try
        {
       s  = new Scanner(new File(args[0]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
    numOfStates = s.nextInt();
     
        System.out.println(numOfStates);
     
        String[][] splits = new String[numOfStates][2];
     
     
     
     
        String line = s.nextLine();
        if (line.equals(""))
        line = s.nextLine();
     
     
     
     
        System.out.println(line);
     
        char[] chars = line.toCharArray();
     
     
     
     
        char empty = ' ';
     
     
        String tab = "\t";
        char tab2 = tab.charAt(0);
     
        for (int i =0; i < chars.length; i++)
        {
        if (chars[i]!= empty && chars[i]!=tab2)
        {
        alphabet.add(chars[i]);
        }
        }
     
        alphabet.add('¸');
     
         System.out.println(alphabet.toString());
     
         characters = alphabet.size();
     
     
         for (int i=0; i < numOfStates; i++)
         {
         transitionLines.add(s.nextLine());
     
         }
     
         System.out.println(transitionLines.toString());
     
     
         for (int i =0; i < splits.length; i++)
         {
         splits[i] = transitionLines.get(i).split(":");
     
         }
     
     
         for (int i =0; i < splits.length; i++)
         {
         for (int j=0; j < splits[i].length; j++)
         {
         System.out.println(splits[i][j]);
     
         }
     
     
         }
     
       //  String[][] test = 
         ArrayList<String[]> fredBowling = new ArrayList<String[]>();
     
         for (int i =0; i < splits.length; i++)
         {
         fredBowling.add(splits[i][1].split("}"));
         }
         splits[0][1].split("}");
     
         ArrayList<Integer> state = new ArrayList<Integer>();
     
         for (int i=0; i < splits.length; i++)
         {
         state.add(Integer.parseInt(splits[i][0]));
     
         }
     
     
       //  System.out.println(java.util.Arrays.toString(test));
     
     
        // String smaller[] = new String[test.length];
     
        ArrayList<ArrayList<String>> smaller = new ArrayList<ArrayList<String>>();
     
         for (int i =0; i < fredBowling.size(); i++)
         {
         smaller.add(new ArrayList<String>());
         for (int j = 0; j < fredBowling.get(i).length; j++)
         {
         int temp = fredBowling.get(i)[j].indexOf("{");
     
         smaller.get(i).add(fredBowling.get(i)[j].substring(temp + 1));
         }
     
         }
     
       //  System.out.println(java.util.Arrays.toString(smaller));
     
        //  ArrayList<String[]> finale = new ArrayList<String[]>();
         ArrayList<ArrayList<String[]>> finale = new ArrayList<ArrayList<String[]>>();
     
         // for (int i =0; i < smaller.length; i++)
      //    {
       //   finale.add(smaller[i].split(","));
     
     //    }
     
         for (int i =0; i < smaller.size(); i++)
         {
         finale.add(new ArrayList<String[]>());
         for (int j =0; j < smaller.get(i).size(); j++)
         {
         finale.get(i).add(smaller.get(i).get(j).split(","));
     
         }
     
     
         }
     
         for (int i =0; i < finale.size(); i++)
         {
         for (int j =0; j < finale.get(i).size(); j++)
         {
         for (int k =0; k < finale.get(i).get(j).length; k++)
         {
         System.out.println("MySet[" + i + "]["+j +"].get(" + k + ") = " +finale.get(i).get(j)[k]);
         }
     
     
         }
     
         }
     
          ms = new MySet[finale.size()][finale.get(0).size()];
     
         for (int i =0; i < finale.size(); i++)
         { // begin for
     
         for (int j =0; j < finale.get(i).size(); j++)
         { // begin for 
         try{
     
         ms[i][j] = new MySet();
     
     
     
         for (int k =0; k < finale.get(i).get(j).length; k++)
         { // begin for
         if (finale.get(i).get(j)[k] != null && !finale.get(i).get(j)[k].equals(""))
         { // begin if 
         ms[i][j].add(Integer.parseInt(finale.get(i).get(j)[k]));
     
         } // end if
     
         } // end for
     
         } // end try
           catch(ArrayIndexOutOfBoundsException aioobe)
         {
         System.err.println("Index " + i + "," + j + " is out of bounds.");
         }
     
         } // end for
     
     
         } // end for
     
         for (int i =0; i < ms.length; i++)
         {
         for (int j =0; j < ms[i].length; j++)
         {
         for (int k =0; k < ms[i][j].size(); k++)
         {
         System.out.println("ms[" + i + "]["+j +"].get(" + k + ") = "+ ms[i][j].get(k));
     
         }
         }
     
         }
     
         int startingState = s.nextInt();
         System.out.println("s:" + startingState);
     
         String fs = s.nextLine();
     
         if (fs.equals("") || fs == null)
         fs = s.nextLine();
     
         String[] firstHalf = fs.split("}");
     
     
          int temp = firstHalf[0].indexOf("{");
     
          String secondHalf = firstHalf[0].substring(temp+1);
     
          String[] finalHalf = secondHalf.split(",");
     
     
         ArrayList<Integer> finalState = new ArrayList<Integer>();
     
         for (int i =0; i < finalHalf.length; i++)
    {
    if (finalHalf[i] != null && !finalHalf[i].equals(""))
    {
    finalState.add(Integer.parseInt(finalHalf[i]));
    }
     
    }
     
    System.out.println("Accepting state: " + finalState.toString());
     
     
         getEpsilons2(0);
     
     
         for (int i =0; i < characters - 1; i++)
         {
         ah.add(new HashSet<Integer>());
     
         }
     
         for (int i =0; i < characters -1; i++)
         {
         getClosures(0,i, ah.get(i));
     
         }
     
         System.out.println("To DFA: ");
     
     
     
         System.out.println(epsilons.toString());
     
         int oldSize = ah.size() -1;
     
         ah.add(new HashSet<Integer>());
         ah.add(new HashSet<Integer>());
     
     
     
          for (int i =0; i < characters -1; i++)
         {
         System.out.println(ah.get(i).toString());
     
         }
     
          ah.add(getNewSet(ah.get(0), 0));
         ah.add(getNewSet(ah.get(1), 0));
         ah.add(getNewSet(ah.get(0), 1));
         ah.add(getNewSet(ah.get(characters+1), 0));
     
         System.out.println(ah.get(characters-1).toString());
         System.out.println(ah.get(characters).toString());
         System.out.println(ah.get(characters+1).toString());
         System.out.println(ah.get(characters+2).toString());
     
         HashSet<HashSet<Integer>> hshs = new HashSet<HashSet<Integer>>();
     
         for (int i =0; i < ah.size(); i++)
         {
         if (!ah.get(i).isEmpty())
     
         hshs.add(ah.get(i));
     
         }
     
         hshs.add(epsilons);
     
         System.out.println(hshs.toString());
     
    try
        {
       s2  = new Scanner(new File(args[1]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
     
     
     
     
     
        }
     
     
        public static void getClosures2(Integer state, Integer character, boolean hit, HashSet<Integer> h)
        {
     
        if (hit == true)
        {
        getEpsilons3(state, h);
     
        }
     
        else
        {
     
     
        }
     
        }
        public static void getClosures(Integer state, Integer character, HashSet<Integer> h)
        {
     
     
        h.add(0);
        if (ms[state][character] != null && ms[state][ms[0].length-1] != null)
        {
     
        for (int i =0; i < ms[state][character].size(); i++)
        {
     
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true,h);
     
        }
     
        for (int j =0; j < ms[state][characters-1].size(); j++)
        {
        getClosures(ms[state][characters -1].get(j), character, h);
        }
     
        }
     
     
        else if (ms[state][character] == null && ms[state][ms[0].length-1]!=null)
        {
        for (int i = 0; i < ms[state][characters-1].size(); i++)
        {
        getClosures(ms[state][characters-1].get(i), character, h);
     
        }
     
        }
     
        else if (ms[state][character]!=null && ms[state][ms[0].length-1] == null)
        {
        for (int i =0; i < ms[state][character].size(); i++)
        {
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true, h);
     
        }
        }
     
        else
        {
        // if there is no closures at all, then it's a good idea to stop
     
        return;
     
        }
     
     
        }
     
      public static HashSet<Integer> getNewSet(HashSet<Integer> oldSet, Integer character)
        {
     
        HashSet<Integer> newSet = new HashSet<Integer>();
     
        Iterator<Integer> itr = oldSet.iterator();
     
        while (itr.hasNext())
        {
     
        getClosures(itr.next(), character, newSet);
     
     
        }
     
        return newSet;
     
        }
     
           public static void getEpsilons3(Integer state, HashSet<Integer> h)
        {
     
         h.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
     
     
       h.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons3(ms[state][ms[0].length-1].get(i), h);
     
        }
     
        }
        else
     
        return;
     
     
        }
        public static void getEpsilons2(Integer state)
        {
     
         epsilons.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
        epsilons.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons2(ms[state][ms[0].length-1].get(i));
     
        }
     
        }
        else
     
        return;
     
     
        }
     
     
    }

    It's at this point that I'm trying to recursively keep doing the stuff below till it runs out of new sets.

     ah.add(getNewSet(ah.get(0), 0));
         ah.add(getNewSet(ah.get(1), 0));
         ah.add(getNewSet(ah.get(0), 1));
         ah.add(getNewSet(ah.get(characters+1), 0));
     
         System.out.println(ah.get(characters-1).toString());
         System.out.println(ah.get(characters).toString());
         System.out.println(ah.get(characters+1).toString());
         System.out.println(ah.get(characters+2).toString());
    Last edited by javapenguin; April 21st, 2012 at 09:43 PM.

  14. #14
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    *does some Wikipedia readings*

    That is actually really cool. So, basically your program jumps from state to state based on a String by taking the characters one at a time and putting them through an algorithm?

    If I understand correctly ^, would your problem right now be that you are having trouble weeding out duplicate sets (states)?

    What exactly denotes a set? I know you have an array of arrays of MySets, but how do you use those to get sets you want?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  15. #15
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    In a way yes. Another problem is that it often seems to be overriding my old sets.

    What I was doing was trying to get the sets gotten with getNewState() or whatever I called it, to once more do that again with the new sets and if it gets any new sets, to keep going on that new set until all that's left is the same sets over and over.


    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Iterator;
     
     
    public class fsm
    {
     
    private static class FileParser
    {
     
    private Scanner s;
     
    public FileParser(Scanner s)
    {
    setScanner(s);
     
     
     
    }
     
    public void setScanner(Scanner s)
    {
    this.s = s;
    }
     
    public Scanner getScanner()
    {
    return s;
    }
     
     
    }
     
    private static class MySet
    {
     
    private  ArrayList<Integer> intList;
     
    public MySet()
    {
    intList = new ArrayList<Integer>();
     
    }
     
    public  void add(Integer data)
    {
    intList.add(data);
     
    }
     
    public void remove()
    {
    intList.remove(0);
     
    }
     
    public int size()
    {
    return intList.size();
    }
     
    public Integer get(int index) throws NullPointerException
    {
    return intList.get(index);
     
    }
     
    public ArrayList<Integer> getList()
    {
    return intList;
    }
     
     
     
    }
     
     static MySet[][] ms;
     static int characters;
     static HashSet<HashSet<Integer>> sos;
     static HashSet<Integer> epsilons;
     
     static ArrayList<HashSet<Integer>> ah;
        public static void main(String[] args)
        {
    	Scanner s = null;
    	Scanner s2 = null;
    	ArrayList<Integer> stateList = new ArrayList<Integer>();
    	ArrayList<Character> alphabet = new ArrayList<Character>();
     
    	ArrayList<String> transitionLines = new ArrayList<String>();
    	ArrayList<String[]> splitArray = new ArrayList<String[]>();
     
    	epsilons = new HashSet<Integer>();
     
    	ah = new ArrayList<HashSet<Integer>>();
     
     
     
     
    	int numOfStates = 0;
     
    try
        {
       s  = new Scanner(new File(args[0]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
    numOfStates = s.nextInt();
     
        System.out.println(numOfStates);
     
        String[][] splits = new String[numOfStates][2];
     
     
     
     
        String line = s.nextLine();
        if (line.equals(""))
        line = s.nextLine();
     
     
     
     
        System.out.println(line);
     
        char[] chars = line.toCharArray();
     
     
     
     
        char empty = ' ';
     
     
        String tab = "\t";
        char tab2 = tab.charAt(0);
     
        for (int i =0; i < chars.length; i++)
        {
        if (chars[i]!= empty && chars[i]!=tab2)
        {
        alphabet.add(chars[i]);
        }
        }
     
        alphabet.add('¸');
     
         System.out.println(alphabet.toString());
     
         characters = alphabet.size();
     
     
         for (int i=0; i < numOfStates; i++)
         {
         transitionLines.add(s.nextLine());
     
         }
     
         System.out.println(transitionLines.toString());
     
     
         for (int i =0; i < splits.length; i++)
         {
         splits[i] = transitionLines.get(i).split(":");
     
         }
     
     
         for (int i =0; i < splits.length; i++)
         {
         for (int j=0; j < splits[i].length; j++)
         {
         System.out.println(splits[i][j]);
     
         }
     
     
         }
     
       //  String[][] test = 
         ArrayList<String[]> fredBowling = new ArrayList<String[]>();
     
         for (int i =0; i < splits.length; i++)
         {
         fredBowling.add(splits[i][1].split("}"));
         }
         splits[0][1].split("}");
     
         ArrayList<Integer> state = new ArrayList<Integer>();
     
         for (int i=0; i < splits.length; i++)
         {
         state.add(Integer.parseInt(splits[i][0]));
     
         }
     
     
       //  System.out.println(java.util.Arrays.toString(test));
     
     
        // String smaller[] = new String[test.length];
     
        ArrayList<ArrayList<String>> smaller = new ArrayList<ArrayList<String>>();
     
         for (int i =0; i < fredBowling.size(); i++)
         {
         smaller.add(new ArrayList<String>());
         for (int j = 0; j < fredBowling.get(i).length; j++)
         {
         int temp = fredBowling.get(i)[j].indexOf("{");
     
         smaller.get(i).add(fredBowling.get(i)[j].substring(temp + 1));
         }
     
         }
     
       //  System.out.println(java.util.Arrays.toString(smaller));
     
        //  ArrayList<String[]> finale = new ArrayList<String[]>();
         ArrayList<ArrayList<String[]>> finale = new ArrayList<ArrayList<String[]>>();
     
         // for (int i =0; i < smaller.length; i++)
      //    {
       //   finale.add(smaller[i].split(","));
     
     //    }
     
         for (int i =0; i < smaller.size(); i++)
         {
         finale.add(new ArrayList<String[]>());
         for (int j =0; j < smaller.get(i).size(); j++)
         {
         finale.get(i).add(smaller.get(i).get(j).split(","));
     
         }
     
     
         }
     
         for (int i =0; i < finale.size(); i++)
         {
         for (int j =0; j < finale.get(i).size(); j++)
         {
         for (int k =0; k < finale.get(i).get(j).length; k++)
         {
         System.out.println("MySet[" + i + "]["+j +"].get(" + k + ") = " +finale.get(i).get(j)[k]);
         }
     
     
         }
     
         }
     
          ms = new MySet[finale.size()][finale.get(0).size()];
     
         for (int i =0; i < finale.size(); i++)
         { // begin for
     
         for (int j =0; j < finale.get(i).size(); j++)
         { // begin for 
         try{
     
         ms[i][j] = new MySet();
     
     
     
         for (int k =0; k < finale.get(i).get(j).length; k++)
         { // begin for
         if (finale.get(i).get(j)[k] != null && !finale.get(i).get(j)[k].equals(""))
         { // begin if 
         ms[i][j].add(Integer.parseInt(finale.get(i).get(j)[k]));
     
         } // end if
     
         } // end for
     
         } // end try
           catch(ArrayIndexOutOfBoundsException aioobe)
         {
         System.err.println("Index " + i + "," + j + " is out of bounds.");
         }
     
         } // end for
     
     
         } // end for
     
         for (int i =0; i < ms.length; i++)
         {
         for (int j =0; j < ms[i].length; j++)
         {
         for (int k =0; k < ms[i][j].size(); k++)
         {
         System.out.println("ms[" + i + "]["+j +"].get(" + k + ") = "+ ms[i][j].get(k));
     
         }
         }
     
         }
     
         int startingState = s.nextInt();
         System.out.println("s:" + startingState);
     
         String fs = s.nextLine();
     
         if (fs.equals("") || fs == null)
         fs = s.nextLine();
     
         String[] firstHalf = fs.split("}");
     
     
          int temp = firstHalf[0].indexOf("{");
     
          String secondHalf = firstHalf[0].substring(temp+1);
     
          String[] finalHalf = secondHalf.split(",");
     
     
         ArrayList<Integer> finalState = new ArrayList<Integer>();
     
         for (int i =0; i < finalHalf.length; i++)
    {
    if (finalHalf[i] != null && !finalHalf[i].equals(""))
    {
    finalState.add(Integer.parseInt(finalHalf[i]));
    }
     
    }
     
    System.out.println("Accepting state: " + finalState.toString());
     
     
         getEpsilons2(0);
     
     
         for (int i =0; i < characters - 1; i++)
         {
         ah.add(new HashSet<Integer>());
     
         }
     
         for (int i =0; i < characters -1; i++)
         {
         getClosures(0,i, ah.get(i));
     
         }
     
         System.out.println("To DFA: ");
     
     
     
         System.out.println(epsilons.toString());
     
         int oldSize = ah.size() -1;
     
        // ah.add(new HashSet<Integer>());
       //  ah.add(new HashSet<Integer>());
     
     
     
          for (int i =0; i < characters -1; i++)
         {
         System.out.println(ah.get(i).toString());
     
         }
     
      //    ah.add(getNewSet(ah.get(0), 0));
       //  ah.add(getNewSet(ah.get(1), 0));
       //  ah.add(getNewSet(ah.get(0), 1));
       //  ah.add(getNewSet(ah.get(characters+1), 0));
     
         for (int i =0; i < characters -1; i++)
         {
     
         for (int j =0; j < ah.size(); j++)
         {
     
         if (!getNewSet(ah.get(j), i).isEmpty())
         ah.add(getNewSet(ah.get(j), i));
     
         }
     
         }
     
      //   System.out.println(ah.get(characters-1).toString());
      //  System.out.println(ah.get(characters).toString());
       //  System.out.println(ah.get(characters+1).toString());
       //  System.out.println(ah.get(characters+2).toString());
     
         HashSet<HashSet<Integer>> hshs = new HashSet<HashSet<Integer>>();
     
         for (int i =0; i < ah.size(); i++)
         {
         if (!ah.get(i).isEmpty())
     
         hshs.add(ah.get(i));
     
         }
     
         hshs.add(epsilons);
     
         System.out.println(hshs.toString());
     
    try
        {
       s2  = new Scanner(new File(args[1]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
     FileParser fp = new FileParser(s2);
     
     
     
     
     
     
        }
     
     
        public static void getClosures2(Integer state, Integer character, boolean hit, HashSet<Integer> h)
        {
     
        if (hit == true)
        {
        getEpsilons3(state, h);
     
        }
     
        else
        {
     
     
        }
     
        }
        public static void getClosures(Integer state, Integer character, HashSet<Integer> h)
        {
     
     
        h.add(0);
        if (ms[state][character] != null && ms[state][ms[0].length-1] != null)
        {
     
        for (int i =0; i < ms[state][character].size(); i++)
        {
     
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true,h);
     
        }
     
        for (int j =0; j < ms[state][characters-1].size(); j++)
        {
        getClosures(ms[state][characters -1].get(j), character, h);
        }
     
        }
     
     
        else if (ms[state][character] == null && ms[state][ms[0].length-1]!=null)
        {
        for (int i = 0; i < ms[state][characters-1].size(); i++)
        {
        getClosures(ms[state][characters-1].get(i), character, h);
     
        }
     
        }
     
        else if (ms[state][character]!=null && ms[state][ms[0].length-1] == null)
        {
        for (int i =0; i < ms[state][character].size(); i++)
        {
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true, h);
     
        }
        }
     
        else
        {
        // if there is no closures at all, then it's a good idea to stop
     
        return;
     
        }
     
     
        }
     
      public static HashSet<Integer> getNewSet(HashSet<Integer> oldSet, Integer character)
        {
     
        HashSet<Integer> newSet = new HashSet<Integer>();
     
        Iterator<Integer> itr = oldSet.iterator();
     
        while (itr.hasNext())
        {
     
        getClosures(itr.next(), character, newSet);
     
     
        }
     
        return newSet;
     
        }
     
           public static void getEpsilons3(Integer state, HashSet<Integer> h)
        {
     
         h.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
     
     
       h.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons3(ms[state][ms[0].length-1].get(i), h);
     
        }
     
        }
        else
     
        return;
     
     
        }
        public static void getEpsilons2(Integer state)
        {
     
         epsilons.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
        epsilons.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons2(ms[state][ms[0].length-1].get(i));
     
        }
     
        }
        else
     
        return;
     
     
        }
     
     
    }

     for (int i =0; i < characters -1; i++)
         {
     
         for (int j =0; j < ah.size(); j++)
         {
     
         if (!getNewSet(ah.get(j), i).isEmpty())
         ah.add(getNewSet(ah.get(j), i));
     
         }
     
         }
    Right now I think the for loop is going to change because the size is getting bigger, and that's ok, but it's going into some kind of infinite loop. However, that might be where I'm printing out the toString() of the HashSet of HashSets where it's going wrong.
    Last edited by javapenguin; April 22nd, 2012 at 01:54 PM.

  16. #16
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    Ok, I'm a bit farther along now. However, I'm wondering how to recursively do something.

    I have like 800 lines of code and since the error, or the coding problem, is the latest part in the execution, I need to include all of it.

    It's in my FileReader class. I think I'm supposed to do something like this for the parser:

    0: a

    1: a

    1: b

    3: a

    1: a

    1: accepted

    0: a
    1: b
    3: b
    3: b
    3: a
    1: a
    1: a
    1: b
    3: a
    1: b
    3: a
    1: a
    1: b
    3: reject

    I already have the code and it works for nfa2.nfa anyway. I think it's possible some of the other ones might have too many states. But oh well. I can hopefully fix that tomorrow.

    However, today I'd like to get a lot of this done. I have also to do an extra credit thing for this class as my grade is pretty low (It's hard to get above a 6/10 average on those quizzes. Fortunately, there's like 18% or so possible extra credit. Plus this program, which I need to do well on.

    This might not be as hard if I weren't as tired as a rabbit run over by a 16 wheel semi.

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.HashMap;
     
     
    public class fsm
    {
     
    private static class FileParser
    {
     
    private Scanner s;
     
    public FileParser(Scanner s)
    {
    setScanner(s);
     
    while (s.hasNext())
    {
     
    String line = s.nextLine();
    String line2 = line.toString(); // I fear a direct assingment statement as I don't want a shallow copy
     
    if (line == null || line.equals(""))
    System.out.println("The String [empty String] is not valid.");
    else
    {
    Scanner temp = new Scanner(line);
     
    int count =0;
    int size = line.length();
     
     
     
     
    boolean accepting = true;
     
    while (accepting == true && count < size)
    { // begin while
    char c = line.charAt(count);
     
    if (accepted(c) == false)
     
    accepting = false;
     
    count++;
    } // end while
     
    if (accepting == true)
    System.out.println("String " + line + " is accepted.");
    else
    System.out.println("String " + line + " is rejected.");
     
    } // end while
     
     
    }
     
     
    }
     
     
     
    public void setScanner(Scanner s)
    {
    this.s = s;
    }
     
    public static boolean accepted(char c)
    {
     
    if (alphabet.contains(c) == false)
    return false;
    if (getClosures3(0, hm.get(c)).size() == 1)
    {
     
    System.out.println(getClosures3(0, hm.get(c)).toString());
    return false;
     
    }
    else
    {
    System.out.println(getClosures3(0, hm.get(c)).toString());
    return true;
    }
     
    }
     
    public Scanner getScanner()
    {
    return s;
    }
     
     
    }
     
    private static class MySet
    {
     
    private  ArrayList<Integer> intList;
     
    public MySet()
    {
    intList = new ArrayList<Integer>();
     
    }
     
    public  void add(Integer data)
    {
    intList.add(data);
     
    }
     
    public void remove()
    {
    intList.remove(0);
     
    }
     
    public int size()
    {
    return intList.size();
    }
     
    public Integer get(int index) throws NullPointerException
    {
    return intList.get(index);
     
    }
     
    public ArrayList<Integer> getList()
    {
    return intList;
    }
     
     
     
    }
     
     static MySet[][] ms;
     static int characters;
     static HashSet<HashSet<Integer>> sos;
     static HashSet<Integer> epsilons;
     static HashSet<HashSet<Integer>> hshs;
     static HashSet<HashSet<Integer>> hshs3;
     static ArrayList<HashSet<Integer>> ah;
     static HashMap<Character, Integer> hm;
     static ArrayList<Character> alphabet;
        public static void main(String[] args)
        {
    	Scanner s = null;
    	Scanner s2 = null;
    	ArrayList<Integer> stateList = new ArrayList<Integer>();
    	 alphabet = new ArrayList<Character>();
     
    	ArrayList<String> transitionLines = new ArrayList<String>();
    	ArrayList<String[]> splitArray = new ArrayList<String[]>();
     
    	epsilons = new HashSet<Integer>();
     
    	ah = new ArrayList<HashSet<Integer>>();
     
     
     
     
    	int numOfStates = 0;
    	hm = new HashMap<Character, Integer>();
     
    try
        {
       s  = new Scanner(new File(args[0]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
    numOfStates = s.nextInt();
     
        System.out.println(numOfStates);
     
        String[][] splits = new String[numOfStates][2];
     
     
     
     
        String line = s.nextLine();
        if (line.equals(""))
        line = s.nextLine();
     
     
     
     
        System.out.println(line);
     
        char[] chars = line.toCharArray();
     
     
     
     
        char empty = ' ';
     
     
        String tab = "\t";
        char tab2 = tab.charAt(0);
     
        for (int i =0; i < chars.length; i++)
        {
        if (chars[i]!= empty && chars[i]!=tab2)
        {
        alphabet.add(chars[i]);
        }
        }
     
        alphabet.add('¸');
     
        for (int i =0; i < alphabet.size(); i++)
        {
        hm.put(alphabet.get(i), i);
     
        }
     
         System.out.println(alphabet.toString());
     
         characters = alphabet.size();
     
     
         for (int i=0; i < numOfStates; i++)
         {
         transitionLines.add(s.nextLine());
     
         }
     
         System.out.println(transitionLines.toString());
     
     
         for (int i =0; i < splits.length; i++)
         {
         splits[i] = transitionLines.get(i).split(":");
     
         }
     
     
         for (int i =0; i < splits.length; i++)
         {
         for (int j=0; j < splits[i].length; j++)
         {
         System.out.println(splits[i][j]);
     
         }
     
     
         }
     
     
         ArrayList<String[]> fredBowling = new ArrayList<String[]>();
     
         for (int i =0; i < splits.length; i++)
         {
         fredBowling.add(splits[i][1].split("}"));
         }
         splits[0][1].split("}");
     
         ArrayList<Integer> state = new ArrayList<Integer>();
     
         for (int i=0; i < splits.length; i++)
         {
         state.add(Integer.parseInt(splits[i][0]));
     
         }
     
        ArrayList<ArrayList<String>> smaller = new ArrayList<ArrayList<String>>();
     
         for (int i =0; i < fredBowling.size(); i++)
         {
         smaller.add(new ArrayList<String>());
         for (int j = 0; j < fredBowling.get(i).length; j++)
         {
         int temp = fredBowling.get(i)[j].indexOf("{");
     
         smaller.get(i).add(fredBowling.get(i)[j].substring(temp + 1));
         }
     
         }
     
     
         ArrayList<ArrayList<String[]>> finale = new ArrayList<ArrayList<String[]>>();
     
     
     
         for (int i =0; i < smaller.size(); i++)
         {
         finale.add(new ArrayList<String[]>());
         for (int j =0; j < smaller.get(i).size(); j++)
         {
         finale.get(i).add(smaller.get(i).get(j).split(","));
     
         }
     
     
         }
     
         for (int i =0; i < finale.size(); i++)
         {
         for (int j =0; j < finale.get(i).size(); j++)
         {
         for (int k =0; k < finale.get(i).get(j).length; k++)
         {
         System.out.println("MySet[" + i + "]["+j +"].get(" + k + ") = " +finale.get(i).get(j)[k]);
         }
     
     
         }
     
         }
     
          ms = new MySet[finale.size()][finale.get(0).size()];
     
         for (int i =0; i < finale.size(); i++)
         { // begin for
     
         for (int j =0; j < finale.get(i).size(); j++)
         { // begin for 
         try{
     
         ms[i][j] = new MySet();
     
     
     
         for (int k =0; k < finale.get(i).get(j).length; k++)
         { // begin for
         if (finale.get(i).get(j)[k] != null && !finale.get(i).get(j)[k].equals(""))
         { // begin if 
         ms[i][j].add(Integer.parseInt(finale.get(i).get(j)[k]));
     
         } // end if
     
         } // end for
     
         } // end try
           catch(ArrayIndexOutOfBoundsException aioobe)
         {
         System.err.println("Index " + i + "," + j + " is out of bounds.");
         }
     
         } // end for
     
     
         } // end for
     
         for (int i =0; i < ms.length; i++)
         {
         for (int j =0; j < ms[i].length; j++)
         {
         for (int k =0; k < ms[i][j].size(); k++)
         {
         System.out.println("ms[" + i + "]["+j +"].get(" + k + ") = "+ ms[i][j].get(k));
     
         }
         }
     
         }
     
         int startingState = s.nextInt();
         System.out.println("s:" + startingState);
     
         String fs = s.nextLine();
     
         if (fs.equals("") || fs == null)
         fs = s.nextLine();
     
         String[] firstHalf = fs.split("}");
     
     
          int temp = firstHalf[0].indexOf("{");
     
          String secondHalf = firstHalf[0].substring(temp+1);
     
          String[] finalHalf = secondHalf.split(",");
     
     
         ArrayList<Integer> finalState = new ArrayList<Integer>();
     
         for (int i =0; i < finalHalf.length; i++)
    {
    if (finalHalf[i] != null && !finalHalf[i].equals(""))
    {
    finalState.add(Integer.parseInt(finalHalf[i]));
    }
     
    }
     
    System.out.println("Accepting state: " + finalState.toString());
     
     
         getEpsilons2(0);
     
     
         for (int i =0; i < characters - 1; i++)
         {
         ah.add(new HashSet<Integer>());
     
         }
     
         for (int i =0; i < characters -1; i++)
         {
         getClosures(0,i, ah.get(i));
     
         }
     
         System.out.println("To DFA: ");
     
     
     
         System.out.println(epsilons.toString());
     
         int oldSize = ah.size();
     
     
          for (int i =0; i < characters -1; i++)
         {
         System.out.println(ah.get(i).toString());
     
         }
     
     
     
         for (int i =0; i < characters -1; i++)
         {
     
         for (int j =0; j < oldSize; j++)
         {
     
         if (!getNewSet(ah.get(j), i).isEmpty())
         {
         ah.add(getNewSet(ah.get(j), i));
         System.out.println("Added " + i);
         }
     
         }
     
         }
     System.out.println(ah.toString());
     
     
          hshs = new HashSet<HashSet<Integer>>();
     
         for (int i =0; i < ah.size(); i++)
         {
         if (!ah.get(i).isEmpty())
     
         hshs.add(ah.get(i));
     
         }
     
     
     
         Iterator<HashSet<Integer>> theIterator = hshs.iterator();
     
         HashSet<HashSet<Integer>> hshs2 = new HashSet<HashSet<Integer>>();
     
         while (theIterator.hasNext())
         {
         hshs2.add(theIterator.next());
     
     
         }
     
         Iterator<HashSet<Integer>> theIterator2 = hshs2.iterator();
     
         while (theIterator2.hasNext())
         {
     
         for (int i =0; i < characters -1; i++)
         {
         try
         {
         getNewSet2(theIterator2.next(), i);
         }
     
         catch (java.util.NoSuchElementException nsee)
         {
         System.out.println("No such element at " + i);
         }
     
         }
     
         }
     
         hshs.add(epsilons);
     
     System.out.println("To DFA: ");
         System.out.println(hshs.toString());
     
         System.out.println("Went here");
     
         Iterator<HashSet<Integer>> fit = hshs.iterator();
         HashSet<HashSet<Integer>>newFinalStates = new HashSet<HashSet<Integer>>();
     
         while (fit.hasNext())
         {
     
         HashSet<Integer> temp2 = fit.next();
         for (int i =0; i < finalState.size(); i++)
         {
     
         if (temp2.contains(finalState.get(i)))
         newFinalStates.add(temp2);
     
         }
     
         }
     
         System.out.println("Accepting: " +newFinalStates.toString());
     
     
         hshs3 = new HashSet<HashSet<Integer>>();
         ArrayList<ArrayList<HashSet<Integer>>> ahs = new ArrayList<ArrayList<HashSet<Integer>>>();
     
         Iterator<HashSet<Integer>> it3 = hshs.iterator();
     
     
         while (it3.hasNext())
         {
         ArrayList<HashSet<Integer>> tempal = new ArrayList<HashSet<Integer>>();
         HashSet<Integer> tempHash = it3.next();
     
         for (int i =0; i < characters -1; i++)
         {
     
     
        try
        {
     
     
         hshs3.add(getNewSet(tempHash, i));
         tempal.add(getNewSet(tempHash, i));
     
     
     
         }
     
     
         catch(java.util.NoSuchElementException nsee)
         {
         System.out.println("No such element at " + i);
     
         }
     
     
          }
          ahs.add(tempal);
     
     
         }
     
     System.out.println(hshs3.toString());
     System.out.println("ahs:");
     System.out.println(ahs.toString());
     
     
      Iterator<HashSet<Integer>> theIt= hshs.iterator();
     
     int counting = 0;
     while(theIt.hasNext())
     {
     System.out.println(counting + ": " + theIt.next());
     counting++;
     
     }
     
     System.out.print("Sigma: ");
     
     for (int i =0; i < characters -1; i++)
     {
     
     System.out.print(alphabet.get(i) + " ");
     
     }
     System.out.println();
     
     int counting2 = 0;
    for (int i =0; i < ahs.size(); i++)
    {
    System.out.print(counting2 + ": ");
    counting2++;
    for (int j =0; j < ahs.get(0).size(); j++)
    {
    System.out.print(ahs.get(i).get(j).toString() + " ");
     
    }
    System.out.println();
     
    }
     
    System.out.println(hm.toString());
     
    try
        {
       s2  = new Scanner(new File(args[1]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
     FileParser fp = new FileParser(s2);
     
     
     
     
     
     
        }
     
        public static void getNewSet2(HashSet<Integer> oldSet, Integer character)
    { // begin method
     
    HashSet<Integer> newSet = new HashSet<Integer>();
     
     
     
     HashSet<HashSet<Integer>> hshs2 = new HashSet<HashSet<Integer>>();
     Iterator<HashSet<Integer>> theIterator = hshs.iterator();
     
         while (theIterator.hasNext())
         {
         hshs2.add(theIterator.next());
     
     
         }
     
         Iterator<HashSet<Integer>> theIterator2 = hshs2.iterator();
    Iterator<HashSet<Integer>> hit = hshs.iterator();
     
    while (theIterator2.hasNext())
    { // begin while
    HashSet<Integer> hash = getNewSet(theIterator2.next(), character);
    boolean yes  = hshs.add(hash);
    if (yes == true)
    { // begin if
    for (int i =0; i < characters -1; i++)
    { // begin for
    getNewSet2(hash, i);
     
    } // end for
    } // end if
     
    } // end while
     
    } // end method
     
     
        public static void getClosures2(Integer state, Integer character, boolean hit, HashSet<Integer> h)
        {
     
        if (hit == true)
        {
        getEpsilons3(state, h);
     
        }
     
        else
        {
     
     
        }
     
        }
        public static void getClosures(Integer state, Integer character, HashSet<Integer> h)
        {
     
     
        h.add(0);
        if (ms[state][character] != null && ms[state][ms[0].length-1] != null)
        {
     
        for (int i =0; i < ms[state][character].size(); i++)
        {
     
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true,h);
     
        }
     
        for (int j =0; j < ms[state][characters-1].size(); j++)
        {
        getClosures(ms[state][characters -1].get(j), character, h);
        }
     
        }
     
     
        else if (ms[state][character] == null && ms[state][ms[0].length-1]!=null)
        {
        for (int i = 0; i < ms[state][characters-1].size(); i++)
        {
        getClosures(ms[state][characters-1].get(i), character, h);
     
        }
     
        }
     
        else if (ms[state][character]!=null && ms[state][ms[0].length-1] == null)
        {
        for (int i =0; i < ms[state][character].size(); i++)
        {
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true, h);
     
        }
        }
     
        else
        {
        // if there is no closures at all, then it's a good idea to stop
     
        return;
     
        }
     
     
        }
     
         public static HashSet<Integer> getClosures3(Integer state, Integer character)
        {
     
     
        HashSet<Integer> h = new HashSet<Integer>();
        h.add(0);
        if (ms[state][character] != null && ms[state][ms[0].length-1] != null)
        {
     
        for (int i =0; i < ms[state][character].size(); i++)
        {
     
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true,h);
     
        }
     
        for (int j =0; j < ms[state][characters-1].size(); j++)
        {
        h.addAll(getClosures3(ms[state][characters -1].get(j), character));
        }
     
        }
     
     
        else if (ms[state][character] == null && ms[state][ms[0].length-1]!=null)
        {
        for (int i = 0; i < ms[state][characters-1].size(); i++)
        {
        h.addAll(getClosures3(ms[state][characters-1].get(i), character));
     
        }
     
        }
     
        else if (ms[state][character]!=null && ms[state][ms[0].length-1] == null)
        {
        for (int i =0; i < ms[state][character].size(); i++)
        {
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true, h);
     
        }
        }
     
        else
        {
        // if there is no closures at all, then it's a good idea to stop
     
        return h;
     
        }
          return h;
     
        }
     
     
      public static HashSet<Integer> getNewSet(HashSet<Integer> oldSet, Integer character)
        {
     
        HashSet<Integer> newSet = new HashSet<Integer>();
     
        Iterator<Integer> itr = oldSet.iterator();
     
        while (itr.hasNext())
        {
     
     
        getClosures(itr.next(), character, newSet);
     
     
        }
     
        return newSet;
     
        }
     
           public static void getEpsilons3(Integer state, HashSet<Integer> h)
        {
     
         h.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
     
     
       h.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons3(ms[state][ms[0].length-1].get(i), h);
     
        }
     
        }
        else
     
        return;
     
     
        }
        public static void getEpsilons2(Integer state)
        {
     
         epsilons.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
        epsilons.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons2(ms[state][ms[0].length-1].get(i));
     
        }
     
        }
        else
     
        return;
     
     
        }
     
     
    }

    I've used my methods to get the sets I want.

     

    6
    a b
    [a, b, ¸]
    [0: {1} {4} {2}, 1: {5} {2} {}, 2: {5} {} {}, 3: {} {} {0}, 4: {} {1} {3}, 5: {} {} {4}]
    0
    {1} {4} {2}
    1
    {5} {2} {}
    2
    {5} {} {}
    3
    {} {} {0}
    4
    {} {1} {3}
    5
    {} {} {4}
    MySet[0][0].get(0) = 1
    MySet[0][1].get(0) = 4
    MySet[0][2].get(0) = 2
    MySet[1][0].get(0) = 5
    MySet[1][1].get(0) = 2
    MySet[1][2].get(0) =
    MySet[2][0].get(0) = 5
    MySet[2][1].get(0) =
    MySet[2][2].get(0) =
    MySet[3][0].get(0) =
    MySet[3][1].get(0) =
    MySet[3][2].get(0) = 0
    MySet[4][0].get(0) =
    MySet[4][1].get(0) = 1
    MySet[4][2].get(0) = 3
    MySet[5][0].get(0) =
    MySet[5][1].get(0) =
    MySet[5][2].get(0) = 4
    ms[0][0].get(0) = 1
    ms[0][1].get(0) = 4
    ms[0][2].get(0) = 2
    ms[1][0].get(0) = 5
    ms[1][1].get(0) = 2
    ms[2][0].get(0) = 5
    ms[3][2].get(0) = 0
    ms[4][1].get(0) = 1
    ms[4][2].get(0) = 3
    ms[5][2].get(0) = 4
    s:0
    Accepting state: [5]
    To DFA:
    [0, 2]
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    Added 0
    Added 0
    Added 1
    Added 1
    [[0, 1, 2, 3, 4, 5], [0, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
    No such element at 1
    To DFA:
    [[0, 2], [0, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5]]
    Went here
    Accepting: [[0, 1, 2, 3, 4, 5]]
    [[0, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5]]
    ahs:
    [[[0, 1, 2, 3, 4, 5], [0, 2, 3, 4]], [[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4]], [[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4]], [[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4]]]
    0: [0, 2]
    1: [0, 2, 3, 4]
    2: [0, 1, 2, 3, 4]
    3: [0, 1, 2, 3, 4, 5]
    Sigma: a b
    0: [0, 1, 2, 3, 4, 5] [0, 2, 3, 4]
    1: [0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4]
    2: [0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4]
    3: [0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4]
    {¸=2, b=1, a=0}
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    String aabaa is accepted.
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    [0, 2, 3, 4]
    [0, 2, 3, 4]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    String abbbaaababaab is accepted.
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    String aaaaa is accepted.
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    String aaaaaaaaaa is accepted.
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    String aaaaaaaa is accepted.
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    String aaa is accepted.
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    String aaaaaaaaaaaaaaa is accepted.
    [0, 2, 3, 4]
    [0, 2, 3, 4]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    [0, 2, 3, 4]
    [0, 1, 2, 3, 4, 5]
    String bbaabba is accepted.
    The String [empty String] is not valid.
    [0, 2, 3, 4]
    [0, 1, 2, 3, 4, 5]
    String ba is accepted.
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    [0, 2, 3, 4]
    [0, 2, 3, 4]
    [0, 2, 3, 4]
    [0, 2, 3, 4]
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    String ababababbbbbab is accepted.
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    [0, 2, 3, 4]
    [0, 2, 3, 4]
    [0, 2, 3, 4]
    [0, 2, 3, 4]
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    String abbbbbab is accepted.
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    String abcdddab is rejected.
    String ddbca is rejected.
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    String abcbcdabc is rejected.
    String cbabc is rejected.
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    String aaaabcdaa is rejected.
    String eccac is rejected.
    String eccacb is rejected.
    String eccacc is rejected.
    String eccacd is rejected.
    String eccacda is rejected.
    String eccacdb is rejected.


    Last edited by javapenguin; April 30th, 2012 at 06:11 PM.

  17. #17
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    Ok, my getNewSet3 method should be a good starting point. (I'm so out of it.)

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.HashMap;
     
     
    public class fsm
    {
     
    private static class FileParser
    {
     
    private Scanner s;
     
    public FileParser(Scanner s)
    {
    setScanner(s);
     
    while (s.hasNext())
    {
     
    String line = s.nextLine();
    String line2 = line.toString(); // I fear a direct assingment statement as I don't want a shallow copy
     
    if (line == null || line.equals(""))
    System.out.println("The String [empty String] is not valid.");
    else
    {
    Scanner temp = new Scanner(line);
     
    int count =0;
    int size = line.length();
     
     
     
     
    boolean accepting = true;
     
    while (accepting == true && count < size)
    { // begin while
    char c = line.charAt(count);
     
    if (accepted(c) == false)
     
    accepting = false;
     
    count++;
    } // end while
     
    if (accepting == true)
    System.out.println("String " + line + " is accepted.");
    else
    System.out.println("String " + line + " is rejected.");
     
    } // end while
     
     
    }
     
     
    }
     
     
     
    public void setScanner(Scanner s)
    {
    this.s = s;
    }
     
    public static boolean accepted(char c)
    {
     
    if (alphabet.contains(c) == false)
    return false;
     
    HashSet<Integer> ms3 = getNextState3(0, c);
     
    System.out.println("A good start: " + ms3.toString());
     
     
    if (getClosures3(0, hm.get(c)).size() == 1)
    {
     
    System.out.println(getClosures3(0, hm.get(c)).toString());
    return false;
     
    }
    else
    {
    System.out.println(getClosures3(0, hm.get(c)).toString());
    return true;
    }
     
    }
     
    public Scanner getScanner()
    {
    return s;
    }
     
     
    }
     
    private static class MySet
    {
     
    private  ArrayList<Integer> intList;
     
    public MySet()
    {
    intList = new ArrayList<Integer>();
     
    }
     
    public  void add(Integer data)
    {
    intList.add(data);
     
    }
     
    public void remove()
    {
    intList.remove(0);
     
    }
     
    public int size()
    {
    return intList.size();
    }
     
    public Integer get(int index) throws NullPointerException
    {
    return intList.get(index);
     
    }
     
    public ArrayList<Integer> getList()
    {
    return intList;
    }
     
     
     
    }
     
     static MySet[][] ms;
     static int characters;
     static HashSet<HashSet<Integer>> sos;
     static HashSet<Integer> epsilons;
     static HashSet<HashSet<Integer>> hshs;
     static HashSet<HashSet<Integer>> hshs3;
     static ArrayList<HashSet<Integer>> ah;
     static HashMap<Character, Integer> hm;
     static ArrayList<Character> alphabet;
     static ArrayList<ArrayList<HashSet<Integer>>> ahs;
        public static void main(String[] args)
        {
    	Scanner s = null;
    	Scanner s2 = null;
    	ArrayList<Integer> stateList = new ArrayList<Integer>();
    	 alphabet = new ArrayList<Character>();
     
    	ArrayList<String> transitionLines = new ArrayList<String>();
    	ArrayList<String[]> splitArray = new ArrayList<String[]>();
     
    	epsilons = new HashSet<Integer>();
     
    	ah = new ArrayList<HashSet<Integer>>();
     
     
     
     
    	int numOfStates = 0;
    	hm = new HashMap<Character, Integer>();
     
    try
        {
       s  = new Scanner(new File(args[0]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
    numOfStates = s.nextInt();
     
        System.out.println(numOfStates);
     
        String[][] splits = new String[numOfStates][2];
     
     
     
     
        String line = s.nextLine();
        if (line.equals(""))
        line = s.nextLine();
     
     
     
     
        System.out.println(line);
     
        char[] chars = line.toCharArray();
     
     
     
     
        char empty = ' ';
     
     
        String tab = "\t";
        char tab2 = tab.charAt(0);
     
        for (int i =0; i < chars.length; i++)
        {
        if (chars[i]!= empty && chars[i]!=tab2)
        {
        alphabet.add(chars[i]);
        }
        }
     
        alphabet.add('¸');
     
        for (int i =0; i < alphabet.size(); i++)
        {
        hm.put(alphabet.get(i), i);
     
        }
     
         System.out.println(alphabet.toString());
     
         characters = alphabet.size();
     
     
         for (int i=0; i < numOfStates; i++)
         {
         transitionLines.add(s.nextLine());
     
         }
     
         System.out.println(transitionLines.toString());
     
     
         for (int i =0; i < splits.length; i++)
         {
         splits[i] = transitionLines.get(i).split(":");
     
         }
     
     
         for (int i =0; i < splits.length; i++)
         {
         for (int j=0; j < splits[i].length; j++)
         {
         System.out.println(splits[i][j]);
     
         }
     
     
         }
     
     
         ArrayList<String[]> fredBowling = new ArrayList<String[]>();
     
         for (int i =0; i < splits.length; i++)
         {
         fredBowling.add(splits[i][1].split("}"));
         }
         splits[0][1].split("}");
     
         ArrayList<Integer> state = new ArrayList<Integer>();
     
         for (int i=0; i < splits.length; i++)
         {
         state.add(Integer.parseInt(splits[i][0]));
     
         }
     
        ArrayList<ArrayList<String>> smaller = new ArrayList<ArrayList<String>>();
     
         for (int i =0; i < fredBowling.size(); i++)
         {
         smaller.add(new ArrayList<String>());
         for (int j = 0; j < fredBowling.get(i).length; j++)
         {
         int temp = fredBowling.get(i)[j].indexOf("{");
     
         smaller.get(i).add(fredBowling.get(i)[j].substring(temp + 1));
         }
     
         }
     
     
         ArrayList<ArrayList<String[]>> finale = new ArrayList<ArrayList<String[]>>();
     
     
     
         for (int i =0; i < smaller.size(); i++)
         {
         finale.add(new ArrayList<String[]>());
         for (int j =0; j < smaller.get(i).size(); j++)
         {
         finale.get(i).add(smaller.get(i).get(j).split(","));
     
         }
     
     
         }
     
         for (int i =0; i < finale.size(); i++)
         {
         for (int j =0; j < finale.get(i).size(); j++)
         {
         for (int k =0; k < finale.get(i).get(j).length; k++)
         {
         System.out.println("MySet[" + i + "]["+j +"].get(" + k + ") = " +finale.get(i).get(j)[k]);
         }
     
     
         }
     
         }
     
          ms = new MySet[finale.size()][finale.get(0).size()];
     
         for (int i =0; i < finale.size(); i++)
         { // begin for
     
         for (int j =0; j < finale.get(i).size(); j++)
         { // begin for 
         try{
     
         ms[i][j] = new MySet();
     
     
     
         for (int k =0; k < finale.get(i).get(j).length; k++)
         { // begin for
         if (finale.get(i).get(j)[k] != null && !finale.get(i).get(j)[k].equals(""))
         { // begin if 
         ms[i][j].add(Integer.parseInt(finale.get(i).get(j)[k]));
     
         } // end if
     
         } // end for
     
         } // end try
           catch(ArrayIndexOutOfBoundsException aioobe)
         {
         System.err.println("Index " + i + "," + j + " is out of bounds.");
         }
     
         } // end for
     
     
         } // end for
     
         for (int i =0; i < ms.length; i++)
         {
         for (int j =0; j < ms[i].length; j++)
         {
         for (int k =0; k < ms[i][j].size(); k++)
         {
         System.out.println("ms[" + i + "]["+j +"].get(" + k + ") = "+ ms[i][j].get(k));
     
         }
         }
     
         }
     
         int startingState = s.nextInt();
         System.out.println("s:" + startingState);
     
         String fs = s.nextLine();
     
         if (fs.equals("") || fs == null)
         fs = s.nextLine();
     
         String[] firstHalf = fs.split("}");
     
     
          int temp = firstHalf[0].indexOf("{");
     
          String secondHalf = firstHalf[0].substring(temp+1);
     
          String[] finalHalf = secondHalf.split(",");
     
     
         ArrayList<Integer> finalState = new ArrayList<Integer>();
     
         for (int i =0; i < finalHalf.length; i++)
    {
    if (finalHalf[i] != null && !finalHalf[i].equals(""))
    {
    finalState.add(Integer.parseInt(finalHalf[i]));
    }
     
    }
     
    System.out.println("Accepting state: " + finalState.toString());
     
     
         getEpsilons2(0);
     
     
         for (int i =0; i < characters - 1; i++)
         {
         ah.add(new HashSet<Integer>());
     
         }
     
         for (int i =0; i < characters -1; i++)
         {
         getClosures(0,i, ah.get(i));
     
         }
     
         System.out.println("To DFA: ");
     
     
     
         System.out.println(epsilons.toString());
     
         int oldSize = ah.size();
     
     
          for (int i =0; i < characters -1; i++)
         {
         System.out.println(ah.get(i).toString());
     
         }
     
     
     
         for (int i =0; i < characters -1; i++)
         {
     
         for (int j =0; j < oldSize; j++)
         {
     
         if (!getNewSet(ah.get(j), i).isEmpty())
         {
         ah.add(getNewSet(ah.get(j), i));
         System.out.println("Added " + i);
         }
     
         }
     
         }
     System.out.println(ah.toString());
     
     
          hshs = new HashSet<HashSet<Integer>>();
     
         for (int i =0; i < ah.size(); i++)
         {
         if (!ah.get(i).isEmpty())
     
         hshs.add(ah.get(i));
     
         }
     
     
     
         Iterator<HashSet<Integer>> theIterator = hshs.iterator();
     
         HashSet<HashSet<Integer>> hshs2 = new HashSet<HashSet<Integer>>();
     
         while (theIterator.hasNext())
         {
         hshs2.add(theIterator.next());
     
     
         }
     
         Iterator<HashSet<Integer>> theIterator2 = hshs2.iterator();
     
         while (theIterator2.hasNext())
         {
     
         for (int i =0; i < characters -1; i++)
         {
         try
         {
         getNewSet2(theIterator2.next(), i);
         }
     
         catch (java.util.NoSuchElementException nsee)
         {
         System.out.println("No such element at " + i);
         }
     
         }
     
         }
     
         hshs.add(epsilons);
     
     System.out.println("To DFA: ");
         System.out.println(hshs.toString());
     
         System.out.println("Went here");
     
         Iterator<HashSet<Integer>> fit = hshs.iterator();
         HashSet<HashSet<Integer>>newFinalStates = new HashSet<HashSet<Integer>>();
     
         while (fit.hasNext())
         {
     
         HashSet<Integer> temp2 = fit.next();
         for (int i =0; i < finalState.size(); i++)
         {
     
         if (temp2.contains(finalState.get(i)))
         newFinalStates.add(temp2);
     
         }
     
         }
     
         System.out.println("Accepting: " +newFinalStates.toString());
     
     
         hshs3 = new HashSet<HashSet<Integer>>();
         ahs = new ArrayList<ArrayList<HashSet<Integer>>>();
     
         Iterator<HashSet<Integer>> it3 = hshs.iterator();
     
     
         while (it3.hasNext())
         {
         ArrayList<HashSet<Integer>> tempal = new ArrayList<HashSet<Integer>>();
         HashSet<Integer> tempHash = it3.next();
     
         for (int i =0; i < characters -1; i++)
         {
     
     
        try
        {
     
     
         hshs3.add(getNewSet(tempHash, i));
         tempal.add(getNewSet(tempHash, i));
     
     
     
         }
     
     
         catch(java.util.NoSuchElementException nsee)
         {
         System.out.println("No such element at " + i);
     
         }
     
     
          }
          ahs.add(tempal);
     
     
         }
     
     System.out.println(hshs3.toString());
     System.out.println("ahs:");
     System.out.println(ahs.toString());
     
     
      Iterator<HashSet<Integer>> theIt= hshs.iterator();
     
     int counting = 0;
     while(theIt.hasNext())
     {
     System.out.println(counting + ": " + theIt.next());
     counting++;
     
     }
     
     System.out.print("Sigma: ");
     
     for (int i =0; i < characters -1; i++)
     {
     
     System.out.print(alphabet.get(i) + " ");
     
     }
     System.out.println();
     
     int counting2 = 0;
    for (int i =0; i < ahs.size(); i++)
    {
    System.out.print(counting2 + ": ");
    counting2++;
    for (int j =0; j < ahs.get(0).size(); j++)
    {
    System.out.print(ahs.get(i).get(j).toString() + " ");
     
    }
    System.out.println();
     
    }
     
    System.out.println(hm.toString());
     
     Integer[][] combiner = new Integer[ahs.size()][ahs.get(0).size()];
     
     Iterator<HashSet<Integer>> theIterator3 = newFinalStates.iterator();
     HashSet<Integer> finale2 = new HashSet<Integer>();
     
     while (theIterator3.hasNext())
     {
     finale2 = theIterator3.next();
     }
     for (int i =0; i < ahs.size(); i++)
     {
     for (int j =0; j <ahs.get(i).size(); j++)
     {
     if (ahs.get(i).get(j).equals(finale2))
     {
     combiner[i][j] = 1;
     }
     else
     combiner[i][j] = 0;
     
     }
     
     }
     
     for (int i =0; i < combiner.length; i++)
     {
     System.out.println(java.util.Arrays.toString(combiner[i]));
     
     }
    try
        {
       s2  = new Scanner(new File(args[1]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
     FileParser fp = new FileParser(s2);
     
     
     
     
     
     
        }
     
        public static void getNewSet2(HashSet<Integer> oldSet, Integer character)
    { // begin method
     
    HashSet<Integer> newSet = new HashSet<Integer>();
     
     
     
     HashSet<HashSet<Integer>> hshs2 = new HashSet<HashSet<Integer>>();
     Iterator<HashSet<Integer>> theIterator = hshs.iterator();
     
         while (theIterator.hasNext())
         {
         hshs2.add(theIterator.next());
     
     
         }
     
         Iterator<HashSet<Integer>> theIterator2 = hshs2.iterator();
    Iterator<HashSet<Integer>> hit = hshs.iterator();
     
    while (theIterator2.hasNext())
    { // begin while
    HashSet<Integer> hash = getNewSet(theIterator2.next(), character);
    boolean yes  = hshs.add(hash);
    if (yes == true)
    { // begin if
    for (int i =0; i < characters -1; i++)
    { // begin for
    getNewSet2(hash, i);
     
    } // end for
    } // end if
     
    } // end while
     
    } // end method
     
     
        public static void getClosures2(Integer state, Integer character, boolean hit, HashSet<Integer> h)
        {
     
        if (hit == true)
        {
        getEpsilons3(state, h);
     
        }
     
        else
        {
     
     
        }
     
        }
        public static void getClosures(Integer state, Integer character, HashSet<Integer> h)
        {
     
     
        h.add(0);
        if (ms[state][character] != null && ms[state][ms[0].length-1] != null)
        {
     
        for (int i =0; i < ms[state][character].size(); i++)
        {
     
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true,h);
     
        }
     
        for (int j =0; j < ms[state][characters-1].size(); j++)
        {
        getClosures(ms[state][characters -1].get(j), character, h);
        }
     
        }
     
     
        else if (ms[state][character] == null && ms[state][ms[0].length-1]!=null)
        {
        for (int i = 0; i < ms[state][characters-1].size(); i++)
        {
        getClosures(ms[state][characters-1].get(i), character, h);
     
        }
     
        }
     
        else if (ms[state][character]!=null && ms[state][ms[0].length-1] == null)
        {
        for (int i =0; i < ms[state][character].size(); i++)
        {
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true, h);
     
        }
        }
     
        else
        {
        // if there is no closures at all, then it's a good idea to stop
     
        return;
     
        }
     
     
        }
     
         public static HashSet<Integer> getClosures3(Integer state, Integer character)
        {
     
     
        HashSet<Integer> h = new HashSet<Integer>();
        h.add(0);
        if (ms[state][character] != null && ms[state][ms[0].length-1] != null)
        {
     
        for (int i =0; i < ms[state][character].size(); i++)
        {
     
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true,h);
     
        }
     
        for (int j =0; j < ms[state][characters-1].size(); j++)
        {
        h.addAll(getClosures3(ms[state][characters -1].get(j), character));
        }
     
        }
     
     
        else if (ms[state][character] == null && ms[state][ms[0].length-1]!=null)
        {
        for (int i = 0; i < ms[state][characters-1].size(); i++)
        {
        h.addAll(getClosures3(ms[state][characters-1].get(i), character));
     
        }
     
        }
     
        else if (ms[state][character]!=null && ms[state][ms[0].length-1] == null)
        {
        for (int i =0; i < ms[state][character].size(); i++)
        {
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true, h);
     
        }
        }
     
        else
        {
        // if there is no closures at all, then it's a good idea to stop
     
        return h;
     
        }
          return h;
     
        }
     
     
      public static HashSet<Integer> getNewSet(HashSet<Integer> oldSet, Integer character)
        {
     
        HashSet<Integer> newSet = new HashSet<Integer>();
     
        Iterator<Integer> itr = oldSet.iterator();
     
        while (itr.hasNext())
        {
     
     
        getClosures(itr.next(), character, newSet);
     
     
        }
     
        return newSet;
     
        }
     
           public static void getEpsilons3(Integer state, HashSet<Integer> h)
        {
     
         h.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
     
     
       h.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons3(ms[state][ms[0].length-1].get(i), h);
     
        }
     
        }
        else
     
        return;
     
     
        }
        public static void getEpsilons2(Integer state)
        {
     
         epsilons.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
        epsilons.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons2(ms[state][ms[0].length-1].get(i));
     
        }
     
        }
        else
     
        return;
     
     
        }
     
        public static HashSet<Integer> getNextState3(Integer state, char c)
        {
     
        return ahs.get(state).get(hm.get(c));
     
        }
     
     
     
     
    }

    I had an idea just now. I was going to shrink the number of "states" now in a way so that the states in the new DFM are numbered as the state.

    I have the mapping showing which one corresponds to which new one:

    I want to make that a "state" of sorts and follow it to it's new one, it should show on the thing. There is a map that will show it, though it might be hard to find in the output.

    Right now, it's starting to do the minimzing part before it reads in the String, I'll move that later, but I want the read in to work first then I'll go for minimizing.

     

    6
    a b
    [a, b, ¸]
    [0: {1} {4} {2}, 1: {5} {2} {}, 2: {5} {} {}, 3: {} {} {0}, 4: {} {1} {3}, 5: {} {} {4}]
    0
    {1} {4} {2}
    1
    {5} {2} {}
    2
    {5} {} {}
    3
    {} {} {0}
    4
    {} {1} {3}
    5
    {} {} {4}
    MySet[0][0].get(0) = 1
    MySet[0][1].get(0) = 4
    MySet[0][2].get(0) = 2
    MySet[1][0].get(0) = 5
    MySet[1][1].get(0) = 2
    MySet[1][2].get(0) =
    MySet[2][0].get(0) = 5
    MySet[2][1].get(0) =
    MySet[2][2].get(0) =
    MySet[3][0].get(0) =
    MySet[3][1].get(0) =
    MySet[3][2].get(0) = 0
    MySet[4][0].get(0) =
    MySet[4][1].get(0) = 1
    MySet[4][2].get(0) = 3
    MySet[5][0].get(0) =
    MySet[5][1].get(0) =
    MySet[5][2].get(0) = 4
    ms[0][0].get(0) = 1
    ms[0][1].get(0) = 4
    ms[0][2].get(0) = 2
    ms[1][0].get(0) = 5
    ms[1][1].get(0) = 2
    ms[2][0].get(0) = 5
    ms[3][2].get(0) = 0
    ms[4][1].get(0) = 1
    ms[4][2].get(0) = 3
    ms[5][2].get(0) = 4
    s:0
    Accepting state: [5]
    To DFA:
    [0, 2]
    [0, 1, 2, 3, 4, 5]
    [0, 2, 3, 4]
    Added 0
    Added 0
    Added 1
    Added 1
    [[0, 1, 2, 3, 4, 5], [0, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
    No such element at 1
    To DFA:
    [[0, 2], [0, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5]]
    Went here
    Accepting: [[0, 1, 2, 3, 4, 5]]
    [[0, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5]]
    ahs:
    [[[0, 1, 2, 3, 4, 5], [0, 2, 3, 4]], [[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4]], [[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4]], [[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4]]]
    0: [0, 2]
    1: [0, 2, 3, 4]
    2: [0, 1, 2, 3, 4]
    3: [0, 1, 2, 3, 4, 5]
    Sigma: a b
    0: [0, 1, 2, 3, 4, 5] [0, 2, 3, 4]
    1: [0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4]
    2: [0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4]
    3: [0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4]
    {¸=2, b=1, a=0}
    [1, 0]
    [1, 0]
    [1, 0]
    [1, 0]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    String aabaa is accepted.
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    String abbbaaababaab is accepted.
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    String aaaaa is accepted.
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    String aaaaaaaaaa is accepted.
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    String aaaaaaaa is accepted.
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    String aaa is accepted.
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    String aaaaaaaaaaaaaaa is accepted.
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    String bbaabba is accepted.
    The String [empty String] is not valid.
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    String ba is accepted.
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    String ababababbbbbab is accepted.
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    String abbbbbab is accepted.
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    String abcdddab is rejected.
    String ddbca is rejected.
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    String abcbcdabc is rejected.
    String cbabc is rejected.
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 1, 2, 3, 4, 5]
    [0, 1, 2, 3, 4, 5]
    A good start: [0, 2, 3, 4]
    [0, 2, 3, 4]
    String aaaabcdaa is rejected.
    String eccac is rejected.
    String eccacb is rejected.
    String eccacc is rejected.
    String eccacd is rejected.
    String eccacda is rejected.
    String eccacdb is rejected.




    I believe the lines that show the state maps, or whatever, are:

    0: [0, 2]
    1: [0, 2, 3, 4]
    2: [0, 1, 2, 3, 4]
    3: [0, 1, 2, 3, 4, 5]

    And this one shows the layout:

    Sigma: a b
    0: [0, 1, 2, 3, 4, 5] [0, 2, 3, 4]
    1: [0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4]
    2: [0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4]
    3: [0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4]


    It's the same as:

    Sigma a b
    0: 3 1
    1: 3 2
    2: 3 2
    3: 3 2


    Any help would be nice. Also later I'm probably going to need help with the minimuzation, but first I'm going to ask the instructor and/or someone else in the class to help clarify that first.

    Please help!

    ^

  18. #18
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Having trouble figuring out how to recursively do this.

    Ok, right now in the DFA, I'm trying to map the state number to its set of HashSets, though I'll probably use an ArrayList as there could be duplicates.

    I'm getting very close to getting done, or as done as I can get, as some parts of it simply are rejecting every String I read it in for like half of my input files.

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.HashMap;
     
     
    public class fsm
    {
     
    private static class FileParser
    {
     
    private Scanner s;
    private static Integer state2;
     
    private static  HashSet<Integer> theHash;
    public FileParser(Scanner s)
    {
    setScanner(s);
     
    theHash = new HashSet<Integer>();
    Iterator<HashSet<Integer>> theIt = newFinalStates.iterator();
     
    while (theIt.hasNext())
    {
    theHash = theIt.next();
    }
     
     
    while (s.hasNext())
    {
     
    String line = s.nextLine();
    String line2 = line.toString(); // I fear a direct assingment statement as I don't want a shallow copy
     
    if (line == null || line.equals(""))
    System.out.println("The String [empty String] is not valid.");
    else
    {
    Scanner temp = new Scanner(line);
     
    int count =0;
    int size = line.length();
     
     
     
     
    boolean accepting = true;
     
    while ( count < size)
    { // begin while
    char c = line.charAt(count);
     
    if (alphabet.contains(c) == false)
    {
    accepting = false;
    break;
     
    }
     
    if (count == 0)
    {
     
    if (accepted(0, c) == false)
     
    accepting = false;
     
    else 
    accepting = true;
    }
     
    else
    {
    if (accepted(getNextState(), c) == false)
     
    accepting = false;
     
    else 
    accepting = true;
     
     
    }
     
     
    System.out.println("Next state: " + getNextState());
    count++;
    } // end while
     
    if (accepting == true)
    System.out.println("String " + line + " is accepted.");
    else
    System.out.println("String " + line + " is rejected.");
     
    } // end while
     
     
    }
     
     
    }
     
     
     
    public void setScanner(Scanner s)
    {
    this.s = s;
    }
     
    public static boolean accepted(Integer state, char c)
    {
     
    if (alphabet.contains(c) == false)
    return false;
     
    Integer theInt = getNextState3(state, c);
     
    setNextState(theInt);
     
    if (theInt == newMap2.get(theHash))
    return true;
    return false;
     
    // System.out.println("A good start: " + ms3.toString());
     
     
     
     
    }
     
    public Scanner getScanner()
    {
    return s;
    }
     
    public static void setNextState(Integer state3)
    {
    state2 = state3;
     
    }
     
    public static Integer getNextState()
    {
     
    return state2;
    }
     
     
    }
     
    private static class MySet
    {
     
    private  ArrayList<Integer> intList;
     
    public MySet()
    {
    intList = new ArrayList<Integer>();
     
    }
     
    public  void add(Integer data)
    {
    intList.add(data);
     
    }
     
    public void remove()
    {
    intList.remove(0);
     
    }
     
    public int size()
    {
    return intList.size();
    }
     
    public Integer get(int index) throws NullPointerException
    {
    return intList.get(index);
     
    }
     
    public ArrayList<Integer> getList()
    {
    return intList;
    }
     
     
     
    }
     
     static MySet[][] ms;
     static int characters;
     static HashSet<HashSet<Integer>> sos;
     static HashSet<Integer> epsilons;
     static HashSet<HashSet<Integer>> hshs;
     static HashSet<HashSet<Integer>> hshs3;
     static ArrayList<HashSet<Integer>> ah;
     static HashMap<Character, Integer> hm;
     static ArrayList<Character> alphabet;
     static ArrayList<ArrayList<HashSet<Integer>>> ahs;
     static HashMap<Integer, HashSet<Integer>> newMap;
     static HashMap<HashSet<Integer>, Integer> newMap2;
     static HashSet<HashSet<Integer>>newFinalStates;
     static Integer state3;
     static ArrayList<HashSet<Integer>> accepting;
     static ArrayList<HashSet<Integer>> nonaccepting;
     static Integer state5;
        public static void main(String[] args)
        {
    	Scanner s = null;
    	Scanner s2 = null;
    	ArrayList<Integer> stateList = new ArrayList<Integer>();
    	 alphabet = new ArrayList<Character>();
    	 newMap2 = new HashMap<HashSet<Integer>, Integer>();
     
    	ArrayList<String> transitionLines = new ArrayList<String>();
    	ArrayList<String[]> splitArray = new ArrayList<String[]>();
     
    	epsilons = new HashSet<Integer>();
     
    	ah = new ArrayList<HashSet<Integer>>();
     
     
     
     
    	int numOfStates = 0;
    	hm = new HashMap<Character, Integer>();
     
    try
        {
       s  = new Scanner(new File(args[0]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
    numOfStates = s.nextInt();
     
        System.out.println(numOfStates);
     
        String[][] splits = new String[numOfStates][2];
     
     
     
     
        String line = s.nextLine();
        if (line.equals(""))
        line = s.nextLine();
     
     
     
     
        System.out.println(line);
     
        char[] chars = line.toCharArray();
     
     
     
     
        char empty = ' ';
     
     
        String tab = "\t";
        char tab2 = tab.charAt(0);
     
        for (int i =0; i < chars.length; i++)
        {
        if (chars[i]!= empty && chars[i]!=tab2)
        {
        alphabet.add(chars[i]);
        }
        }
     
        alphabet.add('¸');
     
        for (int i =0; i < alphabet.size(); i++)
        {
        hm.put(alphabet.get(i), i);
     
        }
     
         System.out.println(alphabet.toString());
     
         characters = alphabet.size();
     
     
         for (int i=0; i < numOfStates; i++)
         {
         transitionLines.add(s.nextLine());
     
         }
     
         System.out.println(transitionLines.toString());
     
     
         for (int i =0; i < splits.length; i++)
         {
         splits[i] = transitionLines.get(i).split(":");
     
         }
     
     
         for (int i =0; i < splits.length; i++)
         {
         for (int j=0; j < splits[i].length; j++)
         {
         System.out.println(splits[i][j]);
     
         }
     
     
         }
     
     
         ArrayList<String[]> fredBowling = new ArrayList<String[]>();
     
         for (int i =0; i < splits.length; i++)
         {
         fredBowling.add(splits[i][1].split("}"));
         }
         splits[0][1].split("}");
     
         ArrayList<Integer> state = new ArrayList<Integer>();
     
         for (int i=0; i < splits.length; i++)
         {
         state.add(Integer.parseInt(splits[i][0]));
     
         }
     
        ArrayList<ArrayList<String>> smaller = new ArrayList<ArrayList<String>>();
     
         for (int i =0; i < fredBowling.size(); i++)
         {
         smaller.add(new ArrayList<String>());
         for (int j = 0; j < fredBowling.get(i).length; j++)
         {
         int temp = fredBowling.get(i)[j].indexOf("{");
     
         smaller.get(i).add(fredBowling.get(i)[j].substring(temp + 1));
         }
     
         }
     
     
         ArrayList<ArrayList<String[]>> finale = new ArrayList<ArrayList<String[]>>();
     
     
     
         for (int i =0; i < smaller.size(); i++)
         {
         finale.add(new ArrayList<String[]>());
         for (int j =0; j < smaller.get(i).size(); j++)
         {
         finale.get(i).add(smaller.get(i).get(j).split(","));
     
         }
     
     
         }
     
         for (int i =0; i < finale.size(); i++)
         {
         for (int j =0; j < finale.get(i).size(); j++)
         {
         for (int k =0; k < finale.get(i).get(j).length; k++)
         {
         System.out.println("MySet[" + i + "]["+j +"].get(" + k + ") = " +finale.get(i).get(j)[k]);
         }
     
     
         }
     
         }
     
          ms = new MySet[finale.size()][finale.get(0).size()];
     
         for (int i =0; i < finale.size(); i++)
         { // begin for
     
         for (int j =0; j < finale.get(i).size(); j++)
         { // begin for 
         try{
     
         ms[i][j] = new MySet();
     
     
     
         for (int k =0; k < finale.get(i).get(j).length; k++)
         { // begin for
         if (finale.get(i).get(j)[k] != null && !finale.get(i).get(j)[k].equals(""))
         { // begin if 
         ms[i][j].add(Integer.parseInt(finale.get(i).get(j)[k]));
     
         } // end if
     
         } // end for
     
         } // end try
           catch(ArrayIndexOutOfBoundsException aioobe)
         {
         System.err.println("Index " + i + "," + j + " is out of bounds.");
         }
     
         } // end for
     
     
         } // end for
     
         for (int i =0; i < ms.length; i++)
         {
         for (int j =0; j < ms[i].length; j++)
         {
         for (int k =0; k < ms[i][j].size(); k++)
         {
         System.out.println("ms[" + i + "]["+j +"].get(" + k + ") = "+ ms[i][j].get(k));
     
         }
         }
     
         }
     
         int startingState = s.nextInt();
         System.out.println("s:" + startingState);
     
         String fs = s.nextLine();
     
         if (fs.equals("") || fs == null)
         fs = s.nextLine();
     
         String[] firstHalf = fs.split("}");
     
     
          int temp = firstHalf[0].indexOf("{");
     
          String secondHalf = firstHalf[0].substring(temp+1);
     
          String[] finalHalf = secondHalf.split(",");
     
     
         ArrayList<Integer> finalState = new ArrayList<Integer>();
     
         for (int i =0; i < finalHalf.length; i++)
    {
    if (finalHalf[i] != null && !finalHalf[i].equals(""))
    {
    finalState.add(Integer.parseInt(finalHalf[i]));
    }
     
    }
     
    System.out.println("Accepting state: " + finalState.toString());
     
     
         getEpsilons2(0);
     
     
         for (int i =0; i < characters - 1; i++)
         {
         ah.add(new HashSet<Integer>());
     
         }
     
         for (int i =0; i < characters -1; i++)
         {
         getClosures(0,i, ah.get(i));
     
         }
     
         System.out.println("To DFA: ");
     
     
     
         System.out.println(epsilons.toString());
     
         int oldSize = ah.size();
     
     
          for (int i =0; i < characters -1; i++)
         {
         System.out.println(ah.get(i).toString());
     
         }
     
     
     
         for (int i =0; i < characters -1; i++)
         {
     
         for (int j =0; j < oldSize; j++)
         {
     
         if (!getNewSet(ah.get(j), i).isEmpty())
         {
         ah.add(getNewSet(ah.get(j), i));
         System.out.println("Added " + i);
         }
     
         }
     
         }
     System.out.println(ah.toString());
     
     newMap = new HashMap<Integer, HashSet<Integer>>();
     
          hshs = new HashSet<HashSet<Integer>>();
     
         for (int i =0; i < ah.size(); i++)
         {
         if (!ah.get(i).isEmpty())
     
         hshs.add(ah.get(i));
     
         }
     
     
     
         Iterator<HashSet<Integer>> theIterator = hshs.iterator();
     
         HashSet<HashSet<Integer>> hshs2 = new HashSet<HashSet<Integer>>();
     
         while (theIterator.hasNext())
         {
         hshs2.add(theIterator.next());
     
     
         }
     
         Iterator<HashSet<Integer>> theIterator2 = hshs2.iterator();
     
         while (theIterator2.hasNext())
         {
     
         for (int i =0; i < characters -1; i++)
         {
         try
         {
         getNewSet2(theIterator2.next(), i);
         }
     
         catch (java.util.NoSuchElementException nsee)
         {
         System.out.println("No such element at " + i);
         }
     
         }
     
         }
     
         hshs.add(epsilons);
     
     System.out.println("To DFA: ");
         System.out.println(hshs.toString());
     
         System.out.println("Went here");
     
         Iterator<HashSet<Integer>> fit = hshs.iterator();
         newFinalStates = new HashSet<HashSet<Integer>>();
     
         accepting = new ArrayList<HashSet<Integer>>();
         nonaccepting = new ArrayList<HashSet<Integer>>();
     
         while (fit.hasNext())
         {
     
         HashSet<Integer> temp2 = fit.next();
         for (int i =0; i < finalState.size(); i++)
         {
     
         if (temp2.contains(finalState.get(i)))
         newFinalStates.add(temp2);
     
         }
     
         }
     
         System.out.println("Accepting: " +newFinalStates.toString());
     
     
         hshs3 = new HashSet<HashSet<Integer>>();
         ahs = new ArrayList<ArrayList<HashSet<Integer>>>();
     
         Iterator<HashSet<Integer>> it3 = hshs.iterator();
     
     
         while (it3.hasNext())
         {
         ArrayList<HashSet<Integer>> tempal = new ArrayList<HashSet<Integer>>();
         HashSet<Integer> tempHash = it3.next();
     
         for (int i =0; i < characters -1; i++)
         {
     
     
        try
        {
     
     
         hshs3.add(getNewSet(tempHash, i));
         tempal.add(getNewSet(tempHash, i));
     
     
     
         }
     
     
         catch(java.util.NoSuchElementException nsee)
         {
         System.out.println("No such element at " + i);
     
         }
     
     
          }
          ahs.add(tempal);
     
     
         }
     
         Iterator<HashSet<Integer>> yetAnotherIterator = hshs.iterator();
    int moreCounting = 0;
     
    while (yetAnotherIterator.hasNext())
    {
    newMap2.put(yetAnotherIterator.next(), moreCounting);
    moreCounting++;
     
    }
     
    System.out.println(newMap2.toString());
     
     System.out.println(hshs3.toString());
     System.out.println("ahs:");
     System.out.println(ahs.toString());
     
     
     Iterator<HashSet<Integer>> moreIt = hshs.iterator();
     
     int stuffs = 0;
     while (moreIt.hasNext())
     {
     
     newMap.put(stuffs, moreIt.next());
     stuffs++;
     
     
     }
      Iterator<HashSet<Integer>> theIt= hshs.iterator();
     
     int counting = 0;
     while(theIt.hasNext())
     {
     System.out.println(counting + ": " + theIt.next());
     counting++;
     
     }
     
     System.out.print("Sigma: ");
     
     for (int i =0; i < characters -1; i++)
     {
     
     System.out.print(alphabet.get(i) + " ");
     
     }
     System.out.println();
     
     int counting2 = 0;
    for (int i =0; i < ahs.size(); i++)
    {
    System.out.print(counting2 + ": ");
    counting2++;
    for (int j =0; j < ahs.get(0).size(); j++)
    {
    System.out.print(newMap2.get(ahs.get(i).get(j))+ " ");
     
    }
    System.out.println();
     
    }
     
    for (int i =0; i < newMap2.size(); i++)
    {
     
     
     
    }
     
     
    System.out.println(hm.toString());
     
     Integer[][] combiner = new Integer[ahs.size()][ahs.get(0).size()];
     
     Iterator<HashSet<Integer>> theIterator3 = newFinalStates.iterator();
     HashSet<Integer> finale2 = new HashSet<Integer>();
     
     while (theIterator3.hasNext())
     {
     finale2 = theIterator3.next();
     }
     for (int i =0; i < ahs.size(); i++)
     {
     for (int j =0; j <ahs.get(i).size(); j++)
     {
     if (ahs.get(i).get(j).equals(finale2))
     {
     combiner[i][j] = 1;
     accepting.add(ahs.get(i).get(j));
     }
     else
     {
     combiner[i][j] = 0;
     nonaccepting.add(ahs.get(i).get(j));
     }
     
     }
     
     }
     
     for (int i =0; i < combiner.length; i++)
     {
     System.out.println(java.util.Arrays.toString(combiner[i]));
     
     }
     
     System.out.println(newMap.toString());
     
     
     System.out.println("Accepting: " + accepting.toString());
     System.out.println("Nonaccepting: " + nonaccepting.toString());
    try
        {
       s2  = new Scanner(new File(args[1]));
        }
     
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
        System.out.println("Error!");
        System.exit(1);
     
        }
     
    catch (FileNotFoundException fnfe)
        {
    	System.out.println("Error!");
    	System.exit(1);
     
        }
     FileParser fp = new FileParser(s2);
     
     
     
     
     
     
        }
     
        public static void getNewSet2(HashSet<Integer> oldSet, Integer character)
    { // begin method
     
    HashSet<Integer> newSet = new HashSet<Integer>();
     
     
     
     HashSet<HashSet<Integer>> hshs2 = new HashSet<HashSet<Integer>>();
     Iterator<HashSet<Integer>> theIterator = hshs.iterator();
     
         while (theIterator.hasNext())
         {
         hshs2.add(theIterator.next());
     
     
         }
     
         Iterator<HashSet<Integer>> theIterator2 = hshs2.iterator();
    Iterator<HashSet<Integer>> hit = hshs.iterator();
     
    while (theIterator2.hasNext())
    { // begin while
    HashSet<Integer> hash = getNewSet(theIterator2.next(), character);
    boolean yes  = hshs.add(hash);
    if (yes == true)
    { // begin if
    for (int i =0; i < characters -1; i++)
    { // begin for
    getNewSet2(hash, i);
     
    } // end for
    } // end if
     
    } // end while
     
    } // end method
     
     
        public static void getClosures2(Integer state, Integer character, boolean hit, HashSet<Integer> h)
        {
     
        if (hit == true)
        {
        getEpsilons3(state, h);
     
        }
     
        else
        {
     
     
        }
     
        }
        public static void getClosures(Integer state, Integer character, HashSet<Integer> h)
        {
     
     
        h.add(0);
        if (ms[state][character] != null && ms[state][ms[0].length-1] != null)
        {
     
        for (int i =0; i < ms[state][character].size(); i++)
        {
     
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true,h);
     
        }
     
        for (int j =0; j < ms[state][characters-1].size(); j++)
        {
        getClosures(ms[state][characters -1].get(j), character, h);
        }
     
        }
     
     
        else if (ms[state][character] == null && ms[state][ms[0].length-1]!=null)
        {
        for (int i = 0; i < ms[state][characters-1].size(); i++)
        {
        getClosures(ms[state][characters-1].get(i), character, h);
     
        }
     
        }
     
        else if (ms[state][character]!=null && ms[state][ms[0].length-1] == null)
        {
        for (int i =0; i < ms[state][character].size(); i++)
        {
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true, h);
     
        }
        }
     
        else
        {
        // if there is no closures at all, then it's a good idea to stop
     
        return;
     
        }
     
     
        }
     
         public static HashSet<Integer> getClosures3(Integer state, Integer character)
        {
     
     
        HashSet<Integer> h = new HashSet<Integer>();
        h.add(0);
        if (ms[state][character] != null && ms[state][ms[0].length-1] != null)
        {
     
        for (int i =0; i < ms[state][character].size(); i++)
        {
     
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true,h);
     
        }
     
        for (int j =0; j < ms[state][characters-1].size(); j++)
        {
        h.addAll(getClosures3(ms[state][characters -1].get(j), character));
        }
     
        }
     
     
        else if (ms[state][character] == null && ms[state][ms[0].length-1]!=null)
        {
        for (int i = 0; i < ms[state][characters-1].size(); i++)
        {
        h.addAll(getClosures3(ms[state][characters-1].get(i), character));
     
        }
     
        }
     
        else if (ms[state][character]!=null && ms[state][ms[0].length-1] == null)
        {
        for (int i =0; i < ms[state][character].size(); i++)
        {
        h.add(ms[state][character].get(i));
        getClosures2(ms[state][character].get(i), character, true, h);
     
        }
        }
     
        else
        {
        // if there is no closures at all, then it's a good idea to stop
     
        return h;
     
        }
          return h;
     
        }
     
     
      public static HashSet<Integer> getNewSet(HashSet<Integer> oldSet, Integer character)
        {
     
        HashSet<Integer> newSet = new HashSet<Integer>();
     
        Iterator<Integer> itr = oldSet.iterator();
     
        while (itr.hasNext())
        {
     
     
        getClosures(itr.next(), character, newSet);
     
     
        }
     
        return newSet;
     
        }
     
           public static void getEpsilons3(Integer state, HashSet<Integer> h)
        {
     
         h.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
     
     
       h.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons3(ms[state][ms[0].length-1].get(i), h);
     
        }
     
        }
        else
     
        return;
     
     
        }
        public static void getEpsilons2(Integer state)
        {
     
         epsilons.add(0);
     
        if (ms[state][ms[0].length-1] != null)
        {
        for (int i =0; i < ms[state][ms[0].length-1].size(); i++)
        {
        epsilons.add(ms[state][ms[0].length-1].get(i));
     
       getEpsilons2(ms[state][ms[0].length-1].get(i));
     
        }
     
        }
        else
     
        return;
     
     
        }
     
        public static Integer getNextState3(Integer state, char c)
        {
     
        return newMap2.get(ahs.get(state).get(hm.get(c)));
     
        }
     
        public static void setNextState4(Integer state4)
        {
        state3 = state4;
     
        }
     
        public static Integer getNextState4()
        {
        return state3;
        }
     
        public static void setNextState5(Integer state6)
        {
        state5 = state6;
     
        }
     
        public static Integer getNextState5()
        {
        return state5;
        }
     
        public static boolean same2(Integer state, Integer state2)
        {
     
     
         for (int i =0; i < alphabet.size() -1; i++)
         {
         if (!same(state, state2, alphabet.get(i)))
         return false;
     
     
         }
         return true;
     
     
        }
     
        public static boolean same(Integer state, Integer state2, char c)
        {
     
        if (alphabet.contains(c) == false)
    return false;
     
    Integer theInt = getNextState3(state, c);
     
    setNextState4(theInt);
     
    Integer theInt2 = getNextState3(state2, c);
     
    setNextState5(theInt2);
     
    if (theInt == theInt2)
    return true;
    return false;
     
     
        }
     
     
    }

    Need help with DFA minimization.

Similar Threads

  1. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM
  2. Having trouble figuring out this method. Involves while-loops!
    By ayelleeeecks in forum Loops & Control Statements
    Replies: 4
    Last Post: October 19th, 2011, 08:10 PM
  3. Need help figuring out the problem (GUI)
    By Prescott in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 8th, 2011, 12:01 AM
  4. printing array recursively
    By kyros in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 19th, 2010, 01:04 AM
  5. Problem in recursion for Solving Maze Recursively program
    By _Coder1985 in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 29th, 2009, 04:37 AM

Tags for this Thread