简体   繁体   English

排序和拓扑排序有什么区别?

[英]What is the difference between sorting and topological-sorting?

What is the difference between sorting and topological-sorting? 排序和拓扑排序有什么区别?

Are they same or different thing? 它们是相同还是不同?

At an abstract level they are connected: As Saeed and Stefan say, it's the difference between a total order and a partial order. 在抽象层面上,它们是相互联系的:正如Saeed和Stefan所说,这是总订单和部分订单之间的差异。 That is a fantastically concise description, but sometimes not helpful when you're learning. 这是一个非常简洁的描述,但有时在你学习时没有帮助。

A total order means that, in the absence of repeats, when you sort something, you're going to get one unique proper answer. 总订单意味着,在没有重复的情况下,当您对某些内容进行排序时,您将获得一个独特的正确答案。 If you sort 3, 6, 2 in ascending order, you had better get one answer: 2, 3, 6. 如果按升序排序3,6,2,最好得到一个答案:2,3,6。

A partial order is a little looser. 部分订单稍微宽松一些。 The canonical example is the order in which you put your clothes on: You could put your shorts, then your pants, then your socks, then your shoes. 典型的例子是你穿上衣服的顺序:你可以穿短裤,然后是裤子,袜子,鞋子。 That's a valid order. 这是一个有效的订单。 Or you could do shorts, socks, pants, shoes. 或者你可以做短裤,袜子,裤子,鞋子。 But intuitively, you can't do shorts, pants, shoes, socks. 但直觉上,你不能做短裤,裤子,鞋子,袜子。 It doesn't make sense to put the socks on after the shoes. 鞋子穿上袜子是没有意义的。

To formalize that dressing example, you usually show a dependency graph with actions ("put on shoes") as nodes, and directed arcs showing what node must precede what other nodes. 为了使该修整示例正式化,您通常会显示一个依赖关系图,其中包含操作(“穿上鞋子”)作为节点,以及显示哪个节点必须先于其他节点的有向弧。 A topological sort is an ordering of all nodes in a graph like that which respects the arcs. 拓扑排序是图中所有节点的排序,如同尊重弧的节点。 Meaning, if there's an arc from socks to shoes, then socks better be before shoes in the order. 意思是,如果从袜子到鞋子有弧形,那么袜子最好在订单之前。

So again, at an abstract level, they're connected. 因此,在抽象层面上,它们是相互联系的。 But they are absolutely NOT the same thing. 但它们绝对不是一回事。

拓扑排序通常是指找到符合某种偏序的总顺序,例如有向非循环图中的可达性关系。

In the topological sort, we work on a partially ordered set but in normal sorting, we work on a total ordered set . 在拓扑排序中,我们处理部分有序的集合,但在正常排序中,我们处理总有序集合

In a topological sort maybe there isn't any relation between a pair of elements of the set, like in the directed graphs, between some nodes there isn't any relation. 在拓扑排序中,可能在集合的一对元素之间没有任何关系,如在有向图中,在一些节点之间没有任何关系。 In normal sorting, all pairs of elements of the set have a relation. 在正常排序中,集合的所有元素对都具有关系。 For instance, in the set of numbers we have the relation <,>,= between all pairs, so it is total ordered. 例如,在数字集合中,我们在所有对之间具有关系<,>,=,因此它是完全有序的。

If a total order is available every object can be compared with every object. 如果总订单可用,则可以将每个对象与每个对象进行比较。 In this case you can sort wrt. 在这种情况下,您可以对wrt进行排序。 that order. 那个命令。 Examples are the integers wrt. 例子是整数wrt。 > (or <, or <=, ...) or string wrt. >(或<,或<=,...)或字符串wrt。 the lexicographical ordering. 词典排序。 If you have a total order sorting is possible. 如果您有总订单,则可以进行分类。

If only a partial order is available, not every object can be compared with every other object. 如果只有部分订单可用,则不是每个对象都可以与每个其他对象进行比较。 Only a relation between certain objects is available. 只有某些对象之间的关系可用。 An example are dependencies between compilation units. 一个例子是编译单元之间的依赖关系。 Topological sorting is the task to find an ordering of the objects such that the partial order is respected (eg by compiling units which depend on some other unit after these units). 拓扑排序是查找对象排序以使得部分顺序得到遵守的任务(例如,通过编译依赖于这些单元之后的某个其他单元的单元)。 Here several solutions (ie orderings) are possible: If A depends on B and there is some other unit C, possible compilation sequences are B,A,C and C,A,B (every sequence where A is compiled before B). 这里有几种解决方案(即排序)是可能的:如果A依赖于B并且存在一些其他单元C,则可能的编译序列是B,A,C和C,A,B(在B之前编译A的每个序列)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM