802.Find-Eventual-Safe-States
Last updated
Was this helpful?
Last updated
Was this helpful?
In a directed graph, we start at some node and every turn, walk along a directed edge of the graph. If we reach a node that is terminal (that is, it has no outgoing directed edges), we stop.
Now, say our starting node is eventually safe if and only if we must eventually walk to a terminal node. More specifically, there exists a natural number K so that for any choice of where to walk, we must have stopped at a terminal node in less than K steps.
Which nodes are eventually safe? Return them as an array in sorted order.
The directed graph has N nodes with labels 0, 1, ..., N-1, where N is the length of graph. The graph is given in the following form: graph[i] is a list of labels j such that (i, j) is a directed edge of the graph.
Example:
Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]]
Output: [2,4,5,6]
Here is a diagram of the above graph.
Illustration of graph
Note:
graph will have length at most 10000.
The number of edges in the graph will not exceed 32000.
Each graph[i] will be a sorted list of different integers, chosen within the range [0, graph.length - 1].
Time: O(N+E) && Space: O(N)
the crux of the problem is whether you reach a cycle or not.
class Solution {
public List<Integer> eventualSafeNodes(int[][] G) {
int N = G.length;
boolean[] safe = new boolean[N];
List<Set<Integer>> graph = new ArrayList();
List<Set<Integer>> rgraph = new ArrayList();
for (int i = 0; i < N; ++i) {
graph.add(new HashSet());
rgraph.add(new HashSet());
}
Queue<Integer> queue = new LinkedList();
for (int i = 0; i < N; ++i) {
if (G[i].length == 0)
queue.offer(i);
for (int j: G[i]) {
graph.get(i).add(j);
rgraph.get(j).add(i);
}
}
while (!queue.isEmpty()) {
int j = queue.poll();
safe[j] = true;
for (int i: rgraph.get(j)) {
graph.get(i).remove(j);
if (graph.get(i).isEmpty())
queue.offer(i);
}
}
List<Integer> ans = new ArrayList();
for (int i = 0; i < N; ++i) if (safe[i])
ans.add(i);
return ans;
}
}
We mark a node gray = 1 on entry, and black = 2 on exit. If we see a gray node during our DFS, it must be part of a cycle. In a naive view, we'll clear the colors between each search.
class Solution {
public List<Integer> eventualSafeNodes(int[][] graph) {
int N = graph.length;
int[] color = new int[N];
List<Integer> ans = new ArrayList();
for (int i = 0; i < N; i++) {
if (dfs(i, color, graph)) {
ans.add(i);
}
}
return ans;
}
// colors: WHITE 0, GRAY 1, BLACK 2;
private boolean dfs(int node, int[] color, int[][] graph) {
if (color[node] > 0) {
return color[node] == 2; // cycle: color == 1
}
color[node] = 1; // mark entry color = 1
for (int nei: graph[node]) {
if (color[node] == 2) {
continue;
}
if (color[nei] == 1 || !dfs(nei, color, graph)) { // cycle: color == 1
return false;
}
}
color[node] = 2;
return true;
}
}
class Solution {
public List<Integer> eventualSafeNodes(int[][] graph) {
int N = graph.length;
int[] degree = new int[N];
Map<Integer, Set<Integer>> neighbors = new HashMap<>(); // reverse graph
for (int i = 0; i < graph.length; i++) {
for (int neighbor: graph[i]) {
if (!neighbors.containsKey(neighbor)) {
neighbors.put(neighbor, new HashSet<Integer>());
}
neighbors.get(neighbor).add(i);
degree[i]++;
}
}
Set<Integer> res = new HashSet<>();
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < N; i++) {
if (degree[i] == 0) {
queue.add(i);
}
}
while (!queue.isEmpty()) {
int v = queue.poll();
res.add(v);
if (neighbors.containsKey(v)) {
for (int neighbor: neighbors.get(v)) {
degree[neighbor]--;
if (degree[neighbor] == 0) {
queue.offer(neighbor);
}
}
}
}
List<Integer> list = new ArrayList<Integer>(res);
Collections.sort(list);
return list;
}
}