-
Notifications
You must be signed in to change notification settings - Fork 1
/
dfs.ts
46 lines (40 loc) · 991 Bytes
/
dfs.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
import Graph from './graph'
/**
* 使用深度优先搜索查找图中的路径
*
* @class DepthFirstSearch
*/
export default class DepthFirstPaths {
private marked: boolean[] // 这个顶点上调用过 dfs() 吗
private edgeTo: number[] // 从起点到一个顶点的已知路径上的最后一个顶点
private s: number // 起点
constructor(G: Graph, s: number) {
this.marked = []
this.edgeTo = []
this.s = s
this.dfs(G, s)
}
private dfs(G: Graph, v: number) {
this.marked[v] = true
const adj = G.getAdj(v)
while (adj.hasNext()) {
const w = adj.next()
if (!this.marked[w]) {
this.edgeTo[w] = v
this.dfs(G, w)
}
}
}
hasPathTo(v: number): boolean {
return this.marked[v]
}
pathTo(v: number): number[] {
if (!this.hasPathTo(v)) return null
const path = []
for (let x = v; x !== this.s; x = this.edgeTo[x]) {
path.push(x)
}
path.push(this.s)
return path
}
}