[抄题]:

现在你总共有 n 门课需要选,记为 0 到 n - 1.
一些课程在修之前需要先修另外的一些课程,比如要学习课程 0 你需要先学习课程 1 ,表示为[0,1]
给定n门课以及他们的先决条件,判断是否可能完成所有课程?

给定 n = 2,先决条件为 [[1,0]] 返回 true
给定 n = 2,先决条件为 [[1,0],[0,1]] 返回 false

[思维问题]:

不知道为啥是图:抽象图,很多条边构造出来的图

[一句话思路]:

拓扑排序:先统计度、边数,再把度=0的点放进queue,进行BFS,减度

BFS时每层数一遍先修课程数相同的点,最后看总数是否相等

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 队列存的时候不指定类型,否则课程多了存不下。取出的时候转为整数

[二刷]:

  1. numCourses的个数>=int[][] prerequisites的组数,因此edge degree都要分配numCourses的空间
  2. 每一个edges[i]也是数组,因为里面存了很多条边。
  3. 队列里添加的是12345,统计个数,而不是统计degree[i](都是1)
  4. int pointer = (int)edges[course].get(i); 第course个边数组中的第i条边取出来,用pointer缩写,可以减度

[三刷]:

  1. 在所有的课中,如果入度 = 0,就压入queue之中。因此循环上限是numCourses
  2. List[] edges = new ArrayList[];List edges = new ArrayList; ArrayList实现的是List,ArrayList数组实现的是List数组,二者不同

[四刷]:

[五刷]:

[总结]:

  1. 图的BFS一定要用queue来实现。拓扑排序中,每次都添加度为0的下一层。搞明白原理
  2. 把变量名按自然语言写全了,这样更像是一个工业产品,而不是一些潦草的草稿
  3. degree表示入度,degree=0的点是先修课程 没有入度

[debug]:数组角标写错了,没查出来

[复杂度]:Time complexity: O(边+点) Space complexity: O(边+点)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

Topological Sorting:由偏序得到全序。统计入度、把入度=0的点放进队列、BFS

[其他解法]:

[Follow Up]:

返回序列

[LC给出的题目变变变]:

courese 3:范围排序,用PQ

310. Minimum Height Trees:长得宽的树,用宽搜

public class Solution {
/*
* @param numCourses: a total of n courses
* @param prerequisites: a list of prerequisite pairs
* @return: true if can finish all courses or false
*/
public boolean canFinish(int numCourses, int[][] prerequisites) {
//corner case
if (numCourses == 0 && prerequisites == null) {
return false;
}
//count degree & edges
int[] degree = new int[numCourses];
List[] edges = new ArrayList[numCourses];// for (int i = 0; i < numCourses; i++) {
edges[i] = new ArrayList<Integer>();
}
for (int i = 0; i < prerequisites.length; i++) {
degree[prerequisites[i][0]] ++ ;
edges[prerequisites[i][1]].add(prerequisites[i][0]);
}
//put in queue
Queue queue = new LinkedList();
for (int i = 0; i < prerequisites.length; i++) {
if (degree[i] == 0) {
queue.add(i);
}
}
//bfs
int count = 0;
while (!queue.isEmpty()) {
int course = (int)queue.poll();
count ++;
int n = edges[course].size();
for (int i = 0; i < n; i++) {
int pointer = (int)edges[course].get(i);
degree[pointer] --;
if (degree[pointer] == 0) {
queue.add(pointer);
}
}
}
return count == numCourses;
}
}

Course Schedule课程表12(用Topological Sorting)的更多相关文章

  1. 拓扑排序(Topological Sorting)

    一.什么是拓扑排序 在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列.且该序列必须满足下面两个 ...

  2. Topological Sorting拓扑排序

    定义: Topological Sorting is a method of arranging the vertices in a directed acyclic graph (DAG有向无环图) ...

  3. hdu.5195.DZY Loves Topological Sorting(topo排序 && 贪心)

    DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  4. Lintcode: Topological Sorting

    Given an directed graph, a topological order of the graph nodes is defined as follow: For each direc ...

  5. URAL(timus) 1280 Topological Sorting(模拟)

    Topological Sorting Time limit: 1.0 secondMemory limit: 64 MB Michael wants to win the world champio ...

  6. Topological Sorting

    Topological sorting/ ordering is a linear ordering of its vertices such that for every directed edge ...

  7. Union - Find 、 Adjacency list 、 Topological sorting Template

    Find Function Optimization: After Path compression: int find(int x){ return root[x] == x ? x : (root ...

  8. hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)

    DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  9. hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序

    DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...

随机推荐

  1. 1117 Eddington Number (25 分)

    1117 Eddington Number (25 分) British astronomer Eddington liked to ride a bike. It is said that in o ...

  2. grep正则表达的零宽断言

    先看一组神奇的命令 [root@elastix82 tmp]# echo $html111<td>aaa</td>222[root@elastix82 tmp]# [root@ ...

  3. 一篇文章让你入门Shell !

    Shell脚本,就是利用Shell的命令解释的功能,对一个纯文本的文件进行解析,然后执行这些功能,也可以说Shell脚本就是一系列命令的集合. Shell可以直接使用在win/Unix/Linux上面 ...

  4. unity3d之GUI

    1.Button void OnGUI() { , , , ), "up")) {flg=true; } , , , ), "down")) {flg=fals ...

  5. RHEL6.x查看网卡槽位对应设备文件及路径

    先查看网卡mac地址,由于我的服务器做了mac地址绑定,所以会有相同的hwaddr地址,请忽略. [root@node-0a0a05d3- net]# ifconfig eth0 | grep HWa ...

  6. SpringBoot入门篇--使用Thymeleaf模板引擎进行页面的渲染

    在做WEB开发的时候,我们不可避免的就是在前端页面之间进行跳转,中间进行数据的查询等等操作.我们在使用SpringBoot之前包括我在内其实大部分都是用的是JSP页面,可以说使用的已经很熟悉.但是我们 ...

  7. 生存分析与R

    生存分析与R 2018年05月19日 19:55:06 走在码农路上的医学狗 阅读数:4399更多 个人分类: R语言   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...

  8. OpenCL 管道

    ▶ 按书上写的管道的代码,需要使用 OpenCL2.0 的平台和设备,目前编译不通过,暂时不知道是什么问题,先把代码堆上来,以后换了新的设备再说 ● 程序主要功能:用主机上的数组 srcHost 创建 ...

  9. OpenACC 《大规模并行处理器编程实战》教材讲解

    ▶ <大规模并行处理器编程实战>第15章,关于OpenACC 的部分,散点 ● OpenACC 中,主机存储器和设备存储器是分开处理的,程序员只要制定要传输的存储器对象即可,编译器会自动生 ...

  10. redhat 7安装CentOS 7 yum源

    http://www.bubuko.com/infodetail-2004218.html http://www.bubuko.com/infodetail-2004218.html ******** ...