0

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!!

user3293462
  • 39
  • 2
  • 3
  • Have you reviewed the documentation for the algorithm2e package. It is quite useful in describing how to typeset pseudo-code, and it will surely allow you to attempt to set this algorithm yourself. – Paul Stiverson Jun 16 '15 at 14:05
  • I don't have much experience in Latex. So I tried to go through the documentation of algorithm2e but didn't got like this. I have wriiten iterative algorithms but haven't wriiten a recursive one – user3293462 Jun 16 '15 at 16:22
  • Since this is very much a do-it-for-me question, so I consider it a duplicate of Print programs with its proper syntax. – Werner Jun 16 '15 at 18:06
  • 1
    @Werner I agree with you. Initially I provided an answer, but no effort and no response from the OP so far, so I deleted the answer. Perhaps we should close this as a duplicate of the question you linked? – Gonzalo Medina Jun 17 '15 at 14:26
  • 1
    @GonzaloMedina: I'd suggest you undelete your answer (since it's completely valid) and we close this question. That way we accomplish both goals, and I don't think it's that bad, since the answer is only very helpful to the OP and not a wide range of people. – Werner Jun 18 '15 at 04:02
  • @Werner OK. Answer undeleted. Voted to close as duplicate. – Gonzalo Medina Jun 18 '15 at 13:02

1 Answers1

1

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

Gonzalo Medina
  • 505,128