Topological Sorting拓扑排序
定义:
Topological Sorting is a method of arranging the vertices in a directed acyclic graph (DAG有向无环图) as a sequence, such that for every directed edge(u,v), 即u-> v, vertex u comes before v in the ordering
注意:
- A topological ordering is possible if and only if the graph has no directed cycles
- Topological orderings are NOT unique

应用场景:
- build systems: a program cannot be built unless its dependencies are first built
- pre-requisite problems: a course cannot be selected unless its pre-requisite courses are taken
- dependency problems
- task schedule
时间复杂度:
O(V+E)
Topological Sort(){
1. Call DFS to compute finish time f[v] for each vertex
2. At each vertex is finished, insert it onto the front of a linked list
3. Return the linked list of vertices
}
内部原理:

比如从任意一个vertex出发(比如,随便选个NodeH), 来DFS搜索

从NodeH出发,DFS可以走 NodeH-> NodeJ

从NodeJ出发, DFS可以 NodeJ -> NodeM
发现NodeM后面没有元素了, 我们把NodeM放到Stack里面(基于Stack的FILO特性)

从NodeM往回走,到NodeJ, 从NodeJ出发,DFS可以NodeJ-> NodeL
发现NodeL后面没有元素了, 我们把NodeL放到Stack里面

以此类推,DFS走完整个有向无环图,并用Stack保证了u-> v, vertex u comes before v in the ordering
综上,
1. 从有向无环图的任意点出发
2. 进行DFS搜索, 直到搜到当前元素已经"nowhere left to go"了
3. 将该元素放到Stack里,并将该元素mark为visited
4. 从该元素backtrack,继续DFS
Topological Sorting拓扑排序的更多相关文章
- HDU 5195 DZY Loves Topological Sorting 拓扑排序
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- hdu.5195.DZY Loves Topological Sorting(topo排序 && 贪心)
DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 ...
- 拓扑排序(Topological Sorting)
一.什么是拓扑排序 在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列.且该序列必须满足下面两个 ...
- hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]
传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131 ...
- 拓扑排序 (Topological Sorting)
拓扑排序(Topological Sorting) 一.拓扑排序 含义 构造AOV网络全部顶点的拓扑有序序列的运算称为拓扑排序(Topological Sorting). 在图论中,拓扑排序(Topo ...
- hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序
DZY Loves Topological Sorting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...
- hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)
DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 ...
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
- 拓扑排序 POJ 1049 Sorting It All Out
题目传送门 /* 拓扑排序裸题:有三种情况: 1. 输入时发现与之前的矛盾,Inconsistency 2. 拓扑排序后,没有n个点(先判断cnt,即使一些点没有边连通,也应该是n,此时错误是有环): ...
随机推荐
- jquery循环方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- mui longtap 事件无效
1.mui 的部分事件默认是关闭的 需要在init中单独配置事件开关 mui.init({ gestureConfig: { longtap: true, //默认为false } })
- Oracle 学习总结 - 问题诊断
搜集常用诊断sql https://blog.csdn.net/yangshangwei/article/details/52449489 lock相关: 1. 查看lock, 打开两个事物,事物1更 ...
- [ SHELL编程 ] 数组、关联数组和awk数组
本文主要对shell编程中常用的数组.关联数组和awk数组定义.操作以及注意事项做个总结,并提供具体案例. 数组 数组定义:一对圆括号表示数组,数组元素之间用空格符号分割. Array=(val1 v ...
- [Linux]实际操作中命令 su 与 sudo 的区别
------------------------------------------------------------------------------------------------ 首先我 ...
- Structs复习 Action传递参数
Structs传递参数通常有三种方式 下面我来一个个介绍 1.属性 Jar包 web.xml <?xml version="1.0" encoding="UTF-8 ...
- .Net中使用ODP.net访问Oracle数据库
ODP.Net是Oracle提供的数据库访问类库,其功能和效率上都有所保证,它还有一个非常方便特性:在客户端上,可以不用安装Oracle客户端,直接拷贝即可使用. .net framework4中会将 ...
- 代码规范【经理培训内容记录】[有参考:http://kb.cnblogs.com/page/179593/]
一.命名规范 方法:所有首字母大写,如BloodControl; 类:所有首字母大写: 变量:第一个首字母小写,其他首字母大写:如bloodControl; 常量:全部字母大写,可用下划线分隔:如BL ...
- CDH6离线安装
CentOS7下完全离线安装CDH6集群 CDH Linux 大数据 shareSHARE 245 浏览量 分享到微博 分享到 Twitter 分享到 QQ 本文是在CentOS7.5下进行C ...
- mySQL遇到的问题
学习mySQL遇到以下错误. 仔细检查才发现,是字段不一样. 所以插入数据,应该一一对应.