-
Notifications
You must be signed in to change notification settings - Fork 2
/
course-schedule.go
78 lines (70 loc) · 1.81 KB
/
course-schedule.go
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
package graph
import "aha-algorithm/src/util"
// https://leetcode-cn.com/problems/course-schedule/
// Graph Topology Sort + bfs
func canFinish(numCourses int, prerequisites [][]int) bool {
// edge: from current to another
edges := make([][]int, numCourses)
indegree := make([]int, numCourses)
for _, pair := range prerequisites {
from, to := pair[1], pair[0]
arr := edges[from] // pair is <post course, pre course>
arr = append(arr, to)
edges[pair[1]] = arr
indegree[pair[0]]++ // every in-degree of node
}
// init add all nodes whose indegree is zero to the queue.
var queue []int
for i := 0; i < numCourses; i++ {
if indegree[i] == 0 {
queue = append(queue, i)
}
}
// bfs
visited := 0
for len(queue) > 0 {
visited++
course := queue[0]
queue = queue[1:]
for _, c := range edges[course] {
indegree[c]--
if indegree[c] == 0 {
queue = append(queue, c)
}
}
}
return visited == numCourses
}
// Graph Topology Sort + dfs
func canFinishByDfs(numCourses int, prerequisites [][]int) bool {
edges := make([][]int, numCourses)
indegree := make([]int, numCourses)
for _, pair := range prerequisites {
from, to := pair[1], pair[0]
arr := edges[from] // pair is <post course, pre course>
arr = append(arr, to)
edges[pair[1]] = arr
indegree[pair[0]]++ // every in-degree of node
}
// init add all nodes whose indegree is zero to the queue.
var stack []int
for i := 0; i < numCourses; i++ {
if indegree[i] == 0 {
stack = append(stack, i)
}
}
// dfs
visited := make([]int, numCourses)
for len(stack) > 0 {
course := stack[len(stack)-1]
stack = stack[:len(stack)-1]
visited[course] = 1
for _, c := range edges[course] {
indegree[c]--
if indegree[c] == 0 && visited[c] != 1 {
stack = append(stack, c)
}
}
}
return util.ArraySum(visited) == numCourses
}