<p>Look at the number of changes from one letter to the other in your string, which you could see as a measure for the string's inhomogenity. With every (useful) move of a subsequence you reduce this number by one if the subsequence you move is preceded and followed by two distinct letters. Otherwise you reduce the inhomogenity by two. </p>

<p>So for a string with k changes you need at most <em>k - l + 1</em>  moves where l is the number of different letters in the string, because in the end <em>l - 1</em> changes will remain. As a string of length n can have at most n-1 letter changes, it can need at most <em>n - l</em> moves. The least possible number is half of this.</p>

<p>The best strategy seems thus to look for subsequences of the form abbba and to move the bbb away from there. When there are none left, move whatever. You could still try to do those operations that create new abbba situations, but I think the gain will be very little. Since the worst possible strategy (without silly moves that increase the inhomogenity) uses at most twice as many moves as the optimal one, the little you might gain seems to be in no reasonable relation to the effort as the answer by <em>isaacg</em> with the characterization as NP-hard suggests. Unless, of course, you really only count the number of move operations and do not care about the time to decide which moves to make.</p>

<p>The worst case is therefore a string where every letter is different from its predecessor (and you do not get any abbba bonuses). Here you need a number of operations linear in the length of the string and almost equal to this length.</p>

<p>In your example, you have 5 -> 4 -> 3 changes, and 3 equals the number of letters (4) minus 1.</p>

<p>Side note: For an alphabet of size only two, every move that does not move a prefix or suffix of the string reduces the inhomogenity by two and thus every sequence of reasonable moves is optimal. </p>

CC BY-SA 3.0
2017-07-01T06:39:42.110Z
You claim that a move can reduce the number of changes by at most 2, but actually it can reduce the number of changes by up to 3. For example, converting "aabcabc" to "aaabbcc" by moving the first substring "bc" into the middle of the second substring "bc" results in a decrease of the number of changes in the string from 5 to 2.
Mikhail Rudoy
CC BY-SA 3.0
2017-07-01T10:56:52.750Z
Hi, @MikhailRudoy. The questions states that the operation is "pick up a subsequence of letters that are alike", so bc is not allowed as far as I understand.
Peter Leupold
CC BY-SA 3.0
2017-07-02T05:20:26.167Z
I completely missed that detail. You are correct in that case.
Mikhail Rudoy
CC BY-SA 3.0
2017-07-03T15:33:00.857Z
Peter is correct: those moves are not permitted.
Aditya Parameswaran
CC BY-SA 3.0
2017-07-03T15:39:29.113Z
Re: the rest of the answer -- indeed, these observations re: lower-bounds, optimality of the alphabet size 2 case, and heuristics for what to do at any point are valuable. Since any move in the worst case only benefits that sequence of letters, and in the best case merges at most two sequences of letters like in your abbba, a 2-approximation seems natural.
Aditya Parameswaran
CC BY-SA 3.0
2017-06-30T11:35:25.807Z
2017-07-03T10:16:16.263Z
2017-07-03T10:16:16.263Z
Peter Leupold
<p>This problem is NP-complete, by reduction from <a href="https://en.wikipedia.org/wiki/Set_cover_problem#Hitting_set_formulation" rel="nofollow noreferrer">Minimum Hitting Set</a>.</p>

<p>In minimum hitting set, we are given a universe, $U$, and a set of sets $S$ such that $\forall s \in S, s \subset U$. The objective is to find $H \subset U$ of smallest size such that $\forall s \in S, \exists h \in H$ such that $h \in s$.</p>

<p>The reduction is as follows:</p>

<ul>
<li><p>The string is as follows: For each element $u \in U$, there will be a two characters of the string, $u'$. Between these characters will a character $s'$ for each $s \in S$ such that $u \in s$. Between the pairs of $u'$s, there will be unique characters that are not repeated in the string.</p></li>
<li><p>To homogenize the string, the $s'$ character must be moved $|s|-1$ times, for each $s$.  Additionally, for each $u$, the $u'$ character must be moved once, unless every $s'$ between the pair of $u'$ has been moved elsewhere.</p></li>
<li><p>Therefore, to minimize the number of moves necessary to homogenize the string, we wish to maximize the number of $u'$ such that every $s'$ has been moved elsewhere. The $u'$s where the $s'$s have not been moved elsewhere must together contain a $s'$ for every $s \in S$, so they must for a hitting set. Moreover, any such hitting set may serve as the ending locations of the $s'$s, by moving each $s$ to the $u$ which hits it.</p></li>
<li><p>Thus, the number of moves to homogenize this string is equal to $\sum |s| + |H^*|$, where $H^*$ is the minimum hitting set.</p></li>
</ul>

<p>Since minimum hitting set is NP-Hard, optimally homogenizing a string is as well. Since the moves form a witness, it is NP-Complete.</p>

CC BY-SA 3.0
2017-07-03T15:29:58.250Z
This is an elegant reduction -- thank you!
Aditya Parameswaran
CC BY-SA 3.0
2017-07-02T11:05:49.000Z
2017-07-03T17:09:48.133Z
2017-07-03T17:09:48.133Z
isaacg
<p><b>Motivation</b>: While developing tools for data versioning, we ended up looking into algorithms for "diff"ing two sets of integers, by coming up with a sequence of transformations that take one set of integers to the other. We were able to reduce that problem to the following very natural problem that seems to have connections to edit distance, <a href="https://link.springer.com/article/10.1007/BF01759041" rel="noreferrer">grouping by swapping</a>, and <a href="https://arxiv.org/abs/1305.0649" rel="noreferrer">minimum common string partition</a>. </p>

<p><b>Problem</b>: We are given a string, i.e., a sequence of letters, and our goal is to  <em>homogenize</em> it at minimum cost. That is, we want a rearranged sequence such that all letters that are alike are next to each other. </p>

<p>The only <em>operation</em> that is permitted is to pick up a subsequence of letters that are alike, and move that subsequence anywhere, and that costs me 1 unit.</p>

<p>Any help characterizing the complexity of this problem would be much appreciated!</p>

<p><b>Example</b>:</p>

<ul>
<li>a a b c d a b: Input</li>
<li>b c d <strong>a a</strong> a b: After moving the first <strong>a a</strong> to the position right after "d"</li>
<li><strong>b</strong> b c d a a a: After moving the trailing <strong>b</strong> to the first position</li>
</ul>

<p>Since the resulting string is homogeneous, we have a cost of 2. </p>

<p>Note that we are not constrained in any way with respect to the output: as long as it is homogeneous, we don't need to ensure any specific order. </p>

<cc.complexity-theory><ds.algorithms><reference-request><string-matching><edit-distance>
Complexity of Homogenizing a String
Aditya Parameswaran
# Complexity of Homogenizing a String

(Asked by: username_0 on 2017-06-28)

**Motivation**: While developing tools for data versioning, we ended up looking into algorithms for "diff"ing two sets of integers, by coming up with a sequence of transformations that take one set of integers to the other. We were able to reduce that problem to the following very natural problem that seems to have connections to edit distance, [grouping by swapping](https://link.springer.com/article/10.1007/BF01759041), and [minimum common string partition](https://arxiv.org/abs/1305.0649).

**Problem**: We are given a string, i.e., a sequence of letters, and our goal is to *homogenize* it at minimum cost. That is, we want a rearranged sequence such that all letters that are alike are next to each other.

The only *operation* that is permitted is to pick up a subsequence of letters that are alike, and move that subsequence anywhere, and that costs me 1 unit.

Any help characterizing the complexity of this problem would be much appreciated!

**Example**:

* a a b c d a b: Input
* b c d **a a** a b: After moving the first **a a** to the position right after "d"
* **b** b c d a a a: After moving the trailing **b** to the first position

Since the resulting string is homogeneous, we have a cost of 2.

Note that we are not constrained in any way with respect to the output: as long as it is homogeneous, we don't need to ensure any specific order.

**Tags:** cc.complexity-theory, ds.algorithms, reference-request, string-matching, edit-distance

---

## 2 answers

### Answer by username_3 on 2017-07-02. Score: 6 | Accepted answer

This problem is NP-complete, by reduction from [Minimum Hitting Set](https://en.wikipedia.org/wiki/Set_cover_problem#Hitting_set_formulation).

In minimum hitting set, we are given a universe, $U$, and a set of sets $S$ such that $\forall s \in S, s \subset U$. The objective is to find $H \subset U$ of smallest size such that $\forall s \in S, \exists h \in H$ such that $h \in s$.

The reduction is as follows:

* The string is as follows: For each element $u \in U$, there will be a two characters of the string, $u'$. Between these characters will a character $s'$ for each $s \in S$ such that $u \in s$. Between the pairs of $u'$s, there will be unique characters that are not repeated in the string.
* To homogenize the string, the $s'$ character must be moved $|s|-1$ times, for each $s$. Additionally, for each $u$, the $u'$ character must be moved once, unless every $s'$ between the pair of $u'$ has been moved elsewhere.
* Therefore, to minimize the number of moves necessary to homogenize the string, we wish to maximize the number of $u'$ such that every $s'$ has been moved elsewhere. The $u'$s where the $s'$s have not been moved elsewhere must together contain a $s'$ for every $s \in S$, so they must for a hitting set. Moreover, any such hitting set may serve as the ending locations of the $s'$s, by moving each $s$ to the $u$ which hits it.
* Thus, the number of moves to homogenize this string is equal to $\sum |s| + |H^\*|$, where $H^\*$ is the minimum hitting set.

Since minimum hitting set is NP-Hard, optimally homogenizing a string is as well. Since the moves form a witness, it is NP-Complete.

> **Comments:**
> **username_0**: This is an elegant reduction -- thank you!  

---

### Answer by username_1 on 2017-06-30. Score: 2

Look at the number of changes from one letter to the other in your string, which you could see as a measure for the string's inhomogenity. With every (useful) move of a subsequence you reduce this number by one if the subsequence you move is preceded and followed by two distinct letters. Otherwise you reduce the inhomogenity by two.

So for a string with k changes you need at most *k - l + 1* moves where l is the number of different letters in the string, because in the end *l - 1* changes will remain. As a string of length n can have at most n-1 letter changes, it can need at most *n - l* moves. The least possible number is half of this.

The best strategy seems thus to look for subsequences of the form abbba and to move the bbb away from there. When there are none left, move whatever. You could still try to do those operations that create new abbba situations, but I think the gain will be very little. Since the worst possible strategy (without silly moves that increase the inhomogenity) uses at most twice as many moves as the optimal one, the little you might gain seems to be in no reasonable relation to the effort as the answer by *isaacg* with the characterization as NP-hard suggests. Unless, of course, you really only count the number of move operations and do not care about the time to decide which moves to make.

The worst case is therefore a string where every letter is different from its predecessor (and you do not get any abbba bonuses). Here you need a number of operations linear in the length of the string and almost equal to this length.

In your example, you have 5 -> 4 -> 3 changes, and 3 equals the number of letters (4) minus 1.

Side note: For an alphabet of size only two, every move that does not move a prefix or suffix of the string reduces the inhomogenity by two and thus every sequence of reasonable moves is optimal.

> **Comments:**
> **username_2**: You claim that a move can reduce the number of changes by at most 2, but actually it can reduce the number of changes by up to 3. For example, converting "aabcabc" to "aaabbcc" by moving the first substring "bc" into the middle of the second substring "bc" results in a decrease of the number of changes in the string from 5 to 2.  
> **username_1**: Hi, @MikhailRudoy. The questions states that the operation is "pick up a subsequence of letters that are alike", so bc is not allowed as far as I understand.  
> **username_2**: I completely missed that detail. You are correct in that case.  
> **username_0**: Peter is correct: those moves are not permitted.  
> **username_0**: Re: the rest of the answer -- indeed, these observations re: lower-bounds, optimality of the alphabet size 2 case, and heuristics for what to do at any point are valuable. Since any move in the worst case only benefits that sequence of letters, and in the best case merges at most two sequences of letters like in your abbba, a 2-approximation seems natural.  

---

