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?

For example:

2, [[1,0]]
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.

2, [[1,0],[0,1]]
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.

Hints:
This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
Topological sort could also be done via BFS.

思路:拓扑排序。

拓扑排序的含义是:对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边(u,v)∈E(G),则u在线性序列中出现在v之前。

拓扑排序的方法是:用一个队列存入度为0的节点,依次出队,将与出队节点相连的节点的入度减1,如果入度减为0,将其放入队列中,直到队列为空。如里最后还有入度不为0的节点的话,说明有环(有环的情况必定有节点入度无法减到0,比如环刚开始处的那个节点),否则无环。

class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<vector<int>> graph(numCourses, vector<int>());
vector<int> inDegree(numCourses,);
queue<int> que;
int cur; for(auto p: prerequisites){
graph[p.first].push_back(p.second);
inDegree[p.second]++;
} for(int i = ; i < numCourses; i++){ //find the first point
if(inDegree[i]==) que.push(i);
} while(!que.empty()){ //BFS by using queue; stack can be used to realize DFS
cur = que.front();
que.pop();
for(auto p: graph[cur]){
inDegree[p]--;
if(inDegree[p]==) que.push(p);
}
} for(int i = ; i < numCourses; i++){
if(inDegree[i]!=) return false;
}
return true;
}
};

207. Course Schedule(Graph; BFS)的更多相关文章

  1. LeetCode - 207. Course Schedule

    207. Course Schedule Problem's Link ---------------------------------------------------------------- ...

  2. 207. Course Schedule

    https://blog.csdn.net/wongleetion/article/details/79433101 问题的实质就是判断一个有向图是否有环,利用入度去解决这个问题 使用bfs解决问题. ...

  3. LN : leetcode 207 Course Schedule

    lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...

  4. [LeetCode] 207. Course Schedule 课程清单

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

  5. [LeetCode] 207. Course Schedule 课程安排

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  6. Java for LeetCode 207 Course Schedule【Medium】

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. 【LeetCode】207. Course Schedule (2 solutions)

    Course Schedule There are a total of n courses you have to take, labeled from 0 to n - 1. Some cours ...

  8. [LeetCode] 207 Course Schedule_Medium tag: BFS, DFS

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

  9. HDU 5876 Sparse Graph BFS+set删点

    Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices s ...

随机推荐

  1. STM32之USB电路(摘要笔记)

  2. 机器学习入门-数值特征-进行二值化变化 1.Binarizer(进行数据的二值化操作)

    函数说明: 1. Binarizer(threshold=0.9) 将数据进行二值化,threshold表示大于0.9的数据为1,小于0.9的数据为0 对于一些数值型的特征:存在0还有其他的一些数 二 ...

  3. C# 申请非托管内存

    方式一:使用 stackalloc 关键字 int* block = stackalloc int[100]; 注:此关键字仅在局部变量初始值设定项中有效. 以下代码导致编译器错误. int* blo ...

  4. hadoop配置2.6.1 centos7

    上传文件(分发)的三种方式: 1.本地: -file 的模式,上传一些小的文件. 例如: -file ./test INPUT_FILE_PATH_1="/The_Man_of_Proper ...

  5. svn下载地址

    SVN svn服务器端下载: https://www.visualsvn.com/server/download/ svn eclipse插件地址(new soft install): http:// ...

  6. python正则表达式查找汉字

    使用正则表达是查找汉字之前,要将所有的字符串都转码成utf8 import re string_test = "This is test string 这是测试字符串" strin ...

  7. MySQL更新优化(转)

    通常情况下,当访问某张表的时候,读取者首先必须获取该表的锁,如果有写入操作到达,那么写入者一直等待读取者完成操作(查询开始之后就不能中断,因此允许读取者完成操作).当读取者完成对表的操作的时候,锁就会 ...

  8. Linux:使用互斥量进行线程同步

    基础知识 同步概念 所谓同步,即同时起步,协调一致.不同的对象,对"同步"的理解方式略有不同.如,设备同步,是指在两个设备之间规定一个共同的时间参考:数据库同步,是指让两个或多个数 ...

  9. 使用django实现自定义用户认证

    参考资料:https://docs.djangoproject.com/en/1.10/topics/auth/customizing/    直接拉到最后看栗子啦 django自定义用户认证(使用自 ...

  10. 使用AutoMapper时Expression的转换

    此文章为转载:http://www.bubuko.com/infodetail-699735.html 参考链接: http://q.cnblogs.com/q/34480/   dudu有回复,其中 ...