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,此时错误是有环): ...
随机推荐
- Delphi 文件目录相关的操作函数
需要User SysUtils 取文件名 ExtractFileName(FileName); 取文件扩展名: ExtractFileExt(filename); 取文件名,不带扩展名: 方法一: ...
- sourcetree 跳过注册
https://www.cnblogs.com/lucio110/p/8192792.html
- TCP/IP协议分层详解
TCP/IP 和 ISO/OSI ISO/OSI模型,即开放式通信系统互联参考模型(Open System Interconnection Reference Model),是国际标准化组织(ISO) ...
- git 每次push都需要输入用户和密码
git remote -v origin https://github.com/userName/xx.git (fetch) origin https://github.com/userName/x ...
- 遍历DOM树,获取子节点
获取子节点的方法有: 方法 说明 children() 选取子节点,可以带过滤参数.但只能选择子节点,不能选择孙子节点. find() 选取子节点,可以带过滤参数.可以选择子节点及孙子节点 ...
- [PHP]基于角色的访问控制RBAC
---------------------------------------------------------------------------------------------------- ...
- Android 开发进入Linux系统执行命令 2018-5-25 Fri.
/** * 进入linux cmd执行命令 * * @param command * @return */ private boolean runRootCommand(String command) ...
- 【原】linux学习路径
1. <<The Linux Command Line A Complete Introduction>> 2. <<Advanced Programming ...
- mysql 存储过程小问题
mysql写的存储过程的一些小问题 DELIMITER $$ USE `yzhoteldb`$$ DROP PROCEDURE IF EXISTS `yz_waveData`$$ CREATE DEF ...
- 学JS的心路历程-函式(二)arguments
参数(argument)与函式参数(parameter) 在讨论函式时,很多人都会把这两个搞混,我自己也不例外. 虽然讲错别人也听得懂,但是我们还是要搞清楚这两个的定义到底是什么! 参数是当我们呼叫函 ...