2016-01-11 59 views
1

我想通过我的我的DFS的堆栈遍历时尽可能快地得到这个代码运行当前的输入文件是像这样:一个DFS算法的Implemeting堆栈遍历 - Java的

0 2 
2 1 
1 4 
4 5 
5 6 
10 8 
8 9 
9 6 
7 6 
3 4 
0 1 
3 9 
0 4 

我的Maze课程将把这些数字连接在一起并为我创建一个图表。图创建后,我的DFS类遍历遍历,给出提交的.txt文件的一个或所有解决方案。我最近更改了我的Maze类,因为它可以更有效地运行,但是被抛出的错误和数据解析到我的输出DFS。我Maze类如下:

import java.io.*; 
import java.util.*; 

public class Maze { 

    private final Map<Integer, Set<Integer>> adjList = new HashMap<>(); 

    /** 
    * The main constructor that takes a String for reading maze file. 
    * 
    * @param file 
    */ 
    public Maze(File file) throws FileNotFoundException { 
     try (Scanner scan = new Scanner(file)) { 
      while (scan.hasNextInt()) { 
       int node1 = scan.nextInt(); 
       int node2 = scan.nextInt(); 
       this.connect(node1, node2); 
       this.connect(node2, node1); 
      } 
     } 
    } 

    /** 
    * Makes a unidirectional connection from node1 to node2. 
    */ 
    private void connect(int node1, int node2) { 
     if (!this.adjList.containsKey(node1)) { 
      this.adjList.put(node1, new HashSet<Integer>()); 
     } 
     this.adjList.get(node1).add(node2); 
    } 

    /** 
    * Returns a human-readable description of the adjacency lists. 
    */ 
    public String toString() { 
     StringBuilder s = new StringBuilder(); 
     for (Map.Entry<Integer, Set<Integer>> adj : this.adjList.entrySet()) { 
      int from = adj.getKey(); 
      Set<Integer> to = adj.getValue(); 
      s.append(from).append(" connected to ").append(to).append('\n'); 
     } 
     return s.toString(); 
    } 

    /** 
    * Returns the set of nodes connected to a particular node. 
    * 
    * @param node - the node whose neighbors should be fetched 
    */ 
    public Iterable<Integer> getadjList(int node) { 
     return Collections.unmodifiableSet(adjList.get(node)); 
    } 

    /** 
    * Demonstration of file reading. 
    */ 
    public static void main(String[] args) throws FileNotFoundException { 
     System.err.print("Enter File: "); 
     Scanner scanFile = new Scanner(System.in); 
     String file = scanFile.nextLine(); 
     Maze m = new Maze(new File(file)); 
     System.out.println(m); 
    } 

} 

而且我DFS看起来像这样。

import java.util.Scanner; 
import java.util.Stack; 

public class DFS { 
    //starting node, the route to the next node, has node been visited 
    private int startNode; 
    private int[] route; 
    private boolean[] visited; 


    // 2 main arguments - Maze File & user input 
    public DFS(Maze maze, int inputInt) { 
     int startNode = 0; 
     int goalNode = 1; 
     route = new int[maze.node]; 
     visited = new boolean[maze.node]; 
     //Takes user's input and runs desired function 
     if(inputInt == 1){ 
     startDFSone(maze, startNode, goalNode); 
     } 
     else if (inputInt == 2){ 
     startDFSall(maze, startNode, goalNode); 
     } 
     else { 
      System.out.println("input invalid. No Solution Returned"); 
     } 
    } 



    //Put path to goal in the stack 
    public Stack<Integer> route(int toGoalNode) { 
     if (!visited[toGoalNode]) { 
      return null; 
     } 
     Stack<Integer> pathStack = new Stack<Integer>(); 
     for (int routeGoalNode = toGoalNode; routeGoalNode != startNode; routeGoalNode = route[routeGoalNode]) { 
      pathStack.push(routeGoalNode); 
     } 
     pathStack.push(startNode); 
     reverseStack(pathStack); 
     return pathStack; 
    } 

    //Reverse the stack 
    public void reverseStack(Stack<Integer> stackToBeReverse) { 

     if (stackToBeReverse.isEmpty()) { 
      return; 
     } 

     int bottom = popBottomStack(stackToBeReverse); 
     reverseStack(stackToBeReverse); 
     stackToBeReverse.push(bottom); 
    } 

    //Pop the bottom of the stack 
    private int popBottomStack(Stack<Integer> stackToBeReverse) { 
     int popTopStack = stackToBeReverse.pop(); 
     if (stackToBeReverse.isEmpty()) { 
      return popTopStack; 
     } else { 
      int bottomStack = popBottomStack(stackToBeReverse); 
      stackToBeReverse.push(popTopStack); 
      return bottomStack; 
     } 
    } 

    //performs DFS and unsets visited to give the result of all paths 
    private void startDFSall(Maze maze, int node, int goal) { 
     visited[node] = true; 
     if(node == goal) { 
      printPath(goal); 
     } else { 
      for (int con : maze.getadjList(node)) { 
       if (!visited[con]) { 
        route[con] = node; 
        startDFSall(maze, con, goal); 
       } 
      } 
     } 
     visited[node] = false; 
    } 

    //performs DFS and maintains visited marker giving only one path 
    private void startDFSone(Maze maze, int node, int goal) { 
      visited[node] = true; 
      for (int con : maze.getadjList(node)) { 
       if (!visited[con]) { 
        route[con] = node; 
        startDFSone(maze, con, goal); 
       } 
      } 
     } 

    //Traverse the connections to the goal and print the path taken 
    public void printPath(int toGoal) { 
     int goalNode = 1; 
     if (visited[toGoal]) { 
      System.out.println("Completed Path: "); 
      for (int t : route(toGoal)) { 
       if (t == toGoal) { 
        System.out.print(t); 
       } else { 
        System.out.print(t + " -> "); 
       } 
      } 
      System.out.println(); 
     } 
    } 


    public static void main(String[] args) { 
     Scanner scanFile = new Scanner(System.in); 
     int goalNode = 1; 
     System.out.print("Enter maze file: "); 
     String file = scanFile.nextLine(); 
     Maze maze = new Maze(file); //**Most error's show here** 
     Scanner scanInt = new Scanner(System.in); 
     System.out.print("Enter desired feedback (1 = one soultion, 2 = all): "); 
     int inputInt = scanInt.nextInt(); 
     maze.toString(); //**Most error's show here** 
     System.out.println();   
     DFS dfs = new DFS(maze, inputInt); //**error's show here** 
     dfs.printPath(goalNode); //**error's show here** 
     } 

} 

我已经找过了一段时间,并不能弄清楚到底为什么数据分析和使用。我在这里和那里改变了一些东西,但被抛出了更多的错误。任何方式希望有人可以提供帮助。由于

编辑:我认为这个问题是由于试图使用一个文件类型为String我已经标志着主在我认为DFS类的问题可能源于

+0

你有两个版本:'startDFSall'和'startDFSone'。你遇到哪个问题?那么,除了'startDFSone'有问题,你永远不会调用'printPath()'。 – Andreas

+0

我在主函数中调用printPath到goalNode? – Ben411916

+0

我在“迷宫迷宫=新迷宫(文件)”中的主要功能中不断收到错误;'但当我改变它在我的迷宫班相同,我得到很多更多的错误 – Ben411916

回答

1

变化

Maze maze = new Maze(file) 

Maze maze = new Maze(new File(file)); 

但该方案仍有其他错误是固定的。 Eclipse,Netbean,intellij或其他IDE将帮助您。

+0

从字面上只是做了这个,因为你的答案弹出:)谢谢 – Ben411916