LeetCode - 207.Course Schedule
link: https://leetcode.com/problems/course-schedule/
题目
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:
1 | Input: 2, [[1,0]] |
Example 2:
1 | Input: 2, [[1,0],[0,1]] |
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.
问题描述
给出一系列的先修课程关系,判断如果按照这个关系,是否能顺利完成所有课程学习
题目分析
此题目实际是判断有向图中是否存在环,而拓扑排序的一个重要应用就是判断有向图中是否存在环,如果图中不存在环,那么是可以根据这个图求出一个拓扑序列
- 根据拓扑排序的求取方式,我们先记录每个节点的入度数,以及以其为起始节点,对应的所有终止节点
- 每次去选取入度数为0的节点,然后访问掉这个节点,如果节点不存在,那就是说这个图中剩余所有节点都有指向其的边,此时即存在环
- 对应的,将其所指向的节点的入度数全部减1
- 接下来接着去寻找入度为0的节点,已经访问过的节点不再检查,重复步骤,直至节点全部被访问,或者存在环
代码实现
1 | # -*- coding: utf-8 -*- |
运行结果
Time Submitted | Status | Runtime | Language |
---|---|---|---|
a few seconds ago | Accepted | 268 ms | python3 |
吐槽
LeetCode给出的模板,函数名和变量名没有按照PEP8规范来,正常是不应该驼峰命名的,而应该是小写加下划线形式