matrix[5] = new int[]{N, N, N, 4, 5, N, 6};
matrix[6] = new int[]{2, 3, N, N, 4, 6, N};
// 创建Graph
Graph graph = new Graph(vertex, matrix);
// 测试
graph.showGraph();
// 测试迪杰斯特拉算法
graph.dsj(6);
// 查看结果
graph.showDijkstra();
}
}
class Graph {
private char[] vertex; // 顶点数组
private int[][] matrix; // 邻接矩阵
private VisitedVertex vv; // 已经访问的结点集合
// 构造器
public Graph(char[] vertex, int[][] matrix) {
this.vertex = vertex;
this.matrix = matrix;
}
// 显示图的方法
public void showGraph() {
for (int[] link : matrix) {
System.out.println(Arrays.toString(link));
}
}
/**
-
迪杰斯特拉算法实现
-
@param index 表示出发顶点对应的下标
*/
public void dsj(int index) {
vv = new VisitedVertex(vertex.length, index);
update(index); // 更新index顶点到周围顶点距离和前驱顶点
for (int i = 0; i < vertex.length; i++) {
index = vv.updateArr(); // 选择并返回新的访问顶点
update(index); // 更新index下标顶点到周围顶点的距离和周围顶点的前驱结点
}
}
// 更新index下标顶点到周围顶点的距离和周围顶点的前驱结点
private void