|
| 1 | +package LeetCodeJava.DFS; |
| 2 | + |
| 3 | +// https://leetcode.com/problems/find-eventual-safe-states/description/ |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.LinkedList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Queue; |
| 9 | + |
| 10 | +/** |
| 11 | + * 802. Find Eventual Safe States |
| 12 | + * Medium |
| 13 | + * Topics |
| 14 | + * Companies |
| 15 | + * There is a directed graph of n nodes with each node labeled from 0 to n - 1. The graph is represented by a 0-indexed 2D integer array graph where graph[i] is an integer array of nodes adjacent to node i, meaning there is an edge from node i to each node in graph[i]. |
| 16 | + * |
| 17 | + * A node is a terminal node if there are no outgoing edges. A node is a safe node if every possible path starting from that node leads to a terminal node (or another safe node). |
| 18 | + * |
| 19 | + * Return an array containing all the safe nodes of the graph. The answer should be sorted in ascending order. |
| 20 | + * |
| 21 | + * |
| 22 | + * |
| 23 | + * Example 1: |
| 24 | + * |
| 25 | + * Illustration of graph |
| 26 | + * Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]] |
| 27 | + * Output: [2,4,5,6] |
| 28 | + * Explanation: The given graph is shown above. |
| 29 | + * Nodes 5 and 6 are terminal nodes as there are no outgoing edges from either of them. |
| 30 | + * Every path starting at nodes 2, 4, 5, and 6 all lead to either node 5 or 6. |
| 31 | + * Example 2: |
| 32 | + * |
| 33 | + * Input: graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]] |
| 34 | + * Output: [4] |
| 35 | + * Explanation: |
| 36 | + * Only node 4 is a terminal node, and every path starting at node 4 leads to node 4. |
| 37 | + * |
| 38 | + * |
| 39 | + * Constraints: |
| 40 | + * |
| 41 | + * n == graph.length |
| 42 | + * 1 <= n <= 104 |
| 43 | + * 0 <= graph[i].length <= n |
| 44 | + * 0 <= graph[i][j] <= n - 1 |
| 45 | + * graph[i] is sorted in a strictly increasing order. |
| 46 | + * The graph may contain self-loops. |
| 47 | + * The number of edges in the graph will be in the range [1, 4 * 104]. |
| 48 | + * Seen this question in a real interview before? |
| 49 | + * 1/5 |
| 50 | + * Yes |
| 51 | + * No |
| 52 | + * Accepted |
| 53 | + * 292.9K |
| 54 | + * Submissions |
| 55 | + * 454.2K |
| 56 | + * Acceptance Rate |
| 57 | + * 64.5% |
| 58 | + * |
| 59 | + */ |
| 60 | +public class FindEventualSafeStates { |
| 61 | + |
| 62 | + // V0 |
| 63 | + // TODO : implement |
| 64 | +// public List<Integer> eventualSafeNodes(int[][] graph) { |
| 65 | +// |
| 66 | +// } |
| 67 | + |
| 68 | + // V1-1 |
| 69 | + // https://leetcode.com/problems/find-eventual-safe-states/editorial/ |
| 70 | + // IDEA : DFS |
| 71 | + public boolean dfs(int node, int[][] adj, boolean[] visit, boolean[] inStack) { |
| 72 | + // If the node is already in the stack, we have a cycle. |
| 73 | + if (inStack[node]) { |
| 74 | + return true; |
| 75 | + } |
| 76 | + if (visit[node]) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + // Mark the current node as visited and part of current recursion stack. |
| 80 | + visit[node] = true; |
| 81 | + inStack[node] = true; |
| 82 | + for (int neighbor : adj[node]) { |
| 83 | + if (dfs(neighbor, adj, visit, inStack)) { |
| 84 | + return true; |
| 85 | + } |
| 86 | + } |
| 87 | + // Remove the node from the stack. |
| 88 | + inStack[node] = false; |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + public List<Integer> eventualSafeNodes_1_1(int[][] graph) { |
| 93 | + int n = graph.length; |
| 94 | + boolean[] visit = new boolean[n]; |
| 95 | + boolean[] inStack = new boolean[n]; |
| 96 | + |
| 97 | + for (int i = 0; i < n; i++) { |
| 98 | + dfs(i, graph, visit, inStack); |
| 99 | + } |
| 100 | + |
| 101 | + List<Integer> safeNodes = new ArrayList<>(); |
| 102 | + for (int i = 0; i < n; i++) { |
| 103 | + if (!inStack[i]) { |
| 104 | + safeNodes.add(i); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return safeNodes; |
| 109 | + } |
| 110 | + |
| 111 | + // V1-2 |
| 112 | + // https://leetcode.com/problems/find-eventual-safe-states/editorial/ |
| 113 | + // IDEA : Topological Sort Using Kahn's Algorithm |
| 114 | + public List<Integer> eventualSafeNodes_1_2(int[][] graph) { |
| 115 | + int n = graph.length; |
| 116 | + int[] indegree = new int[n]; |
| 117 | + List<List<Integer>> adj = new ArrayList<>(); |
| 118 | + |
| 119 | + for(int i = 0; i < n; i++) { |
| 120 | + adj.add(new ArrayList<>()); |
| 121 | + } |
| 122 | + |
| 123 | + for (int i = 0; i < n; i++) { |
| 124 | + for (int node : graph[i]) { |
| 125 | + adj.get(node).add(i); |
| 126 | + indegree[i]++; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + Queue<Integer> q = new LinkedList<>(); |
| 131 | + // Push all the nodes with indegree zero in the queue. |
| 132 | + for (int i = 0; i < n; i++) { |
| 133 | + if (indegree[i] == 0) { |
| 134 | + q.add(i); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + boolean[] safe = new boolean[n]; |
| 139 | + while (!q.isEmpty()) { |
| 140 | + int node = q.poll(); |
| 141 | + safe[node] = true; |
| 142 | + |
| 143 | + for (int neighbor : adj.get(node)) { |
| 144 | + // Delete the edge "node -> neighbor". |
| 145 | + indegree[neighbor]--; |
| 146 | + if (indegree[neighbor] == 0) { |
| 147 | + q.add(neighbor); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + List<Integer> safeNodes = new ArrayList<>(); |
| 153 | + for (int i = 0; i < n; i++) { |
| 154 | + if (safe[i]) { |
| 155 | + safeNodes.add(i); |
| 156 | + } |
| 157 | + } |
| 158 | + return safeNodes; |
| 159 | + } |
| 160 | + |
| 161 | + // V2 |
| 162 | + // IDEA : DFS |
| 163 | + // https://leetcode.com/problems/find-eventual-safe-states/solutions/3752567/cycle-detection-same-code-begineers-friendly-c-java-python/ |
| 164 | + public boolean dfs2(List<List<Integer>> adj, int src, boolean[] vis, boolean[] recst) { |
| 165 | + vis[src] = true; |
| 166 | + recst[src] = true; |
| 167 | + for (int x : adj.get(src)) { |
| 168 | + if (!vis[x] && dfs2(adj, x, vis, recst)) { |
| 169 | + return true; |
| 170 | + } else if (recst[x]) { |
| 171 | + return true; |
| 172 | + } |
| 173 | + } |
| 174 | + recst[src] = false; |
| 175 | + return false; |
| 176 | + } |
| 177 | + |
| 178 | + public List<Integer> eventualSafeNodes_2(int[][] graph) { |
| 179 | + int n = graph.length; |
| 180 | + List<List<Integer>> adj = new ArrayList<>(); |
| 181 | + for (int i = 0; i < n; i++) { |
| 182 | + List<Integer> list = new ArrayList<>(); |
| 183 | + for (int j = 0; j < graph[i].length; j++) { |
| 184 | + list.add(graph[i][j]); |
| 185 | + } |
| 186 | + adj.add(list); |
| 187 | + } |
| 188 | + boolean[] vis = new boolean[n]; |
| 189 | + boolean[] recst = new boolean[n]; |
| 190 | + for (int i = 0; i < n; i++) { |
| 191 | + if (!vis[i]) { |
| 192 | + dfs2(adj, i, vis, recst); |
| 193 | + } |
| 194 | + } |
| 195 | + List<Integer> ans = new ArrayList<>(); |
| 196 | + for (int i = 0; i < recst.length; i++) { |
| 197 | + if (!recst[i]) { |
| 198 | + ans.add(i); |
| 199 | + } |
| 200 | + } |
| 201 | + return ans; |
| 202 | + } |
| 203 | + |
| 204 | + |
| 205 | + // V3 |
| 206 | + // IDEA : BFS |
| 207 | + // https://leetcode.com/problems/find-eventual-safe-states/solutions/3753320/c-java-python-bfs-dfs-topological-sort-easy-to-understand/ |
| 208 | + public List<Integer> eventualSafeNodes_3(int[][] graph) { |
| 209 | + // Using BFS |
| 210 | + int n = graph.length; |
| 211 | + int[] indegree = new int[n]; |
| 212 | + List<List<Integer>> g = new ArrayList<>(n); |
| 213 | + for (int i = 0; i < n; i++) { |
| 214 | + g.add(new ArrayList<>()); |
| 215 | + } |
| 216 | + for (int i = 0; i < n; i++) { |
| 217 | + for (int itt : graph[i]) { |
| 218 | + g.get(itt).add(i); // reverse the direction of node |
| 219 | + indegree[i]++; |
| 220 | + } |
| 221 | + } |
| 222 | + Queue<Integer> q = new LinkedList<>(); |
| 223 | + for (int i = 0; i < n; i++) { |
| 224 | + if (indegree[i] == 0) { |
| 225 | + q.add(i); |
| 226 | + } |
| 227 | + } |
| 228 | + List<Integer> ans = new ArrayList<>(); |
| 229 | + while (!q.isEmpty()) { |
| 230 | + int node = q.poll(); |
| 231 | + ans.add(node); |
| 232 | + for (int it : g.get(node)) { |
| 233 | + indegree[it]--; |
| 234 | + if (indegree[it] == 0) |
| 235 | + q.add(it); |
| 236 | + } |
| 237 | + } |
| 238 | + ans.sort(null); |
| 239 | + return ans; |
| 240 | + } |
| 241 | + |
| 242 | + // V4 |
| 243 | +} |
0 commit comments