package Test;
import java.util.Arrays;
/**
* 尚硅谷
*/
public class DijkstraAlgorithm {
public static void main(String[] args) {
char[] vertex = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
int[][] matrix = new int[vertex.length][vertex.length];
final int N = 65535;// 表示不可以连接
matrix[0] = new int[]{N, 5, 7, N, N, N, 2};
matrix[1] = new int[]{5, N, N, 9, N, N, 3};
matrix[2] = new int[]{7, N, N, N, 8, N, N};
matrix[3] = new int[]{N, 9, N, N, N, 4, N};
matrix[4] = new int[]{N, N, 8, N, N, 5, 4};
matrix[5] = new int[]{N, N, N, 4, 5, N, 6};
matrix[6] = new int[]{2, 3, N, N, 4, 6, N};
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 showDijkstra() {
vv.show();
}
//显示图
public void showGraph() {
for (int[] link : matrix) {
System.out.println(Arrays.toString(link));
}
}
public void dsj(int index) {
//初始化
vv = new VisitedVertex(vertex.length, index);
update(index);
//G已被访问所以i从1开始
for (int i = 1; i < vertex.length; i++) {
//找下一个顶点的下标
index = vv.updateArr();
update(index);
}
}
//更新距离与前驱节点
public void update(int index) {
int len = 0;
//ex:matrix[6]为[2, 3, 65535, 65535, 4, 6, 65535]
for (int i = 0; i < matrix[index].length; i++) {
len = vv.getDis(index) + matrix[index][i];
//in(i)判断i是否被访问getDis(i) 下标为i的距离
if (!vv.in(i) && len < vv.getDis(i)) {
//ex:前驱节点(G->A)A的前驱为G pre_visited[pre] = index
vv.updatePre(i, index);
vv.updateDis(i, len);
}
}
}
}
class VisitedVertex {
public int[] already_arr;
public int[] pre_visited;
public int[] dis;
public VisitedVertex(int length, int index) {
//已经访问的
this.already_arr = new int[length];
//前驱
this.pre_visited = new int[length];
//距离
this.dis = new int[length];
//距离填充65535
Arrays.fill(dis, 65535);
//index标记为以访问(G)
this.already_arr[index] = 1;
//G赋值为零
this.dis[index] = 0;
}
//是否被访问
public boolean in(int index) {
return already_arr[index] == 1;
}
//更新出发顶点到index的距离
public void updateDis(int index, int len) {
dis[index] = len;
}
//更新pre这个顶点
public void updatePre(int pre, int index) {
pre_visited[pre] = index;
}
//返回出发顶点到index的距离
public int getDis(int index) {
return dis[index];
}
//继续选择并访问新的顶点,比如这里的G完后,就是A为新的访问顶点(不是出发点)
public int updateArr() {
int min = 65535, index = 0;
for (int i = 0; i < already_arr.length; i++) {
if (already_arr[i] == 0 && dis[i] < min) {
min = dis[i];
index = i;
}
}
already_arr[index] = 1;
return index;
}
//显示最后结果
public void show() {
System.out.println("==========================");
for (int i : already_arr) {
System.out.print(i + " ");
}
System.out.println();
for (int i : pre_visited) {
System.out.print(i + " ");
}
System.out.println();
for (int i : dis) {
System.out.print(i + " ");
}
System.out.println();
char[] vertex = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
int count = 0;
for (int i : dis) {
if (i != 65535) {
System.out.print(vertex[count] + "(" + i + ") ");
} else {
System.out.println("N ");
}
count++;
}
System.out.println();
}
}
06-03
1365