tsort - 拓扑排序
tsort - 拓扑排序
本文链接:http://codingstandards.iteye.com/blog/834572 (转载请注明出处)
用途说明
tsort命令通常用于解决一种逻辑问题,即必须通过观察到的部分次序预测出整个次序。tsort命令可以对保存在文本文件中的数据进行拓扑排序,只要你按照一定的规则把数据写在文本文件中,然后使用tsort命令进行排序。
拓扑排序是对有向无环图的一种排序。表示了顶点按边的方向出现的先后顺序。如果有环,则无法表示两个顶点的先后顺序。
在现实生活中,也会有不少应用例子,比如学校课程布置图,要先修完一些基础课,才可以继续修专业课。
在软件开发中,比如多个模块之间的依赖关系、编译顺序,函数之间的调用关系等。
在项目管理中,各个任务之间的先后次序,某些任务完成之后才能进行另外的任务等。
一个简单的求拓扑排序的算法:首先要找到任意入度为0的一个顶点,删除它及所有相邻的边,再找入度为0的顶点,以此类推,直到删除所有顶点。顶点的删除顺序即为拓扑排序。
常用参数
无。
使用示例
示例一 来自info tsort的例子:根据部分顺序预测整体顺序
tsort输入数据的规则,把每两个数据作为一组,这两个数据就表明了一种部分顺序,每个数据之间以空白分隔,tsort处理结果就是根据这些部分顺序来推导出整体顺序来(`tsort' reads its input as pairs of strings, separated by blanks, indicating a partial ordering. The output is a total ordering that corresponds to the given partial ordering.)。比如下面的数据,从上到下,a b是一组,c d又是一组,e f是一组,b c是一组,d e是一组。
[root@new55 ~]# tsort <<EOF
> a b c
> d
> e f
> b c d e
> EOF
a
b
c
d
e
f
[root@new55 ~]#
示例二 来自info tsort的例子:函数之间的调用关系
下面例子中的call-graph文件中保存了一个C程序中函数之间的调用关系(依赖关系),这个程序看起来像是tail命令的实现,文件第一行表明 main函数调用了 parse_options函数,第二行表明 main函数调用了tail_file函数,以此类推。后面使用tsort排序再采用tac逆序排列得到的结果就是在C文件中各个函数的顺序,比如先定义dump_remainder函数,再定义start_lines函数等。
[root@new55 ~]# cat call-graph
main parse_options
main tail_file
main tail_forever
tail_file pretty_name
tail_file write_header
tail_file tail
tail_forever recheck
tail_forever pretty_name
tail_forever write_header
tail_forever dump_remainder
tail tail_lines
tail tail_bytes
tail_lines start_lines
tail_lines dump_remainder
tail_lines file_lines
tail_lines pipe_lines
tail_bytes xlseek
tail_bytes start_bytes
tail_bytes dump_remainder
tail_bytes pipe_bytes
file_lines dump_remainder
recheck pretty_name
[root@new55 ~]# tsort call-graph | tac
dump_remainder
start_lines
file_lines
pipe_lines
xlseek
start_bytes
pipe_bytes
tail_lines
tail_bytes
pretty_name
write_header
tail
recheck
parse_options
tail_file
tail_forever
main
[root@new55 ~]#
示例三 比较sort和tsort
下面的happybirthday.txt中,Happy Birthday是一组,to You!是一组,Dear Tux!是一组,每组都表明了一种顺序,因为各个组之间的数据关联性不大,所以tsort的输出结果只是拓扑排序的一种可能结果而已。这个例子来自ibm的一篇文章,对于 tsort 的使用来说,这并非一个非常有用的演示,只是举例说明了这两个命令输出的不同。
[root@new55 ~]# cat happybirthday.txt
Happy Birthday to You!
Happy Birthday to You!
Happy Birthday Dear Tux!
Happy Birthday to You!
[root@new55 ~]# sort happybirthday.txt
Happy Birthday Dear Tux!
Happy Birthday to You!
Happy Birthday to You!
Happy Birthday to You!
[root@new55 ~]# tsort happybirthday.txt
Dear
Happy
to
Tux!
Birthday
You!
[root@new55 ~]#
实例四 根据jquery/js组件的依赖关系确定加载顺序
下面的例子中js.txt中保存了js脚本之间的依赖关系,每行的第一个文件依赖于后面的一个或多个文件。但这种格式显然不符合tsort的输入数据要求,因此编写脚本deps.sh把它转换成tsort要求的格式,即每行两个文件,前一个文件依赖于后一个文件。转换之后用tsort拓扑排序,然后用tac逆序输出之后就得到了这些js文件的正确加载顺序。
[root@new55 ~]# cat js.txt
pagination.js jquery.js
xheditor.js jquery.js
AreaData.js jquery.js
jquery.progressbar.js jquery.js
jquery.cookie.js jquery.js
hotkeys.js jquery.js
jsTree.js jquery.js
jquery.contextMenu.js jquery.js
jquery.messager.js jquery.js
jquery.jtemplates.js jquery.js
jquery.sound.js jquery.js
jquery.bgiframe.js jquery.js
jquery.ui.core.js jquery.js
jquery.ui.widget.js jquery.ui.core.js jquery.bgiframe.js
jquery.ui.mouse.js jquery.ui.core.js jquery.ui.widget.js
jquery.ui.draggable.js jquery.ui.core.js jquery.ui.mouse.js jquery.ui.widget.js
jquery.ui.position.js jquery.ui.core.js jquery.js jquery.bgiframe.js
jquery.ui.resizable.js jquery.ui.core.js jquery.ui.mouse.js jquery.ui.widget.js
jquery.ui.button.js jquery.ui.widget.js jquery.js
jquery.metadata.js jquery.js
jquery.msgc.js jquery.js json2.js
ajaxfileupload.js jquery.js
jquery.ui.dialog.js jquery.bgiframe.js jquery.ui.core.js jquery.ui.widget.js jquery.ui.button.js jquery.ui.draggable.js jquery.ui.mouse.js jquery.ui.position.js jquery.ui.resizable.js
[root@new55 ~]# cat deps.sh
#!/bin/sh
# 输入数据格式:每行的第一个文件依赖于本行后面其他的文件
# 输出数据格式:符合tsort要求的数据格式,每行两个文件,前面一个依赖于后面那个。
while read js deps
do
for dep in $deps
do
echo $js $dep
done
done
[root@new55 ~]# dos2unix js.txt
dos2unix: converting file js.txt to UNIX format ...
[root@new55 ~]# cat js.txt | ./deps.sh | tsort | tac
jquery.js
jquery.ui.core.js
jquery.bgiframe.js
jquery.ui.widget.js
jquery.ui.mouse.js
json2.js
xheditor.js
pagination.js
jsTree.js
jquery.ui.resizable.js
jquery.ui.position.js
jquery.ui.draggable.js
jquery.ui.button.js
jquery.sound.js
jquery.progressbar.js
jquery.msgc.js
jquery.metadata.js
jquery.messager.js
jquery.jtemplates.js
jquery.cookie.js
jquery.contextMenu.js
hotkeys.js
ajaxfileupload.js
AreaData.js
[root@new55 ~]#
问题思考
相关资料
【1】IBM developerworks 技巧: 用 sort 和 tsort 对文件进行排序
【2】维基百科 tsort (Unix)
【3】百度百科 拓扑排序
【4】gqf2008 图论算法——拓扑排序
【5】熟能生巧 拓扑排序
【6】维基百科 拓扑排序
【7】景高专 拓扑排序演示动画
tsort - 拓扑排序的更多相关文章
- hdu3231 (三重拓扑排序) 2009 Asia Wuhan Regional Contest Hosted by Wuhan University
这道题算是我拓扑排序入门的收棺题了,卡了我好几天,期间分别犯了超时,内存溢出,理解WA,细节WA,格式WA…… 题目的意思大概是在一个三维坐标系中,有一大堆矩形,这些矩形的每条棱都与坐标轴平行. 这些 ...
- bzoj1880: [Sdoi2009]Elaxia的路线(spfa,拓扑排序最长路)
1880: [Sdoi2009]Elaxia的路线 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 1944 Solved: 759[Submit][St ...
- 算法与数据结构(七) AOV网的拓扑排序
今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...
- 有向无环图的应用—AOV网 和 拓扑排序
有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...
- 【BZOJ-2938】病毒 Trie图 + 拓扑排序
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
- BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...
- 图——拓扑排序(uva10305)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- Java排序算法——拓扑排序
package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...
- poj 3687(拓扑排序)
http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...
随机推荐
- iconfont 在项目中的简单使用
font-class引用 font-class是unicode使用方式的一种变种,主要是解决unicode书写不直观,语意不明确的问题. 与unicode使用方式相比,具有如下特点: 兼容性良好,支持 ...
- Ionic 自动创建应用的图标与启动画面
你只需要一个目录和两张图片就可以搞定.图片可以是 .png 的,Photoshop的 .psd,或者Illustrator的 .ai,(例如)命名为icon.png和splash.png.把这些图片放 ...
- Ionic入门八:头部与底部
1.Header(头部) Header是固定在屏幕顶部的组件,可以包如标题和左右的功能按钮. ionic 默认提供了许多种颜色样式,你可以调用不同的样式名,当然也可以自定义一个. <div cl ...
- STM32 串口通信
1. 中断说明 TXE(Tansmit Data Register empty interrupt) - 发送数据寄存器空,产生中断.当使能TXE后,只要Tx DR空了,就会产生中断.---写寄存器D ...
- Vue图片懒加载插件
图片懒加载是一个很常用的功能,特别是一些电商平台,这对性能优化至关重要.今天就用vue来实现一个图片懒加载的插件. 这篇博客采用"三步走"战略--Vue.use().Vue.dir ...
- centOS7下SVN的安装和使用
1. 安装 CentOS通过yum安装subversion. # yum install subversion subversion安装在/bin目录: # which svnserve #查看目录 ...
- [BZOJ3507][CQOI2014]通配符匹配(DP+Hash)
显然f[i][j]表示S匹配到第i个通配符,T匹配到第j个字符,是否可行. 一次一起转移两个通配符之间的所有字符,Hash判断. 稍微有点细节.常数极大卡时过排名倒数,可能是没自然溢出的原因. #in ...
- Socket通信原理简介
Socket通信原理简介 字数1011 阅读1766 评论2 喜欢11 何谓socket 计算机,顾名思义即是用来做计算.因而也需要输入和输出,输入需要计算的条件,输出计算结果.这些输入输出可以抽象为 ...
- bzoj 2961
根据“点在圆内”关系,列出点P(x0,y0)在圆C(x,y)内的关系: (x-x0)^2+(y-y0)^2 <= x^2+y^2 化简得: 2*x0*x+2*y0*y >= x0^2+y0 ...
- HDU 5154 Harry and Magical Computer bfs
Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...