[Tex/LaTex] How to write begin and end statement at the top and bottom of algorithm

algorithms

How can I write 'begin' and 'end' statement at the top and bottom the following code like this?A sample pseuodcode with begin and end statement

Besides using algorithm and algopseudocode package I can write Require and Ensure statement. Those are not properly aligned. Is there any way to align like the attached pic?

    % book example for classicthesis.sty
\documentclass[
  % Replace twoside with oneside if you are printing your thesis on a single side
  % of the paper, or for viewing on screen.
  %oneside,
  twoside,
  11pt, a4paper,
  footinclude=true,
  headinclude=true,
  cleardoublepage=empty
]{scrbook}

\usepackage{lipsum}
\usepackage[linedheaders,parts,pdfspacing]{classicthesis}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{acronym}
\usepackage{algpseudocode}
\usepackage{algorithm}



\begin{document}

\chapter{A Chapter}
\begin{algorithm}
\caption{Apriori algorithm}

\begin{algorithmic}[1]

    \State $L_{1} \leftarrow Frequent 1-itemset $
    \State $k \leftarrow 2$
    \While{$L_{k-1} \neq \phi$}
    \State $Temp \leftarrow candidateItemSet (L_{k-1})$
    \State $C_{k} \leftarrow frequencyOfItemSet (Temp)$
    \State $L_{k} \leftarrow compareItemSetWithMinimumSupport (C_{k}, minsup) $
    \State $k \leftarrow k + 1$
    \EndWhile\\
    \Return L

\end{algorithmic}
\end{algorithm}

\end{document}

Here is the online code

Best Answer

Just define (in algpseudocode style)

\algdef{SE}{Begin}{End}{\textbf{begin}}{\textbf{end}}

and then use \Begin and \End as in the following example:

    % book example for classicthesis.sty
\documentclass[
  % Replace twoside with oneside if you are printing your thesis on a single side
  % of the paper, or for viewing on screen.
  %oneside,
  twoside,
  11pt, a4paper,
  footinclude=true,
  headinclude=true,
  cleardoublepage=empty
]{scrbook}

\usepackage{lipsum}
\usepackage[linedheaders,parts,pdfspacing]{classicthesis}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{acronym}
\usepackage{algpseudocode}
\usepackage{algorithm}

\algdef{SE}{Begin}{End}{\textbf{begin}}{\textbf{end}}

\begin{document}

\chapter{A Chapter} 
\begin{algorithm}
\caption{Apriori algorithm}

\begin{algorithmic}[1]
    \Begin
    \State $L_{1} \leftarrow Frequent 1-itemset $
    \State $k \leftarrow 2$
    \While {$L_{k-1} \neq \phi$}
    \State $Temp \leftarrow candidateItemSet (L_{k-1})$
    \State $C_{k} \leftarrow frequencyOfItemSet (Temp)$
    \State $L_{k} \leftarrow compareItemSetWithMinimumSupport (C_{k}, minsup) $
    \State $k \leftarrow k + 1$
    \EndWhile
    \State \Return L
    \End
\end{algorithmic}
\end{algorithm}

\end{document} 

Output

enter image description here

Just a remark. You should not write

\EndWhile\\
\Return L

but

\EndWhile
\State \Return L

as in my example.


If you want a different indentation for \Return you can do the following.

Add

\def\BState{\State\hskip-.5em}

in the preamble and use \BState instead of \State in front of \Return.

MWE

    % book example for classicthesis.sty
\documentclass[
  % Replace twoside with oneside if you are printing your thesis on a single side
  % of the paper, or for viewing on screen.
  %oneside,
  twoside,
  11pt, a4paper,
  footinclude=true,
  headinclude=true,
  cleardoublepage=empty
]{scrbook}

\usepackage{lipsum}
\usepackage[linedheaders,parts,pdfspacing]{classicthesis}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{acronym}
\usepackage{algpseudocode}
\usepackage{algorithm}

\algdef{SE}{Begin}{End}{\textbf{begin}}{\textbf{end}}
\def\BState{\State\hskip-.5em}

\begin{document}

\chapter{A Chapter} 
\begin{algorithm}
\caption{Apriori algorithm}

\begin{algorithmic}[1]
    \Begin
    \State $L_{1} \leftarrow Frequent 1-itemset $
    \State $k \leftarrow 2$
    \While {$L_{k-1} \neq \phi$}
    \State $Temp \leftarrow candidateItemSet (L_{k-1})$
    \State $C_{k} \leftarrow frequencyOfItemSet (Temp)$
    \State $L_{k} \leftarrow compareItemSetWithMinimumSupport (C_{k}, minsup) $
    \State $k \leftarrow k + 1$
    \EndWhile
    \BState \Return L
    \End
\end{algorithmic}
\end{algorithm}

\end{document}

Output

enter image description here