I have a set of items and a comparator function which defines a partial ordering -- given two items, it returns "=", "<", ">", or "no defined ordering" (say "<>"). I want to produce a sorted list of items that respects this partial ordering.
If I look for algorithms to do a topological sort, they generally start with a directed acyclic graph. But I don't have a DAG, and I can't see a simple way to construct one without doing a large number (N*N perhaps?) of comparisons. What I would like is some kind of QuickSort-like algorithm that works by comparing and swapping selected items in a list. Is there such an algorithm? I'm assuming most classical sorting algorithms would fail because of the indeterminism.
I thought of trying to use a classical sort algorithm and treating "<>" as "=", but it doesn't work because I can have the situation A < B, A <> C, B <> C, so I can't treat C as being equal to both A and B.
Any ideas or pointers?
=
in some cases makes it slightly different from regular poset sorting, but it's probably possible to adapt to that, perhaps by using a disjoint-set data structure to group equal elements together when you find them, and just sort the groups. I'd have to think a bit more about the details of that, though. – Rentsch<
means "less than" and anything else,>
,=
or<>
, means "not less than" (since, from the poset definition,a
andb
are considered incomparable simply ifa
is not less thanb
andb
is not less thana
). – Horsehair=
as meaning the same as<>
, but that means throwing away some information, and hence losing some potential efficiency. For example, if the comparator always returns=
then you only have to do O(n) comparisons to know that any ordering is fine; if the comparator always returns<>
then you have to do O(n^2) comparisons to confirm that any ordering is fine. A poset can indeed be defined just using<
, but what that means is that this comparator (which can return=
) defines something that isn't exactly a poset. – Rentsch