1 | export class Dijkstra {
|
2 | private static MaxSize = 1000;
|
3 | |
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
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 |
|
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 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | public static getShortedPathAll(graph: number[][], start: number, path: number[][]): number[] {
|
74 | let length = graph.length;
|
75 |
|
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 |
|
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 | }
|