定义:

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拓扑排序的更多相关文章

  1. HDU 5195 DZY Loves Topological Sorting 拓扑排序

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

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

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

  3. 拓扑排序(Topological Sorting)

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

  4. 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 ...

  5. 拓扑排序 (Topological Sorting)

    拓扑排序(Topological Sorting) 一.拓扑排序 含义 构造AOV网络全部顶点的拓扑有序序列的运算称为拓扑排序(Topological Sorting). 在图论中,拓扑排序(Topo ...

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

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

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

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

  8. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  9. 拓扑排序 POJ 1049 Sorting It All Out

    题目传送门 /* 拓扑排序裸题:有三种情况: 1. 输入时发现与之前的矛盾,Inconsistency 2. 拓扑排序后,没有n个点(先判断cnt,即使一些点没有边连通,也应该是n,此时错误是有环): ...

随机推荐

  1. codes often WA

    枚举: 1.完美立方 #include<iostream> #include <cstdio> using namespace std; int main() { int N; ...

  2. 福州大学软件工程W班-助教总结

    背景 福州大学软件工程W班,总人数46人,讲师汪老师. 前期期望 希望自己能够在课程当中起到引导作用,发挥助教最大的用处. 实际执行情况 第一个问题是自动化测试工具,该工具主要是用来测试程序WordC ...

  3. Delphi判断是否有全屏程序

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  4. sublime text3 激活码——许可证

    亲测: 现在是公元2018年6月4日.晴 ``` ----- BEGIN LICENSE ----- sgbteam Single User License EA7E-1153259 8891CBB9 ...

  5. 组播协议——IGMP v2报文头介绍

    TYPE:占一个字节,其值有:0x16.0x12.0x17三种类型. Max Resp Time:最大响应时间,占一个字节. Checksum:校验和,占两个字节. Group address:组播地 ...

  6. Android代码规范

    Android代码规范——文章来源<IT蓝豹>http://itlanbao.com/preview.aspx#1,0 [-]一Import的次序二缩进Indentation总则示例代码规 ...

  7. Oracle ORA-00911: 无效字符

    SQL语句后多了个分号 “ ; ”.

  8. PowerDesigner导入sql脚本生成物理模型

    https://www.cnblogs.com/zsswpb/p/5771623.html

  9. react学习入门

    先在在学习react,react是faceBook推出的框架,因为虚拟DOM使页面性能提高很大,特别react Native非常适合移动端,现做一个学习总结: 1.react 获取DOM的两种方式是R ...

  10. php优化-》常用到的部分优化

    1.循环内部尽可能不要声明变量: 2.在可以用PHP内部字符串操作函数的情况下,尽量不要用正则表达式: 3.foreach效率更高,尽量用foreach代替while和for循环: 4.用单引号替代双 ...