|
@@ -144,8 +144,48 @@ public class GraphService {
|
|
|
visitStartNode.set(startVertex,startVertex);
|
|
visitStartNode.set(startVertex,startVertex);
|
|
|
Stack<Integer> stack=new Stack<Integer>();
|
|
Stack<Integer> stack=new Stack<Integer>();
|
|
|
stack.push(startVertex);
|
|
stack.push(startVertex);
|
|
|
|
|
+ visited.set(startVertex,1);
|
|
|
|
|
+ visitSequence.add(startVertex);
|
|
|
|
|
+ steps.add(new GraphTraversalStep("visit",startVertex,startVertex,true));
|
|
|
|
|
+ boolean isTerminal=false;
|
|
|
|
|
+ int formerNode=0;
|
|
|
while(!stack.isEmpty()){
|
|
while(!stack.isEmpty()){
|
|
|
- boolean isTerminal=false;
|
|
|
|
|
|
|
+ int curVertex=stack.peek();
|
|
|
|
|
+ int nextNode=0;
|
|
|
|
|
+ if(visitSequence.get(visitSequence.size()-1)==curVertex) { //如果正在遍历某支路,没有折返
|
|
|
|
|
+ isTerminal=true;
|
|
|
|
|
+ for (int i = 0; i < nodeNum; i++) {
|
|
|
|
|
+ if (content.get(curVertex).get(i) == 1) {
|
|
|
|
|
+ if (visited.get(i) == 0) {
|
|
|
|
|
+ nextNode = i;
|
|
|
|
|
+ visited.set(nextNode, 1);
|
|
|
|
|
+ visitSequence.add(nextNode);
|
|
|
|
|
+ stack.push(nextNode);
|
|
|
|
|
+ isTerminal = false;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if(isTerminal){
|
|
|
|
|
+ formerNode=stack.pop();
|
|
|
|
|
+ }
|
|
|
|
|
+ }else{ //如果在折返状态中
|
|
|
|
|
+ if(formerNode<nodeNum-1) {
|
|
|
|
|
+ boolean needReturn=true;
|
|
|
|
|
+ for (int i = formerNode + 1; i < nodeNum; i++) {
|
|
|
|
|
+ if(visited.get(i)==0){
|
|
|
|
|
+ stack.push(i);
|
|
|
|
|
+ visited.set(i,1);
|
|
|
|
|
+ visitSequence.add(i);
|
|
|
|
|
+ needReturn=false;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if(needReturn){
|
|
|
|
|
+ formerNode=stack.pop();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|