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, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

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 the correct course order is [0,1]

4, [[1,0],[2,0],[3,1],[3,2]]

There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3].

Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.

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

207. Course Schedule的拓展,解法与其类似,这里要按顺序添加要完成的课程。拓扑排序,最后加一步判断是否存在环,如果存在环则返回空集合。使用BFS和DFS均可,区别在于是按照入度还是出度来考虑。

C++:

class Solution {
public:
/**
* 完成所有的课程的顺序
* bfs拓扑排序
* @param numCourses 课程数量
* @param prerequisites 课程先序关系
* @return 能完成返回课程顺序,否则返回空
*/
vector<int> findOrder(int numCourses, vector<pair<int, int> >& prerequisites) {
vector<int> heads(numCourses, -1), degree(numCourses, 0), points, args;
pair<int, int> p;
int from, to, count = 0, len = prerequisites.size(); /* 构造有向图,邻接表 */
for (int i = 0; i < len; ++i) {
p = prerequisites[i];
from = p.second;
to = p.first;
++degree[to];
args.push_back(heads[from]);
points.push_back(to);
heads[from] = count++;
} /* bfs拓扑排序,依次移除入度为0的点 */
vector<int> ret;
queue<int> q;
for (int i = 0; i < numCourses; ++i)
if (degree[i] == 0) q.push(i);
while (!q.empty()) {
from = q.front();
ret.push_back(from); // 课程完成,添加到结果集中
q.pop();
to = heads[from];
while (to != -1) {
if(--degree[points[to]] == 0) q.push(points[to]);
to = args[to];
}
} /* 判定是否所有的点入度都为0,若是则不存在环,否则存在环 */
for (int i = 0; i < numCourses; ++i)
if (degree[i] > 0) {
ret.clear();
break;
} return ret;
}
};

C++:DFS

class Solution {
public:
/**
* 完成所有的课程的顺序
* dfs拓扑排序
* @param numCourses 课程数量
* @param prerequisites 课程先序关系
* @return 能完成返回课程顺序,否则返回空
*/
vector<int> findOrder(int numCourses, vector<pair<int, int> >& prerequisites) {
vector<int> heads(numCourses, -1), degree(numCourses, 0), points, args;
pair<int, int> p;
int from, to, count = 0, len = prerequisites.size(); /* 构造有向图,邻接表 */
for (int i = 0; i < len; ++i) {
p = prerequisites[i];
from = p.second;
to = p.first;
++degree[from];
args.push_back(heads[to]);
points.push_back(from);
heads[to] = count++;
} /* dfs拓扑排序,依次移除出度为0的点 */
vector<int> ret;
queue<int> q;
for (int i = 0; i < numCourses; ++i)
if (degree[i] == 0) q.push(i);
while (!q.empty()) {
to = q.front();
ret.push_back(to); // 课程完成添加到结果集中
q.pop();
from = heads[to];
while (from != -1) {
if(--degree[points[from]] == 0) q.push(points[from]);
from = args[from];
}
} /* 判定是否所有的点入度都为0,若是则不存在环,否则存在环 */
for (int i = 0; i < numCourses; ++i)
if (degree[i] > 0) {
ret.clear();
break;
} /* 逆序 */
reverse(ret.begin(), ret.end());
return ret;
}
};

  

  

最新文章

  1. JavaScript错误/异常处理
  2. 一个基于jQuery的移动端条件选择查询插件(原创)
  3. 用LinkedList模拟栈数据结构的集合
  4. [转]IntelliJ Idea 常用快捷键 列表(实战终极总结!!!!)
  5. Hbase条件筛选
  6. 将HTMLCollection/NodeList/伪数组转换成数组
  7. hdu1690 Bus System(最短路 Dijkstra)
  8. HBase全分布安装配置
  9. vb脚本自动更新版本信息
  10. POJ 1862 Stripies
  11. mac下安装nginx问题解决
  12. Linq to List
  13. python的项目结构
  14. java设计模式-----单例设计模式
  15. P2757 [国家集训队]等差子序列
  16. dll多个版本问题
  17. 排序算法(8)--Merge Sorting--归并排序--Merge sort--归并排序
  18. 万恶的浏览器缓存 Vuex state里面的成员改名后浏览器不会马上更新
  19. ORA-65179: cannot keep datafiles for a pluggable database that is not unplugged
  20. vc++之stdafx.h

热门文章

  1. unix域套接字
  2. iptables 通用语句
  3. Centos7 Rsync怎么实现热备份笔记
  4. HTTP Status 500 - DateConverter does not support default String to &#39;Date&#39; conversion.错误
  5. 学习Microsoft Visio(1)
  6. springboot(二)
  7. learning java 转换流
  8. Dump文件数据存储格式(一)
  9. Redis存储Hash
  10. HTML相对路径和绝对路径