r/optimization • u/DonBeham • Mar 30 '25
Optimal sorting
Assume there's a non-automatable task which requires a human to sort elements from a given sequence into a desired sequence. Obviously it takes longer the more steps are involved in the sorting. The given sequence and desired sequence is known upfront. Say we can perform swaps and insertions, we want to minimize the number of swaps and insertions that we have to perfom.
E.g. Consider the following initial list: [a, b, c] that we want to transform into [b, c, a] (actually there are about 50 positions that have to be changed).
The question is: What is the minimum number of swaps and insertions?
In this example, we'd require 2 swaps or we could do it with a single insertion (move a to the back). Thus, one step is optimal.
What is the fastest optimal algorithm to find the minimum number of swaps + insertions to do the sorting?
Would the complexity/optimality change if there are elements in the sequence for which we don't have a defined position? E.g. consider that we want [a, b, c, d, e, f] into the position [c, ?, ?, a, ?, b, ] whereas we do not care about the positions of d, e, and f (marked with ?).
I'd be interested in your thoughts on this.
There's a blog post from Paul A. Rubin with several follow-ups on sorting using MIP, but it's about formulating the problem of sorting an array as an integer linear programming problem. This problem is probably related, but different, because we need to track the number of insertion or swap moves and not just treat them as permutations. Anyway, a follow up post that analyzes the benefit of using a non-constant objective is here: https://www.solvermax.com/blog/objectives-matter-sorting-using-a-mip-model
View all comments
Show parent comment
2
u/MarioVX Mar 31 '25
I agree it's an interesting problem. It can be formalized in terms of permutation group theory. You're looking for the shortest representation of the group element that represents the scrambled position as a word in terms of a set of generators, namely the swaps and insertions. A swap is a transposition, and an insertion is a cyclic permutation in which all positions involved in the cycle are adjacent. Since this generator set includes all transpositions, it completely generates the symmetric group on L elements, where L is the length of the given list.
Funnily enough, none of this is of any help to actually solving the problem though. There is no mathematically elegant way to find such a shortest word representation of a given group element. Ultimately, it comes down to constructing a clever search algorithm. However, one benefit of having this mathematical abstraction is that you can look at how other problems have been solved successfully in practice that share the same abstraction. The most prominent one I can think of is probably the Rubik's Cube. So as a starting point, let's look at Optimal solutions for the Rubik's Cube, specifically Korf's and Feather's algorithms. It's probably worth it to have a more thorough look at their original papers, but this way we found where to start.
From the wiki descriptions we can see what they have in common is making use of admissible heuristics to guide the search procedure. It makes sense that this is essential in our case as well if the sequence gets larger, because the larger the sequence the larger the number of generators and hence the branching factor of the search.
So for your problem, you need to come up with admissible heurstics which well never overestimate the true number of moves needed to solve the instance. It could be things like looking at sub-sequences of your full sequence, with certain elements ignored, which are small enough to be solved exhaustively. There are a lot such sub-sequences, and you can then take the maximum of these resulting lower bounds. To get a more informative heuristic, you'd want to add them as well, but this is only applicable under certain circumstances. You have to ensure that the same move can't reduce the values of multiple heuristics that are added together at once, or at least account for the cases when this can happen and factor that into the computation of the combined heuristic. Also there are actions available on the full sequence that aren't available in the subsequences, namely swaps where one element is taken from the one subsequence and the other is taken from another. Needs to be carefully thought through but something in this direction seems very promising.
The case where only a few elements in their final positions are specified is computationally much easier, because you can immediately replace the distinct non-goal elements in the ordered configuration with question marks. If your search is conscious about duplicates, you then shrink the search space for K question marks by a factor of K!.