0

So I need to use Java Dijkstra algorithm. I found here a working example of Dijkstra. But in my case I have around 5000 vertex. And I get the error: The code of method main(String[]) is exceeding the 65535 bytes limit. There are some topics like this on stackoverflow but I can't find the implementation how to solve this. Can someone give me some code on how to solve this..

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);
    }
}

EDIT

I am trying to get the data from a MySQL table but I have problems with declaring vertex.

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();  
Community
  • 1
  • 1
DJack
  • 631
  • 1
  • 8
  • 34

1 Answers1

3

Are you hard-coding the definition of the graph in your code? Don't do that. Read the vertices and edges from a data file instead.

200_success
  • 7,286
  • 1
  • 43
  • 74
  • Yes. I don't know how :/ – DJack Oct 15 '15 at 08:27
  • Search for [`read graph from file [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) is some code that might point you in the right direction. We're not going to write the code for you, though. – 200_success Oct 15 '15 at 08:31