简体   繁体   中英

Can't figure out correct algorithm for moving within a graph

I've posted this question before but thought I'd revise it and try with a little more detail. I have this image below. What I am trying to accomplish is start in the even "bubble" and transition from ones and zeros to the other states. Here is what I've accomplished:

1) Create mainMap with all the states (even and odd) as well as another map that correctly has the inputs (0s and 1s) that lead away from that state. 2) Sort the state from even to odd transitions.

平价

Here is my code so far:

public static void main(String[] args) {

  //Map<Even, ArrayMap[0->even,1->odd]> for first line
  Map<String, Map<String,String>> mainMap = new ArrayMap<String, Map<String,String>>();
 //Map<int,even/odd>

  TypedBufferReader input = new TypedBufferReader("Enter Finite Automaton Description File: ");
 //read the file.
  for (;;) {
     try {
          String line = input.readLine();
          StringTokenizer st = new StringTokenizer(line, ";");
          String  state = st.nextToken();

          Map<String,String> transitions = mainMap.get(state);

          transitions = new ArrayMap<String, String>();
          while (st.hasMoreTokens()) {
              String intStateInput = st.nextToken();
              String inputState = st.nextToken();
              transitions.put(intStateInput, inputState);
          }
        mainMap.put(state, transitions);
     } catch (EOFException e) { break;}
  }

  //Print in alphabetical order of states. odd/even to even/odd
  List<String> mapList = new ArrayList<String>(mainMap.keys());
  Collections.sort(mapList);
  for (String s : mapList) {
      Map<String, String> tempMap = mainMap.get(s);
      System.out.println(s + " transitions = " + tempMap.toString());
  }

  //Process one line file.
  TypedBufferReader oneLineInput = new TypedBufferReader("Enter start state/inputs file: ");

  try {
      String oneLine = oneLineInput.readLine();
      StringTokenizer st = new StringTokenizer(oneLine,";");
      String initialState = st.nextToken();
      System.out.println("Initial state = " + initialState);

      while (st.hasMoreTokens()) {
          String inputNum = st.nextToken();

      }

  } catch (EOFException e) {}



  }

}

I'm supposed to read a file with one line as follows: "even; 1;0;1;1;0;1" So, it would print the initial state of even and continue moving through the mainMap.

For an initial state of even it would be like this:

initial state = even

input = 1 state = odd

input = 0 state = odd

input = 1 state = even

input = 1 state = odd

input = 0 state = odd

input = 1 state = even

final state = even

Please help me with this problem.

I think that would be easy to do.. You need to remember previous state, So you begin with even

Step 1: prevState = EVEN
Step 2: Read currentState (Read them as Integer)
Step 3: Repeate Until EOF
          if currentState | prevState == 0  then
             state = EVEN
             print EVEN 
          else
             state =  ODD
             print ODD

In your loop, you get the transitions linked to your current state name, then get the name of the new state for the given transition :

while (st.hasMoreTokens()) {
   String inputNum = st.nextToken();          
   Map<String,String> mainMap transitions = mainMap.get(initialState);
   initialState = transitions.get(inputNum);
} 

i would also rename the initialSate in currentState, but its just personnal preference :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM