Open In App
Related Articles

Shortest Superstring Problem

Improve Article
Improve
Save Article
Save
Like Article
Like

Given a set of n strings arr[], find the smallest string that contains each string in the given set as substring. We may assume that no string in arr[] is substring of another string.
Examples: 

Input:  arr[] = {"geeks", "quiz", "for"}
Output: geeksquizfor
Input: arr[] = {"catg", "ctaagt", "gcta", "ttca", "atgcatc"}
Output: gctaagttcatgcatc

Shortest Superstring Greedy Approximate Algorithm 

Shortest Superstring Problem is a NP Hard problem. A solution that always finds shortest superstring takes exponential time. Below is an Approximate Greedy algorithm. 

Let arr[] be given set of strings.
1) Create an auxiliary array of strings, temp[]. Copy contents
of arr[] to temp[]
2) While temp[] contains more than one strings
a) Find the most overlapping string pair in temp[]. Let this
pair be 'a' and 'b'.
b) Replace 'a' and 'b' with the string obtained after combining
them.
3) The only string left in temp[] is the result, return it.

Two strings are overlapping if prefix of one string is same suffix of other string or vice versa. The maximum overlap mean length of the matching prefix and suffix is maximum.

Working of above Algorithm: 

arr[] = {"catgc", "ctaagt", "gcta", "ttca", "atgcatc"}
Initialize:
temp[] = {"catgc", "ctaagt", "gcta", "ttca", "atgcatc"}
The most overlapping strings are "catgc" and "atgcatc"
(Suffix of length 4 of "catgc" is same as prefix of "atgcatc")
Replace two strings with "catgcatc", we get
temp[] = {"catgcatc", "ctaagt", "gcta", "ttca"}
The most overlapping strings are "ctaagt" and "gcta"
(Prefix of length 3 of "ctaagt" is same as suffix of "gcta")
Replace two strings with "gctaagt", we get
temp[] = {"catgcatc", "gctaagt", "ttca"}
The most overlapping strings are "catgcatc" and "ttca"
(Prefix of length 2 of "catgcatc" as suffix of "ttca")
Replace two strings with "ttcatgcatc", we get
temp[] = {"ttcatgcatc", "gctaagt"}
Now there are only two strings in temp[], after combing
the two in optimal way, we get tem[] = {"gctaagttcatgcatc"}
Since temp[] has only one string now, return it.

Below is the implementation of the above algorithm.  

C++




// C++ program to find shortest
// superstring using Greedy
// Approximate Algorithm
#include <bits/stdc++.h>
using namespace std;
 
// Utility function to calculate
// minimum of two numbers
int min(int a, int b)
{
    return (a < b) ? a : b;
}
 
// Function to calculate maximum
// overlap in two given strings
int findOverlappingPair(string str1,
                     string str2, string &str)
{
     
    // Max will store maximum
    // overlap i.e maximum
    // length of the matching
    // prefix and suffix
    int max = INT_MIN;
    int len1 = str1.length();
    int len2 = str2.length();
 
    // Check suffix of str1 matches
    // with prefix of str2
    for (int i = 1; i <=
                      min(len1, len2); i++)
    {
         
        // Compare last i characters
        // in str1 with first i
        // characters in str2
        if (str1.compare(len1-i, i, str2,
                                 0, i) == 0)
        {
            if (max < i)
            {
                // Update max and str
                max = i;
                str = str1 + str2.substr(i);
            }
        }
    }
 
    // Check prefix of str1 matches
    // with suffix of str2
    for (int i = 1; i <=
                        min(len1, len2); i++)
    {
         
        // compare first i characters
        // in str1 with last i
        // characters in str2
        if (str1.compare(0, i, str2,
                              len2-i, i) == 0)
        {
            if (max < i)
            {
                 
                // Update max and str
                max = i;
                str = str2 + str1.substr(i);
            }
        }
    }
 
    return max;
}
 
// Function to calculate
// smallest string that contains
// each string in the given
// set as substring.
string findShortestSuperstring(string arr[],
                                    int len)
{
     
    // Run len-1 times to
    // consider every pair
    while(len != 1)
    {
         
        // To store  maximum overlap
        int max = INT_MIN;  
       
        // To store array index of strings
        int l, r;   
       
        // Involved in maximum overlap
        string resStr;   
       
        // Maximum overlap
        for (int i = 0; i < len; i++)
        {
            for (int j = i + 1; j < len; j++)
            {
                string str;
 
                // res will store maximum
                // length of the matching
                // prefix and suffix str is
                // passed by reference and
                // will store the resultant
                // string after maximum
                // overlap of arr[i] and arr[j],
                // if any.
                int res = findOverlappingPair(arr[i],
                                         arr[j], str);
 
                // check for maximum overlap
                if (max < res)
                {
                    max = res;
                    resStr.assign(str);
                    l = i, r = j;
                }
            }
        }
 
        // Ignore last element in next cycle
        len--;  
 
        // If no overlap, append arr[len] to arr[0]
        if (max == INT_MIN)
            arr[0] += arr[len];
        else
        {
           
            // Copy resultant string to index l
            arr[l] = resStr; 
           
            // Copy string at last index to index r
            arr[r] = arr[len]; 
        }
    }
    return arr[0];
}
 
// Driver program
int main()
{
    string arr[] = {"catgc", "ctaagt",
                    "gcta", "ttca", "atgcatc"};
    int len = sizeof(arr)/sizeof(arr[0]);
 
    // Function Call
    cout << "The Shortest Superstring is "
         << findShortestSuperstring(arr, len);
 
    return 0;
}
// This code is contributed by Aditya Goel


Java




// Java program to find shortest
// superstring using Greedy
// Approximate Algorithm
import java.io.*;
import java.util.*;
 
class GFG
{
 
    static String str;
 
    // Utility function to calculate
    // minimum of two numbers
    static int min(int a, int b)
    {
        return (a < b) ? a : b;
    }
 
    // Function to calculate maximum
    // overlap in two given strings
    static int findOverlappingPair(String str1,
                                   String str2)
    {
         
        // max will store maximum
        // overlap i.e maximum
        // length of the matching
        // prefix and suffix
        int max = Integer.MIN_VALUE;
        int len1 = str1.length();
        int len2 = str2.length();
 
        // check suffix of str1 matches
        // with prefix of str2
        for (int i = 1; i <=
                            min(len1, len2); i++)
        {
 
            // compare last i characters
            // in str1 with first i
            // characters in str2
            if (str1.substring(len1 - i).compareTo(
                        str2.substring(0, i)) == 0)
            {
                if (max < i)
                {
 
                    // Update max and str
                    max = i;
                    str = str1 + str2.substring(i);
                }
            }
        }
 
        // check prefix of str1 matches
        // with suffix of str2
        for (int i = 1; i <=
                           min(len1, len2); i++)
        {
 
            // compare first i characters
            // in str1 with last i
            // characters in str2
            if (str1.substring(0, i).compareTo(
                      str2.substring(len2 - i)) == 0)
            {
                if (max < i)
                {
 
                    // update max and str
                    max = i;
                    str = str2 + str1.substring(i);
                }
            }
        }
 
        return max;
    }
 
    // Function to calculate smallest
    // string that contains
    // each string in the given set as substring.
    static String findShortestSuperstring(
                          String arr[], int len)
    {
         
        // run len-1 times to consider every pair
        while (len != 1)
        {
             
            // To store maximum overlap
            int max = Integer.MIN_VALUE;
           
            // To store array index of strings
            // involved in maximum overlap
            int l = 0, r = 0;
                  
            // to store resultant string after
            // maximum overlap
            String resStr = "";
 
            for (int i = 0; i < len; i++)
            {
                for (int j = i + 1; j < len; j++)
                {
 
                    // res will store maximum
                    // length of the matching
                    // prefix and suffix str is
                    // passed by reference and
                    // will store the resultant
                    // string after maximum
                    // overlap of arr[i] and arr[j],
                    // if any.
                    int res = findOverlappingPair
                                  (arr[i], arr[j]);
 
                    // Check for maximum overlap
                    if (max < res)
                    {
                        max = res;
                        resStr = str;
                        l = i;
                        r = j;
                    }
                }
            }
 
            // Ignore last element in next cycle
            len--;
 
            // If no overlap,
            // append arr[len] to arr[0]
            if (max == Integer.MIN_VALUE)
                arr[0] += arr[len];
            else
            {
               
                // Copy resultant string
                // to index l
                arr[l] = resStr;
               
                // Copy string at last index
                // to index r
                arr[r] = arr[len];
            }
        }
        return arr[0];
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String[] arr = { "catgc", "ctaagt",
                      "gcta", "ttca", "atgcatc" };
        int len = arr.length;
 
        System.out.println("The Shortest Superstring is " +
                        findShortestSuperstring(arr, len));
    }
}
 
// This code is contributed by
// sanjeev2552


Python




# python code for the above approach
import sys
 
# Utility function to calculate
# minimum of two numbers
def minimum(a, b):
    return a if a < b else b
 
# Function to calculate maximum
# overlap in two given strings
def findOverlappingPair(str1, str2):
    # Max will store maximum
    # overlap i.e maximum
    # length of the matching
    # prefix and suffix
    max_len = -sys.maxsize
    len1 = len(str1)
    len2 = len(str2)
    str_ = ""
 
    # Check suffix of str1 matches
    # with prefix of str2
    for i in range(1, minimum(len1, len2)+1):
        # Compare last i characters
        # in str1 with first i
        # characters in str2
        if str1[len1-i:] == str2[:i]:
            if max_len < i:
                # Update max and str_
                max_len = i
                str_ = str1 + str2[i:]
     
    # Check prefix of str1 matches
    # with suffix of str2
    for i in range(1, minimum(len1, len2)+1):
        # compare first i characters
        # in str1 with last i
        # characters in str2
        if str1[:i] == str2[len2-i:]:
            if max_len < i:
                # Update max and str_
                max_len = i
                str_ = str2 + str1[i:]
     
    return max_len, str_
 
# Function to calculate
# smallest string that contains
# each string in the given
# set as substring.
def findShortestSuperstring(arr, n):
    # Run n-1 times to
    # consider every pair
    while n != 1:
        # To store  maximum overlap
        max_len = -sys.maxsize  
        # To store array index of strings
        l, r = 0, 0   
        # Involved in maximum overlap
        res_str = ""   
       
        # Maximum overlap
        for i in range(n):
            for j in range(i+1, n):
                str_ = ""
                # res will store maximum
                # length of the matching
                # prefix and suffix str is
                # passed by reference and
                # will store the resultant
                # string after maximum
                # overlap of arr[i] and arr[j],
                # if any.
                res, str_ = findOverlappingPair(arr[i], arr[j])
 
                # check for maximum overlap
                if max_len < res:
                    max_len = res
                    res_str = str_
                    l, r = i, j
         
        # Ignore last element in next cycle
        n -= 1  
 
        # If no overlap, append arr[n-1] to arr[0]
        if max_len == -sys.maxsize:
            arr[0] += arr[n]
        else:
            # Copy resultant string to index l
            arr[l] = res_str
            # Copy string at last index to index r
            arr[r] = arr[n]
     
    return arr[0]
 
# Driver program
if __name__ == "__main__":
    arr = ["catgc", "ctaagt", "gcta", "ttca", "atgcatc"]
    n = len(arr)
 
    # Function Call
    print("The Shortest Superstring is", findShortestSuperstring(arr, n))
 
# this code is contributed by bhardwajji


C#




// C# program to find shortest
// superstring using Greedy
// Approximate Algorithm
using System;
 
class GFG
{
 
    static String str;
 
    // Utility function to calculate
    // minimum of two numbers
    static int min(int a, int b)
    {
        return (a < b) ? a : b;
    }
 
    // Function to calculate maximum
    // overlap in two given strings
    static int findOverlappingPair(String str1,
                                   String str2)
    {
         
        // max will store maximum
        // overlap i.e maximum
        // length of the matching
        // prefix and suffix
        int max = Int32.MinValue;
        int len1 = str1.Length;
        int len2 = str2.Length;
 
        // check suffix of str1 matches
        // with prefix of str2
        for (int i = 1; i <=
                            min(len1, len2); i++)
        {
 
            // compare last i characters
            // in str1 with first i
            // characters in str2
            if (str1.Substring(len1 - i).CompareTo(
                        str2.Substring(0, i)) == 0)
            {
                if (max < i)
                {
 
                    // Update max and str
                    max = i;
                    str = str1 + str2.Substring(i);
                }
            }
        }
 
        // check prefix of str1 matches
        // with suffix of str2
        for (int i = 1; i <=
                           min(len1, len2); i++)
        {
 
            // compare first i characters
            // in str1 with last i
            // characters in str2
            if (str1.Substring(0, i).CompareTo(
                      str2.Substring(len2 - i)) == 0)
            {
                if (max < i)
                {
 
                    // update max and str
                    max = i;
                    str = str2 + str1.Substring(i);
                }
            }
        }
 
        return max;
    }
 
    // Function to calculate smallest
    // string that contains
    // each string in the given set as substring.
    static String findShortestSuperstring(String []arr, int len)
    {
         
        // run len-1 times to consider every pair
        while (len != 1)
        {
             
            // To store maximum overlap
            int max = Int32.MinValue;
           
            // To store array index of strings
            // involved in maximum overlap
            int l = 0, r = 0;
                  
            // to store resultant string after
            // maximum overlap
            String resStr = "";
 
            for (int i = 0; i < len; i++)
            {
                for (int j = i + 1; j < len; j++)
                {
 
                    // res will store maximum
                    // length of the matching
                    // prefix and suffix str is
                    // passed by reference and
                    // will store the resultant
                    // string after maximum
                    // overlap of arr[i] and arr[j],
                    // if any.
                    int res = findOverlappingPair
                                  (arr[i], arr[j]);
 
                    // Check for maximum overlap
                    if (max < res)
                    {
                        max = res;
                        resStr = str;
                        l = i;
                        r = j;
                    }
                }
            }
 
            // Ignore last element in next cycle
            len--;
 
            // If no overlap,
            // append arr[len] to arr[0]
            if (max == Int32.MinValue)
                arr[0] += arr[len];
            else
            {
               
                // Copy resultant string
                // to index l
                arr[l] = resStr;
               
                // Copy string at last index
                // to index r
                arr[r] = arr[len];
            }
        }
        return arr[0];
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String[] arr = { "catgc", "ctaagt",
                      "gcta", "ttca", "atgcatc" };
        int len = arr.Length;
 
        Console.Write("The Shortest Superstring is " +
                        findShortestSuperstring(arr, len));
    }
}
 
// This code is contributed by shivanisinghss2110


Javascript




function min(a, b) {
    return (a < b) ? a : b;
}
 
function findOverlappingPair(str1, str2) {
    let max = Number.MIN_SAFE_INTEGER;
    let len1 = str1.length;
    let len2 = str2.length;
    let str = "";
 
    for (let i = 1; i <= min(len1, len2); i++) {
        if (str1.substring(len1 - i) === str2.substring(0, i)) {
            if (max < i) {
                max = i;
                str = str1 + str2.substring(i);
            }
        }
    }
 
    for (let i = 1; i <= min(len1, len2); i++) {
        if (str1.substring(0, i) === str2.substring(len2 - i)) {
            if (max < i) {
                max = i;
                str = str2 + str1.substring(i);
            }
        }
    }
 
    return { max: max, str: str };
}
 
function findShortestSuperstring(arr) {
    let len = arr.length;
 
    while (len !== 1) {
        let max = Number.MIN_SAFE_INTEGER;
        let l = 0, r = 0;
        let resStr = "";
 
        for (let i = 0; i < len; i++) {
            for (let j = i + 1; j < len; j++) {
                let { max: res, str } = findOverlappingPair(arr[i], arr[j]);
                if (max < res) {
                    max = res;
                    resStr = str;
                    l = i;
                    r = j;
                }
            }
        }
 
        len--;
 
        if (max === Number.MIN_SAFE_INTEGER) {
            arr[0] += arr[len];
        } else {
            arr[l] = resStr;
            arr[r] = arr[len];
        }
    }
 
    return arr[0];
}
 
let arr = ["catgc", "ctaagt", "gcta", "ttca", "atgcatc"];
console.log("The Shortest Superstring is " + findShortestSuperstring(arr));


Output

The Shortest Superstring is gctaagttcatgcatc





The time complexity of this algorithm is O(n^3 * m), where n is the number of strings in the input array and m is the maximum length of any string in the array. This is because the main loop runs n-1 times and the findOverlappingPair function takes O(m) time, and it is called n^2 times.

The space complexity is O(n * m), which is the space required to store the input array and the result string.

Performance of above algorithm: 

The above Greedy Algorithm is proved to be 4 approximate (i.e., length of the superstring generated by this algorithm is never beyond 4 times the shortest possible superstring). This algorithm is conjectured to 2 approximate (nobody has found case where it generates more than twice the worst). Conjectured worst case example is {abk, bkc, bk+1}. For example {“abb”, “bbc”, “bbb”}, the above algorithm may generate “abbcbbb” (if “abb” and “bbc” are picked as first pair), but the actual shortest superstring is “abbbc”. Here ratio is 7/5, but for large k, ration approaches 2.

Another Approach:

By “greedy approach” I mean: each time we merge the two strings with a maximum length of overlap, remove them from the string array, and put the merged string into the string array.

Then the problem becomes to: find the shortest path in this graph which visits every node exactly once. This is a Travelling Salesman Problem.

Apply Travelling Salesman Problem DP solution. Remember to record the path.

Below is the implementation of the above approach:

C++




#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the overlap between two strings
int calcOverlap(string a, string b) {
    for (int i = 1; i < a.length(); i++) {
        if (b.find(a.substr(i)) == 0) {
            return b.length() - a.length() + i;
        }
    }
    return b.length();
}
 
// Function to calculate the shortest superstring
string shortestSuperstring(vector<string> A) {
    int n = A.size();
    vector<vector<int>> graph(n, vector<int>(n));
 
    // Build the graph
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            graph[i][j] = calcOverlap(A[i], A[j]);
            graph[j][i] = calcOverlap(A[j], A[i]);
        }
    }
 
    // Create dp and path arrays
    vector<vector<int>> dp(1 << n, vector<int>(n));
    vector<vector<int>> path(1 << n, vector<int>(n));
    int last = -1, minVal = INT_MAX;
 
    // Start TSP DP
    for (int i = 1; i < (1 << n); i++) {
        fill(dp[i].begin(), dp[i].end(), INT_MAX);
        for (int j = 0; j < n; j++) {
            if ((i & (1 << j)) > 0) {
                int prev = i - (1 << j);
                if (prev == 0) {
                    dp[i][j] = A[j].length();
                } else {
                    for (int k = 0; k < n; k++) {
                        if (dp[prev][k] < INT_MAX && dp[prev][k] + graph[k][j] < dp[i][j]) {
                            dp[i][j] = dp[prev][k] + graph[k][j];
                            path[i][j] = k;
                        }
                    }
                }
            }
            if (i == (1 << n) - 1 && dp[i][j] < minVal) {
                minVal = dp[i][j];
                last = j;
            }
        }
    }
 
    // Build the path
    string res;
    int cur = (1 << n) - 1;
    stack<int> s;
    while (cur > 0) {
        s.push(last);
        int temp = cur;
        cur -= (1 << last);
        last = path[temp][last];
    }
 
    // Build the result
    int i = s.top();
    s.pop();
    res += A[i];
    while (!s.empty()) {
        int j = s.top();
        s.pop();
        res += A[j].substr(A[j].length() - graph[i][j]);
        i = j;
    }
     
    return res;
}
 
int main() {
    vector<string> arr{"catgc", "ctaagt", "gcta", "ttca", "atgcatc"};
     
    cout << "The Shortest Superstring is " << shortestSuperstring(arr) << endl;
     
   return 0;
}


Java




// Java program for above approach
import java.io.*;
import java.util.*;
 
class Solution
{
 
  // Function to calculate shortest
  // super string
  public static String shortestSuperstring(
                                   String[] A)
  {
    int n = A.length;
    int[][] graph = new int[n][n];
 
    // Build the graph
    for (int i = 0; i < n; i++)
    {
      for (int j = 0; j < n; j++)
      {
        graph[i][j] = calc(A[i], A[j]);
        graph[j][i] = calc(A[j], A[i]);
      }
    }
 
    // Creating dp array
    int[][] dp = new int[1 << n][n];
 
    // Creating path array
    int[][] path = new int[1 << n][n];
    int last = -1, min = Integer.MAX_VALUE;
 
    // start TSP DP
    for (int i = 1; i < (1 << n); i++)
    {
      Arrays.fill(dp[i], Integer.MAX_VALUE);
       
      // Iterate j from 0 to n - 1
      for (int j = 0; j < n; j++)
      {
        if ((i & (1 << j)) > 0)
        {
          int prev = i - (1 << j);
           
          // Check if prev is zero
          if (prev == 0)
          {
            dp[i][j] = A[j].length();
          }
          else
          {
             
            // Iterate k from 0 to n - 1
            for (int k = 0; k < n; k++)
            {
              if (dp[prev][k] < Integer.MAX_VALUE &&
                  dp[prev][k] + graph[k][j] < dp[i][j])
              {
                dp[i][j] = dp[prev][k] + graph[k][j];
                path[i][j] = k;
              }
            }
          }
        }
        if (i == (1 << n) - 1 && dp[i][j] < min)
        {
          min = dp[i][j];
          last = j;
        }
      }
    }
     
    // Build the path
    StringBuilder sb = new StringBuilder();
    int cur = (1 << n) - 1;
     
    // Creating a stack
    Stack<Integer> stack = new Stack<>();
     
    // Until cur is zero
    // push last
    while (cur > 0)
    {
      stack.push(last);
      int temp = cur;
      cur -= (1 << last);
      last = path[temp][last];
    }
 
    // Build the result
    int i = stack.pop();
    sb.append(A[i]);
     
    // Until stack is empty
    while (!stack.isEmpty())
    {
      int j = stack.pop();
      sb.append(A[j].substring(A[j].length() -
                                graph[i][j]));
      i = j;
    }
    return sb.toString();
  }
 
  // Function to check
  public static int calc(String a, String b)
  {
    for (int i = 1; i < a.length(); i++)
    {
      if (b.startsWith(a.substring(i)))
      {
        return b.length() - a.length() + i;
      }
    }
     
    // Return size of b
    return b.length();
  }
   
  // Driver Code
  public static void main(String[] args)
  {
    String[] arr = { "catgc", "ctaagt",
                    "gcta", "ttca", "atgcatc" };
     
    // Function Call
    System.out.println("The Shortest Superstring is " +
                    shortestSuperstring(arr));
   }
}


Python3




# Python program for the above approach
 
def shortestSuperstring(A):
    n = len(A)
    graph = [[0 for i in range(n)] for j in range(n)]
 
    # Build the graph
    for i in range(n):
        for j in range(n):
            graph[i][j] = calc(A[i], A[j])
            graph[j][i] = calc(A[j], A[i])
 
    # Creating dp array
    dp = [[0 for i in range(n)] for j in range(1 << n)]
 
    # Creating path array
    path = [[0 for i in range(n)] for j in range(1 << n)]
    last = -1
    min_val = float('inf')
 
    # start TSP DP
    for i in range(1, (1 << n)):
        for j in range(n):
            dp[i][j] = float('inf')
 
        for j in range(n):
            if (i & (1 << j)) > 0:
                prev = i - (1 << j)
 
                # Check if prev is zero
                if prev == 0:
                    dp[i][j] = len(A[j])
                else:
                    # Iterate k from 0 to n - 1
                    for k in range(n):
                        if dp[prev][k] < float('inf') and dp[prev][k] + graph[k][j] < dp[i][j]:
                            dp[i][j] = dp[prev][k] + graph[k][j]
                            path[i][j] = k
 
                if i == (1 << n) - 1 and dp[i][j] < min_val:
                    min_val = dp[i][j]
                    last = j
 
    # Build the path
    sb = ""
    cur = (1 << n) - 1
 
    # Creating a stack
    stack = []
 
    # Until cur is zero
    # push last
    while cur > 0:
        stack.append(last)
        temp = cur
        cur -= (1 << last)
        last = path[temp][last]
 
    # Build the result
    i = stack.pop()
    sb += A[i]
 
    # Until stack is empty
    while len(stack) > 0:
        j = stack.pop()
        sb += A[j][len(A[j]) - graph[i][j]:]
        i = j
 
    return sb
 
# Function to check
def calc(a, b):
    for i in range(1, len(a)):
        if b.startswith(a[i:]):
            return len(b) - len(a) + i
     
    # Return size of b
    return len(b)
 
# Driver Code
if __name__ == '__main__':
    arr = [ "catgc", "ctaagt", "gcta", "ttca", "atgcatc" ]
     
    # Function Call
    print("The Shortest Superstring is " + shortestSuperstring(arr))


C#




using System;
using System.Collections.Generic;
 
class Program
{
    // Function to calculate the overlap between two strings
    static int CalcOverlap(string a, string b)
    {
        for (int i = 1; i < a.Length; i++)
        {
            if (b.IndexOf(a.Substring(i)) == 0)
            {
                return b.Length - a.Length + i;
            }
        }
        return b.Length;
    }
 
    // Function to calculate the shortest superstring
    static string ShortestSuperstring(List<string> A)
    {
        int n = A.Count;
        int[,] graph = new int[n, n];
 
        // Build the graph
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                graph[i, j] = CalcOverlap(A[i], A[j]);
                graph[j, i] = CalcOverlap(A[j], A[i]);
            }
        }
 
        // Create dp and path arrays
        int[][] dp = new int[1 << n][];
        int[][] path = new int[1 << n][];
        int last = -1, minVal = int.MaxValue;
 
        for (int i = 0; i < (1 << n); i++)
        {
            dp[i] = new int[n];
            path[i] = new int[n];
 
            for (int j = 0; j < n; j++)
            {
                dp[i][j] = int.MaxValue;
            }
        }
 
        // Start TSP DP
        for (int mask = 1; mask < (1 << n); mask++)
        {
            for (int j = 0; j < n; j++)
            {
                if ((mask & (1 << j)) > 0)
                {
                    int prevMask = mask - (1 << j);
                    if (prevMask == 0)
                    {
                        dp[mask][j] = A[j].Length;
                    }
                    else
                    {
                        for (int k = 0; k < n; k++)
                        {
                            if (dp[prevMask][k] < int.MaxValue &&
                                dp[prevMask][k] + graph[k, j] < dp[mask][j])
                            {
                                dp[mask][j] = dp[prevMask][k] + graph[k, j];
                                path[mask][j] = k;
                            }
                        }
                    }
                }
                if (mask == (1 << n) - 1 && dp[mask][j] < minVal)
                {
                    minVal = dp[mask][j];
                    last = j;
                }
            }
        }
 
        // Build the path
        string res = string.Empty;
        int currentMask = (1 << n) - 1;
        Stack<int> s = new Stack<int>();
        while (currentMask > 0)
        {
            s.Push(last);
            int tempMask = currentMask;
            currentMask -= (1 << last);
            last = path[tempMask][last];
        }
 
        // Build the result
        int firstStringIndex = s.Pop();
        res += A[firstStringIndex];
        while (s.Count > 0)
        {
            int nextStringIndex = s.Pop();
            res += A[nextStringIndex].
                   Substring(A[nextStringIndex].Length - graph[firstStringIndex,
                             nextStringIndex]);
            firstStringIndex = nextStringIndex;
        }
 
        return res;
    }
 
    static void Main()
    {
        List<string> arr = new List<string> { "catgc", "ctaagt", "gcta", "ttca", "atgcatc" };
 
        Console.WriteLine("The Shortest Superstring is " + ShortestSuperstring(arr));
    }
}
 
// This code is contributed by akshitaguprzj3


Javascript




// Function to calculate shortest super string
function shortestSuperstring(A) {
    let n = A.length;
    let graph = new Array(n).fill(0).map(() => new Array(n).fill(0));
    // Build the graph
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            graph[i][j] = calc(A[i], A[j]);
            graph[j][i] = calc(A[j], A[i]);
        }
    }
    // Creating dp array
    let dp = new Array(1 << n).fill(0).map(() => new Array(n).fill(0));
    // Creating path array
    let path = new Array(1 << n).fill(0).map(() => new Array(n).fill(0));
    let last = -1, min = Number.MAX_VALUE;
    // start TSP DP
    for (let i = 1; i < (1 << n); i++) {
        dp[i].fill(Number.MAX_VALUE);
        // Iterate j from 0 to n - 1
        for (let j = 0; j < n; j++) {
            if ((i & (1 << j)) > 0) {
                let prev = i - (1 << j);
                // Check if prev is zero
                if (prev == 0) {
                    dp[i][j] = A[j].length;
                } else {
                    // Iterate k from 0 to n - 1
                    for (let k = 0; k < n; k++) {
                        if (dp[prev][k] < Number.MAX_VALUE && dp[prev][k] + graph[k][j] < dp[i][j]) {
                            dp[i][j] = dp[prev][k] + graph[k][j];
                            path[i][j] = k;
                        }
                    }
                }
            }
            if (i == (1 << n) - 1 && dp[i][j] < min) {
                min = dp[i][j];
                last = j;
            }
        }
    }
    // Build the path
    let sb = "";
    let cur = (1 << n) - 1;
    // Creating a stack
    let stack = [];
    // Until cur is zero
    // push last
    while (cur > 0) {
        stack.push(last);
        let temp = cur;
        cur -= (1 << last);
        last = path[temp][last];
    }
    // Build the result
    let i = stack.pop();
    sb += A[i];
    // Until stack is empty
    while (stack.length > 0) {
        let j = stack.pop();
        sb += A[j].substring(A[j].length - graph[i][j]);
        i = j;
    }
    return sb;
}
// Function to check
function calc(a, b) {
    for (let i = 1; i < a.length; i++) {
        if (b.startsWith(a.substring(i))) {
            return b.length - a.length + i;
        }
    }
    // Return size of b
    return b.length;
}
// Driver Code
let arr = ["catgc", "ctaagt", "gcta", "ttca", "atgcatc"];
// Function Call
console.log("The Shortest Superstring is " + shortestSuperstring(arr));


Output

The Shortest Superstring is gctaagttcatgcatc






Time complexity: O(n^2 * 2^n), where N is the length of the string array.
Auxiliary Space: O(2^N * N).

There exist better approximate algorithms for this problem. Please refer to below link. 
Shortest Superstring Problem | Set 2 (Using Set Cover)

Another Approach Using Bitmask and Dynamic Programming:

This is actually bitmasking problem: if we look at our strings as nodes, then we can evaluate distance between one string and another, for example for abcde and cdefghij distance is 5, because we need to use 5 more symbols fghij to continue first string to get the second. Note, that this is not symmetric, so our graph is oriented.

Java




// Java program for above approach
import java.io.*;
import java.util.*;
 
class Solution
{
 
  // Function to calculate shortest
  // super string
  public static String shortestSuperstring(String[] words) {
         
        int n = words.length;
        // different substring length
        int[][] distance = new int[n][n];
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                int min = Math.min(words[i].length(), words[j].length());
                for(int k=min; k>=0; k--){
                    //maximum matched substring
                    if(words[i].endsWith(words[j].substring(0,k))) {
                        distance[i][j] = words[j].length() - k;
                        break;
                    }
                }
            }
        }
         
        int dp[][] = new int[(1<<n)][n];
        int path[][] =  new int[(1<<n)][n];
        String ans = "";
        int len = Integer.MAX_VALUE;
        for(int i = 0; i<n; i++){
           for(int j=0; j< ( 1<<n); j++){
                Arrays.fill(dp[j], -1);
                Arrays.fill(path[j], -1);
            }
            // calculate tsp from each word
            int tsp = tsp(i, 0, distance, dp, path, n);
            // create string for each tsp
            String str = createPath(words, i, distance, path);
            // check length of the str and update
            if(str.length() < len){
                ans = str;
                len = str.length();
            }
             
        }
        return ans;
 
    }
     
    private static int tsp(int city, int mask, int[][] distance, int[][] dp, int[][] path, int n){
        // return 0 as we won't include the distance from current node to starting node (not as TSP)
        if(mask == ((1<<n) - 1)) return 0; //distance[p][0];
        if(dp[mask][city] != -1){
            return dp[mask][city];
        }
        int ans = Integer.MAX_VALUE;
        int nextCity = -1;
         
        for(int i = 0; i<n; i++){
            // i th string node is not visited
            if((mask & (1<<i)) == 0 ){
                int dis = distance[city][i] + tsp(i, (mask | (1<<i)), distance, dp, path, n);
                if(dis < ans){
                    ans = dis;
                    nextCity = i;
                }
            }
        }
        // update path and dp
        path[mask][city] = nextCity;
        dp[mask][city] = ans;
        return ans;
    }
     
    private static String createPath(String[] words, int start, int[][] distance, int[][] path){
        int c = start;
        StringBuilder sb = new StringBuilder(words);
        int mask = (1<<start);
        int x = path[mask];
        while(x != -1){
            sb.append(words[x].substring(words[x].length() - distance[x]));
            mask |= (1<<x);
            c = x;
            x = path[mask][x];
        }
        return sb.toString();
    }
   
  // Driver Code
  public static void main(String[] args)
  {
    String[] arr = { "catgc", "ctaagt",
                    "gcta", "ttca", "atgcatc" };
     
    // Function Call
    System.out.println("The Shortest Superstring is " +
                    shortestSuperstring(arr));
   }
}


Python




def shortest_superstring(words):
    n = len(words)
     
    # Calculate the overlap distance between two words
    def calc_overlap(a, b):
        min_len = min(len(a), len(b))
        for k in range(min_len, -1, -1):
            if a.endswith(b[:k]):
                return len(b) - k
        return len(b)
     
    # Initialize a distance matrix
    distance = [[0] * n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            distance[i][j] = calc_overlap(words[i], words[j])
     
    # Initialize dp and path arrays
    dp = [[float('inf')] * n for _ in range(1 << n)]
    path = [[-1] * n for _ in range(1 << n)]
     
    ans = ""
    min_len = float('inf')
     
    for i in range(n):
        for j in range(1 << n):
            dp[j] = [-1] * n
            path[j] = [-1] * n
         
        tsp_value = tsp(i, 1 << i, distance, dp, path, n)
        superstring = create_path(words, i, distance, path)
         
        if len(superstring) < min_len:
            ans = superstring
            min_len = len(superstring)
     
    return ans
 
def tsp(city, mask, distance, dp, path, n):
    if mask == (1 << n) - 1:
        return 0
    if dp[mask][city] != -1:
        return dp[mask][city]
     
    ans = float('inf')
    next_city = -1
     
    for i in range(n):
        if not (mask & (1 << i)):
            dis = distance[city][i] + tsp(i, mask | (1 << i), distance, dp, path, n)
            if dis < ans:
                ans = dis
                next_city = i
     
    path[mask][city] = next_city
    dp[mask][city] = ans
    return ans
 
def create_path(words, start, distance, path):
    c = start
    sb = [words]
    mask = 1 << start
    x = path[mask]
     
    while x != -1:
        sb.append(words[x][-(distance[x]):])
        mask |= (1 << x)
        c = x
        x = path[mask][x]
     
    return "".join(sb)
 
# Driver code
arr = ["catgc", "ctaagt", "gcta", "ttca", "atgcatc"]
result = shortest_superstring(arr)
print("The Shortest Superstring is", result)


Javascript




function shortestSuperstring(words) {
    const n = words.length;
 
    // Calculate the overlap distance between two words
    function calcOverlap(a, b) {
        const minLen = Math.min(a.length, b.length);
        for (let k = minLen; k >= 0; k--) {
            if (a.endsWith(b.substring(0, k))) {
                return b.length - k;
            }
        }
        return b.length;
    }
 
    // Initialize a distance matrix
    const distance = new Array(n).fill(0).map(() => new Array(n).fill(0));
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            distance[i][j] = calcOverlap(words[i], words[j]);
        }
    }
 
    // Initialize dp and path arrays
    const dp = new Array(1 << n).fill(0).map(() => new Array(n).fill(Infinity));
    const path = new Array(1 << n).fill(0).map(() => new Array(n).fill(-1));
 
    let ans = "";
    let minLen = Infinity;
 
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < (1 << n); j++) {
            dp[j] = new Array(n).fill(-1);
            path[j] = new Array(n).fill(-1);
        }
 
        const tspValue = tsp(i, 1 << i, distance, dp, path, n);
        const superstring = createPath(words, i, distance, path);
 
        if (superstring.length < minLen) {
            ans = superstring;
            minLen = superstring.length;
        }
    }
 
    return ans;
}
 
function tsp(city, mask, distance, dp, path, n) {
    if (mask === (1 << n) - 1) {
        return 0;
    }
 
    if (dp[mask][city] !== -1) {
        return dp[mask][city];
    }
 
    let ans = Infinity;
    let nextCity = -1;
 
    for (let i = 0; i < n; i++) {
        if (!(mask & (1 << i))) {
            const dis = distance[city][i] + tsp(i, mask | (1 << i), distance, dp, path, n);
            if (dis < ans) {
                ans = dis;
                nextCity = i;
            }
        }
    }
 
    path[mask][city] = nextCity;
    dp[mask][city] = ans;
    return ans;
}
 
function createPath(words, start, distance, path) {
    let c = start;
    const sb = [words];
    let mask = 1 << start;
    let x = path[mask];
 
    while (x !== -1) {
        sb.push(words[x].substring(words[x].length - distance[x]));
        mask |= 1 << x;
        c = x;
        x = path[mask][x];
    }
 
    return sb.join("");
}
 
// Driver code
const arr = ["catgc", "ctaagt", "gcta", "ttca", "atgcatc"];
const result = shortestSuperstring(arr);
console.log("The Shortest Superstring is", result);
 
 
// This code is contributed by akshitaguprzj3


Output

The Shortest Superstring is gctaagttcatgcatc






Time complexity: O(2^n*n^2*M), where M is the length of answer
Auxiliary Space: O(2^n*n*M) as well.


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 26 Oct, 2023
Like Article
Save Article
Similar Reads
Related Tutorials