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,此时错误是有环): ...
随机推荐
- 1.ECS(CentOS7)主机名修改命令
命令:hostnamectl 使用--help参数查看hostnamectl里面每个参数对应的含义: [root@localhost ~]# hostnamectl --help Query or c ...
- C#实现联合体
[StructLayout(LayoutKind.Explicit, Size = )] public struct TypeTransform { [FieldOffset()] public fl ...
- Idiom: a Lot on my Plate
Idiom: a Lot on my Plate Share Tweet Share Tagged With: Idioms I’ve got a lot on my plate. American ...
- 一个简单的dropdown(CSS+jquery)
by 司徒正美 .dropdown{ position: relative; } .dropdown div{ position: relative; width:200px; height:30px ...
- LeetCode OJ 2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- C#调用C++
c++ extern "C" __declspec(dllexport) char* WINAPI base64_decode( char *data,char base[]) { ...
- java容器的理解(collection)
容器类(Conllection)对于一个开发者来说是最强大的工具之一,可以大幅提高编程能力.容器是一个将多个元素组合到一个单元的对象,是代表一组对象的对象,容器中的对象成为它的元素. 容器适用于处理各 ...
- CFDA
cfda数据抓取 1.网站数据是加密的,需要浏览器进行数据解析 2.网址url有js加密 3.PhantomJS无法解析数据, chrome无法获取数据,所有最终选择用Firefox浏览器 impor ...
- TensorFlow 辨异 —— tf.add(a, b) 与 a+b(tf.assign 与 =)、tf.nn.bias_add 与 tf.add(转)
1. tf.add(a, b) 与 a+b 在神经网络前向传播的过程中,经常可见如下两种形式的代码: tf.add(tf.matmul(x, w), b) tf.matmul(x, w) + b 简而 ...
- Java的学习02
今天依旧记录一下今天的学习的知识. /** * 测试StringBuilder StringBuffer,可变字符序列 * String对象称为“不可变对象"指的是对象内部成员变量的值无法再 ...