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 ... 
随机推荐
- UESTC_小panpan学图论 2015 UESTC Training for Graph Theory<Problem J>
			J - 小panpan学图论 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) S ... 
- ios 读取通讯录数据
			#import <Foundation/Foundation.h> @interface LoadingContactData : NSObject // 读取通讯录 + (Loading ... 
- 地下迷宫(bfs输出路径)
			题解:开一个pre数组用编号代替当前位置,编号用结构题另存,其实也可以i*m+j来代替,我写的有点麻烦了; 代码: #include <iostream> #include <cst ... 
- 开源 免费 java CMS - FreeCMS2.0 会员我的评论
			项目地址:http://www.freeteam.cn/ 我的评论 从左側管理菜单点击我的评论进入. 在这里能够查看当前登录会员的全部评论记录. 删除评论 选择评论然后点击删除button能够完毕删除 ... 
- flexible.js字体大小诡异现象解析及解决方案
			最近在做一个手机端页面时,遇到了一个奇怪的问题:字体的显示大小,与在CSS中指定的大小不一致.大家可以查看这个Demo(记得打开Chrome DevTools). 就如上图所示,你可以发现,原本指定的 ... 
- Linux 编译安装 apache 2.4
			在安装apache之前需要准备一些必要的依赖包 gcc安装: #yum install -y gcc gcc-c++安装: #yum install gcc-c++ apr安装: 下载包:apr-1 ... 
- Docker搭建私有仓库
			1,下载仓库镜像. docker pull registry //主要用于搭建私有仓库的. 2,将宿主机端口映射到容器中去,容器的5000端口是不能更改的. docker run -d -p ... 
- AE分级渲染
			分级渲染classbreakrenderer位于carto类库中,进行分级渲染时,首先要将相应图层按照某一Field分级.可使用esrisystem类库中的iclassifyGEN类的classify ... 
- 基于avalon1.4.x ----分页组件编写
			avalon分页组件 (1.4.x版本) 随着avalon2的推出,avalon1的官网已经不再维护了,现在似乎是找不到avalon 1.4版本的官方文档了,所以本文章所有的内容均不保证正确性,只能保 ... 
- HDU 5735 - Born Slippy
			题意: 一棵 n 个节点的根树,i 节点权重 wi 对每一个节点s,找到这样一个长 m 的标号序列 v : 1. vi是vi-1 的祖先 2. f[s] = w[vi] + ∑(i=2, m) (w[ ... 
