[英]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. 1)创建具有所有状态(偶数和奇数)的mainMap以及正确具有远离该状态的输入(0和1)的另一个映射。 2) Sort the state from even to odd transitions.
2)将状态从偶数转换为奇数转换。
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. 我应该按如下方式读取一行文件:“even; 1; 0; 1; 1; 0; 1”因此,它将打印偶数的初始状态并继续通过mainMap移动。
For an initial state of even it would be like this: 对于初始状态甚至它将是这样的:
initial state = even 初始状态=偶数
input = 1 state = odd input = 1 state = odd
input = 0 state = odd input = 0 state = odd
input = 1 state = even input = 1 state = even
input = 1 state = odd input = 1 state = odd
input = 0 state = odd input = 0 state = odd
input = 1 state = even 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 :) 我也会在currentState中重命名initialSate,但它只是个人偏好:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.