207. Course Schedule
Problem description:
There are a total of n courses you have to take, labeled from 0 to n-1.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
Example 1:
Input: 2, [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
Example 2:
Input: 2, [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you should
also have finished course 1. So it is impossible.
Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
You may assume that there are no duplicate edges in the input prerequisites.
Solution1 DFS:
We can use DFS to solve this question. From above example, Input: 2, [[1,0],[0,1]]
, we can see that if the graph contains a cycle
, we can not finish all the courses.
First of all, let’s see how we generate a graph. we can use a vector<unordered_set>
to do so. Every index stores the courses that can be taken after finish the index
course.
Second, we need to traverse the graph and find if there’s any cycle. The idea of finding a cycle is to record the nodes that we’ve already traversed on current path. If we encounter a node that we’ve already traversed, then it must contains a cycle in the graph. After current path is checked, reset it to false
.
Third, another visited array to store which node is already traversed. help to save time.
1 | class Solution { |
Python DFS
- if node v has not been visited, then mark it as 0.
- if node v is being visited, then mark it as -1. If we find a vertex marked as -1 in DFS, then their is a ring.
- if node v has been visited, then mark it as 1. If a vertex was marked as 1, then no ring contains v or its successors.
1 | class Solution: |
Solution2 BFS:
The BFS solution is to apply topological sort. The idea is to check if every node does not have any indegree after we traverse the graph.
First, we need to generate a graph. Pretty similar to DFS solution above.
Second, we need a indegree array, size is numCourses. Add one if a course have prerequisite.
After calculated the indegree, some of the node might not have any indegree, so we start to traverse from them.
Third, BFS. If a node’s indegree becomes zero
, then we need to push it into the queue.
Finally, check indegree array to see if all zero.
1 | class Solution { |
Solution Python BFS topological sort
The idea is to build graph, then add every node’s prerequisites to a set.
Notice the graph
is build to find if I complete course y
then what I could move next x
.
But the todo
is if want to take x
, what y
should take.
- Walk through the node that have no prerequisites first
- Remove node
n
from other’stodo
, because noden
is completed. - When we find other node
X
havetodo
with length0
, ie: nothing else to do after complete noden
, then we addX
to queue. - Finally, check if
todo
is empty. If it’s not, then there’s a cycle.
1 | class Solution: |
1 | class Solution: |
reference:
https://www.youtube.com/watch?v=zkTOIVUdW-I
https://goo.gl/iN1qG3