Topological Sort

In what kind of situation will we need to use topological sort?

Precedence(优先级) scheduling, say given a set of tasks to be completed with precedence constraints, in which order should we schedule the tasks? One simple example are classes in university, you need to take introduction to computer science before you take advanced programming, so this is the constraint, you need to do A before you do B, now I give you many tasks, and they have many constraints, in what kind of order should you do these tasks?

Where does topological sort come from?

It comes from Digraph Processing, because this kind of problem can be modeled as a DiGraph, vertex = task, edge = precedence constraint.

Limitation

Topological Sort only exists in DAG(Directed Acyclic Graph), so you need to run a DFS first to make sure that the graph is a DAG.

Algorithm

1, Run depth-first search

2, Return vertices in reverse postorder

Reverse DFS postorder of a DAG is a topological order

Code

public class DepthFirstOrder
{
    private boolean[] marked;
    private Stack<Integer> reversePost;

    public DepthFirstOrder(Digraph G)
    {
        reversePost = new Stack<Integer>();
        marked = new Boolean[G.V()];
        for (int v = 0; v < G.V(); v++)
        {
            if (!marked[v])
            {
                dfs(G, v);
            }
        }
    }

    private void dfs(Digraph G, int v)
    {
        marked[v] = true;
        for (int w : G.adj(v))
        {
            if (!marked[w])
            {
                dfs(G, w);
            }
        }
        reversePost.push(v);
    }

    public Iterable<Integer> reversePost()
    {
        return reversePost;
    }
}

时间: 2024-10-10 19:12:50

Topological Sort的相关文章

PTA Topological Sort

生病了好蓝过啊,哎还是家里好,生病都不能码代码了TAT-- 下面是题目: Write a program to find the topological order in a digraph. Format of functions: bool TopSort( LGraph Graph, Vertex TopOrder[] ); where LGraph is defined as the following: typedef struct AdjVNode *PtrToAdjVNode; s

Some facts about topological sort

Definition: a topological sort of a DAG G is a sort such that for all edge (i,j) in G, i precedes j. Then we have following corollaries: A sort is a topological sort of a DAG G iff for all i, j such that there is a path from i to j, i precedes j in t

拓扑排序(Topological Sort)

Graph 拓扑排序(Topological Sort) 假设一个应用场景:你用 C 编写了一个爬虫工具,其中有很多自定义的库:queue.c.queue.h.stack.c.stack.h.heap.c.heap.h 等等,且这些文件没有其他自定义库的依赖:另外还有一些基于上述自定义库的库:bfs.c.bfs.h.dfs.c.dfs.h.dijkstra.c.dijkstra.h.tcpSocket.c.tcpSocket.h 等等:基于以上的库,你开发了一些爬虫程序 scrawlYoutub

&lt;算法基础&gt;图的三种遍历方法————DFS,BFS,Topological sort

1.BFS(Breadth First Search) 具体实现的时候用栈来实现更简单.从start point开始,一圈圈向外. 对于例图的访问顺序是——s,a,c,d,e,b,g,f 2.DFS(Depth First Search) DFS(s){ 首先访问定点s: if(s尚有未被访问的邻居){任取其一u,递归执行DFS(u);}else{return;} } 对于例图的访问顺序是——s,a,e,f,g,b,c,d 3.Topological Sort 仅仅用于有向无环图. 从AOV网中

Topological Sort (25分)

Write a program to find the topological order in a digraph. Format of functions: bool TopSort( LGraph Graph, Vertex TopOrder[] ); where LGraph is defined as the following: typedef struct AdjVNode *PtrToAdjVNode; struct AdjVNode{ Vertex AdjV; PtrToAdj

Leetcode: Alien Dictionary &amp;&amp; Summary: Topological Sort

There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the

LeetCode编程训练 - 拓扑排序(Topological Sort)

拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序(被依赖的排在前面),或给出排序结果. 最常用解决拓扑排序问题的方法是Kahn算法,步骤可以概括为: 1. 根据依赖关系,构建邻接矩阵或邻接表.入度数组 2. 取入度为0的数据(即不依赖其他数据的数据),根据邻接矩阵/邻接表依次减小依赖其的数据的入度 3. 判断减小后是否有新的入度为0的数据,继续进

Hdoj 5195 DZY Loves Topological Sorting 【拓扑】+【线段树】

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 922 Accepted Submission(s): 269 Problem Description A topological sort or topological ordering of a directed graph i

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

DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5195 Description A topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for ev