[Tex/LaTex] Writting Recursive algorithm (LCS) using algorithm2e package similar to the one shown in attached image

algorithm2e

enter image description here

I want to write this algorithm (recursive) in latex but I don't know how to do it. I have attached image of the algorithm. Please help me with the code. I am using algorithm2e package. Thanks!!

Best Answer

Nothing very special here. You can do something like this (notice the use of \SetKwFunction to declare a function):

\documentclass{article}
\usepackage[linesnumbered,noline,noend]{algorithm2e}
\usepackage{amsmath}

\SetNlSty{}{}{}

\let\oldnl\nl% Store \nl in \oldnl
\newcommand\nonl{%
  \renewcommand{\nl}{\let\nl\oldnl}}% Remove line number for one line

\begin{document}

\begin{algorithm}
\SetKwFunction{printlcs}{\textsc{Print}-LCS}
\Indm\nonl\printlcs{$b,X,i,j$}\\
\Indp
  \If{$i=0$ or $j=0$}{\KwRet{}}
  \If{$b[i,j]=\text{``}\nwarrow\hspace{-3pt}\text{''}$}{
    \printlcs{$b,X,i-1,j-1$}\\
    print $x_i$
  }
  \ElseIf{$b[i,j]=\text{``}\uparrow\hspace{-3pt}\text{''}$}{
    \printlcs{$b,X,i-1,j$}
  }
  \Else{\printlcs{$b,X,i,j-1$}}
\end{algorithm}

\end{document}

enter image description here

Related Question