我有一个ShortestPath
包含 Dijkstra 算法的类和一个名为computeRoutes
. 我还有一个带有搜索按钮的表单——我想computeRoutes
从这个按钮调用该方法,但不知道该怎么做。
public class ShortestPath {
public static void computeRoutes(Node source){
source.minimumDistance = 0;
PriorityQueue<Node> nodeQueue = new PriorityQueue<Node>();
nodeQueue.add(source);
while(!nodeQueue.isEmpty()){
Node u = nodeQueue.poll();
for(Edge e : u.neighbours){
Node n = e.goal;
int weight = e.weight;
int distThruU = u.minimumDistance + weight;
if(distThruU < n.minimumDistance){
nodeQueue.remove(n);
n.minimumDistance = distThruU;
n.previousNode = u;
nodeQueue.add(n);
}
}
}
}
public static List<Node> getShortestRouteTo(Node goal){
List<Node> route = new ArrayList<Node>();
for(Node node = goal; node != null; node = node.previousNode)
route.add(node);
Collections.reverse(route);
return route;
}
}
public class BPForm extends javax.swing.JFrame {
....
private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {
(I want to call the computeRoutes method here)