0

Story: I was trying to do a leetcode problem that wanted me to calculate the minimum number of non-adjacent swaps (i.e transpositions) to sort an array. This led me into a rabbit hole, learning about cycle decompositions of permutations. Although it's interesting, I am on a time crunch.

Question: Why does sorting an array of length n require at least n - c transpositions, i.e why can't it be done in less?

I saw an answer here: Prove or disprove: The minimum number of transpositions needed to decompose $\sigma$ is $n-S$. but I don't understand the notation. Could someone explain that in layman's terms?

  • What is $c$? We cannot tell about symbols you don't define. Also there is no "non-adjacent" condition in the definition of a "transposition". If you are going to add that condition, then you can't sort an array of length 2 at all. In the notation $(a,b,c,\dots,d)$ for a cyclic permutation, the letters each represent a location in the array, and the permutation moves the content of location $a$ to location $b$, whose content is moved to $c$, etc, until finally the content of $d$ is moved to location $a$. If $\sigma, \tau$ are two permutations, $\sigma\tau$ performs $\tau$ first, then $\sigma$. – Paul Sinclair Apr 08 '23 at 13:05

1 Answers1

1

Any permutation of an array (we normally don't describe things in terms of "arrays", but it is the same idea, and language you are familiar with) can be broken in cycles as follows: Start with the first location. Where was its content moved to? Okay, the content of that location had to be moved somewhere else to make room. Where did it go? Keep going like that until you encounter the location whose content was moved to the first location. This forms a cycle.

There is no guarantee this cycle included every location in the array. If not, find the first location that was missed, and do the same for it. You cannot encounter a location in the first cycle, as we already know what was moved into it, and it came from somewhere else. Eventually, you will encounter a location that closes the cycle by moving its content to the location you started with. Rinse and repeat. Sometimes your starting location didn't move at all. That is a cycle of length $1$.

Now that you have broken the permutation $\sigma$ into cycles, suppose you perform a transposition $\tau = (a_1, a_2)$ first, exchanging the contents of locations $a_1$ and $a_2$. Each of $a_1$ and $a_2$ is in a cycle of $\sigma$:

They may be in the same cycle or in different cycles.

Applying $\tau$ puts the content $C_1$ of $a_1$ in $a_2$. When $\sigma$ is applied afterwords, it moves $C_1$ from $a_2$ to the next location in its cycle, and similarly for the original content of $a_2$:

When $a_1,a_2$ are in different cycles, if we trace the cycle of $a_1$ in $\sigma\tau$, we see that it immediately drops to the lower cycle, then follows that cycle around until it comes back to $a_2$, where it switches back to the upper cycle, and follows it around until it comes back to $a_1$. The two separate cycles of $\sigma$ have been combined into a single cycle of $\sigma\tau$. The remaining cycles of $\sigma$ are completely unaffected by $\tau$, since it didn't change their contents at all. Thus in this case, $\sigma\tau$ has one fewer cycles than $\sigma$.

The other case is when $a_1$ and $a_2$ lie on the same cycle of $\sigma$. Following the cycle from $a_1$, we eventually get to $a_2$, and then eventually get back to $a_1$. But in $\sigma\tau$, the swap means that starting from $a_1$, we immediately skip to the successor of $a_2$ and then follow the remainder of the cycle around to $a_1$. Similarly, starting at $a_2$, we immediately skip to the successor of $a_1$ and follow the remainder of the cycle around to $a_2$ again. So the single original cycle in $\sigma$ is now two cycles in $\sigma\tau$, which has one more cycle than $\sigma$. Thus as Angina Seng said in the other thread, $\sigma\tau$ has either one fewer or one more cycle than $\sigma$.


Now to decompose $\sigma$ into transpositions is to show that $\sigma = \tau_1\tau_2\dots\tau_k$ for some $k$, where each of the $\tau_i$ is a transposition. Let $\iota$ be the identity permutation - i.e., it doesn't move anything. Then we can also write $$\sigma = \iota\tau_1\tau_2\dots\tau_k$$.

But note that if a location is unchanged by a permutation, it forms its own cycle of length $1$. Thus for an array of size $n$, the identity permutation $\iota$ has $n$ cycles. And by the argument above, $\iota\tau_1$ must have at least $n-1$ cycles. So $\iota\tau_1\tau_2$ must have at least $n-2$ cycles, and so on. Thus $\iota\tau_1\dots\tau_k$ must have at least $n - k$ cycles. If $\sigma$ has $c$ cycles, then $c \ge n - k$ and so $k \ge n - c$.

A permutation of $n$ elements with $c$ cycles requires at least $n - c$ tranpositions to decompose.

Paul Sinclair
  • 45,932
  • Thanks for the response! Im most following your logic, but it doesn’t make any intuitive sense to me still.

    What's a simple, intuitive explanation for why there's no magic combination of k−2 transpositions (or less) for a k−cycle?

    – 9j09jf02jsd Apr 11 '23 at 10:18
  • How can you get more "intuitive" than 'You cannot get to a destination $k-1$ away by taking only $k-2$ steps of size $1$"? – Paul Sinclair Apr 11 '23 at 11:42
  • Some steps can be size 2, if the swap happens to put both in the right place, so it is not as simple as that. I’m looking for an intuitive explanation for why this is impossible. – 9j09jf02jsd Apr 12 '23 at 19:17
  • No. No steps can be two. That is what I showed in the post. If you think you can get two, show me an example. Starting at the identity permutation, which has $k$ separate cycles of length 1, each added transposition will either increase the number of cycles by $1$ or decrease the number of cycles by $1$. Assuming you always choose transpositions that decrease the number of cycles, it takes $k-1$ transpositions to get the number of cycles down to one for the single $k$-cycle being built. – Paul Sinclair Apr 12 '23 at 19:48
  • I think we're talking about different things. What I'm saying is this: suppose we had an array of [3, 1, 2]. Then we swap 3 and 1, so we have [1,3,2]. This moves "1" in its correct position. Then we swap 3 and 2, so we have [1,2,3]. This moves both "2" and "3" in their correct positions. Imagine we had a larger array. What's an intuitive explanation for why we couldn't, in principle, make multiple swaps of the latter type where two elements get put in their correct positions? In which case, we'd have less than n - c total transpositions, where c is the number of cycles? – 9j09jf02jsd Apr 13 '23 at 03:12
  • No, you would not, as I've already explained. I've shown you that applying a transposition only changes the number of cycles by 1. In your description, you've just decided to not bother keeping track of the cycles. But just because you are refusing to look at them does not make the cycles go away and suddenly allow the thing I've already shown you cannot occur. You are still applying transpositions, and they are still changing the number of cycles one-at-a-time, and you still cannot go from $1$ to $n$ or vice-versa in less than $n-1$ steps. – Paul Sinclair Apr 13 '23 at 03:49
  • Fair enough. I've been trying to find an explanation that doesn't rely on the underlying structure, that is, the cycles, but perhaps it is ultimately necessary to do so to explain it. Your cycle explanation is excellent. – 9j09jf02jsd Apr 13 '23 at 21:54
  • It would be hard to prove that $n - c$ is the minimum number of transpositions without looking at cycles, when $c$ is defined as the number of cycles. – Paul Sinclair Apr 14 '23 at 01:55
  • Yep you're right; since I was seeking intuition for my original confusion, which was why cycle sort is guaranteed to achieve the minimum number of swaps, I should have formulated as such. – 9j09jf02jsd Apr 14 '23 at 07:03