-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtopological.ts
37 lines (31 loc) · 1012 Bytes
/
topological.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
import { cloneDeep } from 'lodash'
import Stack from '../1-3/node-stack'
import Digraph from './digraph'
import DirectedCycle from './directed-cycle'
import DepthFirstOrder from './depth-first-order'
import EdgeWeightedDigraph from './edge-weighted-digraph'
import EdgeWeightedDirectedCycle from './edge-weighted-directed-cycle'
/**
* 拓补排序
*/
export default class Topological {
private order: Stack<number>
// override Topological constructor
constructor(G: EdgeWeightedDigraph)
constructor(G: Digraph)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(G: any) {
const cloneG = cloneDeep(G)
const cyclefinder = G instanceof EdgeWeightedDigraph ? new EdgeWeightedDirectedCycle(G) : new DirectedCycle(G)
if (!cyclefinder.hasCycle()) {
const dfs = new DepthFirstOrder(cloneG)
this.order = dfs.getReversePost()
}
}
getOrder() {
return this.order
}
isDAG() {
return this.order !== undefined && this.order !== null
}
}