I'm doing some comp. mathematics work where I'm trying to sort a sequence with a complex mathematical sorting predicate, which isn't always defined between two elements in the sequence. I'm trying to learn more about sorting algorithms that gracefully handle element-wise comparisons that cannot be made, as I've only managed a very rudimentary approach so far.
My apologies if this question is some classical problem and it takes me some time to define it, algorithmic design isn't my strong suit.
Suppose I have a sequence A = {a, b, c, d, e}
. Let's define f(x,y)
to be a binary function which returns 0
if x < y
and 1
if y <= x
, by applying some complex sorting criteria.
Under normal conditions, this would provide enough detail for us to sort A
. However, f
can also return -1
, if the sorting criteria is not well-defined for that particular pair of inputs. The undefined-ness of a pair of inputs is commutative, i.e.f(q,r)
is undefined if and only if f(r,q)
is undefined.
I want to try to sort the sequence A
if possible with the sorting criterion that are well defined.
For instance let's suppose that
f(a,d) = f(d,a)
is undefined. f
are well defined. Then despite not knowing the inequality relation between a
and d
, we will be able to sort A
based on the well-defined sorting criteria as long as a
and d
are not adjacent to one another in the resulting "sorted" sequence.
For instance, suppose we first determined the relative sorting of A - {d}
to be {c, a, b, e}
, as all of those pairs to f
are well-defined. This could invoke any sorting algorithm, really.
Then we might call f(d,c)
, and
d < c
we are done - the sorted sequence is indeed {d, c, a, b, e}
.f(a, d)
. This is undefined, so we cannot establish d
's position from this angle. f(d, e)
, and move from right to left element-wise.
x
where d > x
, we are done. f(a, d)
once again, we have established that we cannot sort our sequence based on the well-defined sorting criterion we have. Is there a classification for these kinds of sorting algorithms, which handle undefined comparison pairs?
Better yet although not expected, is there a well-known "efficient" approach? I have defined my own extremely rudimentary brute-force algorithm which solves this problem, but I am certain it is not ideal.
It effectively just throws out all sequence elements which cannot be compared when encountered, and sorts the remaining subsequence if any elements remain, before exhaustively attempting to place all of the sequence elements which are not comparable to all other elements into the sorted subsequence.
Simply a path on which to do further research into this topic would be great - I lack experience with algorithms and consequently have struggled to find out where I should be looking for some more background on these sorts of problems.
This is very close to topological sorting, with your binary relation being edges. In particular, this is just extending a partial order into a total order. Naively if you consider all pairs using toposort (which is O(V+E)) you have a worst case O(n^2) algorithm (actually O(n+p) with n being the number of elements and p the number of comparable pairs).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With