(adjList map[int][]int, colors []int, course int)
| 83 | } |
| 84 | |
| 85 | func hasCycleDFS(adjList map[int][]int, colors []int, course int) bool { |
| 86 | // if this node is an ancestor of the DFS path taken, there is a cycle |
| 87 | if colors[course] == 1 { |
| 88 | return true |
| 89 | } |
| 90 | |
| 91 | // mark the course as seen |
| 92 | colors[course] = 1 |
| 93 | |
| 94 | cycles := false |
| 95 | for _, prereq := range adjList[course] { |
| 96 | cycles = cycles || hasCycleDFS(adjList, colors, prereq) |
| 97 | } |
| 98 | |
| 99 | // unmark the course as seen on way back up the call stack |
| 100 | colors[course] = 0 |
| 101 | |
| 102 | return cycles |
| 103 | } |