Topological Sorting
Topological sorting/ ordering is a linear ordering of its vertices such that for every directed edge uv from vertex u to vertex v, u comes before v in the ordering. We can conduct topological sorting only on DAG (directed acyclic graph). Therefore, topological sorting is often used in scheduling a sequence of jobs or tasks based on their dependencies. Another typical usage is to check whether a graph has circle.
Here is a more detailed description -> Wiki Topological sorting

Below are several algorithms to implement topological sorting. Mostly, the time complexity is O(|V| + |E|).
Algorithm 1 -- Ordinary Way
Time complexity O(|V|^2 + |E|) In Step 1, time complexity O(|V|) for each vertex.
1. Identify vertices that have no incoming edge (If no such edges, graph has cycles (cyclic graph))
2. Select one such vertex
3. Delete this vertex of in-degree 0 and all its outgoing edges from the graph. Place it in the output.
4. Repeat Steps 1 to Step 3 until graph is empty

(referrence: cs.washington Topological Sorting)
Algorithm 2 -- Queue
Improve algorithm 1 by use queue to store vertex whose in-degree is 0. Time complex O(|V| + |E|).

Algorithm 3 -- DFS
When we conduct DFS on graph, we find that if we order vertices according to its complete time, the result if one topological sorting solution.
Time complexity O(|V| + |E|)
import java.util.*;
public class TopologicalSort {
// When we use iteration to implement DFS, we only need 2 status for each vertex
static void dfs(List<Integer>[] graph, boolean[] used, List<Integer> res, int u) {
used[u] = true;
for (int v : graph[u])
if (!used[v])
dfs(graph, used, res, v);
res.add(u);
}
public static List<Integer> topologicalSort(List<Integer>[] graph) {
int n = graph.length;
boolean[] used = new boolean[n];
List<Integer> res = new ArrayList<>();
for (int i = 0; i < n; i++)
if (!used[i])
dfs(graph, used, res, i);
Collections.reverse(res);
return res;
}
}
(referrence: DFS: Topological sorting)
Topological Sorting的更多相关文章
- hdu.5195.DZY Loves Topological Sorting(topo排序 && 贪心)
DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 ...
- Lintcode: Topological Sorting
Given an directed graph, a topological order of the graph nodes is defined as follow: For each direc ...
- URAL(timus) 1280 Topological Sorting(模拟)
Topological Sorting Time limit: 1.0 secondMemory limit: 64 MB Michael wants to win the world champio ...
- Union - Find 、 Adjacency list 、 Topological sorting Template
Find Function Optimization: After Path compression: int find(int x){ return root[x] == x ? x : (root ...
- 拓扑排序(Topological Sorting)
一.什么是拓扑排序 在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列.且该序列必须满足下面两个 ...
- Topological Sorting拓扑排序
定义: Topological Sorting is a method of arranging the vertices in a directed acyclic graph (DAG有向无环图) ...
- Course Schedule课程表12(用Topological Sorting)
[抄题]: 现在你总共有 n 门课需要选,记为 0 到 n - 1.一些课程在修之前需要先修另外的一些课程,比如要学习课程 0 你需要先学习课程 1 ,表示为[0,1]给定n门课以及他们的先决条件,判 ...
- hdu 5195 DZY Loves Topological Sorting (拓扑排序+线段树)
DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 ...
- hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序
DZY Loves Topological Sorting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...
随机推荐
- GBT28181中的RTP
国标中说h264数据按照RFC3984打包,但是国标的测试工具——SPVMN,却不支持RFC3984的打包方式.无奈之下直接用RFC3550的方式打包,其实就是分包,然后加上RTP头,对于一帧的结束, ...
- 【转】第一个MiniGUI程序:模仿QQ界面
最近几天在学MiniGui,最好的学习方法就是实践,先写个练练笔.其实只是一个界面,不知道什么时候才能真正写个完整的程序.初次写GUI程序,感觉写得不好,还请高手来指教. //============ ...
- 前Google人谈团队管理:针对不同员工的情境管理法和如何选择合理的团队规模
本文作者Tomasz Tunguz是Redpoint Ventures的风险投资人,曾在Google担任产品经理并参与过AdSense项目. 我有一个朋友,他创立了一家很成功的公司,而且还在迅速发展. ...
- maven报错cannot change version of project facet
用Eclipse创建Maven结构的web项目的时候选择了默认的catalog,由于这个catalog比较老,用的servlet还是2.3,而现在最少也是2.5,所以经常会出现问题,在Projecdt ...
- 网络基础知识HTTP(1) --转载
为什么要写网络? 作为网站开发人员,你所开发的软件产品最终是要在网络上运行的.这就像一个生产商,要生产供给东北地区的产品,而生产商对东北的天气.地理.人文毫无了解.生产商的产品肯定是不可用的,或者低端 ...
- ibatisnet框架使用说明
ibatis配置文件主要包括三个 sqlmap.config,providers.config,database.config,注意所有文件生成操作都为嵌入的资源.其中database.config主 ...
- ora-24247:网络访问被访问控制列表(ACL)拒绝
用dba账户使用下面脚本授予报错账户访问外部网络服务的权限,以SCOTT为例: BEGIN -- Only uncomment the following line if ACL "netw ...
- Oracle - 找不到原因的无效字符
当执行Oracle语句时,提示“无效字符”,而语句并无错误时,尝试把语句中的空格替换成半角状态的. 一般直接复制的语句会出现这种问题.
- C# 执行存储过程
SqlParameter[] paras = BuildParas(id, time, name); bool bRet = ExcuteProcedure(“数据库连接字符串”, "存储过 ...
- 本地计算机上的OracleOraDb11g_home2TNSListener服务启动又停止了。
电脑上装了oracle后启动很慢,然后我就不oracle服务设置成手动启动,没想到今天启动的时候居然报错 折腾了一上午,终于搞定, 在环境变量中把ORACLE_HOME 设置成D:\app\XL\pr ...