-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDirectedGraph.js
More file actions
393 lines (341 loc) · 10.3 KB
/
DirectedGraph.js
File metadata and controls
393 lines (341 loc) · 10.3 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
* Copyright (c) 2020
*
* MIT
*
* implement of directed graph
*
* @summary directed graph
* @author CoderMonkey <maonianyou@gmail.com>
*
* Created at : 2020-01-22 21:35:06
* Last modified : 2020-01-27 20:58:02
*/
import {
isFunction,
} from '../common/toollib'
import {
Stack
} from '../Stack.js'
import {
GraphBase,
VisitStatus,
} from './GraphBase'
import { Queue } from '../Queue/Queue'
/**
* @description
* 有向图
* 使用十字链表实现
*
* @export
* @class DirectedGraph
*/
export class DirectedGraph extends GraphBase {
constructor(customizedComparer = null) {
super(customizedComparer)
this.toString = function () {
let str = ''
this.__vertexes.forEach(v => {
str += `${v.data.toString()} ->\r\n Out: [`
let arcOut = v.firstOut
while (arcOut) {
str += `${arcOut.end.data.toString()},`
arcOut = arcOut.nextOut
}
str += '],\r\n In: ['
let arcIn = v.firstIn
while (arcIn) {
str += `${arcIn.start.data.toString()}`,
arcIn= arcIn.nextIn
}
str += ']\r\n'
})
return str
}
}
/**
* @description 添加顶点
*
* @param {*} data 顶点数据
* @returns 添加结果:成功true/失败false
* @memberof DirectedGraph
*/
addVertex(data) {
if (data == null) throw new Error(`parameter error: data is ${data}`)
if (this.hasVertex(data)) return false
let v = new VertexNode(data)
this.__vertexes.push(v)
return true
}
/**
* @description 添加/更新边
*
* @param {*} o1 顶点数据1
* @param {*} o2 顶点数据2
* @param {*} [info=null] 边的数据
* @returns 添加/更新结果:成功true/失败fasle
* @memberof DirectedGraph
*/
setArc(o1, o2, info = null) {
let v1 = this.__findVertex(o1)
let v2 = this.__findVertex(o2)
let arc = null
// 检查该边是否存在
if (v1 && v2) {
arc = this.__findArc(v1, v2)
} else {
if (!v1) {
v1 = new VertexNode(data1)
this.__vertexes.push(v1)
}
if (!v2) {
v2 = new VertexNode(data2)
this.__vertexes.push(v2)
}
}
// 不存在:添加边
if (!arc) {
arc = new Arc(v1, v2, info)
// 从 v1 到 v2,画一条边线(v1 的出边,v2 的入边)
this.__addOutArc(v1, arc)
this.__addInArc(v2, arc)
}
// 存在:更新边(权重值等信息)
else {
arc.info = info
}
return true
}
/**
* @description 广度优先遍历
*
* @param {*} start 遍历的起始顶点
* @param {function} callback 回调函数
* @memberof UndirectedGraph
*/
bfsTraverse(start, callback) {
if (this.vertexCount === 0) return
let vertex = null
// 参数检查:
// --1. 两个参数的时候:指定起点和遍历的回调函数
if (start && callback) {
if (!isFunction(callback)) throw new Error(`${callback} is not a function.`)
vertex = this.__findVertex(start)
if (vertex === undefined) throw new Error(`vertex of ${start} doesnot exist.`)
}
// --2.1 一个参数的时候,仅指定遍历的回调函数
else if (isFunction(start) && !callback) {
callback = start
start = null
vertex = this.__vertexes[0]
}
// --2.2 两个参数的时候,仅指定遍历的回调函数
else if (start == null && isFunction(callback)) {
vertex = this.__vertexes[0]
}
// --3. 参数错误的其它情况
else {
throw new Error(`parameter error: need [vStart, callback] or [callback].\r\nreceived: ${start}, ${callback}`)
}
// 广度优先:使用队列
let queue = new Queue()
// 初始化探索状态
let map = this.__initVisitStatus()
while (vertex) {
// 如果当前顶点未被探索,则探索(回调访问)
if (map.get(vertex) !== VisitStatus.VISITED) {
let stop = callback(vertex.data)
if (stop) return
map.set(vertex, VisitStatus.VISITED)
}
let arc = vertex.firstOut
while (arc) {
let vEnd = arc.end
// 如果另一端顶点未被发现或探索,则加入队列并标记
if (map.get(vEnd) === VisitStatus.INIT) {
queue.enqueue(vEnd)
map.set(vEnd, VisitStatus.DISCOVERED)
}
// 下一组出边
arc = arc.nextOut
}
// 下一层顶点
vertex = (queue.dequeue() || this.__getNextVertex(map))
}
}
/**
* @description 深度优先遍历
*
* @param {*} start 遍历的起始顶点
* @param {function} callback 回调函数
* @memberof UndirectedGraph
*/
dfsTraverse(start, callback) {
if (this.vertexCount === 0) return
let vertex = null
// 参数检查:
// --1. 两个参数的时候:指定起点和遍历的回调函数
if (start && callback) {
if (!isFunction(callback)) throw new Error(`${callback} is not a function.`)
vertex = this.__findVertex(start)
if (vertex === undefined) throw new Error(`vertex of ${start} doesnot exist.`)
}
// --2.1 一个参数的时候,仅指定遍历的回调函数
else if (isFunction(start) && !callback) {
callback = start
start = null
vertex = this.__vertexes[0]
}
// --2.2 两个参数的时候,仅指定遍历的回调函数
else if (start == null && isFunction(callback)) {
vertex = this.__vertexes[0]
}
// --3. 参数错误的其它情况
else {
throw new Error(`parameter error: need [vStart, callback] or [callback].\r\nreceived: ${start}, ${callback}`)
}
// 深度优先:使用栈
let stack = new Stack()
// 初始化探索状态
let map = this.__initVisitStatus()
while (vertex) {
// 如果未探索该顶点,则探索(回调)并标记
if (map.get(vertex) !== VisitStatus.VISITED) {
let stop = callback(vertex.data)
if (stop) return
map.set(vertex, VisitStatus.VISITED)
stack.push(vertex)
}
let arc = vertex.firstOut
while (arc) {
let vNext = arc.end
// 如果未探索,则探索(回调)并标记
if (map.get(vNext) !== VisitStatus.VISITED) {
callback(vNext.data)
map.set(vNext, VisitStatus.VISITED)
// 记录该顶点以备回溯
stack.push(vNext)
arc = vNext.firstOut
}
// 回溯后探索其它分支
else {
arc = arc.nextOut
}
}
// 回溯或跳转顶点
vertex = (stack.pop() || this.__getNextVertex(map))
}
}
/**
* @description 给顶点添加入边
*
* @param {VertexNode} v
* @param {Arc} arc
*/
__addInArc(v, arc) {
let adjArc = v.firstIn
// 没有入边
if (adjArc === null) {
v.firstIn = arc
}
// 已有入边
else {
while (adjArc.nextIn) {
adjArc = adjArc.nextIn
}
adjArc.nextIn = arc
}
}
/**
* @description 给顶点添加出边
*
* @param {VertexNode} v
* @param {Arc} arc
*/
__addOutArc(v, arc) {
let adjArc = v.firstOut
// 没有出边
if (adjArc === null) {
v.firstOut = arc
}
// 已有出边
else {
while (adjArc.nextOut) {
adjArc = adjArc.nextOut
}
adjArc.nextOut = arc
}
}
/**
* @description 查找边
*
* @param {VertexNode} v1 顶点1
* @param {VertexNode} v2 顶点2
* @returns Arc | undefined
* @memberof DirectedGraph
*/
__findArc(v1, v2) {
let arc = v1.firstOut
while (arc) {
if (v2 === arc.end) return arc
arc = arc.nextOut
}
}
/**
* @description
* 如果指定的起始顶点没有出边,
* 则无法进行后面其它顶点的遍历,
* 这里找出下一个未访问过的顶点,
* 继续循环遍历(广度/深度优先)
*
* @param {Map<VertexNode, VisitStatus>} map
* @memberof DirectedGraph
*/
__getNextVertex(map) {
let list = Array.from(map)
let statusKV = list.find(kv => {
return kv[1] !== VisitStatus.VISITED
})
let vertex = statusKV && statusKV[0]
return vertex
}
}
/**
* @description 有向图内部用顶点类
*
* @class VertexNode
*/
class VertexNode {
constructor(data) {
if (data == null) throw new Error(`vertex cannot be null`)
this.data = data
this.firstIn = null // Arc
this.firstOut = null // Arc
}
toString() {
return `${this.data.toString()}`
}
}
/**
* @description 有向图内部用边类
*
* @class Arc
*/
class Arc {
constructor(start, end, info = null) {
this.start = start // VertexNode
this.end = end // VertexNode
this.nextIn = null // Arc
this.nextOut = null // Arc
this.info = info // Weight info etc.
}
toString() {
return `
{
start: ${this.start.toString()},
end: ${this.end.toString()},
info: ${this.info ? this.info.toString() : null},
}
`
}
}