什么是排序部分有序列表的最佳方法是什么?部分、方法、列表

2023-09-11 22:55:51 作者:Lucky One

可能是最好的一个小例子来说明。 由于关系

Probably best illustrated with a small example. Given the relations

A < B < C
A < P < Q

正确的输出将

Correct outputs would be

ABCPQ or APQBC or APBCQ ... etc.

在换言之,任何顺序是有效,其中给定的关系式成立。

In other words, any ordering is valid in which the given relationships hold.

我最感兴趣的解决方案是最容易实现的,但最好为O(n)的速度和时间有趣的是也。

I am most interested in the solution that is easiest to implement, but the best O(n) in speed and time is interesting as well.

推荐答案

这就是所谓的拓扑排序。

标准的算法,输出一个最小的元素,然后将其删除,重复,直到完成。

The standard algorithm is to output a minimal element, then remove it and repeat until done.