-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDirectedCycle.js
More file actions
38 lines (37 loc) · 901 Bytes
/
DirectedCycle.js
File metadata and controls
38 lines (37 loc) · 901 Bytes
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
class DirectedCycle {
constructor(G) {
this.marked = new Array(G.V).fill(false)
this.onStack = new Array(G.V).fill(false)
this.edgeTo = new Array(G.V).fill(0)
this.cycle = null
for (let v = 0; v < G.V; v++) {
if (!this.marked[v] && this.cycle === null) {
this.dfs(G, v)
}
}
}
dfs(G, v) {
this.onStack[v] = true
this.marked[v] = true
for (let w of G.adj[v]) {
if (this.cycle !== null) {
return
} else if (!this.marked[w]) {
this.edgeTo[w] = v
this.dfs(G, w)
} else if (this.onStack[w]) {
this.cycle = []
for (let x = v; x != w; x = this.edgeTo[x]) {
this.cycle.unshift(x)
}
this.cycle.unshift(w)
this.cycle.unshift(v)
}
}
this.onStack[v] = false
}
hasCycle() {
return this.cycle !== null
}
}
module.exports = DirectedCycle