-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUndirectedGraph.js
More file actions
392 lines (347 loc) · 10.3 KB
/
UndirectedGraph.js
File metadata and controls
392 lines (347 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
/**
* Copyright (c) 2020
* License MIT
*
* implement of undirected graph
*
* @summary undirected graph
* @author CoderMonkey <maonianyou@gmail.com>
*
* Created at : 2020-01-24 15:17:02
* Last modified : 2020-01-27 20:58:10
*/
import {
GraphBase,
VisitStatus,
} from './GraphBase'
import {
isFunction,
} from '../common/toollib'
import {
Stack
} from '../Stack'
import {
Queue
} from '../Queue/Queue.js'
/**
* @description
* 无向图
* 使用邻接多重表实现
*
* @export UndirectedGraph
* @class UndirectedGraph
*/
export class UndirectedGraph extends GraphBase {
constructor(customizedComparer = null) {
super(customizedComparer)
/**
* 用字符串表示出该图的信息
* 显示所有的邻接点
*/
this.toString = function () {
let str = ''
this.__vertexes.forEach(v => {
str += `${v.data.toString()} -> [`
let arc = v.first
while (arc) {
if (v === arc.vertex1) {
str += `${arc.vertex2.data.toString()},`
arc = arc.orderNext
} else {
// v === arc.vertex2
str += `${arc.vertex1.data.toString()},`
arc = arc.reverseNext
}
}
str += ']\r\n'
})
return str
}
}
/**
* @description 添加顶点
*
* @param {*} data
* @memberof UndirectedGraph
*/
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 {*} data1
* @param {*} data2
* @param {*} info
* @memberof UndirectedGraph
*/
setArc(data1, data2, info) {
let v1 = this.__findVertex(data1)
let v2 = this.__findVertex(data2)
let arc
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)
this.__addArc(v1, arc)
this.__addArc(v2, arc)
}
// 对于已存在的边,更新边(权重等)数据
else {
arc.info = info
}
}
/**
* @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.first
while (arc) {
let otherVertex = arc.otherVertex(vertex)
// 如果另一端顶点未被发现或探索,则加入队列并标记
if (map.get(otherVertex) === VisitStatus.INIT) {
queue.enqueue(otherVertex)
map.set(otherVertex, VisitStatus.DISCOVERED)
}
arc = arc.nextArc(vertex)
}
// 下一层顶点
vertex = queue.dequeue()
}
}
/**
* @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()
// 作为入口的首个顶点直接探索(回调)并标记
let stop = callback(vertex.data)
if (stop) return
map.set(vertex, VisitStatus.VISITED)
// 回溯用
stack.push(vertex);
while (vertex) {
let arc = vertex.first
while (arc) {
// 根据深度优先原则,探索另一端顶点
let otherVertex = arc.otherVertex(vertex)
// 如果未探索,则探索(回调)并标记
if (map.get(otherVertex) !== VisitStatus.VISITED) {
stop = callback(otherVertex.data)
if (stop) return
map.set(otherVertex, VisitStatus.VISITED)
// 记录该顶点以备回溯
vertex = otherVertex
arc = otherVertex.first
stack.push(vertex)
}
// 顶点已被发现或探索,寻找其它边
else {
arc = arc.nextArc(vertex)
}
}
// 回溯
vertex = stack.pop()
}
}
/**
* @description 查找边
*
* @param {VertexNode} v1
* @param {VertexNode} v2
* @returns Arc | null
* @memberof UndirectedGraph
*/
__findArc(v1, v2) {
let arc = v1.first
while (arc) {
if (arc.hasVertex(v2)) return arc
arc = arc.nextArc(v1)
}
return null
}
/**
* @description 内部函数,给顶点添加边
*
* @param {VertexNode} v 顶点
* @param {Arc} arc 边
* @memberof UndirectedGraph
*/
__addArc(v, arc) {
if (arc && !arc.hasVertex(v)) throw new Error(`canot add arc:${arc.toString()} to vertex:${v.toString()}`)
let tempArc = v.first
let pre = tempArc
if (!tempArc) {
v.first = arc
} else {
while (tempArc) {
pre = tempArc
tempArc = tempArc.nextArc(v)
}
if (pre.vertex1 === v) {
pre.orderNext = arc
} else {
pre.reverseNext = arc
}
}
}
}
/**
* @description 无向图内部用顶点类
* @class VertexNode
*/
class VertexNode {
constructor(data) {
this.data = data // * data
this.first = null // Arc
}
toString() {
return `${this.data.toString()}`
}
}
/**
* @description 无向图内部用边类
* @class Arc
*/
class Arc {
constructor(v1, v2, info) {
this.vertex1 = v1 // VertexNode
this.vertex2 = v2 // VertexNode
this.orderNext = null // Arc
this.reverseNext = null // Arc
this.info = info // *
}
/**
* @description 指定顶点的下一条邻接边
*
* @param {VertexNode} v 顶点
* @returns Arc | null
* @memberof Arc
*/
nextArc(v) {
if (v === this.vertex1) {
return this.orderNext
} else if (v === this.vertex2) {
return this.reverseNext
}
return null
}
/**
* @description 当前边是否包含指定顶点
*
* @param {VertexNode} v 顶点
* @returns true | false
* @memberof Arc
*/
hasVertex(v) {
return (v === this.vertex1 || v === this.vertex2)
}
/**
* @description 指定顶点所在当前邻接边的另一端
*
* @param {VertexNode} v 顶点
* @returns VertexNode | null
* @memberof Arc
*/
otherVertex(v) {
if (v === this.vertex1) {
return this.vertex2
} else if (v === this.vertex2) {
return this.vertex1
}
return null
}
toString() {
return `
{
v1: ${this.vertex1.toString()},
v2: ${this.vertex2.toString()},
info: ${this.info ? this.info.toString() : null},
}
`
}
}