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,此时错误是有环): ...
随机推荐
- Java,AWTUtilities,eclipse报编译错误:Access restriction: The type 'AWTUtilities' is not API (restriction on required library 'C:\Program Files\Java\jre7\lib\rt.jar')
[场景]调用com.sun.awt.AWTUtilities时,eclipse提示编译错误: Access restriction: The type 'AWTUtilities' is not AP ...
- epoll_wait 时 POLLERR 与 POLLIN 同时返回的现象解析(转)
今天code review时,同事B对我代码中的poll()的处理做法提出了异议.于是做了些研究,还发现了一些好玩的故事. 异议的代码 我的代码是参考manpage写的,类似下面的做法.同事B说没有处 ...
- How to Pronounce the Months of the Year
How to Pronounce the Months of the Year Share Tweet Share Tagged With: Most Popular Some of the mont ...
- Cache雪崩效应
大概半年前,Guang.com曾发生一次由于首页部分cache失效,导致网站故障. 故障分析: 当时逛正在做推广,流量突然暴增,QPS达到5000+,当首页部分cache失效时,需要查询DB, 但由于 ...
- GIS案例学习笔记-多边形内部缓冲区地理模型
GIS案例学习笔记-多边形内部缓冲区地理模型 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:对于多边形,建立内部缓冲区. 问题:ArcGIS缓冲工具不支持内部 ...
- C# 使用post的方式提交raw格式的数据,数据为json格式,多层嵌套
原文地址:https://cnodejs.org/topic/539ff8a5c3ee0b5820938d60 raw方式使用的是纯字符串的数据上传方式,所以在POST之前,可能需要手工的把一些JSO ...
- 吴裕雄 29-MySQL 处理重复数据
MySQL 处理重复数据有些 MySQL 数据表中可能存在重复的记录,有些情况我们允许重复数据的存在,但有时候我们也需要删除这些重复的数据.本章节我们将为大家介绍如何防止数据表出现重复数据及如何删除数 ...
- Applese走迷宫-bfs
链接:https://ac.nowcoder.com/acm/contest/330/C来源:牛客网 题目描述 精通程序设计的 Applese 双写了一个游戏. 在这个游戏中,它被困在了一个 n×mn ...
- vuejs 组件通讯
在应用复杂时,推荐使用vue官网推荐的vuex,以下讨论简单SPA(single-page application 简称为 SPA)中的组件间传值. 一.路由传值 路由对象如下图所示: 在跳转页面的时 ...
- 用js实现九九乘法口诀两种方式
js实现九九乘法口诀两种方式: 第一种是用户输入一个数弹出所对应的乘法口诀: <script type="text/javascript"> function art( ...