Skip to content

Commit c465d51

Browse files
committed
[Function add]
1. Add leetcode solutions with tag graph.
1 parent 57475dd commit c465d51

File tree

3 files changed

+153
-1
lines changed

3 files changed

+153
-1
lines changed

leetcode/207. Course Schedule.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,40 @@ class Solution {
141141
}
142142
```
143143

144+
### Third Time
145+
* Method 1: Topological sort + bfs
146+
```Java
147+
class Solution {
148+
public boolean canFinish(int numCourses, int[][] prerequisites) {
149+
Map<Integer, List<Integer>> map = new HashMap<>();
150+
int[] indegree = new int[numCourses];
151+
Set<Integer> remain = new HashSet<>();
152+
for(int i = 0; i < numCourses; i++) remain.add(i);
153+
for(int[] req : prerequisites){
154+
indegree[req[1]]++;
155+
List<Integer> next = map.containsKey(req[0]) ? map.get(req[0]): new ArrayList<Integer>();
156+
next.add(req[1]);
157+
map.put(req[0], next);
158+
}
159+
Queue<Integer> queue = new LinkedList<>();
160+
for(int i = 0; i < numCourses; i++){
161+
if(indegree[i] == 0)
162+
queue.offer(i);
163+
}
164+
while(!queue.isEmpty()){
165+
int course = queue.poll();
166+
remain.remove(course);
167+
List<Integer> courses = map.get(course);
168+
if(courses == null) continue;
169+
for(Integer c : courses){
170+
if(--indegree[c] == 0)
171+
queue.offer(c);
172+
}
173+
}
174+
return remain.isEmpty();
175+
}
176+
}
177+
```
178+
144179
### Reference
145180
1. [【图论】有向无环图的拓扑排序](https://www.cnblogs.com/en-heng/p/5085690.html)

leetcode/210. Course Schedule II.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,48 @@ class Solution {
151151
}
152152
}
153153
}
154-
```
154+
```
155+
156+
### Third Time
157+
* Method 1: Topological sort + bfs
158+
```Java
159+
class Solution {
160+
public int[] findOrder(int numCourses, int[][] prerequisites) {
161+
int[] order = new int[numCourses];
162+
int[] indegree = new int[numCourses];
163+
int index = 0;
164+
Map<Integer, List<Integer>> map = new HashMap<>();
165+
Set<Integer> remain = new HashSet<>();
166+
for(int i = 0; i < numCourses; i++) remain.add(i);
167+
for(int[] req : prerequisites){
168+
indegree[req[0]]++;
169+
List<Integer> nexts = map.containsKey(req[1]) ? map.get(req[1]): new ArrayList<>();
170+
nexts.add(req[0]);
171+
map.put(req[1], nexts);
172+
}
173+
Queue<Integer> queue = new LinkedList<>();
174+
for(int i = 0; i < numCourses; i++){
175+
if(indegree[i] == 0){
176+
queue.offer(i);
177+
}
178+
}
179+
while(!queue.isEmpty()){
180+
int cur = queue.poll();
181+
remain.remove(cur);
182+
List<Integer> courses = map.get(cur);
183+
order[index++] = cur;
184+
if(courses == null){
185+
remain.remove(cur);
186+
}else{
187+
for(Integer course : courses){
188+
indegree[course]--;
189+
if(indegree[course] == 0){
190+
queue.offer(course);
191+
}
192+
}
193+
}
194+
}
195+
return remain.isEmpty() ? order: new int[0];
196+
}
197+
}
198+
```
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
![Imgur](https://i.imgur.com/1oOwEFl.png)
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

Comments
 (0)