-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFindEventualSafeStates802.kt
94 lines (75 loc) · 2.35 KB
/
FindEventualSafeStates802.kt
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package medium
import java.util.*
import kotlin.collections.HashMap
object FindEventualSafeStates802 {
fun eventualSafeNodes(graph: Array<IntArray>): List<Int> {
val n = graph.size
val indegree = IntArray(n)
val adj = mutableListOf<MutableList<Int>>()
for (i in 0 until n) {
adj.add(mutableListOf<Int>())
}
for (i in 0 until n) {
for (node in graph[i]) {
adj[node].add(i)
indegree[i]++
}
}
val queue: Queue<Int> = LinkedList<Int>()
// push all the nodes with indegree zero in the queue
for (i in 0 until n) {
if (indegree[i] == 0) {
queue.add(i)
}
}
val safe = BooleanArray(n)
while (queue.isNotEmpty()) {
val node = queue.poll()
safe[node] = true
for (neighbor in adj[node]) {
// delete the edge "node -> neighbor|
indegree[neighbor]--
if (indegree[neighbor] == 0)
queue.add(neighbor)
}
}
val safeNodes = mutableListOf<Int>()
for (i in 0 until n) {
if (safe[i]) {
safeNodes.add(i)
}
}
return safeNodes
}
fun eventualSafeNodesSolution2(graph: Array<IntArray>): List<Int> {
val n = graph.size
val visit = BooleanArray(n)
val inStack = BooleanArray(n)
for (i in 0 until n) {
dfs(node = i, adj = graph, visit = visit, inStack = inStack)
}
val safeNodesResult = mutableListOf<Int>()
for (i in 0 until n) {
if (inStack[i].not())
safeNodesResult.add(i)
}
return safeNodesResult
}
}
fun dfs(node: Int, adj: Array<IntArray>, visit: BooleanArray, inStack: BooleanArray): Boolean {
// If the node is already in the stack, then we have a cycle
if (inStack[node])
return true
if (visit[node])
return false
// Mark this current node as visited and part of the current stack of recursion
visit[node] = true
inStack[node] = true
for (neighbor in adj[node]) {
if (dfs(neighbor, adj, visit, inStack))
return true
}
// Remove the node from the stack
inStack[node] = false
return false
}