定义:

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. 伪AJAX

    <h3>3,伪ajax</h3> <h6>学习iframe(嵌套别人家网站的)</h6> <div> <input id=" ...

  2. Linux:结束线程的三种方式

    一般情况下,线程终止后,其终止状态一直保留到其它线程调用pthread_join获取它的状态为止.但是线程也可以被置为detach状态,这样的线程一旦终止就立刻回收它占用的所有资源,而不保留终止状态. ...

  3. 3.AOP入门1.md

    目录 1.定义 1.1基本概念 2. 1.定义 1.1基本概念 AOP:aspect object programing面向切面编程 aop编程的要点在于关注点和切入点 关注点:指的是代码中的重复部分 ...

  4. Hibernate 再接触 组件映射

    将另外一个类嵌入到另外一个类 从而合并生成一张表 Husband.java package com.bjsxt.hibernate; import javax.persistence.Embedded ...

  5. python 装饰器、递归原理、模块导入方式

    1.装饰器原理 def f1(arg): print '验证' arg() def func(): print ' #.将被调用函数封装到另外一个函数 func = f1(func) #.对原函数重新 ...

  6. 如何用java完成一个中文词频统计程序

    要想完成一个中文词频统计功能,首先必须使用一个中文分词器,这里使用的是中科院的.下载地址是http://ictclas.nlpir.org/downloads,由于本人电脑系统是win32位的,因此下 ...

  7. hdu4497-GCD and LCM-(欧拉筛+唯一分解定理+组合数)

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  8. MySql union与order by

    [MySql union与order by] 如果您想使用ORDER BY或LIMIT子句来对全部UNION结果进行分类或限制,则应对单个地SELECT语句加圆括号,并把ORDER BY或LIMIT放 ...

  9. js 中的原型链与继承

    ECMAScript中将原型链作为实现继承的主要方法,其基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法. 1.原型链 先回忆一下构造函数和原型以及实例的关系:每个构造函数都有一个原型对 ...

  10. rsync+inotify实现数据实时同步

    rsync rsync是linux系统下的数据镜像备份工具.支持远程同步,本地复制,或者与其他SSH.rsync主机同步. 优点: 1).可以镜像保存整个目录树和文件系统.保存源目录整个目录树和文件系 ...