-
Notifications
You must be signed in to change notification settings - Fork 10
/
Dijkstra.html
207 lines (177 loc) · 5.95 KB
/
Dijkstra.html
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>单源最短路径 - 迪杰斯特拉算法</title>
</head>
<body>
<script src="../Graph%20Theory/Edge.js"></script>
<script>
//思路:利用最小索引堆,分为个步骤:
//1、从源点出发标记到源点距离为0,向最小堆插入源点
//2、遍历从索引堆取出最小值(假设这个值为x点)到所到点的所有值(假设每个点为y)
//3、如果y点没有经过,则marked数组标记y点为true,如果到y点没有记录或当前到y点的值小于已记录的值,则进行修改,同时更新最小索引堆(松驰操作)
class Dijkstra {
constructor(graph, s) {
this.s = s;
this.disTo = Array(graph.length).fill(0);
this.marked = Array(graph.length).fill(false);
this.from = Array(graph.length).fill(null);
this.ipq = new IndexMinHeap(graph.length);
this.graph = [];
this.initGraph(graph);
this.dijkstra(s);
console.log(this.disTo)
}
initGraph(graph) {
for (let i = 0; i < graph.length; i++) {
this.graph[i] = [];
for (let j = 0; j < graph[i].length; j++) {
let e = graph[i][j];
if (e) {
this.graph[i].push(new Edge(i, j, e))
}
}
}
}
dijkstra(s) {
this.disTo[s] = 0;
this.ipq.insert(s, this.disTo[s]);
while (!this.ipq.isEmpty()) {
let v = this.ipq.extractMinIndex(),
e, w;
this.marked[v] = true;
// Relaxation
for (let i = 0; i < this.graph[v].length; i++) {
e = this.graph[v][i];
w = e.other(v);
if (!this.marked[w]) {
if (!this.from[w] || e.wt() + this.disTo[v] < this.disTo[w]) {
this.disTo[w] = e.wt() + this.disTo[v];
this.from[w] = e;
if (this.ipq.contain(w)) {
this.ipq.change(w, this.disTo[w])
} else {
this.ipq.insert(w, this.disTo[w])
}
}
}
}
}
}
shortestPathTo(w) {
return this.disTo[w]
}
hasPathTo(w) {
return this.marked[w]
}
shortestPath(w, vec) {
let stack = [],
e = this.from[w];
while (e.v() !== this.s) {
stack.push(e);
e = this.from[e.v()]
}
stack.push(e);
while (stack.length) {
e = stack.pop();
vec.push(e.v())
}
vec.push(w)
}
showPath(w) {
let vector = [],
path = '';
this.shortestPath(w, vector);
for (let i = 0; i < vector.length; i++) {
if (i === vector.length - 1) {
path += vector[i]
} else {
path += `${vector[i]} -> `
}
}
console.log(`shortestPath ${this.s} to ${w}: ${this.shortestPathTo(w)}`)
console.log(path)
}
}
//region 最小索引堆
class IndexMinHeap {
constructor(capacity) {
this.data = Array(capacity + 1);
this.indexes = Array(capacity + 1);
this.reverse = Array(capacity + 1).fill(0);
this.count = 0;
this.capacity = capacity;
}
insert(i, val) {
if (this.count + 1 > this.capacity || this.contain(i)) return;
this.data[++i] = val;
this.indexes[++this.count] = i;
this.reverse[i] = this.count;
this.shiftUp(this.count);
}
shiftUp(k) {
while (k > 1 && this.data[this.indexes[k]] < this.data[this.indexes[k >> 1]]) {
this.swapIndex(k, k >> 1);
k = k >> 1;
}
}
shiftDown(k) {
while (k * 2 <= this.count) {
let j = k * 2;
if (j + 1 <= this.count && this.data[this.indexes[j + 1]] < this.data[this.indexes[j]]) {
j++
}
if (this.data[this.indexes[k]] < this.data[this.indexes[j]]) return;
this.swapIndex(k, j)
k = j
}
}
contain(i) {
return this.reverse[i + 1] !== 0;
}
extractMin() {
if (!this.count) return;
let res = this.data[this.indexes[1]];
this.swapIndex(1, this.count);
this.reverse[this.indexes[this.count]] = 0;
this.count--;
this.shiftDown(1);
return res
}
extractMinIndex() {
if (!this.count) return;
let res = this.indexes[1] - 1;
this.swapIndex(1, this.count);
this.reverse[this.indexes[this.count]] = 0;
this.count--;
this.shiftDown(1);
return res
}
isEmpty() {
return this.count === 0
}
swapIndex(i, j) {
[this.indexes[i], this.indexes[j]] = [this.indexes[j], this.indexes[i]]
this.reverse[this.indexes[i]] = i;
this.reverse[this.indexes[j]] = j;
}
change(i, val) {
i++;
this.data[i] = val;
this.shiftUp(this.reverse[i])
this.shiftDown(this.reverse[i])
}
}
//endregion
const graph = [
[0, 5, 2, 6, 0],
[0, 0, 0, 0, 1],
[0, 1, 0, 3, 5],
[0, 0, 0, 0, 2],
[0, 0, 0, 0, 0]
]
new Dijkstra(graph, 0).showPath(4)
</script>
</body>
</html>