How to resolve the algorithm Shortest common supersequence step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Shortest common supersequence step by step in the Haskell programming language

Table of Contents

Problem Statement

The   shortest common supersequence   is a problem closely related to the   longest common subsequence,   which you can use as an external function for this task.

Given two strings

u

{\displaystyle u}

and

v

{\displaystyle v}

, find the shortest possible sequence

s

{\displaystyle s}

, which is the shortest common super-sequence of

u

{\displaystyle u}

and

v

{\displaystyle v}

where both

u

{\displaystyle u}

and

v

{\displaystyle v}

are a subsequence of

s

{\displaystyle s}

. Defined as such,

s

{\displaystyle s}

is not necessarily unique. Demonstrate this by printing

s

{\displaystyle s}

where

u

{\displaystyle u=}

“abcbdab” and

v

{\displaystyle v=}

“bdcaba”.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Shortest common supersequence step by step in the Haskell programming language

The provided Haskell code defines a function scs that takes two lists of elements of type a (which must be instances of the Eq typeclass) as input and returns a new list that is the shortest common subsequence (SCS) of the two input lists.

Here's how the code works:

  • The scs function takes two lists, xss and yss, as input.

  • It first checks if either list is empty. If xss is empty, it returns yss. If yss is empty, it returns xss.

  • If neither list is empty, it compares the first elements of the two lists (x and y).

  • If x and y are equal, it means they are part of the SCS, so it prepends x to the result of calling scs recursively on the tails of xss and yss (i.e., xs and ys).

  • If x and y are not equal, it means they are not part of the SCS. So, it calculates two possible SCSs: us by calling scs recursively on xs and yss, and vs by calling scs recursively on xss and ys.

  • It then chooses the shorter of us and vs as the SCS and prepends the first element of the chosen list (x or y) to it.

  • Finally, it returns the calculated SCS.

When you run the code with the input lists "abcbdab" and "bdcaba", it calculates and prints the SCS, which is "abcd".

Source code in the haskell programming language

scs :: Eq a  => [a] -> [a] -> [a]
scs [] ys = ys
scs xs [] = xs
scs xss@(x:xs) yss@(y:ys)
  | x == y = x : scs xs ys
  | otherwise = ws
      where
      us = scs xs yss
      vs = scs xss ys
      ws  | length us < length vs = x : us
          | otherwise = y : vs

main = putStrLn $ scs "abcbdab" "bdcaba"


  

You may also check:How to resolve the algorithm SEDOLs step by step in the F# programming language
You may also check:How to resolve the algorithm Bitwise operations step by step in the Tcl programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/PCG32 step by step in the 11l programming language
You may also check:How to resolve the algorithm AKS test for primes step by step in the Rust programming language
You may also check:How to resolve the algorithm Truth table step by step in the Ruby programming language