# [Tex/LaTex] Breaking up an algorithm into several parts

algorithmicxalgorithms

I would to divide an algorithm I wrote into parts as shown in the attached file

This is what I did so far :

\begin{algorithm}
\caption{Selection algorithm}
\label{mpral}
\begin{algorithmic}[1]
\State initialization
\State ini = $4$
\State init = $0$
\Procedure {DriftingPhase}{}
\For{each source $S$}
\For{each IWD}
\vspace{0.05in}
\State \small{Compute soil ($i$,$j$) = $(1-\Pisymbol{psy}{114}).Soil ($i$,$j$)-\Pisymbol{psy}{114}\Delta Soil($i$,$j$)$}
\vspace{0.01in}
\State Compute $P_{r}$ = ($i$,$j$) = $\frac{f(soil(i,j))}{\sum\nolimits_{k\notin vc(IWD)}{f(soil(i,k)}}$
\State f (soil(i,j)) = $\frac{1}{soil(i,j)}$
\EndFor
\EndFor
\EndProcedure
\Statex
\Procedure{FindingRoute phase}{}
\For{each destination $D$ }
\State MPRset ($D$) = max{$P_{r}$($z$)\textbar\ $z$ $\in$$R}} \State Send IWD\textendash Rep 2\textendash hop away \EndFor \EndProcedure \Statex \Procedure{finalPhase}{} \For{each source S } \State MPRset (S) = max{P_{r}(z)\textbar\ z \in$$R$}}
\EndFor
\State MPRRoute= [S]
\EndProcedure
\end{algorithmic}
\end{algorithm}


These are the used packages

\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}


The following minimal example provides \phase{<desc>} which sets a Phase X - <desc> description within the algorithm. Note that it's usage only works as expected at the top level of indentation. However, I would think this is sufficient in terms of the presentation of one's algorithm.

\documentclass{article}
\usepackage{algorithm,algpseudocode,amsmath}

\newcommand{\var}[1]{\text{\texttt{#1}}}
\newcommand{\func}[1]{\text{\textsl{#1}}}

\makeatletter
\newcounter{phase}[algorithm]
\newlength{\phaserulewidth}
\newcommand{\setphaserulewidth}{\setlength{\phaserulewidth}}
\newcommand{\phase}[1]{%
\vspace{-1.25ex}
% Top phase rule
\Statex\leavevmode\llap{\rule{\dimexpr\labelwidth+\labelsep}{\phaserulewidth}}\rule{\linewidth}{\phaserulewidth}
\Statex\strut\refstepcounter{phase}\textit{Phase~\thephase~--~#1}% Phase text
% Bottom phase rule
\vspace{-1.25ex}\Statex\leavevmode\llap{\rule{\dimexpr\labelwidth+\labelsep}{\phaserulewidth}}\rule{\linewidth}{\phaserulewidth}}
\makeatother

\setphaserulewidth{.7pt}
\begin{document}

\begin{algorithm}
\caption{Selection algorithm}
\begin{algorithmic}[1]
\State initialization
\State $\var{ini} = 4$
\State $\var{init} = 0$
\phase{Drifting phase}
\Procedure {DriftingPhase}{}
\For{each source~$S$}
\For{each IWD}
\vspace{0.05in}
\State Compute $\func{soil}(i,j) = (1-\Pi\psi) \times \func{soil}(i,j)-\Pi\psi \Delta \func{soil}(i,j)$
\vspace{0.01in}
\State Compute $P_r = (i,j) = \frac{f(\func{soil}(i,j))}{\sum\nolimits_{k \notin \func{vc}(IWD)}{f(\func{soil}(i,k)}}$
\State $f(\func{soil}(i,j)) = \frac{1}{\func{soil}(i,j)}$
\EndFor
\EndFor
\EndProcedure
\phase{FindingRoute phase}
\Procedure{FindingRoute phase}{}
\For{each destination $D$ }
\State $\func{MPRset}(D) = \max\{P_r(z) \cup z \in R\}$
\State Send IWD-Rep $2$-hop away
\EndFor
\EndProcedure
\phase{Final phase}
\Procedure{finalPhase}{}
\For{each source $S$}
\State $\func{MPRset}(S) = \max\{P_r(z) \cup z \in R\}$
\EndFor
\State $\var{MPRRoute} = [S]$
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}