Is there a known algorithm or formula to determine how many swaps are to make a binary array A look like B?
For example if:
Array A = [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Array B = [1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0]
I took this example from here , it says that we need 10 swaps to make array B to be ordered like array A. How can we come up with such a number?
Also, it says that:
to get from the random state to the ideal state would take 6 x 9/2 flips (there are 6 bad and 9 goods)
How do we come with such a rule of ((number of zeros)*(number of ones)) / 2 To determine how many swaps it would take to operate from a random state?
EDIT:
What if the array A is more sparse? How do we determine the minimum number of swaps?
Array A = [1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1]
Assuming that the "ideal state" is a state of the form
then we can think of the process of turning an array into the ideal state as moving all the
1s to the left. If a particular1has $k$0s to its left, then we will need to do $k$ swaps where we swap that1with a0to its left.In your example
The number of
0s to the left of each1isand so the total number of swaps needed to get to the ideal state is $1 + 2 + 2 + 5 = 10$. Any algorithm which doesn't do anything stupid (that is, any algorithm which only performs
0, 1to1, 0swaps) will take this many steps.In a random state, if there are $a$
1s and $b$0s, then each1has an expected number of $\frac b2$0s to its right, and so by linearity of expectation, the total number of swaps is $a \cdot \frac b2 = \frac{ab}{2}$, which is the rule you're quoting.If we were instead trying to get from Array B to something like
then to compute the number of steps we should figure out the same quantity - the number of
0s to the left - for each1in the target Array C:Since it never makes sense to swap two adjacent
1s (that would accomplish nothing), we know that the $i^{\text{th}}$1in Array B should become the $i^{\text{th}}$1in Array C. If we compare these side by side (dropping the-s in the expressions above) we getThen we know that, for example, the $5^{\text{th}}$
1should change from having $2$0s to its left to having $6$0s to its left, which means it should be swapped with a0$4$ times. Therefore the number of swaps necessary is just the sum of the absolute differences between theZero*arrays: in this example,$$|0-0| + |0-2| + |1-2| + |2-2| + |2-6| + |5-8| = 10.$$
This algorithm has the first algorithm as a special case, since if Array A had all its
1s before all its0s then we'd haveso the sum of the absolute differences between the
Zero*arrays is just the sum of the entries ofZero(B): the sum of the number of0s to the left of each1.