-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path802.evantualSafeStates.adjMat.dfs.recursive.java
73 lines (65 loc) · 3.01 KB
/
802.evantualSafeStates.adjMat.dfs.recursive.java
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Solution {
public List<Integer> eventualSafeNodes(int[][] graph) {
//Naive way is we write code to detect cycle from node-a.
// if it says that cycle exists then we don't add into cycle.
// we reset visited & samePath array & try with next node.
boolean visited[]=new boolean[graph.length]; // to check if node is visited or not
boolean samePath[]=new boolean[graph.length]; // to check cycle
boolean safeNodes[]=new boolean[graph.length]; // list of node which are safe.
List<Integer> ans=new ArrayList<>();
for(int i=0;i<graph.length;i++){
if(graph[i].length==0){
safeNodes[i]=true; // if no outgoing edge means it's terminal & also cycle cannot exist.
// add it to safeNode list.
continue;
}
if(visited[i]==false){
// if node is not visited.
visited[i]=true; //mark as visited
samePath[i]=true; // since we are exploring i, we mark take i in path.
dfs(i,graph,visited,samePath,safeNodes);
}
}
for(int i=0;i<graph.length;i++){
if(safeNodes[i]==true){
ans.add(i);
}
}
return ans;
}
boolean dfs(int currNode,int [][]graph,boolean[] visited,boolean[]samePath,boolean[]safeNodes){
// samePath will have node set to true if cycle exists. we don't set to false even if have started exploring other component.
for(Integer adjNodes:graph[currNode]){
if(visited[adjNodes]==false){
// if adjacent node is not visited.
visited[adjNodes]=true;
samePath[adjNodes]=true;
if( dfs(adjNodes,graph,visited,samePath,safeNodes)) {
//samePath[adjNodes]=false;
// NOTE THAT we do not mark adjNode in samePath false here.
// we are here because cycle exists
return true;
}
// after exploring current node's adj we didn't find cycle. (we might have found safe states.)
// we remove current adj node from path.
samePath[adjNodes]=false;
}else{
if(samePath[adjNodes]) {
// if node is visited & in the path we return true.
//samePath[adjNodes]=false;
// NOTE THAT we do not mark adjNode in samePath false here.
// we are here because cycle exists
return true;
}
}
}
// we are returning false because we haven't detected cycle.
// we are storing node as safe because even by exploring all child (adj) of current node we haven't found cycle.
safeNodes[currNode]=true;
return false;
}
}
// we solved with cycle detection technique. we can also try topo sort