-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0207_course_schedule.rs
More file actions
222 lines (180 loc) · 6.25 KB
/
s0207_course_schedule.rs
File metadata and controls
222 lines (180 loc) · 6.25 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#![allow(unused)]
pub struct Solution {}
use std::collections::HashMap;
#[derive(Clone)]
enum State {
Unknown,
Visiting,
Visited,
}
impl Solution {
// Time Limit Exceeded
// Time O(E + V^2) Space O(E + V)
pub fn can_finish_backtracking(num_courses: i32, prerequisites: Vec<Vec<i32>>) -> bool {
// course -> list of next courses
let mut course_map: HashMap<usize, Vec<usize>> = HashMap::new();
// build the graph first
for relation in prerequisites {
// relation[0] depends on relation[1]
let edges = course_map
.entry(relation[1] as usize)
.or_insert(vec![relation[0] as usize]);
edges.push(relation[0] as usize);
}
let mut path = vec![false; num_courses as usize];
for curr_course in 0..num_courses as usize {
if Self::is_cyclic_backtracking(curr_course, &course_map, &mut path) {
return false;
}
}
true
}
/*
* backtracking method to check that no cycle would be formed starting from currCourse
*/
fn is_cyclic_backtracking(
curr_course: usize,
course_map: &HashMap<usize, Vec<usize>>,
path: &mut Vec<bool>,
) -> bool {
if path[curr_course] {
// come across a previously visited node, i.e. detect the cycle
return true;
}
// no following courses, no loop
if !course_map.contains_key(&curr_course) {
return false;
}
// before backtracking, mark the node in the path
path[curr_course] = true;
// backtracking
let mut ret = false;
for next_course in course_map.get(&curr_course).unwrap().iter() {
ret = Self::is_cyclic_backtracking(*next_course, course_map, path);
if ret {
break;
}
}
// after backtracking, remove the node from the path
path[curr_course] = false;
ret
}
// Time O(E + V) Space O(E + V)
pub fn can_finish_dfs(num_courses: i32, prerequisites: Vec<Vec<i32>>) -> bool {
// course -> list of next courses
let mut course_map: HashMap<usize, Vec<usize>> = HashMap::new();
// build the graph first
for relation in prerequisites {
// relation[0] depends on relation[1]
let edges = course_map
.entry(relation[1] as usize)
.or_insert(vec![relation[0] as usize]);
edges.push(relation[0] as usize);
}
let mut path = vec![false; num_courses as usize];
let mut checked = vec![false; num_courses as usize];
for curr_course in 0..num_courses as usize {
if Self::is_cyclic_dfs(curr_course, &course_map, &mut path, &mut checked) {
return false;
}
}
true
}
/*
* postorder DFS check that no cycle would be formed starting from currCourse
*/
fn is_cyclic_dfs(
curr_course: usize,
course_map: &HashMap<usize, Vec<usize>>,
path: &mut Vec<bool>,
checked: &mut Vec<bool>,
) -> bool {
// bottom cases
if checked[curr_course] {
// this node has been checked, no cycle would be formed with this node.
return false;
}
if path[curr_course] {
// come across a previously visited node, i.e. detect the cycle
return true;
}
// no following courses, no loop
if !course_map.contains_key(&curr_course) {
return false;
}
// before backtracking, mark the node in the path
path[curr_course] = true;
// backtracking
let mut ret = false;
// postorder DFS, to visit all its children first.
for next_course in course_map.get(&curr_course).unwrap().iter() {
ret = Self::is_cyclic_dfs(*next_course, course_map, path, checked);
if ret {
break;
}
}
// after the visits of children, we come back to process the node itself
// remove the node from the path
path[curr_course] = false;
// Now that we've visited the nodes in the downstream,
// we complete the check of this node.
checked[curr_course] = true;
ret
}
// Time O(E + V) Space O(E + V) Topological Sort
pub fn can_finish(num_courses: i32, prerequisites: Vec<Vec<i32>>) -> bool {
// bound check
if num_courses < 1 {
return false;
}
// course -> list of next courses
let mut course_map: HashMap<usize, Vec<usize>> = HashMap::new();
// build the graph first
for relation in prerequisites {
// relation[0] depends on relation[1]
let edges = course_map
.entry(relation[1] as usize)
.or_insert(vec![relation[0] as usize]);
edges.push(relation[0] as usize);
}
let mut state = vec![State::Unknown; num_courses as usize];
for curr_course in 0..num_courses as usize {
if Self::is_cyclic(curr_course, &mut state, &course_map) {
return false;
}
}
true
}
fn is_cyclic(
curr_course: usize,
state: &mut Vec<State>,
course_map: &HashMap<usize, Vec<usize>>,
) -> bool {
match state[curr_course] {
State::Visiting => true,
State::Visited => false,
State::Unknown => {
state[curr_course] = State::Visiting;
if let Some(courses) = course_map.get(&curr_course) {
// Topological Sort, to visit all its children first.
for next_course in courses.iter() {
if Self::is_cyclic(*next_course, state, course_map) {
return true;
}
}
}
state[curr_course] = State::Visited;
false
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_207() {
assert_eq!(Solution::can_finish(2, vec![vec![1, 0]]), true);
assert_eq!(Solution::can_finish(2, vec![vec![1, 0], vec![0, 1]]), false);
}
}