https://www.reddit.com/r/algorithms/comments/7sqa2p/shortest_supersequence/
X. https://github.com/careercup/CtCI-6th-Edition/blob/master/Java/Ch%2017.%20Hard/Q17_18_Shortest_Supersequence/QuestionA.java
Shortest Supersequence: You are given two arrays, one shorter (with all distinct elements) and one longer. Find the shortest subarray in the longer array that contains all the elements in the shorter array. The items can appear in any order.
EXAMPLE
Input:
{1, 5, 9}
{7, 5, 9, 0, 2, 1, 3, 5, 7, 9, 1, 1, 5, 8, 8, 9, 7}
^^^^^^^^^^
Output: [7, 10] (the underlined portion above)
I'll use the symbols B and S both for the lengths and the names of the two arrays. So array S has length S and array B has length B, with S<=B.
The author gives 3 solutions of time complexity O(B2S), O(SB) and O(B logS). I think I have a straightforward O(B) solution.
Let me know if you find any mistakes. I know I could just google for it, but it's more fun and more instructive to explain it to others, read comments, eventual counterexamples, etc... I didn't even test it because I want to use only pen and paper like in the old days.
The main idea
For i=0,1,...,B-1, we want to find the shortest supersequence which starts at position i. Any solution is among those supersequences.
We can use two indices L and R which both start from position 0, go to the right, and never go left.
Let's consider the example above. We start from here:
R
7 (5) (9) 0 2 (1) 3 (5) 7 (9) (1) (1) (5) 8 8 (9) 7
L
Note that the numbers in S (the short array) are in parentheses for our convenience.
Now we move R to the right and stop as soon as we have all the numbers in S:
R
7 (5) (9) 0 2 (1) 3 (5) 7 (9) (1) (1) (5) 8 8 (9) 7
L
We've found are first supersequence. This is clearly the shortest supersequence starting from L, so let's start looking for the shortest one starting from L+1:
R
7 (5) (9) 0 2 (1) 3 (5) 7 (9) (1) (1) (5) 8 8 (9) 7
L
Note that we don't need to touch R because there can't be any shorter supersequence than the one indicated by L and R. If there were, we could use that to find a shorter supersequence from the previous L as well.
Note also that, in our case, we've already found the shortest supersequence from the new L (and it's even the best we've seen so far).
We move L again and we get:
R
7 (5) (9) 0 2 (1) 3 (5) 7 (9) (1) (1) (5) 8 8 (9) 7
L
This time we need to move R because we don't have a supersequence anymore: we need a 5:
R
7 (5) (9) 0 2 (1) 3 (5) 7 (9) (1) (1) (5) 8 8 (9) 7
L
We can proceed this way until the end and we won't miss any (promising) supersequence.
Bookkeeping
We just need:
- counter: HashMap which maps any
e
of S to how many timese
occurs in B[L..R]. - missing: number of missing elements from B[L..R] for it to be a supersequence
- min_len, min_L, min_R: shortest supersequence found so far
Initially, we set each counter to 0. When we increment L, we drop an element
e
so, if e
is in counter
(i.e. in S), we decrement counter[e]. If counter[e] becomes 0, we increment missing
. When we increment R, we add an element e
so, if e
is in counter
(i.e. in S), we increment counter[e]. If counter[e] was 0, we decrement missing
. We have a supersequence iff missing
is 0. We can do all of this in time O(1).
Since L and R start from 0 and are always incremented and each increment takes time O(1), the whole algorithm takes time O(B).
The text says that all the elements of S are distinct, but, actually, I don't see why my method shouldn't work for when they're not distinct with a minor generalization. Instead of incrementing/decrementing
missing
when we move between 0 and 1, we'll do that when we move between num_occurrences_in_S
-1 and num_occurrences_in_S
. For instance, if we need 3 'a', we decrement missing
when we hit 3 and increment it when we go back to 2. That's it, I think.X. https://github.com/careercup/CtCI-6th-Edition/blob/master/Java/Ch%2017.%20Hard/Q17_18_Shortest_Supersequence/QuestionA.java
public static int findNextInstance(int[] array, int element, int index) {
for (int i = index; i < array.length; i++) {
if (array[i] == element) {
return i;
}
}
return -1;
}
/* Given an index, find the closure (i.e., the element which terminates a complete
* subarray containing all elements in smallArray). This will be the max of the
* next locations of each element in smallArray. */
public static int findClosure(int[] bigArray, int[] smallArray, int index) {
int max = -1;
for (int i = 0; i < smallArray.length; i++) {
int next = findNextInstance(bigArray, smallArray[i], index);
if (next == -1) {
return -1;
}
max = Math.max(next, max);
}
return max;
}
public static Range shortestSupersequence(int[] bigArray, int[] smallArray) {
int bestStart = -1;
int bestEnd = -1;
for (int i = 0; i < bigArray.length; i++) {
int end = findClosure(bigArray, smallArray, i);
if (end == -1) break;
if (bestStart == -1 || end - i < bestEnd - bestStart) {
bestStart = i;
bestEnd = end;
}
}
if (bestStart < 0 || bestEnd < 0) {
return null;
}
return new Range(bestStart, bestEnd);
}