2015-10-15 79 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) –

相关问题