-
Notifications
You must be signed in to change notification settings - Fork 31
/
Floyd.ts
286 lines (256 loc) · 7.62 KB
/
Floyd.ts
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const INF = 2e15
/**
* 返回一个函数,该函数可以求出从`start`到`target`的最短路径长度.
* 如果不存在这样的路径,返回`-1`.
*/
function floyd(n: number, edges: [u: number, v: number, w: number][] | number[][], directed = false): (start: number, target: number) => number {
const dist: number[] = Array(n * n)
for (let i = 0; i < n * n; ++i) dist[i] = INF
for (let i = 0; i < n; ++i) dist[i * n + i] = 0
if (directed) {
edges.forEach(([u, v, w]) => {
dist[u * n + v] = Math.min(dist[u * n + v], w)
})
} else {
edges.forEach(([u, v, w]) => {
dist[u * n + v] = Math.min(dist[u * n + v], w)
dist[v * n + u] = Math.min(dist[v * n + u], w)
})
}
for (let k = 0; k < n; ++k) {
for (let i = 0; i < n; ++i) {
if (dist[i * n + k] === INF) continue
for (let j = 0; j < n; ++j) {
if (dist[k * n + j] === INF) continue
const cand = dist[i * n + k] + dist[k * n + j]
if (dist[i * n + j] > cand) {
dist[i * n + j] = cand
}
}
}
}
return (start: number, target: number) => {
const res = dist[start * n + target]
return res === INF ? -1 : res
}
}
class Floyd {
private _hasBuilt = false
private readonly _n: number
private readonly _dist: number[]
/**
* pre[a][b]表示a作为最短路起点,a->b的最短路上b的前驱节点.
*/
private readonly _pre: Uint32Array
private readonly _directedEdges: [u: number, v: number, w: number][] = []
constructor(n: number) {
const dist = Array(n * n)
const pre = new Uint32Array(n * n)
for (let i = 0; i < n; ++i) {
for (let j = 0; j < n; ++j) {
const cur = i * n + j
pre[cur] = i
dist[cur] = INF
}
}
for (let i = 0; i < n; ++i) dist[i * n + i] = 0
this._n = n
this._pre = pre
this._dist = dist
}
/**
* 添加从`u`到`v`的边权为`w`的无向边.
*/
addEdge(u: number, v: number, w: number): void {
this.addDirectedEdge(u, v, w)
this.addDirectedEdge(v, u, w)
}
/**
* 添加从`u`到`v`的边权为`w`的有向边.
*/
addDirectedEdge(u: number, v: number, w: number): void {
this._hasBuilt = false
this._directedEdges.push([u, v, w])
}
/**
* @complexity O(n^3)
*/
build(): void {
if (this._hasBuilt) return
this._hasBuilt = true
const n = this._n
this._directedEdges.forEach(([u, v, w]) => {
this._dist[u * n + v] = Math.min(this._dist[u * n + v], w)
})
for (let k = 0; k < n; ++k) {
for (let i = 0; i < n; ++i) {
if (this._dist[i * n + k] === INF) continue
for (let j = 0; j < n; ++j) {
if (this._dist[k * n + j] === INF) continue
const cand = this._dist[i * n + k] + this._dist[k * n + j]
if (this._dist[i * n + j] > cand) {
this._dist[i * n + j] = cand
this._pre[i * n + j] = this._pre[k * n + j]
}
}
}
}
}
/**
* 求出从`start`到`target`的最短路径长度,如果不存在这样的路径,返回-1.
* @complexity O(1)
*/
dist(start: number, target: number): number {
if (!this._hasBuilt) this.build()
const res = this._dist[start * this._n + target]
return res === INF ? -1 : res
}
/**
* 求出从`start`到`target`的最短路径.如果不存在这样的路径,返回空数组.
*/
getPath(start: number, target: number): number[] {
if (!this._hasBuilt) this.build()
if (this.dist(start, target) === -1) return []
let cur = target
const path = [target]
while (cur !== start) {
cur = this._pre[start * this._n + cur]
path.push(cur)
}
return path.reverse()
}
/**
* 判断是否存在负环.
* @complexity O(n)
*/
hasNegativeCycle(): boolean {
if (!this._hasBuilt) this.build()
const n = this._n
for (let i = 0; i < n; ++i) {
if (this._dist[i * n + i] < 0) return true
}
return false
}
}
/**
* 动态Floyd算法,支持向图中添加边.
*/
class FloydDynamic {
private _hasBuilt = false
private readonly _n: number
private readonly _dist: number[]
private readonly _directedEdges: [u: number, v: number, w: number][] = []
constructor(n: number) {
const dist = Array(n * n)
for (let i = 0; i < n * n; ++i) dist[i] = INF
for (let i = 0; i < n; ++i) dist[i * n + i] = 0
this._n = n
this._dist = dist
}
/**
* 添加从`u`到`v`的边权为`w`的无向边.
*/
addEdge(u: number, v: number, w: number): void {
this.addDirectedEdge(u, v, w)
this.addDirectedEdge(v, u, w)
}
/**
* 添加从`u`到`v`的边权为`w`的有向边.
*/
addDirectedEdge(u: number, v: number, w: number): void {
this._hasBuilt = false
this._directedEdges.push([u, v, w])
}
/**
* @complexity O(n^3)
*/
build(): void {
if (this._hasBuilt) return
this._hasBuilt = true
const n = this._n
this._directedEdges.forEach(([u, v, w]) => {
this._dist[u * n + v] = Math.min(this._dist[u * n + v], w)
})
for (let k = 0; k < n; ++k) {
for (let i = 0; i < n; ++i) {
if (this._dist[i * n + k] === INF) continue
for (let j = 0; j < n; ++j) {
if (this._dist[k * n + j] === INF) continue
const cand = this._dist[i * n + k] + this._dist[k * n + j]
if (this._dist[i * n + j] > cand) {
this._dist[i * n + j] = cand
}
}
}
}
}
/**
* 向边集中添加一条边.
* @param directed 是否为有向边
* @complexity O(n^2)
* 加边时,枚举每个点对,根据是否经过edge来更新最短路.
*/
updateEdge(u: number, v: number, w: number, directed = true): void {
if (!this._hasBuilt) this.build()
if (directed) {
this._updateDirectedEdge(u, v, w)
} else {
this._updateEdge(u, v, w)
}
}
/**
* 求出从`start`到`target`的最短路径长度,如果不存在这样的路径,返回-1.
* @complexity O(1)
*/
dist(start: number, target: number): number {
if (!this._hasBuilt) this.build()
const res = this._dist[start * this._n + target]
return res === INF ? -1 : res
}
/**
* 判断是否存在负环.
* @complexity O(n)
*/
hasNegativeCycle(): boolean {
if (!this._hasBuilt) this.build()
const n = this._n
for (let i = 0; i < n; ++i) {
if (this._dist[i * n + i] < 0) return true
}
return false
}
private _updateDirectedEdge(u: number, v: number, w: number): void {
const n = this._n
for (let i = 0; i < n; ++i) {
for (let j = 0; j < n; ++j) {
const cand = this._dist[i * n + u] + w + this._dist[v * n + j]
if (this._dist[i * n + j] > cand) {
this._dist[i * n + j] = cand
}
}
}
}
private _updateEdge(u: number, v: number, w: number): void {
const n = this._n
for (let i = 0; i < n; ++i) {
for (let j = 0; j < n; ++j) {
const cand1 = this._dist[i * n + u] + w + this._dist[v * n + j]
if (this._dist[i * n + j] > cand1) {
this._dist[i * n + j] = cand1
}
const cand2 = this._dist[i * n + v] + w + this._dist[u * n + j]
if (this._dist[i * n + j] > cand2) {
this._dist[i * n + j] = cand2
}
}
}
}
}
export { Floyd, FloydDynamic, floyd }
if (require.main === module) {
const n = 800
console.time('floyd')
const f = new FloydDynamic(n)
f.build()
console.timeEnd('floyd')
}