|
| 1 | +## 802. Find Eventual Safe States |
| 2 | + |
| 3 | +### Question: |
| 4 | +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. |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +Which nodes are eventually safe? Return them as an array in sorted order. |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +``` |
| 13 | +Example: |
| 14 | +Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]] |
| 15 | +Output: [2,4,5,6] |
| 16 | +Here is a diagram of the above graph. |
| 17 | +``` |
| 18 | + |
| 19 | +Illustration of graph |
| 20 | + |
| 21 | + |
| 22 | +Note: |
| 23 | +* graph will have length at most 10000. |
| 24 | +* The number of edges in the graph will not exceed 32000. |
| 25 | +* Each graph[i] will be a sorted list of different integers, chosen within the range [0, graph.length - 1]. |
| 26 | + |
| 27 | +### Solution: |
| 28 | +* Method 1: dfs + graph |
| 29 | + * This question is about to find the nodes who is not in a cycle, which means itself and its children are not in a cycle. |
| 30 | + * We take 4 states to record a node's status |
| 31 | + * {UNKNOW, VISIT, SAFE, UNSAFE} |
| 32 | + * UNKNOWN: current node is not visited. Initialized status. |
| 33 | + * VISIT: current node is in the process of dfs. UNKNOW -> VISIT |
| 34 | + * SAFE: current node is safe. All childs are SAFE -> current is SAFE |
| 35 | + * UNSAFE: current node is unsafe. VISIT -> UNSAFE |
| 36 | + ```Java |
| 37 | + class Solution { |
| 38 | + private static final int UNKNOWN = 0; |
| 39 | + private static final int VISIT = 1; |
| 40 | + private static final int SAFE = 2; |
| 41 | + private static final int UNSAFE = 3; |
| 42 | + private int[] state; |
| 43 | + private int len; |
| 44 | + public List<Integer> eventualSafeNodes(int[][] graph) { |
| 45 | + List<Integer> result = new ArrayList<>(); |
| 46 | + if(graph == null || graph.length == 0) return result; |
| 47 | + len = graph.length; |
| 48 | + state = new int[len]; |
| 49 | + for(int i = 0; i < len; i++){ |
| 50 | + if(dfs(graph, i) == SAFE) |
| 51 | + result.add(i); |
| 52 | + } |
| 53 | + return result; |
| 54 | + } |
| 55 | + private int dfs(int[][] graph, int cur){ |
| 56 | + if(state[cur] == VISIT){ |
| 57 | + return state[cur] = UNSAFE; |
| 58 | + }else if(state[cur] != UNKNOWN){ |
| 59 | + return state[cur]; |
| 60 | + } |
| 61 | + state[cur] = VISIT; |
| 62 | + for(int i = 0; i < graph[cur].length; i++){ |
| 63 | + if(dfs(graph, graph[cur][i]) == UNSAFE){ |
| 64 | + return state[cur] = UNSAFE; |
| 65 | + } |
| 66 | + } |
| 67 | + return state[cur] = SAFE; |
| 68 | + } |
| 69 | + } |
| 70 | + ``` |
| 71 | + |
| 72 | +### Reference |
| 73 | +1. [花花酱 LeetCode 802. Find Eventual Safe States](http://zxi.mytechroad.com/blog/graph/leetcode-802-find-eventual-safe-states/) |
0 commit comments