Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute the minimal number of swaps to order a sequence

I'm working on sorting an integer sequence with no identical numbers (without loss of generality, let's assume the sequence is a permutation of 1,2,...,n) into its natural increasing order (i.e. 1,2,...,n). I was thinking about directly swapping the elements (regardless of the positions of elements; in other words, a swap is valid for any two elements) with minimal number of swaps (the following may be a feasible solution):

Swap two elements with the constraint that either one or both of them should be swapped into the correct position(s). Until every element is put in its correct position.

But I don't know how to mathematically prove if the above solution is optimal. Anyone can help?

like image 385
mintaka Avatar asked Mar 01 '13 07:03

mintaka


People also ask

What is the minimum number of swaps?

Now a cycle with 2 nodes will only require 1 swap to reach the correct ordering, similarly, a cycle with 3 nodes will only require 2 swaps to do so. Hence, ans = Σi=1k(cycle_size – 1)

How many swaps are required to sort the given array 2 5 1 4 3 using bubble sort?

Explanation: Even though the first two elements are already sorted, bubble sort needs 4 iterations to sort the given array.

How do you find the number of swaps?

So to find the number of swaps, we just count the number of smaller elements on the right side than the current element.

What is the minimum number of swaps required to sort an array?

So we can generalize that for a cycle of size n, we need at most n – 1 swaps to sort it. So, to sort the entire array, we will need to sum up (cycle size – 1) over all the cycles of the graph, which can be performed easily with a standard Depth First Search.


1 Answers

I was able to prove this with graph-theory. Might want to add that tag in :)

Create a graph with n vertices. Create an edge from node n_i to n_j if the element in position i should be in position j in the correct ordering. You will now have a graph consisting of several non-intersecting cycles. I argue that the minimum number of swaps needed to order the graph correctly is

M = sum (c in cycles) size(c) - 1 

Take a second to convince yourself of that...if two items are in a cycle, one swap can just take care of them. If three items are in a cycle, you can swap a pair to put one in the right spot, and a two-cycle remains, etc. If n items are in a cycle, you need n-1 swaps. (This is always true even if you don't swap with immediate neighbors.)

Given that, you may now be able to see why your algorithm is optimal. If you do a swap and at least one item is in the right position, then it will always reduce the value of M by 1. For any cycle of length n, consider swapping an element into the correct spot, occupied by its neighbor. You now have a correctly ordered element, and a cycle of length n-1.

Since M is the minimum number of swaps, and your algorithm always reduces M by 1 for each swap, it must be optimal.

like image 123
Andrew Mao Avatar answered Oct 18 '22 20:10

Andrew Mao