-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventualSafeNodes.ts
70 lines (55 loc) · 1.63 KB
/
eventualSafeNodes.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
58
59
60
61
62
63
64
65
66
67
68
69
70
// <Recursion, DFS, Topological Sorting>
// Time: O(n + m)
// Space: O(n + m)
// n for the number of nodes
// m for the number of edges
class MyGraph {
private n: number
private graph: number[][]
private reversedGraph: number[][]
private inDegree: number[]
public constructor(graph: number[][]) {
this.n = graph.length
this.graph = graph
this.reversedGraph = Array.from({ length: this.n }, () => [])
this.inDegree = new Array(this.n).fill(0)
this.init()
}
private init() {
for (let i = 0; i < this.n; ++i) {
for (const j of this.graph[i]) {
this.reversedGraph[j].push(i)
}
this.inDegree[i] = this.graph[i].length
}
}
// 1. dfs
private isSafe(i: number): boolean {
// visiting or (visited and no cycle)
if (this.inDegree[i] !== this.graph[i].length) {
return this.inDegree[i] === -Infinity
}
--this.inDegree[i]
for (const neighbor of this.graph[i]) {
if (!this.isSafe(neighbor)) {
return false
}
}
this.inDegree[i] = -Infinity // mark as visited and no cycle
return true
}
public iterate(): number[] {
const safeNodes: number[] = []
for (let i = 0; i < this.n; ++i) {
if (this.isSafe(i)) {
safeNodes.push(i)
}
}
return safeNodes
}
}
function eventualSafeNodes(graph: number[][]): number[] {
const myGraph = new MyGraph(graph)
return myGraph.iterate()
}
export { eventualSafeNodes }