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 the sort.
- A sort is a topological sort of a DAG G iff for all i, j, if i precedes j, then there is no path from j to i.
A Lemma:
- If in a DAG G, for i, j of G, there is no path from i to j and from j to i, then there must be a topological sort of G with i preceding j.
Proof:
Suppose there is a toplogical sort with j preceding i, then with the following procedure:
For every k between j and i, with the one nearest to j processed first:
If there is a path from k to i, then move k right before j
Move i right before j
We can derive a topological order with i preceding j without breaking the validity of the order. The reason is that, since k is between j and i, then there is no path from k to j and no path from i to k. If there is a path from k to i, then there must be no path from j to k (because otherwise there would be a path from j to i). Then k can be moved before j without breaking the validity of the toplogical order. If there has been other vertices between j and i which are moved before j, then moving k right before j would keep the orginal preceding relationship between them. If there are other vertices before k that are not moved, for any such k', since there is no path from k' to i, then there is no path from k' to k (because otherwise there will be a path from k' to i), then k can also be moved in front of k' without breaking the validity of the toplogical sort. Since each step of the above procedure keeps the validity of the toplogical sort, we have derived a valid topological sort with this procedure.
Some facts about topological sort的更多相关文章
- 【拓扑排序】【线段树】Gym - 101102K - Topological Sort
Consider a directed graph G of N nodes and all edges (u→v) such that u < v. It is clear that this ...
- topological sort~~~~初学
今天讲了topological sort 问题: 判环:记录入队的点数,若<n则有环,可证: 算法:o(n):queue or stack,而不是o(n^2)枚举 #. 关系运算图(vijos ...
- topological sort
A topological sortof a dag G is a linear ordering of all its vertices such that if G contains anedg ...
- 拓扑排序(Topological Sort)
Graph 拓扑排序(Topological Sort) 假设一个应用场景:你用 C 编写了一个爬虫工具,其中有很多自定义的库:queue.c.queue.h.stack.c.stack.h.heap ...
- 6-16 Topological Sort(25 分)
Write a program to find the topological order in a digraph. Format of functions: bool TopSort( LGrap ...
- [Algorithms] Topological Sort
Topological sort is an important application of DFS in directed acyclic graphs (DAG). For each edge ...
- [MIT6.006] 14. Depth-First Search (DFS), Topological Sort 深度优先搜索,拓扑排序
一.深度优先搜索 它的定义是:递归探索图,必要时要回溯,同时避免重复. 关于深度优先搜索的伪代码如下: 左边DFS-Visit(V, Adj.s)是只实现visit所有连接某个特定点(例如s)的其他点 ...
- Leetcode: Alien Dictionary && Summary: Topological Sort
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- 拓扑排序 Topological Sort
2018-05-02 16:26:07 在计算机科学领域,有向图的拓扑排序或拓扑排序是其顶点的线性排序,使得对于从顶点u到顶点v的每个有向边uv,u在排序中都在v前.例如,图形的顶点可以表示要执行的任 ...
随机推荐
- JavaScript 之call , apply 和prototype 介绍
1. 前言 为什么将这三个概念放在一起说.原因是这些是会在实现js 继承会需要使用到的 2. call 和 apply call 和 apply 的作用基本类似, 都是去执行function并将这个f ...
- Knight Tournament 合并区间
Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the me ...
- Spring学习--静态工厂方法、实例工厂方法创建 Bean
通过调用静态工厂方法创建 bean: 调用静态工厂方法创建 bean 是将对象创建的过程封装到静态方法中 , 当客户端需要对象时 , 只需要简单地调用静态方法 , 而不需要关心创建对象的细节. 要声明 ...
- ZOJ3545 Rescue the Rabbit
分析 未知定长串中不同已知模板串的出现次数问题,一般做法是AC自动机上dp. 考虑背包,\(dp(i,j,k)\)表示当前串长为\(i\),在AC自动机上对应节点\(j\),已匹配的模板串的状态为\( ...
- MySQL--Alter Table注意事项
======================================================================== ALTER TABLE 和FLUSH TABLE导致的 ...
- 实现多线程的方式之实现Callable接口
package com.hls.juc; import java.util.concurrent.Callable;import java.util.concurrent.ExecutionExcep ...
- java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: scala/runtime/java8/JFunction1$mcII$sp
报错信息: Exception in thread "main" java.lang.BootstrapMethodError: java.lang.NoClassDefFound ...
- ALGO-3_蓝桥杯_算法训练_K好数(DP)
问题描述 如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数.求L位K进制数中K好数的数目.例如K = ,L = 2的时候,所有K好数为11...... 共7个 ...
- Spring IOC - 控制反转(依赖注入) - 单例和多例
Spring容器管理的bean在默认情况下是单例的,即一个bean只会创建一个对象,存在map中,之后无论获取多少次该bean,都返回同一个对象. Spring默认采用单例方式,减少了对象的创建,从而 ...
- 杂项:mPaaS
ylbtech-杂项:mPaaS 1. 概述返回顶部 mPaaS 是源于支付宝 App 的移动开发平台,为移动开发.测试.运营及运维提供云到端的一站式解决方案,能有效降低技术门槛.减少研发成本.提升开 ...