patternMinor
Name of this rearranging/sorting problem?
Viewed 0 times
thissortingproblemrearrangingname
Problem
You are given an array of length $n$. Each element of the array belongs to one of $K$ classes. You are supposed to rearrange the array using minimum number of swap operations so that all elements from the same class are always grouped together, that is they form a contiguous subarray.
For example:
$$
\begin{align*}
&[2, 1, 3, 3, 2, 2] \longrightarrow [2, 2, 2, 1, 3, 3], \text{ or} \\
&[2, 1, 3, 3, 2, 2] \longrightarrow [1, 2, 2, 2, 3, 3], \text{ or} \\
&[2, 1, 3, 3, 2, 2] \longrightarrow [3, 3, 2, 2, 2, 1].
\end{align*}
$$
Three other valid arrangements remain.
What is this problem called in literature? Is there an efficient algorithm for it?
For example:
$$
\begin{align*}
&[2, 1, 3, 3, 2, 2] \longrightarrow [2, 2, 2, 1, 3, 3], \text{ or} \\
&[2, 1, 3, 3, 2, 2] \longrightarrow [1, 2, 2, 2, 3, 3], \text{ or} \\
&[2, 1, 3, 3, 2, 2] \longrightarrow [3, 3, 2, 2, 2, 1].
\end{align*}
$$
Three other valid arrangements remain.
What is this problem called in literature? Is there an efficient algorithm for it?
Solution
Note: It is a hardness proof, and I think there are practical algorithms like integer programming, etc.
Given a BIN_PACKING instance where you want to pack $K$ numbers $n_1,\ldots,n_K$ into $L$ bins of size $m_1,\ldots,m_L$, and it is ensured that $\sum n_i=\sum m_j=N$, then we could design a instance of your problem as follows:
$$m_1,(N+1)^2,m_2,(N+1)^2,m_3,\ldots,(N+1)^2,m_L$$
where each slot of size $(N+1)^2$ is packed with $N+1$ classes, arranged contiguously, and the rest are arbitrarily arranged.
Now a key observation is that it is meaningless to keep at least one class in a $(N+1)^2$ slot unmoved and move other ones (because it won't change the size of a 'bin'). So the original bin packing is available if and only if the minimum number of swaps is no larger than $N$. Since BIN-PACKING is known to be strongly NP-complete, your problem is NP-hard.
Given a BIN_PACKING instance where you want to pack $K$ numbers $n_1,\ldots,n_K$ into $L$ bins of size $m_1,\ldots,m_L$, and it is ensured that $\sum n_i=\sum m_j=N$, then we could design a instance of your problem as follows:
- There are $K+(N+1)(L-1)$ classes;
- The first $K$ classes have size $n_1,\ldots,n_K$ respectively, and each of the rest classes have size $N+1$;;
- The array is partitioned into slots of size:
$$m_1,(N+1)^2,m_2,(N+1)^2,m_3,\ldots,(N+1)^2,m_L$$
where each slot of size $(N+1)^2$ is packed with $N+1$ classes, arranged contiguously, and the rest are arbitrarily arranged.
Now a key observation is that it is meaningless to keep at least one class in a $(N+1)^2$ slot unmoved and move other ones (because it won't change the size of a 'bin'). So the original bin packing is available if and only if the minimum number of swaps is no larger than $N$. Since BIN-PACKING is known to be strongly NP-complete, your problem is NP-hard.
Context
StackExchange Computer Science Q#82277, answer score: 7
Revisions (0)
No revisions yet.