-
Notifications
You must be signed in to change notification settings - Fork 1
/
digraph.ts
57 lines (49 loc) · 999 Bytes
/
digraph.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
import Bag from '@/algs4/1-3/bag'
/**
* 有向图
* 注释参考无向图
*/
export default class Digraph {
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)
this.E++
}
getAdj(v: number) {
return this.adj[v]
}
reverse(): Digraph {
const R = new Digraph(this.V)
for (let v = 0; v < this.V; v++) {
const adj = this.getAdj(v)
while (adj.hasNext()) {
const w = adj.next()
R.addEdge(w, v)
}
}
return R
}
}