2015-10-15 75 views
0

所以我需要使用Java Dijkstra算法。我發現here是Dijkstra的一個實例。但在我的情況下,我有大約5000個頂點。我得到的錯誤:方法main(String [])的代碼超過了65535字節的限制。在stackoverflow上有這樣的話題,但我找不到如何解決這個問題。能有人給我如何解決這樣的代碼..方法main(String [])的代碼超過了65535字節的限制

import java.util.PriorityQueue; 
import java.util.List; 
import java.util.ArrayList; 
import java.util.Collections; 

class Vertex implements Comparable<Vertex> 
{ 
    public final String name; 
    public Edge[] adjacencies; 
    public double minDistance = Double.POSITIVE_INFINITY; 
    public Vertex previous; 
    public Vertex(String argName) { name = argName; } 
    public String toString() { return name; } 
    public int compareTo(Vertex other) 
    { 
     return Double.compare(minDistance, other.minDistance); 
    } 

} 


class Edge 
{ 
    public final Vertex target; 
    public final double weight; 
    public Edge(Vertex argTarget, double argWeight) 
    { target = argTarget; weight = argWeight; } 
} 

public class Dijkstra 
{ 
    public static void computePaths(Vertex source) 
    { 
     source.minDistance = 0.; 
     PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>(); 
    vertexQueue.add(source); 

    while (!vertexQueue.isEmpty()) { 
     Vertex u = vertexQueue.poll(); 

      // Visit each edge exiting u 
      for (Edge e : u.adjacencies) 
      { 
       Vertex v = e.target; 
       double weight = e.weight; 
       double distanceThroughU = u.minDistance + weight; 
     if (distanceThroughU < v.minDistance) { 
      vertexQueue.remove(v); 

      v.minDistance = distanceThroughU ; 
      v.previous = u; 
      vertexQueue.add(v); 
     } 
      } 
     } 
    } 

    public static List<Vertex> getShortestPathTo(Vertex target) 
    { 
     List<Vertex> path = new ArrayList<Vertex>(); 
     for (Vertex vertex = target; vertex != null; vertex = vertex.previous) 
      path.add(vertex); 

     Collections.reverse(path); 
     return path; 
    } 

    public static void main(String[] args) 
    { 
     // mark all the vertices 
     Vertex X1 = new Vertex("A"); 
     //...till Vertex X5000.. 

     // set the edges and weight 
     X1.adjacencies = new Edge[]{ new Edge(X2, 8) }; 
     //...till X5000.adjacencies...  

     computePaths(X1); // run Dijkstra 
     System.out.println("Distance to " + X5 + ": " + X5.minDistance); 
     List<Vertex> path = getShortestPathTo(X5); 
     System.out.println("Path: " + path); 
    } 
} 

編輯

我想從一個MySQL表中的數據,但我有聲明頂點的問題。

String query = "SELECT * FROM coordinates"; 

Statement st = conn.createStatement();   
ResultSet rs = st.executeQuery(query);  
while (rs.next()) 
{ 
    int id = rs.getInt("id"); 
    String vert = Integer.toString(id); 

    //Which approach will work?   
    Vertex vert = new Vertex(vert);   
} 
st.close(); 

回答

3

您是否在代碼中硬編碼圖的定義?不要這樣做。改爲從數據文件中讀取頂點和邊。

+0

是的。我不知道如何:/ – DJack

+0

搜索['從文件[java]'讀取圖形](http://stackoverflow.com/search?q=read+graph+from+file+%5Bjava%5D)。 [Here](http://stackoverflow.com/questions/5756668/how-to-create-a-java-graph-file-from-txt-file)是一些代碼,可能會指出你在正確的方向。不過,我們不會爲您編寫代碼。 –

+0

[更多靈感](http://codereview.stackexchange.com/q/95084/9357) –

相關問題