2015-06-19 87 views
-3

我想在java中應用Dijkstra算法。輸入將來自包含3列的文本文件,第一個是開始節點,第三個是結束節點,第二個是關係名稱,但現在我不需要它,所以我使用第一列和最後一列。 文本文件是:java中的錯誤:線程「主」中的異常

12 \t ECrel \t 15 
 
15 \t ECrel \t 18 
 
11 \t ECrel \t 12 
 
12 \t ECrel \t 14 
 
11 \t ECrel \t 14 
 
11 \t ECrel \t 18 
 
14 \t maplink \t 17 
 
12 \t maplink \t 17 
 
14 \t maplink \t 10 
 
18 \t maplink \t 10 
 
14 \t maplink \t 16 
 
15 \t maplink \t 19 
 
18 \t maplink \t 19 
 
12 \t maplink \t 19

任何一個可以請大家看看我的代碼,並幫助我找到什麼是導致此錯誤代碼的問題。代碼是波紋管:

package shortestPath; 
import java.util.Scanner; 
import java.io.*; 
import java.util.*; 

public class Dijkstra { 


    public static int count; 
    //public static Graph.Edge[] GRAPH = new Graph.Edge[count] ; 

    public static void countLines(String file) throws IOException 
    { 
    LineNumberReader lnr = new LineNumberReader(new FileReader(new File(file))); 
    lnr.skip(Long.MAX_VALUE); 
    Dijkstra.count=lnr.getLineNumber() + 1; //Add 1 because line index starts at 0 
    // Finally, the LineNumberReader object should be closed to prevent resource leak 
    lnr.close(); 
    //return Dijkstra.count; 
    } 

    public static Graph.Edge[] readTextFile(String fileName) { 

    String line = null; 
    Graph.Edge[] Gr=new Graph.Edge[Dijkstra.count]; 
    try { 
     FileReader fileReader = new FileReader("hsa00072.txt"); 
     // Always wrap FileReader in BufferedReader. 
     BufferedReader bufferedReader = new BufferedReader(fileReader); 
     // BufferedReader br = new BufferedReader(new InputStreamReader(new 
     // FileInputStream(file))); 
     int i=0; 
     while ((line = bufferedReader.readLine()) != null) { 
      String[] tokens = line.split("\\t+"); 
      String s = tokens[0]; 
      String e = tokens[2]; 
      Gr[i] =new Graph.Edge(s, e, 1); 
      i=i+1; 
     } 

     // Always close files. 
     bufferedReader.close(); 
    } catch (FileNotFoundException ex) { 
     System.out.println("Unable to open file '" + fileName + "'"); 
    } catch (IOException ex) { 
     System.out.println("Error reading file '" + fileName + "'"); 
    } 
    //return Dijkstra.GRAPH; 
    return Gr; 
    } 


     private static final String START = "10"; 
     private static final String END = "12"; 

     public static void main(String[] args) throws IOException { 
      countLines("hsa00072.txt"); 
      Graph.Edge[] GRAPH=readTextFile("hsa00072.txt"); 
      Graph g = new Graph(GRAPH); 
      g.dijkstra(START); 
      g.printPath(END); 


      //g.printAllPaths(); 
     } 
    } 

    class Graph { 
     private final Map<String, Vertex> graph; // mapping of vertex names to Vertex objects, built from a set of Edges 

     /** One edge of the graph (only used by Graph constructor) */ 
     public static class Edge { 
      public final String v1, v2; 
      public final int dist; 
      public Edge(String v1, String v2, int dist) { 
      this.v1 = v1; 
      this.v2 = v2; 
      this.dist = dist; 
      } 
     } 

     /** One vertex of the graph, complete with mappings to neighbouring vertices */ 
     public static class Vertex implements Comparable<Vertex> { 
      public final String name; 
      public int dist = Integer.MAX_VALUE; // MAX_VALUE assumed to be infinity 
      public Vertex previous = null; 
      public final Map<Vertex, Integer> neighbours = new HashMap<>(); 

      public Vertex(String name) { 
      this.name = name; 
      } 

      private void printPath() { 
      if (this == this.previous) { 
       System.out.printf("%s", this.name); 
      } else if (this.previous == null) { 
       System.out.printf("%s(unreached)", this.name); 
      } else { 
       this.previous.printPath(); 
       System.out.printf(" -> %s(%d)", this.name, this.dist); 
      } 
      } 

      public int compareTo(Vertex other) { 
      return Integer.compare(dist, other.dist); 
      } 
     } 

     /** Builds a graph from a set of edges */ 
     public Graph(Edge[] edges) { 
      graph = new HashMap<>(edges.length); 

      //one pass to find all vertices 
      for (Edge e : edges) { 
      if (!graph.containsKey(e.v1)) graph.put(e.v1, new Vertex(e.v1)); 
      if (!graph.containsKey(e.v2)) graph.put(e.v2, new Vertex(e.v2)); 
      } 

      //another pass to set neighbouring vertices 
      for (Edge e : edges) { 
      graph.get(e.v1).neighbours.put(graph.get(e.v2), e.dist); 
      //graph.get(e.v2).neighbours.put(graph.get(e.v1), e.dist); // also do this for an undirected graph 
      } 
     } 

     /** Runs dijkstra using a specified source vertex */ 
     public void dijkstra(String startName) { 
      if (!graph.containsKey(startName)) { 
      System.err.printf("Graph doesn't contain start vertex \"%s\"\n", startName); 
      return; 
      } 
      final Vertex source = graph.get(startName); 
      NavigableSet<Vertex> q = new TreeSet<>(); 

      // set-up vertices 
      for (Vertex v : graph.values()) { 
      v.previous = v == source ? source : null; 
      v.dist = v == source ? 0 : Integer.MAX_VALUE; 
      q.add(v); 
      } 

      dijkstra(q); 
     } 

     /** Implementation of dijkstra's algorithm using a binary heap. */ 
     private void dijkstra(final NavigableSet<Vertex> q) {  
      Vertex u, v; 
      while (!q.isEmpty()) { 

      u = q.pollFirst(); // vertex with shortest distance (first iteration will return source) 
      if (u.dist == Integer.MAX_VALUE) break; // we can ignore u (and any other remaining vertices) since they are unreachable 

      //look at distances to each neighbour 
      for (Map.Entry<Vertex, Integer> a : u.neighbours.entrySet()) { 
       v = a.getKey(); //the neighbour in this iteration 

       final int alternateDist = u.dist + a.getValue(); 
       if (alternateDist < v.dist) { // shorter path to neighbour found 
        q.remove(v); 
        v.dist = alternateDist; 
        v.previous = u; 
        q.add(v); 
       } 
      } 
      } 
     } 

     /** Prints a path from the source to the specified vertex */ 
     public void printPath(String endName) { 
      if (!graph.containsKey(endName)) { 
      System.err.printf("Graph doesn't contain end vertex \"%s\"\n", endName); 
      return; 
      } 

      graph.get(endName).printPath(); 
      System.out.println(); 
     } 
     /** Prints the path from the source to every vertex (output order is not guaranteed) */ 
     public void printAllPaths() { 
      for (Vertex v : graph.values()) { 
      v.printPath(); 
      System.out.println(); 
      } 
     } 

    }  

的錯誤信息是:

Exception in thread "main" java.lang.NullPointerException 
 
\t at shortestPath.Graph.<init>(Dijkstra.java:125) 
 
\t at shortestPath.Dijkstra.main(Dijkstra.java:69)

線69:

Graph g = new Graph(GRAPH); 

線125是

if (!graph.containsKey(e.v1)) graph.put(e.v1, new Vertex(e.v1)); 
+3

你能告訴我們實際的錯誤信息是什麼嗎? 「線程'main'中的例外」絕對沒有傳達給我們有用的信息。 – hexafraction

+0

將異常堆棧跟蹤和文件hsa00072.txt的內容添加到問題描述中。 –

+0

這就像你去看醫生並且說... *「醫生,醫生,我覺得不舒服,給我開一張處方!」*醫生需要知道症狀......我們需要看到堆棧跟蹤。 (這是......除非你對糖丸的IT效果感到滿意,你是否試過重新安裝操作系統?:-)) –

回答

1

感謝您的所有意見,但特別感謝David Wallace,他的評論幫助我瞭解導致此錯誤的問題。問題出在這一行

Graph.Edge [] Gr = new Graph.Edge [Dijkstra.count];我不得不將尺寸設置爲count-1而不是count。

謝謝。

相關問題