\documentclass[11pt]{article}

\usepackage{fullpage}
\usepackage{graphicx,epsfig}


\begin{document}

\title{{\bf In-class work: Longest common subsequence (LCS) \\
(CLRS 15.4)
}
}


\date{}

\maketitle


A classical dynamic-programming problem. 

\paragraph{Intro:} Biological applications need to compare DNA
sequences. A strand of DNA consists of a string of molecules which are
one of four possible bases: adenine, guanine, cytosine and thymine.
Thus DNA strands can be expressed as arrays or strings over four
symbols, $A, C, G, T$ (standing for adenine, guanine, cytosine and
thymine).

\paragraph{Notation/definition:} 
\begin{itemize} 
\item Suppose we have two sequences (arrays) $X[1..n]$ and $Y[1..m]$, where
each of $X[i], Y[i]$ are one of the four bases $A, C, G, T$.

\item We say that another sequence $Z [1..k]$ is a sub-sequence if $X$ if
there exists a strictly increasing sequence of indices $i_1, i_2, i_3,
..., i_k$ such that we have $X[i_1] = Z[1], X[i_2]=Z[2], ...,
X[i_k]=Z[k]$.

\item We say that $Z$ is a \emph{common subsequence} (of $X,Y$) if $Z$
  is a subsequence of both $X$ and $Y$.
\end{itemize} 


\paragraph{The problem: } Given two sequences $X$ and $Y$ of size $n$
and $m$ respectively, find their longest common subsequence (LCS).

\vspace{1cm} 


Example: Let $X = [ A, B,C,B,D,A,B]$, $Y=  [ B, D, C, A, B, A ]$. 

\vspace{1 cm}

\begin{enumerate} 
  
\item   List some common subsequences of length 2. 

\vspace{2cm}

\item List some common subsequences of length 3. 

\newpage
\item Can you find a common subsequence of length 4?  5? 

\vspace{3cm}




\item Sketch a brute-force algorithm for finding $LCS(X,Y)$.  For
  example you could enumerate all possibilities----How many
  possibilities are there?

  You don't need to write this in detail, just sketch the idea and find it's running time.

\vspace{10cm}









Now we'll come up with a recursive formulation of $LCS(X,Y)$. 

{\bf More notation:} For any sequence $X[1..n]$ let $X_i$ denote the
sequence consisting of the first $i$ elements of $X$ (usually called
the $i$-prefix): $X_i = X[1..i]$.

Let $Z[1..k]$ be the LCS of $X,Y$.

\item Case 1: If $X[n] == Y[m]$: what can you say about the last
  symbol of $Z$, $Z[k]$?

  And, what can you say about $Z= [Z_{k-1}, Z[k]]$?  Express it
  recursively in terms of $X_{n-1}$ and $Y_{m-1}$.

  

\newpage
\item  Case 2 (a): If  $X[n] != Y[m]$ and $Z[k] \neq X[n]$:  Express $Z$ recursively (for e.g. in terms of  $X_{n-1}$ etc).
  
  \vspace{1cm} 

  $Z = LCS(?, ?)$

  \vspace{1cm} 
\item  Case 2 (b): If  $X[n] != Y[m]$ and $Z[k] \neq Y[m]$:  Express $Z$ recursively (for e.g. in terms of  $Y_{n-1}$ etc).
  
  \vspace{1cm} 
  
  $Z = LCS(?, ?)$
  
  \vspace{1cm} 
  

\item Case 2 above assumed you know the last symbol of $Z$.  But the
  thing is, you don't know $Z$, that's precisely what you are trying
  to compute. Still, we made progress: What does Case 2(a) and Case
  2(b) suggest about computing $Z(X,Y)$ (when $X[n] \neq Y[m]$)? 

  \vspace{5cm}

\item We are now ready to write the recursive algorithm for LCS.  For
  simplicity, we'll start by computing \emph{only} the length of the
  LCS of $X,Y$.  Then we'll extend the solution to compute not only
  the length, but the actual LCS.

{\bf   Notation: Denote c(i,j) the length of the LCS of $X_i$ and $Y_j$. }

With this notation, we want  to compute $c(n,m)$.


Start by writing the base case: 

$c(i,0) = ? $
\vspace{1cm} 

$c(0,j) = ? $

\newpage
\item If $X[n] == Y[m]$:    Express $c(i,j)$ recursively. 

  \vspace{1cm} 
  $c(i,j) = $
  
  \vspace{1cm}
  

\item If $X[n] \neq Y[m]$:    Express $c(i,j)$ recursively.

 \vspace{1cm} 
  $c(i,j) = $
  
  \vspace{1cm}
  
\item Write pseudo-code for a recursive algorithm that computes the length of LCS(X,Y) (that is, $c(i,j)$ as per notation above). 

  \vspace{10cm}


\item Analyze the running time of your algorithm (assume it does NOT
  use dynamic programming) and argue that it is exponential. 


\newpage 
\item Describe how you can improve the running time of your algorithm
  using dynamic programming.  Analyse the new running time.

  \vspace{10cm}

\item How would you extend your algorithm above to compute the LCS not just the length? 

  \vspace{10cm}

\item Consider the following example: $$X = [A, B, C, B, D, A, B], ~~~Y = [B,D,C,A,B,A]$$ 

  Draw the table and show how it's filled when calling your dynamic
  programming function to compute $c(7,6)$.
\end{enumerate}


\end{document}
