Topological sorting/ ordering is a linear ordering of its vertices such that for every directed edge uv from vertex u to vertex vu 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的更多相关文章

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

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

  2. Lintcode: Topological Sorting

    Given an directed graph, a topological order of the graph nodes is defined as follow: For each direc ...

  3. URAL(timus) 1280 Topological Sorting(模拟)

    Topological Sorting Time limit: 1.0 secondMemory limit: 64 MB Michael wants to win the world champio ...

  4. Union - Find 、 Adjacency list 、 Topological sorting Template

    Find Function Optimization: After Path compression: int find(int x){ return root[x] == x ? x : (root ...

  5. 拓扑排序(Topological Sorting)

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

  6. Topological Sorting拓扑排序

    定义: Topological Sorting is a method of arranging the vertices in a directed acyclic graph (DAG有向无环图) ...

  7. Course Schedule课程表12(用Topological Sorting)

    [抄题]: 现在你总共有 n 门课需要选,记为 0 到 n - 1.一些课程在修之前需要先修另外的一些课程,比如要学习课程 0 你需要先学习课程 1 ,表示为[0,1]给定n门课以及他们的先决条件,判 ...

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

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

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

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

随机推荐

  1. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅶ(延伸:堆排序的实现)

    2.4.5 堆排序 我们可以把任意优先队列变成一种排序方法.将所有元素插入一个查找最小元素的有限队列,然后再重复调用删除最小元素的操作来将他们按顺序删去.用无序数组实现的优先队列这么做相当于进行一次插 ...

  2. <转载>构造函数声明为Private和Protected

    转载http://www.cnblogs.com/this-543273659/archive/2011/08/02/2125487.html将构造函数,析构函数声明为私有和保护的,那么对象如何创建? ...

  3. handsontable的单元格操作方法

    1.为handsontable添加钩子方法 addHook(key,callback):key为钩子方法名 <span style="font-size:18px;"> ...

  4. H Language Blueprint

    H Language Blueprint I will design the H language in the very-soon future, it will be like: 1- a scr ...

  5. Atitit. 最佳实践 QA----减少cpu占有率--cpu占用太高怎么办

    Atitit. 最佳实践 QA----减少cpu占有率--cpu占用太高怎么办 跟个磁盘队列长度雅十,一到李80%走不行兰.... 1. 寻找线程too 多的.关闭... Taskman>> ...

  6. CCA概述和安装

    什么是CCA? 客户关怀加速器(CCA)为微软动态®CRM通过集中的客户信息从不同的系统在一个集成代理桌面促进剂的效率和有效性. CCA是一个參考应用,利用用户界面集成(UII)为微软Dynamics ...

  7. C++ - 容器(container)的erase()函数

    容器(container)的erase()函数 本文地址: http://blog.csdn.net/caroline_wendy/article/details/23996013 容器(contai ...

  8. jquery使用load开展局部刷新没有效果

    jquery使用load开展局部刷新没有效果   jquery使用load进行局部刷新没有效果我的代码 <html><head><meta charset="u ...

  9. 7. 稀疏表示之OMP,SOMP算法及openCV实现

    一.前言 稀疏表示是自上世纪90年代开始,从人眼的视觉感受野获得启示,逐渐被人们所研究.现在已经发展为一种重要的信息表示方法.所谓稀疏表示是指,一个信号在过完备字典中,可以由少数个原子线性表达, 其数 ...

  10. ie6+7+8等对background-color:rgba(),background-img渐变的兼容

    一,ie8兼容rgba()的解决办法 今天遇到了一个问题,要在一个页面中设置一个半透明的白色div.这个貌似不是难题,只需要给这个div设置如下的属性即可: background: rgba(255, ...