-
Notifications
You must be signed in to change notification settings - Fork 1
/
depth-first-order.ts
52 lines (46 loc) · 1.33 KB
/
depth-first-order.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
import Queue from '../1-3/node-queue'
import Stack from '../1-3/node-stack'
import Digraph from './digraph'
import EdgeWeightedDigraph from './edge-weighted-digraph'
/**
* 有向图中基于深度优先搜索的顶点排序
*/
export default class DepthFirshOrder {
private marked: boolean[]
private pre: Queue<number> // 所有顶点的前序排列
private post: Queue<number> // 所有顶点的后序排列
private reversePost: Stack<number> // 所有顶点的逆后序排列
constructor(G: EdgeWeightedDigraph)
constructor(G: Digraph)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(G: any) {
this.marked = []
this.pre = new Queue<number>()
this.post = new Queue<number>()
this.reversePost = new Stack<number>()
for (let v = 0; v < G.countV(); v++) {
if (!this.marked[v]) this.dfs(G, v)
}
}
private dfs(G: Digraph | EdgeWeightedDigraph, v: number): void {
this.pre.enqueue(v)
this.marked[v] = true
const adj = G.getAdj(v)
while (adj.hasNext()) {
const e = adj.next()
const w = typeof e === 'number' ? e : e.to()
if (!this.marked[w]) this.dfs(G, w)
}
this.post.enqueue(v)
this.reversePost.push(v)
}
getPre() {
return this.pre
}
getPost() {
return this.post
}
getReversePost() {
return this.reversePost
}
}