I've implemented Dijkstra's alorithm but I also want to implement a GUI for it. So I'll have a few quesions for you guys.
public class Dijkstra {
public static void main(String args[]) {
Vertex v0 = new Vertex("A");
Vertex v1 = new Vertex("B");
Vertex v2 = new Vertex("C");
Vertex v3 = new Vertex("D");
Vertex v4 = new Vertex("E");
Vertex v5 = new Vertex("F");
Vertex v6 = new Vertex("G");
Vertex v7 = new Vertex("H");
v0.adjacencies = new Edge[]{new Edge(v1, 9), new Edge(v2, 1), new Edge(v6, 9), new Edge(v3, 2)};
v1.adjacencies = new Edge[]{new Edge(v0, 9), new Edge(v4, 4)};
v2.adjacencies = new Edge[]{new Edge(v0, 1), new Edge(v4, 2), new Edge(v5, 5)};
v3.adjacencies = new Edge[]{new Edge(v0, 2), new Edge(v6, 5), new Edge(v7, 1)};
v4.adjacencies = new Edge[]{new Edge(v1, 4), new Edge(v2, 2), new Edge(v5, 1)};
v5.adjacencies = new Edge[]{new Edge(v4, 1), new Edge(v2, 5), new Edge(v6, 1), new Edge(v7, 4)};
v6.adjacencies = new Edge[]{new Edge(v0, 9), new Edge(v5, 1), new Edge(v3, 5), new Edge(v7, 2)};
v7.adjacencies = new Edge[]{new Edge(v5, 4), new Edge(v6, 2), new Edge(v3, 1)};
Vertex[] vertices = {v0, v1, v2, v3, v4, v5, v6, v7};
computePaths(v0);
for (Vertex v : vertices) {
System.out.println("Distance to " + v + ": " + v.getMinDistance());
List<Vertex> path = getShortestPathTo(v);
System.out.println("Path: " + path);
}
}
public static void computePaths(Vertex source) {
source.setMinDistance(0);
PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
vertexQueue.add(source);
while (!vertexQueue.isEmpty()) {
Vertex u = vertexQueue.poll();
for (Edge e : u.adjacencies) {
Vertex v = e.target;
int weight = e.weight;
int distanceThroughU = u.getMinDistance() + weight;
if (distanceThroughU < v.getMinDistance()) {
vertexQueue.remove(v);
v.setMinDistance(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;
}
}
This is how the implementations looks like (without the GUI). I've seen that there is a library called JUNG for graphs and I downloaded it. I've tried to draw my graph by adding the following code to my main method.
SimpleGraphDraw f = new SimpleGraphDraw();
DirectedSparseGraph g = new DirectedSparseGraph();
g.addVertex(v0);
g.addVertex(v1);
g.addVertex(v2);
g.addVertex(v3);
g.addVertex(v4);
g.addVertex(v5);
g.addVertex(v6);
g.addVertex(v7);
g.addEdge("Edge1", v0, v1);
g.addEdge("Edge2", v0, v2);
g.addEdge("Edge3", v0, v6);
g.addEdge("Edge4", v0, v3);
g.addEdge("Edge5", v1, v0);
g.addEdge("Edge6", v1, v4);
g.addEdge("Edge7", v2, v0);
g.addEdge("Edge8", v2, v4);
g.addEdge("Edge9", v2, v5);
VisualizationImageServer vs = new VisualizationImageServer(new CircleLayout(g), new Dimension(200, 200));
JFrame frame = new JFrame();
frame.getContentPane().add(vs);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
How can I arrange all of my vertices the way I want them (they are in a circle right now) ?
How can I place the vertex name "A" inside the red circle in the GUI, so each vertex name should be displayed.
How can I add the each weight inside the GUI on top of the line between two vertices ?
Thanks in advance