我想将我编写的算法分成几部分,如附件所示
这是我目前所做的:
\begin{algorithm}
\caption{Selection algorithm}
\label{mpral}
\begin{algorithmic}[1]
\State initialization
\State ini = $4$
\State init = $0$
\Procedure {DriftingPhase}{}
\For{each source $S$}
\State Broadcast IWD\textendash
\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}
这些是使用过的包
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}
答案1
以下最小示例提供了在算法中\phase{<desc>}
设置Phase X - <desc>
描述的功能。请注意,它的用法仅在缩进的顶层才按预期工作。但是,我认为这对于算法的呈现来说已经足够了。
\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$}
\State Broadcast IWD--
\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}