-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.ts
50 lines (42 loc) · 1.13 KB
/
graph.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
import Bag from '@/algs4/1-3/bag'
interface IGraph {
countV(): number
countE(): number
addEdge(v: number, w: number): void
getAdj(v: number): Bag<number>
}
export default class Graph implements IGraph {
private V: number // 顶点数目
private E: number // 边的数目
private adj: Bag<number>[] // 邻接表
constructor(V: number)
constructor(V: number, readIn: number[])
constructor(V: never, readIn?: never[]) {
this.V = V
this.E = 0
this.adj = [] // 创建邻接表
for (let v = 0; v < V; v++) {
// 将所有链表初始化为空
this.adj[v] = new Bag<number>()
}
while (readIn && readIn.length) {
const v = readIn.shift() // 读取一个顶点
const w = readIn.shift() // 读取另一个顶点
this.addEdge(v, w) // 添加一条连接它们的边
}
}
countV() {
return this.V
}
countE() {
return this.E
}
addEdge(v: number, w: number) {
this.adj[v].add(w) // 将 w 添加到 v 的链表中
this.adj[w].add(v) // 将 v 添加到 w 的链表中number[]
this.E++
}
getAdj(v: number): Bag<number> {
return this.adj[v]
}
}