8
$\begingroup$

I'd like an efficient method for calculating the minimum number of transpositions needed to sort a list. I don't need to know what the transpositions actually are.

For example, the list [1, 1, 2, 0] requires 2 transpositions:

[1, 1, 2, 0] // Start
[1, 1, 0, 2] // Swap index 2 and 3
[0, 1, 1, 2] // Swap index 0 and 2

The list [0, 1, 0, 0] requires 1 transposition:

[0, 1, 0, 0] // Start
[0, 0, 0, 1] // Swap index 1 and 3

The list [2, 2, 2, 2] requires 0 transpositions because it is already sorted.

Some meta information: 1) The list may have repeated elements, so simply using the Cayley distance between the sort and the identity permutation will not work. 2) This Math Overflow question is related.

$\endgroup$
5
  • 1
    $\begingroup$ Duplicate of cstheory.stackexchange.com/questions/4096/… $\endgroup$
    – derekhh
    Commented Oct 2, 2012 at 7:19
  • 1
    $\begingroup$ I think that the answer(s) posted here should be merged to question 4096. $\endgroup$ Commented Oct 2, 2012 at 12:16
  • 1
    $\begingroup$ @derekhh, this is not a duplicate (or at least the interpretation of the post is different in the other question). I linked to question 4096 in the original post. $\endgroup$ Commented Oct 2, 2012 at 15:59
  • 3
    $\begingroup$ Question 4096 does not state that given elements are distinct. Unfortunately, all the answers posted there silently assumed this, and this oversight can be corrected by merging the answer(s) here to there. $\endgroup$ Commented Oct 2, 2012 at 16:21
  • 1
    $\begingroup$ @emchristiansen Oops, sorry, I haven't noticed that... $\endgroup$
    – derekhh
    Commented Oct 3, 2012 at 6:28

1 Answer 1

7
$\begingroup$

Unfortunately, the problem is NP-hard in general, according to Amir, Hartman, Kapah, Levy and Porat (see http://epubs.siam.org/doi/abs/10.1137/080712969), even if each symbol appears at most three times. I didn't find a mention of a constant factor approximation algorithm, or of something reasonably fast to tackle the problem. Are there any additional restrictions you can think of in your case?

EDIT: the authors also give a 3/2-approximation algorithm, but I don't know if it's good enough for your purposes.

$\endgroup$
2
  • $\begingroup$ Thanks for the reference. I'm dealing with pixel values, so lists of integers in [0, 255]. A common list length is 256 elements. $\endgroup$ Commented Oct 2, 2012 at 15:56
  • $\begingroup$ Can you prove, that when you have a solution (minimal sorting sequence of transposition), you always swap large number on the left with a smaller number on the right? $\endgroup$ Commented Jan 1, 2016 at 0:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.