/
Dijkstra.java
63 lines (62 loc) · 2.46 KB
/
Dijkstra.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public class Dijkstra {
public static int[] dijkstra(int[][] graph,int startVertex){
//初始化 以求出最短路径的点 result[]
int length = graph.length;
int[] result = new int[length];
for (int i = 0; i < length; i++) {
result[i] = -1;
}
result[startVertex] = 0 ;
// 初始化 未求出最短路径的点 notFound[]
int[] notFound = new int[length];
for (int i = 0; i < length; i++) {
notFound[i] = graph[startVertex][i];
}
notFound[startVertex] = -1;
// 开始 Dijkstra 算法
for (int i = 1; i < length; i++) {
//1. 从「未求出最短路径的点」notFound 中取出 最短路径的点
//1.1 找到最短距离的点
int min = Integer.MAX_VALUE;
int minIndex = 0;
for (int j = 0; j < length; j++) {
if (notFound[j] > 0 && notFound[j] < min){
min = notFound[j];
minIndex = j;
}
}
//1.2 将最短距离的点 取出 放入结果中
result[minIndex] = min;
notFound[minIndex] = -1;
//2. 刷新 「未求出最短距离的点」 notFound[] 中的距离
//2.1 遍历刚刚找到最短距离的点 (B) 的出度 (BA、BB、BC、BD)
for (int j = 0; j < length; j++) {
// 出度可通行(例如 BD:graph[1][3] > 0)
// 出度点不能已经在结果集 result中(例如 D: result[3] == -1)
if (graph[minIndex][j] > 0
&& result[j] == -1){
int newDistance = result[minIndex] + graph[minIndex][j];
//通过 B 为桥梁,刷新距离
//(比如`AD = 6 < AB + BD = 4` 就刷新距离)( -1 代表无限大)
if (newDistance < notFound[j] || notFound[j]==-1){
notFound[j] = newDistance;
}
}
}
}
return result;
}
/** 测试案例 */
public static void main(String[] args) {
char[] vertices = new char[]{'A','B','C','D'};
int[][] graph = new int[][]{
{0, 2, -1, 6}
, {2, 0, 3, 2}
, {-1, 3, 0, 2}
, {6, 2, 2, 0}};
int[] dijkstra = dijkstra(graph, 0);
for (int i : dijkstra) {
System.out.println(i);
}
}
}