我发现Dijkstra的算法并将其实现到我创建的图表中 - 它显示了我当地的地图
代码工作正常,但我希望它显示它访问过的所有节点,以便从源节点到达位置Node。例如:如果我将源节点设置为1(Banstead),将位置节点设置为4(Whteleafe) - 我希望它可能将它访问的节点存储在数组中,如Array = {1,2,4}有什么想法吗?我想把它放在一个FXML文件上,并将节点作为椭圆并用线条连接它们 - 但为了做到这一点,我需要存储所访问节点的值。
package dijkstras;
public class Dijkstras {
static class createGraph{
int vertices;
int matrix[][];
public createGraph(int vertex){
this.vertices = vertex;
matrix = new int[vertex][vertex];
}
public void edge(int source, int destination, int weight){
matrix[source][destination] = weight;
matrix[destination][source] = weight;
}
int getMinVertex(boolean [] mst, int [] key){
int minKey = Integer.MAX_VALUE;
int vertex = -1;
for (int i = 1; i < vertices; i++) {
if(mst[i]==false && minKey>key[i]){
minKey = key[i];
vertex =i;
}
}
return vertex;
}
public void dijkstras(int sourceVertex){
boolean[] spt = new boolean[vertices];
int [] distance = new int[vertices];
int infinity = Integer.MAX_VALUE;
//setting all distances to infinity
for(int i=1; i<vertices; i++){
distance[i] = infinity;
}
//test for starting vertext = 1
distance[sourceVertex] = 1;
//create tree
for(int i=1; i<vertices; i++){
郎朗坤
翻阅古今
相关分类