UNPKG

4.06 kBPlain TextView Raw
1export class Dijkstra {
2 private static MaxSize = 1000;
3 /** 从某一源点出发,找到到某一结点的最短路径
4 * @param graph 图数据,它是一个二维矩阵,记录每两个点之间的距离
5 * @param start 起始节点索引号
6 * @param end 终点节点索引号
7 * @param path 数组,用于返回start到end顶点之间的路径
8 * @return 数组,从start到每个点之间的距离
9 */
10 public static getShortedPath(graph: number[][], start: number, end: number, path: number[]): number {
11 let length = graph.length;
12 let s = new Array<boolean>(length); // 表示找到起始结点与当前结点间的最短路径
13 //int min; // 最小距离临时变量
14 let curNode = 0; // 临时结点,记录当前正计算结点
15 let dist = new Array<number>(length), prev = new Array<number>(length);
16
17 // 初始结点信息
18 for (let v = 0; v < length; v++) {
19 s[v] = false;
20 dist[v] = graph[start][v];
21 if (dist[v] > Dijkstra.MaxSize) {
22 prev[v] = 0;
23 } else {
24 prev[v] = start;
25 }
26 }
27 path[0] = end;
28 dist[start] = 0;
29 s[start] = true;
30 // 主循环
31 for (let i = 1; i < length; i++) {
32 let min = Dijkstra.MaxSize;
33 for (let w = 0; w < length; w++) {
34 if (!s[w] && dist[w] && dist[w] < min) {
35 curNode = w;
36 min = dist[w];
37 }
38 }
39
40 s[curNode] = true;
41
42 for (let j = 0; j < length; j++) {
43 let wt = graph[curNode][j] ? graph[curNode][j] : 2000;
44 let wt2 = dist[j] ? dist[j] : 2000;
45 if (!s[j] && min + wt < wt2) {
46 dist[j] = min + graph[curNode][j];
47 prev[j] = curNode;
48 }
49 }
50 }
51 // 输出路径结点
52 let e = end, step = 0;
53 while (e != start) {
54 step++;
55 path[step] = prev[e];
56 e = prev[e];
57 }
58 for (let i = step; i > step / 2; i--) {
59 let temp = path[step - i];
60 path[step - i] = path[i];
61 path[i] = temp;
62 }
63 s = null;
64 return dist[end];
65 }
66
67 /** 从某一源点出发,找到到所有结点的最短路径
68 * @param graph 图数据,它是一个二维矩阵,记录每两个点之间的距离
69 * @param start 起始节点索引号
70 * @param path 二维数据,用于返回start到每个顶点之间的路径
71 * @return 数组,从start到每个点之间的距离
72 */
73 public static getShortedPathAll(graph: number[][], start: number, path: number[][]): number[] {
74 let length = graph.length;
75 //let PathID = new Array<number>(length);// 路径(用编号表示)
76 let s = new Array<boolean>(length); // 表示找到起始结点与当前结点间的最短路径
77 let min = Dijkstra.MaxSize; // 最小距离临时变量
78 let curNode = 0; // 临时结点,记录当前正计算结点
79 let dist = new Array<number>(length), prev = new Array<number>(length);
80 // 初始结点信息
81
82 for (let v = 0; v < length; v++) {
83 s[v] = false;
84 dist[v] = graph[start][v];
85 //if (dist[v] == null || dist[v] > Dijkstra.MaxSize) {
86 if (dist[v] == undefined || dist[v] > Dijkstra.MaxSize) {
87 prev[v] = 0;
88 } else {
89 prev[v] = start;
90 }
91 path[v][0] = v;
92 }
93
94 dist[start] = 0;
95 s[start] = true;
96 // 主循环
97 for (let i = 1; i < length; i++) {
98 min = Dijkstra.MaxSize;
99 for (let w = 0; w < length; w++) {
100 if (!s[w] && dist[w] && dist[w] < min) {
101 curNode = w;
102 min = dist[w];
103 }
104 }
105
106 s[curNode] = true;
107
108 for (let j = 0; j < length; j++) {
109 let wt = graph[curNode][j];
110 if (!wt) {
111 wt = 2000;
112 }
113 let wt2 = dist[j];
114 if (!wt2) {
115 wt2 = 2000;
116 }
117 if (!s[j] && (min + wt < wt2)) {
118 dist[j] = min + graph[curNode][j];
119 prev[j] = curNode;
120 }
121 }
122 }
123 // 输出路径结点
124 for (let k = 0; k < length; k++) {
125 let e = k, step = 0;
126 while (e != start) {
127 step++;
128 path[k][step] = prev[e];
129 e = prev[e];
130 }
131 for (let i = step; i > step / 2; i--) {
132 let temp = path[k][step - i];
133 path[k][step - i] = path[k][i];
134 path[k][i] = temp;
135 }
136 }
137 s = null;
138 return dist;
139
140 }
141
142}