207. Course Schedule
Description
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.
For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
Return true if you can finish all courses. Otherwise, return false.
Thoughts
This is a classical topological sorting question. We can use BFS to solve this kind of problems. But we have to build the graph and count the indegree first.
The graph shows the order for 2 courses – prerequisite course: unlocked courses
The indegree list shows the number of prerequsited courses for every course.
We start from the courses with indegree 0 – meaning they have no prerequisite courses. Then we do BFS from these courses. Its neighbor is the unlocked courses in the graph. For all the neighbors, their indegree can be deducted by 1 because there is one less prerequisited course for them. And now, we may some courses that have 0 indegree. We add the into the queue and do the work again until there is no course in the queue.
Codes
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
if not prerequisites:
return True
'''
build graph and indegree list first
the given input using int to represent each course, so we can use list of list
list_index(which is prerequisited course): [unlocking courses]
be sure to cover all the courses
so we use the numCourses instead of the list prerequisites to build the graph and indegree
there maybe some courses who have no prerequisites hence not appear in prerequisites
later we need to add/undate values into the graph/indegree
'''
# build adjacent courses list
adj_course = [[] for _ in range(numCourses)]
# number of prerequisites of each course
preres = [0] * numCourses
for unlock, prere in prerequisites:
adj_course[prere].append(unlock)
preres[unlock] += 1
queue = collections.deque([])
for i in range(numCourses):
if preres[i] == 0:
queue.append(i)
'''
we want to know if we are able to take all the courses
so we need to take notes on the number of courses taken
we are not able to take all the courses in order
if the number is less then numCourses after all the bfs done
'''
taken = 0
while queue:
current = queue.popleft()
taken += 1
for next_course in adj_course[current]:
preres[next_course] -= 1
if preres[next_course] == 0:
queue.append(next_course)
return taken == numCourses
Notes
This problem is asking for if we are able to take all the courses. It has other format like: return the order, if the topological order unique etc.
-
return the order
we need to build a list to store all the courses and return it if the number of courses taken == numCourses -
if the topological order unique
we need to check if len(queue) == 1 in every round. len(queue) == 2 means we can learn either course in queue hence the order is not unique.

该博客讨论了一种经典图论问题——课程安排。通过建立依赖关系图并使用广度优先搜索(BFS),判断是否能完成所有课程。首先构建邻接课程列表和每个课程的前置课程数,然后从没有前置课程的课程开始进行BFS,更新课程的前置课程数,直至队列为空。如果完成的课程数等于总数,则可以完成所有课程,否则无法完成。
479

被折叠的 条评论
为什么被折叠?



