[英]Drawing graph for dijkstra algorithm
我已經實現了Dijkstra的算法,但是我也想為其實現GUI。 因此,我將向您提出幾個問題。
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;
}
}
這是實現的外觀(沒有GUI)。 我已經看到有一個名為JUNG的圖形庫,我下載了它。 我試圖通過將以下代碼添加到我的main方法來繪制圖形。
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);
如何按我想要的方式排列所有頂點(它們現在在一個圓圈中)?
如何在GUI的紅色圓圈內放置頂點名稱“ A”,因此應顯示每個頂點名稱。
如何在GUI中兩個頂點之間的線的頂部添加每個權重?
提前致謝
雖然我不熟悉JUNG,但是我為我的大學項目實現了Dijkstra算法的GUI( 源代碼 )。
它基於Java Swing的Graphics2D對象繪制邊緣。 由於您已經實現了Dijkstra的算法,因此您可能只對下面給出的繪制邊緣(帶有箭頭和標簽的線)的代碼感興趣。 它使用三角函數繪制方向箭頭。
private void drawLineSegment(Point from, Point to, Color c, int size, String label, Graphics2D g) {
g.setColor(c);
g.setStroke(new BasicStroke(size));
int x1 = from.getX();
int y1 = from.getY();
int x2 = to.getX();
int y2 = to.getY();
g.drawLine(x1, y1, x2, y2);
int sx = (int) ((x1 + x2) / 2.1);
int sy = (int) ((y1 + y2) / 2.1);
int cx = (int) ((x1 + x2) / 2);
int cy = (int) ((y1 + y2) / 2);
int d = 10;
double angle = Util.angle360(from, to);
double anglePlus45 = angle + 45;
if (anglePlus45 > 360)
anglePlus45 = anglePlus45 % 360;
double angleMinus45 = angle - 45;
if (angleMinus45 < 360)
angleMinus45 = angleMinus45 + 360;
anglePlus45 = Math.toRadians(anglePlus45);
angleMinus45 = Math.toRadians(angleMinus45);
int ax1 = (int) (cx - d * Math.cos(anglePlus45));
int ay1 = (int) (cy - d * Math.sin(anglePlus45));
int ax2 = (int) (cx - d * Math.cos(angleMinus45));
int ay2 = (int) (cy - d * Math.sin(angleMinus45));
g.drawLine(cx, cy, ax1, ay1);
g.drawLine(cx, cy, ax2, ay2);
g.drawString(label, sx, sy);
}
這在src / wban / simulate / view / SwingViewer.java中。 如果您對運行代碼感興趣,可以下載可運行的jar 。 項目文檔可在此處和此處獲得 。
要使用其他布局算法,請將代碼中對CircleLayout
的引用替換為其他Layout
實現; 榮提供了幾種選擇。
作為JUNG分發的一部分包含的示例代碼包括幾個示例,這些示例演示了如何使用頂點和邊標簽。
您可能也對JUNG ShortestPathDemo示例代碼感興趣。
相關問題(在JUNG頂點/邊緣標簽上):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.