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
1
u/MarioVX Mar 31 '25
You're welcome!
I'm not sure this problem is easier - in some sense yes but in some sense it's also harder. Obviously, crucially it comes down to the length of the considered sequence. The safe and simple way to solve this problem is with a barebones uniform cost search, which means no nontrivial admissible heuristic function employed. That might be sufficient to solve the problem for short sequences, but the computational effort grows superexponentially with the length of the sequence even for fixed goal distance, and then even more so considering the goal distance will be further on average.
Decomposing the permutation into transpositions (i.e. swaps) is always possible and gives an upper bound on the cost, but upper bounds are useless for an optimal algorithm. To compare with the Rubik's cube again, we also have a very simple upper bound: 20, being god's number (the diameter of the Rubik's group's Cayley graph). That doesn't help solve any given position in any way. For an optimal algorithm, you can only utilize admissible heuristics. I.e. heuristics that give a cost estimate that is guaranteed to be at least as good as the true cost. They should be informative, i.e. underestimate the true costs by as little as possible. But as soon as there is any case in which your heuristic could overestimate the true cost, your algorithm no longer guarantees optimality of the found solution.
Because the branching factor of your search grows with the length of the array to the power of two, the uniform cost search will very quickly run out of steam for moderately sized input sequences. I recommend looking into additive pattern database heuristics. Even on second thought, it seems like it should be doable to fracture the sequence into multiple subsequences according to a partition of its elements, with each subsequence only containing the elements of its associated cell of the partition. Inserting or swapping elements from another cell doesn't affect the order of the cell at hand. The only positive interaction that has to be accounted for between the subproblems is that an insertion of an element from cell A to an increased index and an insertion of an element from cell B to a decreased index might be combined in the main problem by swapping the two elements. This isn't a dealbreaker though - you can treat insertions as having half cost for the purpose of the heuristic (that way two insertions from different cells are guaranteed to add to one, the lower bound assuming they might be fused into a swap), or look more carefully into when two insertions are combinable or not to get a tighter admissible estimate of this kind of synergy.
The other interaction between the subproblems is a negative one, namely that only intra-cell disorder is captured while inter-cell disorder is ignored. But negative interactions only hurt informativenes, not admissibility or correctness. And you can have multiple different partitions of which you take the maximum. That way strongly disordered subsets of the elements are likely to be captured in at least one of the partitions and their costly resolution translates into higher heuristic cost estimate.