| 1 |
\documentclass{article} |
| 2 |
\usepackage{algorithmicx} |
| 3 |
\usepackage[ruled]{algorithm} |
| 4 |
\usepackage{algpseudocode} |
| 5 |
\usepackage{algpascal} |
| 6 |
\usepackage{algc} |
| 7 |
%\algdisablelines |
| 8 |
|
| 9 |
|
| 10 |
\newcommand{\alg}{\texttt{algorithmicx}} |
| 11 |
\newcommand{\old}{\texttt{algorithmic}} |
| 12 |
\newcommand{\euk}{Euclid} |
| 13 |
\newcommand\ASTART{\bigskip\noindent\begin{minipage}[b]{0.5\linewidth}} |
| 14 |
\newcommand\ACONTINUE{\end{minipage}\begin{minipage}[b]{0.5\linewidth}} |
| 15 |
\newcommand\AENDSKIP{\end{minipage}\bigskip} |
| 16 |
\newcommand\AEND{\end{minipage}} |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
\title{The \alg\ package\footnote{This is the documentation for the version 1.2 |
| 21 |
of the package. This package is released under LPPL.}} |
| 22 |
\author{Sz\'asz J\'anos\\[email protected]} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
\begin{document} |
| 27 |
\maketitle |
| 28 |
\begin{abstract} |
| 29 |
The \alg\ package provides many possibilities to customize the layout of algorithms. |
| 30 |
You can use one of the predefined layouts (\textbf{pseudocode}, \textbf{pascal} |
| 31 |
and \textbf{c} and others), with or without modifications, or you can define a |
| 32 |
completely new layout for your specific needs. |
| 33 |
\end{abstract} |
| 34 |
\tableofcontents |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
\section{Introduction} |
| 40 |
All this has begun in my last year at the university. The only thing that I knew of |
| 41 |
\LaTeX\ was that it exists, and that it is ``good''. I started using it, but I needed to typeset some |
| 42 |
algorithms. So I begun searching for a good algorithmic style, and I have found the \old\ package. |
| 43 |
It was a great joy for me, and I started to use it\dots\ |
| 44 |
Well\dots\ Everything went nice, until I needed some block that wasn't defined in there. What to do? |
| 45 |
I was no \LaTeX\ guru, in fact I only knew the few basic macros. But there was no other way, so I opened |
| 46 |
the style file, and I copied one existing block, renamed a few things, and voil\`a! This (and some other |
| 47 |
small changes) where enough for me\dots |
| 48 |
|
| 49 |
One year later --- for one good soul --- I had to make some really big changes on the style. And there on |
| 50 |
a sunny day came the idea. What if I would write some macros to let others create blocks automatically? |
| 51 |
And so I did! Since then the style was completely rewritten\dots\ several times\dots |
| 52 |
|
| 53 |
I had fun writing it, may you have fun using it! I am still no \LaTeX\ guru, so if you are, and you find |
| 54 |
something really ugly in the style, please mail me! All ideas for improvements are welcome! |
| 55 |
|
| 56 |
Thanks go to Benedek Zsuzsa, Ionescu Clara, Sz\H ocs Zolt\'an, Cseke Botond, Kanoc |
| 57 |
%Szotyori Zolt\'an Csaba |
| 58 |
and many-many others. Without them I would have never started or continued \textbf{algorithmicx}. |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
\section{General informations} |
| 64 |
|
| 65 |
\subsection{The package} |
| 66 |
The package \textbf{algorithmicx} itself doesn't define any algorithmic commands, but gives |
| 67 |
a set of macros to define such a command set. You may use only \textbf{algorithmicx}, and define |
| 68 |
the commands yourself, or you may use one of the predefined command sets. |
| 69 |
|
| 70 |
These predefined command sets (layouts) are: |
| 71 |
\begin{description} |
| 72 |
\item[algpseudocode] has the same look\footnote{almost :-)} as the one defined in the |
| 73 |
\old\ package. The main difference is that while the \old\ package doesn't |
| 74 |
allow you to modify predefined structures, or to create new ones, the \alg\ |
| 75 |
package gives you full control over the definitions (ok, there are some |
| 76 |
limitations --- you can not send mail with a, say, \verb:\For: command). |
| 77 |
\item[algcompatible] is fully compatible with the \old\ package, it should be |
| 78 |
used only in old documents. |
| 79 |
\item[algpascal] aims to create a formatted pascal program, it performs |
| 80 |
automatic indentation (!), so you can transform a pascal program into an |
| 81 |
\textbf{algpascal} algorithm description with some basic substitution rules. |
| 82 |
\item[algc] -- yeah, just like the \textbf{algpascal}\dots\ but for c\dots\ |
| 83 |
This layout is incomplete. |
| 84 |
\end{description} |
| 85 |
|
| 86 |
To create floating algorithms you will need \verb:algorithm.sty:. This file may or may not be |
| 87 |
included in the \alg\ package. You can find it on CTAN, in the \old\ package. |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
\subsection{The algorithmic block} |
| 92 |
Each algorithm begins with the \verb:\begin{algorithmic}[lines]: command, the |
| 93 |
optional \verb:lines: controls the line numbering: $0$ means no line numbering, |
| 94 |
$1$ means number every line, and $n$ means number lines $n$, $2n$, $3n$\dots\ until the |
| 95 |
\verb:\end{algorithmic}: command, witch ends the algorithm. |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
\subsection{Simple lines} |
| 100 |
|
| 101 |
A simple line of text is beginned with \verb:\State:. This macro marks the begin of every |
| 102 |
line. You don't need to use \verb:\State: before a command defined in the package, since |
| 103 |
these commands use automatically a new line. |
| 104 |
|
| 105 |
To obtain a line that is not numbered, and not counted when counting the lines for line numbering |
| 106 |
(in case you choose to number lines), use the \verb:Statex: macro. This macro jumps into a new line, |
| 107 |
the line gets no number, and any label will point to the previous numbered line. |
| 108 |
|
| 109 |
We will call \textit{statament\/}s the lines starting with \verb:\State:. The \verb:\Statex: |
| 110 |
lines are not stataments. |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
\subsection{Placing comments in sources}\label{Putting comments in sources} |
| 115 |
|
| 116 |
Comments may be placed everywhere in the source using the \verb:\Comment: macro |
| 117 |
(there are no limitations like those in the \old\ package), feel the freedom! |
| 118 |
If you would like to change the form in witch comments are displayed, just |
| 119 |
change the \verb:\algorithmiccomment: macro: |
| 120 |
|
| 121 |
\begin{verbatim} |
| 122 |
\algrenewcommand{\algorithmiccomment}[1]{\hskip3em$\rightarrow$ #1} |
| 123 |
\end{verbatim} |
| 124 |
|
| 125 |
will result: |
| 126 |
\medskip |
| 127 |
\begin{algorithmic}[1] |
| 128 |
\algrenewcommand{\algorithmiccomment}[1]{\hskip3em$\rightarrow$ #1} |
| 129 |
\State $x\gets x+1$\Comment{Here is the new comment} |
| 130 |
\end{algorithmic} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
\subsection{Labels and references} |
| 135 |
Use the \verb:\label: macro, as usual to label a line. When you use \verb:\ref: to reference |
| 136 |
the line, the \verb:\ref: will be subtitued with the corresponding line number. When using the |
| 137 |
\textbf{algorithmicx} package togedher with the \textbf{algorithm} package, then you can label |
| 138 |
both the algorithm and the line, and use the \verb:\algref: macro to reference a given line |
| 139 |
from a given algorithm: |
| 140 |
|
| 141 |
\begin{verbatim} |
| 142 |
\algref{<algorithm>}{<line>} |
| 143 |
\end{verbatim} |
| 144 |
|
| 145 |
\noindent\begin{minipage}[t]{0.5\linewidth} |
| 146 |
\begin{verbatim} |
| 147 |
The \textbf{while} in algorithm |
| 148 |
\ref{euclid} ends in line |
| 149 |
\ref{euclidendwhile}, so |
| 150 |
\algref{euclid}{euclidendwhile} |
| 151 |
is the line we seek. |
| 152 |
\end{verbatim} |
| 153 |
\end{minipage}\begin{minipage}[t]{0.5\linewidth} |
| 154 |
The \textbf{while} in algorithm \ref{euclid} ends in line \ref{euclidendwhile}, |
| 155 |
so \algref{euclid}{euclidendwhile} is the line we seek. |
| 156 |
\end{minipage} |
| 157 |
|
| 158 |
\subsection{Breaking up long algorithms} |
| 159 |
|
| 160 |
Sometimes you have a long algorithm that needs to be broken into parts, each on a |
| 161 |
separate float. For this you can use the following: |
| 162 |
|
| 163 |
\begin{description} |
| 164 |
\item[]\verb:\algstore{<savename>}: saves the line number, indentation, open blocks of |
| 165 |
the current algorithm and closes all blocks. If used, then this must be the last command |
| 166 |
before closing the algorithmic block. Each saved algorithm must be continued later in the |
| 167 |
document. |
| 168 |
\item[]\verb:\algstore*{<savename>}: Like the above, but the algorithm must not be continued. |
| 169 |
\item[]\verb:\algrestore{<savename>}: restores the state of the algorithm saved under |
| 170 |
\verb:<savename>: in this algorithmic block. If used, then this must be the first command |
| 171 |
in an algorithmic block. A save is deleted while restoring. |
| 172 |
\item[]\verb:\algrestore*{<savename>}: Like the above, but the save will not be deleted, so it |
| 173 |
can be restored again. |
| 174 |
\end{description} |
| 175 |
|
| 176 |
See example in the \textbf{Examples} section. |
| 177 |
|
| 178 |
\subsection{Multiple layouts in the same document} |
| 179 |
|
| 180 |
You can load multiple algorithmicx layouts in the same document. You can switch between the layouts |
| 181 |
using the \verb:\alglanguage{<layoutname>}: command. After this command all new algorithmic |
| 182 |
environments will use the given layout until the layout is changed again. |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
\section{The predefined layouts} |
| 188 |
|
| 189 |
\subsection{The \textbf{algpseudocode} layout}\label{algpseudocode} |
| 190 |
\alglanguage{pseudocode} |
| 191 |
If you are familiar with the \old\ package, then you'll find it easy to |
| 192 |
switch. You can use the old algorithms with the \textbf{algcompatible} layout, but please |
| 193 |
use the \textbf{algpseudocode} layout for new algorithms. |
| 194 |
|
| 195 |
To use \textbf{algpseudocode}, simply load \verb:algpseudocode.sty:: |
| 196 |
|
| 197 |
\begin{verbatim} |
| 198 |
\usepackage{algpseudocode} |
| 199 |
\end{verbatim} |
| 200 |
|
| 201 |
You don't need to manually load the \textbf{algorithmicx} package, as this is done by |
| 202 |
\textbf{algpseudocode}. |
| 203 |
|
| 204 |
The first algorithm one should write is the first algorithm ever (ok, |
| 205 |
an improved version), \textit{\euk's algorithm}: |
| 206 |
|
| 207 |
\begin{algorithm}[H] |
| 208 |
\caption{\euk's algorithm}\label{euclid} |
| 209 |
\begin{algorithmic}[1] |
| 210 |
\Procedure{\euk}{$a,b$}\Comment{The g.c.d. of a and b} |
| 211 |
\State $r\gets a\bmod b$ |
| 212 |
\While{$r\not=0$}\Comment{We have the answer if r is 0} |
| 213 |
\State $a\gets b$ |
| 214 |
\State $b\gets r$ |
| 215 |
\State $r\gets a\bmod b$ |
| 216 |
\EndWhile\label{euclidendwhile} |
| 217 |
\State \Return $b$\Comment{The gcd is b} |
| 218 |
\EndProcedure |
| 219 |
\end{algorithmic} |
| 220 |
\end{algorithm} |
| 221 |
|
| 222 |
|
| 223 |
Created with the following source: |
| 224 |
\begin{verbatim} |
| 225 |
\begin{algorithm} |
| 226 |
\caption{Euclid's algorithm}\label{euclid} |
| 227 |
\begin{algorithmic}[1] |
| 228 |
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b} |
| 229 |
\State $r\gets a\bmod b$ |
| 230 |
\While{$r\not=0$}\Comment{We have the answer if r is 0} |
| 231 |
\State $a\gets b$ |
| 232 |
\State $b\gets r$ |
| 233 |
\State $r\gets a\bmod b$ |
| 234 |
\EndWhile\label{euclidendwhile} |
| 235 |
\State \textbf{return} $b$\Comment{The gcd is b} |
| 236 |
\EndProcedure |
| 237 |
\end{algorithmic} |
| 238 |
\end{algorithm} |
| 239 |
\end{verbatim} |
| 240 |
|
| 241 |
The \verb:\State: stands at the beginning of each simple statement; the respective |
| 242 |
statement is put in a new line, with the needed indentation. |
| 243 |
The \verb:\Procedure: \dots\verb:\EndProcedure: and |
| 244 |
\verb:\While: \dots\verb:\EndWhile: blocks (like any block defined in the |
| 245 |
\textbf{algpseudocode} layout) automatically indent their content. |
| 246 |
The indentation of the source doesn't matter, so |
| 247 |
|
| 248 |
\ASTART |
| 249 |
\begin{verbatim} |
| 250 |
\begin{algorithmic}[1] |
| 251 |
\Repeat |
| 252 |
\Comment{forever} |
| 253 |
\State this\Until{you die.} |
| 254 |
\end{algorithmic} |
| 255 |
\end{verbatim} |
| 256 |
\ACONTINUE |
| 257 |
\begin{algorithmic}[1] |
| 258 |
\Repeat |
| 259 |
\Comment{forever} |
| 260 |
\State this\Until{you die.} |
| 261 |
\Statex |
| 262 |
\end{algorithmic} |
| 263 |
\AENDSKIP |
| 264 |
|
| 265 |
But, generally, it is a good idea to keep the source indented, since you will find |
| 266 |
errors much easier. And your tex file looks better! |
| 267 |
|
| 268 |
All examples and syntax descriptions will be shown as the previous |
| 269 |
example --- the left side shows the \LaTeX\ input, and the right side |
| 270 |
the algorithm, as it appears in your document. I'm cheating! Don't look |
| 271 |
in the \verb:algorithmicx.tex: file! Believe what the examples state! I may use some |
| 272 |
undocumented and dirty stuff to create all these examples. You might be more |
| 273 |
confused after opening \verb:algorithmicx.tex: as you was before. |
| 274 |
|
| 275 |
In the case of syntax |
| 276 |
descriptions the text between $<$ and $>$ is symbolic, so if you type |
| 277 |
what you see on the left side, you will not get the algorithm on the |
| 278 |
right side. But if you replace the text between $<$ $>$ with a proper piece of |
| 279 |
algorithm, then you will probably get what you want. The parts between |
| 280 |
$[$ and $]$ are optional. |
| 281 |
|
| 282 |
|
| 283 |
\subsubsection{The \textbf{for} block} |
| 284 |
The \textbf{for} block may have one of the forms: |
| 285 |
|
| 286 |
\ASTART |
| 287 |
\begin{verbatim} |
| 288 |
\For{<text>} |
| 289 |
<body> |
| 290 |
\EndFor |
| 291 |
\end{verbatim} |
| 292 |
\ACONTINUE |
| 293 |
\begin{algorithmic}[1] |
| 294 |
\For{$<$text$>$} |
| 295 |
\State $<$body$>$ |
| 296 |
\EndFor |
| 297 |
\end{algorithmic} |
| 298 |
\AEND |
| 299 |
|
| 300 |
\ASTART |
| 301 |
\begin{verbatim} |
| 302 |
\ForAll{<text>} |
| 303 |
<body> |
| 304 |
\EndFor |
| 305 |
\end{verbatim} |
| 306 |
\ACONTINUE |
| 307 |
\begin{algorithmic}[1] |
| 308 |
\ForAll{$<$text$>$} |
| 309 |
\State $<$body$>$ |
| 310 |
\EndFor |
| 311 |
\end{algorithmic} |
| 312 |
\AENDSKIP |
| 313 |
|
| 314 |
\noindent Example: |
| 315 |
|
| 316 |
\ASTART |
| 317 |
\begin{verbatim} |
| 318 |
\begin{algorithmic}[1] |
| 319 |
\State $sum\gets 0$ |
| 320 |
\For{$i\gets 1, n$} |
| 321 |
\State $sum\gets sum+i$ |
| 322 |
\EndFor |
| 323 |
\end{algorithmic} |
| 324 |
\end{verbatim} |
| 325 |
\ACONTINUE |
| 326 |
\begin{algorithmic}[1] |
| 327 |
\State $sum\gets 0$ |
| 328 |
\For{$i\gets 1, n$} |
| 329 |
\State $sum\gets sum+i$ |
| 330 |
\EndFor |
| 331 |
\Statex |
| 332 |
\end{algorithmic} |
| 333 |
\AEND |
| 334 |
|
| 335 |
\subsubsection{The \textbf{while} block} |
| 336 |
The \textbf{while} block has the form: |
| 337 |
|
| 338 |
\ASTART |
| 339 |
\begin{verbatim} |
| 340 |
\While{<text>} |
| 341 |
<body> |
| 342 |
\EndWhile |
| 343 |
\end{verbatim} |
| 344 |
\ACONTINUE |
| 345 |
\begin{algorithmic}[1] |
| 346 |
\While{$<$text$>$} |
| 347 |
\State $<$body$>$ |
| 348 |
\EndWhile |
| 349 |
\end{algorithmic} |
| 350 |
\AENDSKIP |
| 351 |
|
| 352 |
\noindent Example: |
| 353 |
|
| 354 |
\ASTART |
| 355 |
\begin{verbatim} |
| 356 |
\begin{algorithmic}[1] |
| 357 |
\State $sum\gets 0$ |
| 358 |
\State $i\gets 1$ |
| 359 |
\While{$i\le n$} |
| 360 |
\State $sum\gets sum+i$ |
| 361 |
\State $i\gets i+1$ |
| 362 |
\EndWhile |
| 363 |
\end{algorithmic} |
| 364 |
\end{verbatim} |
| 365 |
\ACONTINUE |
| 366 |
\begin{algorithmic}[1] |
| 367 |
\State $sum\gets 0$ |
| 368 |
\State $i\gets 1$ |
| 369 |
\While{$i\le n$} |
| 370 |
\State $sum\gets sum+i$ |
| 371 |
\State $i\gets i+1$ |
| 372 |
\EndWhile |
| 373 |
\Statex |
| 374 |
\end{algorithmic} |
| 375 |
\AEND |
| 376 |
|
| 377 |
\subsubsection{The \textbf{repeat} block} |
| 378 |
The \textbf{repeat} block has the form: |
| 379 |
|
| 380 |
\ASTART |
| 381 |
\begin{verbatim} |
| 382 |
\Repeat |
| 383 |
<body> |
| 384 |
\Until{<text>} |
| 385 |
\end{verbatim} |
| 386 |
\ACONTINUE |
| 387 |
\begin{algorithmic}[1] |
| 388 |
\Repeat |
| 389 |
\State $<$body$>$ |
| 390 |
\Until{$<$text$>$} |
| 391 |
\end{algorithmic} |
| 392 |
\AENDSKIP |
| 393 |
|
| 394 |
\noindent Example: |
| 395 |
|
| 396 |
\ASTART |
| 397 |
\begin{verbatim} |
| 398 |
\begin{algorithmic}[1] |
| 399 |
\State $sum\gets 0$ |
| 400 |
\State $i\gets 1$ |
| 401 |
\Repeat |
| 402 |
\State $sum\gets sum+i$ |
| 403 |
\State $i\gets i+1$ |
| 404 |
\Until{$i>n$} |
| 405 |
\end{algorithmic} |
| 406 |
\end{verbatim} |
| 407 |
\ACONTINUE |
| 408 |
\begin{algorithmic}[1] |
| 409 |
\State $sum\gets 0$ |
| 410 |
\State $i\gets 1$ |
| 411 |
\Repeat |
| 412 |
\State $sum\gets sum+i$ |
| 413 |
\State $i\gets i+1$ |
| 414 |
\Until{$i>n$} |
| 415 |
\Statex |
| 416 |
\end{algorithmic} |
| 417 |
\AEND |
| 418 |
|
| 419 |
\subsubsection{The \textbf{if} block} |
| 420 |
The \textbf{if} block has the form: |
| 421 |
|
| 422 |
\ASTART |
| 423 |
\begin{verbatim} |
| 424 |
\If{<text>} |
| 425 |
<body> |
| 426 |
[ |
| 427 |
\ElsIf{<text>} |
| 428 |
<body> |
| 429 |
... |
| 430 |
] |
| 431 |
[ |
| 432 |
\Else |
| 433 |
<body> |
| 434 |
] |
| 435 |
\EndIf |
| 436 |
\end{verbatim} |
| 437 |
\ACONTINUE |
| 438 |
\begin{algorithmic}[1] |
| 439 |
\If{$<$text$>$} |
| 440 |
\State $<$body$>$ |
| 441 |
\Statex [ |
| 442 |
\ElsIf{$<$text$>$} |
| 443 |
\State $<$body$>$ |
| 444 |
\Statex \dots |
| 445 |
\Statex ] |
| 446 |
\Statex [ |
| 447 |
\Else |
| 448 |
\State $<$body$>$ |
| 449 |
\Statex ] |
| 450 |
\EndIf |
| 451 |
\end{algorithmic} |
| 452 |
\AENDSKIP |
| 453 |
|
| 454 |
\noindent Example: |
| 455 |
|
| 456 |
\ASTART |
| 457 |
\begin{verbatim} |
| 458 |
\begin{algorithmic}[1] |
| 459 |
\If{$quality\ge 9$} |
| 460 |
\State $a\gets perfect$ |
| 461 |
\ElsIf{$quality\ge 7$} |
| 462 |
\State $a\gets good$ |
| 463 |
\ElsIf{$quality\ge 5$} |
| 464 |
\State $a\gets medium$ |
| 465 |
\ElsIf{$quality\ge 3$} |
| 466 |
\State $a\gets bad$ |
| 467 |
\Else |
| 468 |
\State $a\gets unusable$ |
| 469 |
\EndIf |
| 470 |
\end{algorithmic} |
| 471 |
\end{verbatim} |
| 472 |
\ACONTINUE |
| 473 |
\begin{algorithmic}[1] |
| 474 |
\If{$quality\ge 9$} |
| 475 |
\State $a\gets perfect$ |
| 476 |
\ElsIf{$quality\ge 7$} |
| 477 |
\State $a\gets good$ |
| 478 |
\ElsIf{$quality\ge 5$} |
| 479 |
\State $a\gets medium$ |
| 480 |
\ElsIf{$quality\ge 3$} |
| 481 |
\State $a\gets bad$ |
| 482 |
\Else |
| 483 |
\State $a\gets unusable$ |
| 484 |
\EndIf |
| 485 |
\Statex |
| 486 |
\end{algorithmic} |
| 487 |
\AEND |
| 488 |
|
| 489 |
\subsubsection{The \textbf{procedure} block} |
| 490 |
The \textbf{procedure} block has the form: |
| 491 |
|
| 492 |
\ASTART |
| 493 |
\begin{verbatim} |
| 494 |
\Procedure{<name>}{<params>} |
| 495 |
<body> |
| 496 |
\EndProcedure |
| 497 |
\end{verbatim} |
| 498 |
\ACONTINUE |
| 499 |
\begin{algorithmic}[1] |
| 500 |
\Procedure{$<$name$>$}{$<$params$>$} |
| 501 |
\State $<$body$>$ |
| 502 |
\EndProcedure |
| 503 |
\end{algorithmic} |
| 504 |
\AENDSKIP |
| 505 |
|
| 506 |
\noindent Example: See \euk's\ algorithm on page \pageref{euclid}. |
| 507 |
|
| 508 |
\subsubsection{The \textbf{function} block}The |
| 509 |
\textbf{function} block has the same syntax as the \textbf{procedure} block: |
| 510 |
|
| 511 |
\ASTART |
| 512 |
\begin{verbatim} |
| 513 |
\Function{<name>}{<params>} |
| 514 |
<body> |
| 515 |
\EndFunction |
| 516 |
\end{verbatim} |
| 517 |
\ACONTINUE |
| 518 |
\begin{algorithmic}[1] |
| 519 |
\Function{$<$name$>$}{$<$params$>$} |
| 520 |
\State $<$body$>$ |
| 521 |
\EndFunction |
| 522 |
\end{algorithmic} |
| 523 |
\AEND |
| 524 |
|
| 525 |
\subsubsection{The \textbf{loop} block} |
| 526 |
The \textbf{loop} block has the form: |
| 527 |
|
| 528 |
\ASTART |
| 529 |
\begin{verbatim} |
| 530 |
\Loop |
| 531 |
<body> |
| 532 |
\EndLoop |
| 533 |
\end{verbatim} |
| 534 |
\ACONTINUE |
| 535 |
\begin{algorithmic}[1] |
| 536 |
\Loop |
| 537 |
\State $<$body$>$ |
| 538 |
\EndLoop |
| 539 |
\end{algorithmic} |
| 540 |
\AEND |
| 541 |
|
| 542 |
\subsubsection{Other commands in this layout} |
| 543 |
|
| 544 |
The starting conditions for the algorithm can be described with the \textbf{require} |
| 545 |
instruction, and its result with the \textbf{ensure} instruction. |
| 546 |
|
| 547 |
A procedure call can be formatted with \verb:\Call:. |
| 548 |
|
| 549 |
\ASTART |
| 550 |
\begin{verbatim} |
| 551 |
\Require something |
| 552 |
\Ensure something |
| 553 |
\Statex |
| 554 |
\State \Call{Create}{10} |
| 555 |
\end{verbatim} |
| 556 |
\ACONTINUE |
| 557 |
\begin{algorithmic}[1] |
| 558 |
\Require something |
| 559 |
\Ensure something |
| 560 |
\Statex |
| 561 |
\State \Call{Create}{10} |
| 562 |
\end{algorithmic} |
| 563 |
\AENDSKIP |
| 564 |
|
| 565 |
\noindent Example: |
| 566 |
|
| 567 |
\ASTART |
| 568 |
\begin{verbatim} |
| 569 |
\begin{algorithmic}[1] |
| 570 |
\Require $x\ge5$ |
| 571 |
\Ensure $x\le-5$ |
| 572 |
\Statex |
| 573 |
\While{$x>-5$} |
| 574 |
\State $x\gets x-1$ |
| 575 |
\EndWhile |
| 576 |
\end{algorithmic} |
| 577 |
\end{verbatim} |
| 578 |
\ACONTINUE |
| 579 |
\begin{algorithmic}[1] |
| 580 |
\Require $x\ge5$ |
| 581 |
\Ensure $x\le-5$ |
| 582 |
\Statex |
| 583 |
\While{$x>-5$} |
| 584 |
\State $x\gets x-1$ |
| 585 |
\EndWhile |
| 586 |
\Statex |
| 587 |
\end{algorithmic} |
| 588 |
\AEND |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
\subsubsection{Package options}\label{algpseudocode package options} |
| 595 |
|
| 596 |
The \texttt{algpseudocode} package supports the following options: |
| 597 |
|
| 598 |
\begin{description} |
| 599 |
\item[compatible/noncompatible]\ \textit{Obsolote, use the algcompatible layout instead.}\\ |
| 600 |
If you would like to use old |
| 601 |
algorithms, written with the \old\ package without (too much) |
| 602 |
modification, then use the \textbf{compatible} option. This option |
| 603 |
defines the uppercase version of the commands. Note that you still need |
| 604 |
to remove the \verb:[...]: comments (these comments appeared due to some |
| 605 |
limitations in the \old\ package, these limitations and comments are gone now). |
| 606 |
The default \textbf{noncompatible} does not define the all uppercase |
| 607 |
commands. |
| 608 |
\item[noend/end]\ \\With \textbf{noend} specified all \textbf{end \dots} |
| 609 |
lines are omitted. You get a somewhat smaller algorithm, and the ugly |
| 610 |
feeling, that something is missing\dots{} The \textbf{end} value is the |
| 611 |
default, it means, that all \textbf{end \dots} lines are in their right |
| 612 |
place. |
| 613 |
\end{description} |
| 614 |
|
| 615 |
|
| 616 |
\subsubsection{Changing command names} |
| 617 |
One common thing for a pseudocode is to change the command names. Many people |
| 618 |
use many different kind of pseudocode command names. In \textbf{algpseudocode} |
| 619 |
all keywords are declared as \verb:\algorithmic<keyword>:. You can change them |
| 620 |
to output the text you need: |
| 621 |
|
| 622 |
\bigskip\noindent\begin{minipage}[b]{0.5\linewidth} |
| 623 |
\begin{verbatim} |
| 624 |
\algrenewcommand\algorithmicwhile{\textbf{am\'\i g}} |
| 625 |
\algrenewcommand\algorithmicdo{\textbf{v\'egezd el}} |
| 626 |
\algrenewcommand\algorithmicend{\textbf{v\'ege}} |
| 627 |
\begin{algorithmic}[1] |
| 628 |
\State $x \gets 1$ |
| 629 |
\While{$x < 10$} |
| 630 |
\State $x \gets x + 1$ |
| 631 |
\EndWhile |
| 632 |
\end{algorithmic} |
| 633 |
\end{verbatim} |
| 634 |
\end{minipage} |
| 635 |
\begin{minipage}[b]{0.5\linewidth} |
| 636 |
\begin{algorithmic}[1] |
| 637 |
\algrenewcommand\algorithmicwhile{\textbf{am\'\i g}} |
| 638 |
\algrenewcommand\algorithmicdo{\textbf{v\'egezd el}} |
| 639 |
\algrenewcommand\algorithmicend{\textbf{v\'ege}} |
| 640 |
\State $x \gets 1$ |
| 641 |
\While{$x < 10$} |
| 642 |
\State $x \gets x + 1$ |
| 643 |
\EndWhile |
| 644 |
\Statex |
| 645 |
\end{algorithmic} |
| 646 |
\end{minipage}\bigskip |
| 647 |
|
| 648 |
In some cases you may need to change even more (in the above example |
| 649 |
\textbf{am\'\i g} and \textbf{v\'ege} should be interchanged in the \verb:\EndWhile: |
| 650 |
text). Maybe the number of the parameters taken by some commands must be changed too. |
| 651 |
this can be done with the command text customizing macros (see section |
| 652 |
\ref{custom text}). Here I'll give only some examples of the most common usage: |
| 653 |
|
| 654 |
\bigskip\noindent\begin{minipage}[b]{0.5\linewidth} |
| 655 |
\begin{verbatim} |
| 656 |
\algrenewcommand\algorithmicwhile{\textbf{am\'\i g}} |
| 657 |
\algrenewcommand\algorithmicdo{\textbf{v\'egezd el}} |
| 658 |
\algrenewcommand\algorithmicend{\textbf{v\'ege}} |
| 659 |
\algrenewtext{EndWhile}{\algorithmicwhile\ \algorithmicend} |
| 660 |
\begin{algorithmic}[1] |
| 661 |
\State $x \gets 1$ |
| 662 |
\While{$x < 10$} |
| 663 |
\State $x \gets x - 1$ |
| 664 |
\EndWhile |
| 665 |
\end{algorithmic} |
| 666 |
\end{verbatim} |
| 667 |
\end{minipage} |
| 668 |
\begin{minipage}[b]{0.5\linewidth} |
| 669 |
\begin{algorithmic}[1] |
| 670 |
\algrenewcommand\algorithmicwhile{\textbf{am\'\i g}} |
| 671 |
\algrenewcommand\algorithmicdo{\textbf{v\'egezd el}} |
| 672 |
\algrenewcommand\algorithmicend{\textbf{v\'ege}} |
| 673 |
\algrenewtext{EndWhile}{\algorithmicwhile\ \algorithmicend} |
| 674 |
\State $x \gets 1$ |
| 675 |
\While{$x < 10$} |
| 676 |
\State $x \gets x - 1$ |
| 677 |
\EndWhile |
| 678 |
\Statex |
| 679 |
\end{algorithmic} |
| 680 |
\end{minipage} |
| 681 |
|
| 682 |
\bigskip\noindent\begin{minipage}[b]{0.5\linewidth} |
| 683 |
\begin{verbatim} |
| 684 |
\algnewcommand\algorithmicto{\textbf{to}} |
| 685 |
\algrenewtext{For}[3]% |
| 686 |
{\algorithmicfor\ #1 \gets #2 \algorithmicto\ #3 \algorithmicdo} |
| 687 |
\begin{algorithmic}[1] |
| 688 |
\State $p \gets 1$ |
| 689 |
\For{i}{1}{n} |
| 690 |
\State $p \gets p * i$ |
| 691 |
\EndFor |
| 692 |
\end{algorithmic} |
| 693 |
\end{verbatim} |
| 694 |
\end{minipage} |
| 695 |
\begin{minipage}[b]{0.5\linewidth} |
| 696 |
\begin{algorithmic}[1] |
| 697 |
\algnewcommand\algorithmicto{\textbf{to}} |
| 698 |
\algrenewtext{For}[3]% |
| 699 |
{\algorithmicfor\ $#1 \gets #2$ \algorithmicto\ $#3$ \algorithmicdo} |
| 700 |
\State $p \gets 1$ |
| 701 |
\For{i}{1}{n} |
| 702 |
\State $p \gets p * i$ |
| 703 |
\EndFor |
| 704 |
\Statex |
| 705 |
\end{algorithmic} |
| 706 |
\end{minipage}\bigskip |
| 707 |
|
| 708 |
You could create a translation package, that included after the \textbf{algpseudocode} |
| 709 |
package translates the keywords to the language you need. |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
\subsection{The \textbf{algpascal} layout} |
| 716 |
\alglanguage{pascal} |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
The most important feature of the \textbf{algpascal} layout is that |
| 721 |
\textit{it performs automatically the block indentation}. In |
| 722 |
section \ref{algorithmicx} you will see how to define such |
| 723 |
automatically indented loops. Here is an example to demonstrate this |
| 724 |
feature: |
| 725 |
|
| 726 |
\ASTART |
| 727 |
\begin{verbatim} |
| 728 |
\begin{algorithmic}[1] |
| 729 |
\Begin |
| 730 |
\State $sum:=0$; |
| 731 |
\For{i=1}{n}\Comment{sum(i)} |
| 732 |
\State $sum:=sum+i$; |
| 733 |
\State writeln($sum$); |
| 734 |
\End. |
| 735 |
\end{algorithmic} |
| 736 |
\end{verbatim} |
| 737 |
\ACONTINUE |
| 738 |
\begin{algorithmic}[1] |
| 739 |
\Begin |
| 740 |
\State $sum:=0$; |
| 741 |
\For{i=1}{n}\Comment{sum(i)} |
| 742 |
\State $sum:=sum+i$; |
| 743 |
\State writeln($sum$); |
| 744 |
\End. |
| 745 |
\Statex |
| 746 |
\end{algorithmic} |
| 747 |
\AENDSKIP |
| 748 |
|
| 749 |
Note, that the \verb:\For: is not closed explicitly, its end is |
| 750 |
detected automatically. Again, the indentation in the source doesn't |
| 751 |
affect the output. |
| 752 |
In this layout every parameter passed to a command is put in |
| 753 |
mathematical mode. |
| 754 |
|
| 755 |
\subsubsection{The \textbf{begin} \dots{} \textbf{end} block} |
| 756 |
|
| 757 |
\noindent\begin{minipage}[b]{0.5\linewidth} |
| 758 |
\begin{verbatim} |
| 759 |
\Begin |
| 760 |
<body> |
| 761 |
\End |
| 762 |
\end{verbatim} |
| 763 |
\ACONTINUE |
| 764 |
\begin{algorithmic}[1] |
| 765 |
\Begin |
| 766 |
\State $<$body$>$ |
| 767 |
\End |
| 768 |
\end{algorithmic} |
| 769 |
\AENDSKIP |
| 770 |
|
| 771 |
The \verb:\Begin: \dots{} \verb:\End: block and the |
| 772 |
\verb:\Repeat: \dots{} \verb:\Until: block are the only blocks in |
| 773 |
the \textbf{algpascal} style (instead of \verb:\Begin: you may write |
| 774 |
\verb:\Asm:). This means, that every other loop is ended automatically |
| 775 |
after the following command (another loop, or a block). |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
\subsubsection{The \textbf{for} loop} |
| 780 |
|
| 781 |
\noindent\begin{minipage}[b]{0.5\linewidth} |
| 782 |
\begin{verbatim} |
| 783 |
\For{<assign>}{<expr>} |
| 784 |
<command> |
| 785 |
\end{verbatim} |
| 786 |
\ACONTINUE |
| 787 |
\begin{algorithmic}[1] |
| 788 |
\For{<$\relax$assign$\relax$>}{<$\relax$expr$\relax$>} |
| 789 |
\State $<$command$>$ |
| 790 |
\end{algorithmic} |
| 791 |
\AENDSKIP |
| 792 |
|
| 793 |
The \textbf{For} loop (as all other loops) ends after the following command (a block counts |
| 794 |
also as a single command). |
| 795 |
|
| 796 |
\ASTART |
| 797 |
\begin{verbatim} |
| 798 |
\begin{algorithmic}[1] |
| 799 |
\Begin |
| 800 |
\State $sum:=0$; |
| 801 |
\State $prod:=1$; |
| 802 |
\For{i:=1}{10} |
| 803 |
\Begin |
| 804 |
\State $sum:=sum+i$; |
| 805 |
\State $prod:=prod*i$; |
| 806 |
\End |
| 807 |
\End. |
| 808 |
\end{algorithmic} |
| 809 |
\end{verbatim} |
| 810 |
\ACONTINUE |
| 811 |
\begin{algorithmic}[1] |
| 812 |
\Begin |
| 813 |
\State $sum:=0$; |
| 814 |
\State $prod:=1$; |
| 815 |
\For{i:=1}{10} |
| 816 |
\Begin |
| 817 |
\State $sum:=sum+i$; |
| 818 |
\State $prod:=prod*i$; |
| 819 |
\End |
| 820 |
\End. |
| 821 |
\Statex |
| 822 |
\end{algorithmic} |
| 823 |
\AEND |
| 824 |
|
| 825 |
\subsubsection{The \textbf{while} loop} |
| 826 |
|
| 827 |
\noindent\begin{minipage}[b]{0.5\linewidth} |
| 828 |
\begin{verbatim} |
| 829 |
\While{<expression>} |
| 830 |
<command> |
| 831 |
\end{verbatim} |
| 832 |
\ACONTINUE |
| 833 |
\begin{algorithmic}[1] |
| 834 |
\While{<$\relax$expression$\relax$>} |
| 835 |
\State $<$command$>$ |
| 836 |
\end{algorithmic} |
| 837 |
\AEND |
| 838 |
|
| 839 |
|
| 840 |
\subsubsection{The \textbf{repeat}\dots\ \textbf{until} block} |
| 841 |
|
| 842 |
\noindent\begin{minipage}[b]{0.5\linewidth} |
| 843 |
\begin{verbatim} |
| 844 |
\Repeat |
| 845 |
<body> |
| 846 |
\Until{<expression>} |
| 847 |
\end{verbatim} |
| 848 |
\ACONTINUE |
| 849 |
\begin{algorithmic}[1] |
| 850 |
\Repeat |
| 851 |
\State $<$body$>$ |
| 852 |
\Until{<$\relax$expression$\relax$>} |
| 853 |
\end{algorithmic} |
| 854 |
\AEND |
| 855 |
|
| 856 |
\subsubsection{The \textbf{if} command} |
| 857 |
|
| 858 |
\noindent\begin{minipage}[b]{0.5\linewidth} |
| 859 |
\begin{verbatim} |
| 860 |
\If{<expression>} |
| 861 |
<command> |
| 862 |
[ |
| 863 |
\Else |
| 864 |
<command> |
| 865 |
] |
| 866 |
\end{verbatim} |
| 867 |
\ACONTINUE |
| 868 |
\begin{algorithmic}[1] |
| 869 |
\If{<$\relax$expression$\relax$>} |
| 870 |
\State $<$command$>$ |
| 871 |
\Statex \hskip-\algorithmicindent\hskip-\algorithmicindent[ |
| 872 |
\Else |
| 873 |
\State $<$command$>$ |
| 874 |
\Statex \hskip-\algorithmicindent\hskip-\algorithmicindent] |
| 875 |
\end{algorithmic} |
| 876 |
\AENDSKIP |
| 877 |
|
| 878 |
Every \verb:\Else: matches the nearest \verb:\If:. |
| 879 |
|
| 880 |
\subsubsection{The \textbf{procedure} command} |
| 881 |
|
| 882 |
\noindent\begin{minipage}[b]{0.5\linewidth} |
| 883 |
\begin{verbatim} |
| 884 |
\Procedure <some text> |
| 885 |
\end{verbatim} |
| 886 |
\ACONTINUE |
| 887 |
\begin{algorithmic}[1] |
| 888 |
\Procedure $<$some text$>$ |
| 889 |
\end{algorithmic} |
| 890 |
\AENDSKIP |
| 891 |
|
| 892 |
\verb:\Procedure: just writes the ``procedure'' word on a new |
| 893 |
line... You will probably put a \verb:\Begin:\dots\ \verb:\End: |
| 894 |
block after it. |
| 895 |
|
| 896 |
|
| 897 |
\subsubsection{The \textbf{function} command} |
| 898 |
|
| 899 |
\noindent\begin{minipage}[b]{0.5\linewidth} |
| 900 |
\begin{verbatim} |
| 901 |
\Function<some text> |
| 902 |
\end{verbatim} |
| 903 |
\ACONTINUE |
| 904 |
\begin{algorithmic}[1] |
| 905 |
\Function $<$some text$>$ |
| 906 |
\end{algorithmic} |
| 907 |
\AENDSKIP |
| 908 |
|
| 909 |
Just like \textbf{Procedure}. |
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
\subsection{The \textbf{algc} layout} |
| 914 |
|
| 915 |
Sorry, the \textbf{algc} layout is unfinished. |
| 916 |
The commands defined are: |
| 917 |
|
| 918 |
\begin{itemize} |
| 919 |
\item\verb:\{:\dots\ \verb:\}: block |
| 920 |
\item\verb:\For: with 3 params |
| 921 |
\item\verb:\If: with 1 param |
| 922 |
\item\verb:\Else: with no params |
| 923 |
\item\verb:\While: with 1 param |
| 924 |
\item\verb:\Do: with no params |
| 925 |
\item\verb:\Function: with 3 params |
| 926 |
\item\verb:\Return: with no params |
| 927 |
\end{itemize} |
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
\section{Custom algorithmic blocks}\label{algorithmicx} |
| 933 |
\alglanguage{default} |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
\subsection{Blocks and loops} |
| 938 |
|
| 939 |
Most of the environments defined in the standard layouts (and most probably |
| 940 |
the ones you will define) are divided in two categories: |
| 941 |
|
| 942 |
\begin{description} |
| 943 |
\item[Blocks] are the environments witch contain an arbitrary number of |
| 944 |
commands or nested blocks. Each block has a name, begins with a starting command |
| 945 |
and ends with an ending command. The commands in a block are |
| 946 |
indented by \verb:\algorithmicindent: (or another amount). |
| 947 |
|
| 948 |
If your algorithm ends without closing all blocks, the \alg\ package gives |
| 949 |
you a nice error. So be good, and close them all! |
| 950 |
|
| 951 |
Blocks are all the environments defined in the \verb:algpseudocode: |
| 952 |
package, the \verb:\Begin: \dots \verb:\End: block in the |
| 953 |
\verb:algpascal: package, and some other ones. |
| 954 |
\item[Loops] (Let us call them loops\dots) The loops are environments |
| 955 |
that include only one command, loop or block; a loop is closed |
| 956 |
automatically after this command. So loops have no ending commands. If |
| 957 |
your algorithm (or a block) ends before the single command of a loop, |
| 958 |
then this is considered an empty command, and the loop is closed. Feel |
| 959 |
free to leave open loops at the end of blocks! |
| 960 |
|
| 961 |
Loops are most of the environments in the \verb:algpascal: and |
| 962 |
\verb:algc: packages. |
| 963 |
\end{description} |
| 964 |
|
| 965 |
For some rare constructions you can create mixtures of the two |
| 966 |
environments (see section \ref{setblock}). |
| 967 |
Each block and loop may be continued with another one (like the \verb:If: |
| 968 |
with \verb:Else:). |
| 969 |
|
| 970 |
\subsection{Defining blocks}\label{defblocks} |
| 971 |
|
| 972 |
There are several commands to define blocks. The difference is in what is defined |
| 973 |
beyond the block. The macro \verb:\algblock: defines a new block with starting and |
| 974 |
ending entity. |
| 975 |
|
| 976 |
\begin{verbatim} |
| 977 |
\algblock[<block>]{<start>}{<end>} |
| 978 |
\end{verbatim} |
| 979 |
|
| 980 |
The defined commands have no parameters, and the text displayed by them is |
| 981 |
\verb:\textbf{<start>}: and \verb:\textbf{<end>}:. You can change these texts later |
| 982 |
(\ref{custom text}). |
| 983 |
|
| 984 |
With \verb:\algblockdefx: you can give the text to be output by the starting |
| 985 |
and ending command and the number of parameters for these commands. In the text |
| 986 |
reference with \#$n$ to the parameter number $n$. Observe that the text |
| 987 |
is given in the form you define or redefine macros, and really, this is what happens. |
| 988 |
|
| 989 |
\begin{verbatim} |
| 990 |
\algblockdefx[<block>]{<start>}{<end>} |
| 991 |
[<startparamcount>][<default value>]{<start text>} |
| 992 |
[<endparamcount>][<default value>]{<end text>} |
| 993 |
\end{verbatim} |
| 994 |
|
| 995 |
This defines a new block called \verb:<block>:, \verb:<start>: opens the block, |
| 996 |
\verb:<end>: closes the block, |
| 997 |
\verb:<start>: displays \verb:<start text>:, and has \verb:<startparamcount>: parameters, |
| 998 |
\verb:<end>: displays \verb:<end text>:, and has \verb:<endparamcount>: parameters. |
| 999 |
For both \verb:<start>: and \verb:<end>:, if |
| 1000 |
\verb:<default value>: is given, then the first parameter is optional, and its default value |
| 1001 |
is \verb:<default value>:. |
| 1002 |
|
| 1003 |
If you want to display different text (and to have a different number of parameters) |
| 1004 |
for \verb:<end>: at the end of different blocks, then use |
| 1005 |
the \verb:\algblockx: macro. Note that it is not possible to display different starting texts, |
| 1006 |
since it is not possible to start different blocks with the same command. The \verb:<start text>: |
| 1007 |
defined with \verb:\algblockx: has the same behavior as if defined with \verb:\algblockdefx:. All ending commands |
| 1008 |
not defined with \verb:\algblockx: will display the same text, and the ones defined with this |
| 1009 |
macro will display the different texts you specified. |
| 1010 |
|
| 1011 |
\begin{verbatim} |
| 1012 |
\algblockx[<block>]{<start>}{<end>} |
| 1013 |
[<startparamcount>][<default value>]{<start text>} |
| 1014 |
[<endparamcount>][<default value>]{<end text>} |
| 1015 |
\end{verbatim} |
| 1016 |
|
| 1017 |
If in the above definitions the \verb:<block>: is missing, then the name of the starting command |
| 1018 |
is used as block name. If a block with the given name |
| 1019 |
already exists, these macros don't define a new block, instead this it will be used the defined |
| 1020 |
block. If \verb:<start>: or \verb:<end>: is empty, then |
| 1021 |
the definition does not define a new starting/ending command for the block, and then the |
| 1022 |
respective text must be missing from the definition. You may have more starting and ending commands |
| 1023 |
for one block. If the block name is missing, then a starting command must be given. |
| 1024 |
|
| 1025 |
|
| 1026 |
\bigskip\noindent\begin{minipage}[b]{0.5\linewidth} |
| 1027 |
\begin{verbatim} |
| 1028 |
\algblock[Name]{Start}{End} |
| 1029 |
\algblockdefx[NAME]{START}{END}% |
| 1030 |
[2][Unknown]{Start #1(#2)}% |
| 1031 |
{Ending} |
| 1032 |
\algblockdefx[NAME]{}{OTHEREND}% |
| 1033 |
[1]{Until (#1)} |
| 1034 |
\begin{algorithmic}[1] |
| 1035 |
\Start |
| 1036 |
\Start |
| 1037 |
\START[One]{x} |
| 1038 |
\END |
| 1039 |
\START{0} |
| 1040 |
\OTHEREND{\texttt{True}} |
| 1041 |
\End |
| 1042 |
\Start |
| 1043 |
\End |
| 1044 |
\End |
| 1045 |
\end{algorithmic} |
| 1046 |
\end{verbatim} |
| 1047 |
\end{minipage} |
| 1048 |
\begin{minipage}[b]{0.5\linewidth} |
| 1049 |
{ |
| 1050 |
\algblock[Name]{Start}{End} |
| 1051 |
\algblockdefx[NAME]{START}{END}% |
| 1052 |
[2][Unknown]{Start #1(#2)}% |
| 1053 |
{Ending} |
| 1054 |
\algblockdefx[NAME]{}{OTHEREND}% |
| 1055 |
[1]{Until (#1)} |
| 1056 |
\begin{algorithmic}[1] |
| 1057 |
\Start |
| 1058 |
\Start |
| 1059 |
\START[One]{x} |
| 1060 |
\END |
| 1061 |
\START{0} |
| 1062 |
\OTHEREND{\texttt{True}} |
| 1063 |
\End |
| 1064 |
\Start |
| 1065 |
\End |
| 1066 |
\End |
| 1067 |
\Statex |
| 1068 |
\end{algorithmic} |
| 1069 |
} |
| 1070 |
\end{minipage} |
| 1071 |
|
| 1072 |
\subsection{Defining loops} |
| 1073 |
|
| 1074 |
The loop defining macros are similar to the block defining macros. A loop has no ending command |
| 1075 |
and ends after the first state, block or loop that follows the loop. |
| 1076 |
Since loops have no ending command, the macro \verb:\algloopx: would not have mutch sense. |
| 1077 |
The loop defining macros are: |
| 1078 |
|
| 1079 |
\begin{verbatim} |
| 1080 |
\algloop[<loop>]{<start>} |
| 1081 |
|
| 1082 |
\algloopdefx[<loop>]{<start>} |
| 1083 |
[<startparamcount>][<default value>]{<start text>} |
| 1084 |
\end{verbatim} |
| 1085 |
|
| 1086 |
Both create a loop named \verb:<loop>: with the starting command \verb:<start>:. |
| 1087 |
The second also sets the number of parameters, and the text displayed by the starting command. |
| 1088 |
|
| 1089 |
\bigskip\noindent\begin{minipage}[b]{0.5\linewidth} |
| 1090 |
\begin{verbatim} |
| 1091 |
\algloop{For} |
| 1092 |
\algloopdefx{If}[1]{\textbf{If} #1 \textbf{then}} |
| 1093 |
\algblock{Begin}{End} |
| 1094 |
\begin{algorithmic}[1] |
| 1095 |
\For |
| 1096 |
\Begin |
| 1097 |
\If{$a < b$} |
| 1098 |
\For |
| 1099 |
\Begin |
| 1100 |
\End |
| 1101 |
\Begin |
| 1102 |
\End |
| 1103 |
\End |
| 1104 |
\end{algorithmic} |
| 1105 |
\end{verbatim} |
| 1106 |
\end{minipage} |
| 1107 |
\begin{minipage}[b]{0.5\linewidth} |
| 1108 |
{ |
| 1109 |
\algloop{For} |
| 1110 |
\algloopdefx{If}% |
| 1111 |
[1]{\textbf{If} #1 \textbf{then}} |
| 1112 |
\algblock{Begin}{End} |
| 1113 |
\begin{algorithmic}[1] |
| 1114 |
\For |
| 1115 |
\Begin |
| 1116 |
\If{$a < b$} |
| 1117 |
\For |
| 1118 |
\Begin |
| 1119 |
\End |
| 1120 |
\Begin |
| 1121 |
\End |
| 1122 |
\End |
| 1123 |
\Statex |
| 1124 |
\end{algorithmic} |
| 1125 |
} |
| 1126 |
\end{minipage} |
| 1127 |
|
| 1128 |
\subsection{Continuing blocks and loops} |
| 1129 |
|
| 1130 |
For each block/loop you may give commands that close the block or loop and open another |
| 1131 |
block or loop. A good example for this is the \textbf{if}~\dots~\textbf{then}~\dots~\textbf{else} |
| 1132 |
construct. The new block or loop can be closed or continued, as any other blocks and loops. |
| 1133 |
|
| 1134 |
To create a continuing block use one of the following: |
| 1135 |
|
| 1136 |
\begin{verbatim} |
| 1137 |
\algcblock[<new block>]{<old block>}{<continue>}{<end>} |
| 1138 |
|
| 1139 |
\algcblockdefx[<new block>]{<old block>}{<continue>}{<end>} |
| 1140 |
[<continueparamcount>][<default value>]{<continue text>} |
| 1141 |
[<endparamcount>][<default value>]{<end text>} |
| 1142 |
|
| 1143 |
\algcblockx[<new block>]{<old block>}{<continue>}{<end>} |
| 1144 |
[<continueparamcount>][<default value>]{<continue text>} |
| 1145 |
[<endparamcount>][<default value>]{<end text>} |
| 1146 |
\end{verbatim} |
| 1147 |
|
| 1148 |
All three macros define a new block named \verb:<new block>:. If \verb:<new block>: is not given, |
| 1149 |
then \verb:<continue>: is used as the new block name. It is not allowed to have both |
| 1150 |
\verb:<new block>: missing, and \verb:<continue>: empty. The \verb:<continue>: command ends the |
| 1151 |
\verb:<old block>: block/loop and opens the \verb:<new block>: block. Since \verb:<continue>: may |
| 1152 |
end different blocks and loops, it can have different text |
| 1153 |
at the end of the different blocks/loops. If the \verb:<continue>: command doesn't find an |
| 1154 |
\verb:<old block>: to close, then an error is reported. |
| 1155 |
|
| 1156 |
Create continuing loops with the followings: |
| 1157 |
|
| 1158 |
\begin{verbatim} |
| 1159 |
\algcloop[<new loop>]{<old block>}{<continue>} |
| 1160 |
|
| 1161 |
\algcloopdefx[<new loop>]{<old block>}{<continue>} |
| 1162 |
[<continueparamcount>][<default value>]{<continue text>} |
| 1163 |
|
| 1164 |
\algcloopx[<new loop>]{<old block>}{<continue>} |
| 1165 |
[<continueparamcount>][<default value>]{<continue text>} |
| 1166 |
\end{verbatim} |
| 1167 |
|
| 1168 |
These macros create a continuing loop, the \verb:<continue>: closes the \verb:<old block>: |
| 1169 |
block/loop, and opens a \verb:<new loop>: loop. |
| 1170 |
|
| 1171 |
\bigskip\noindent\begin{minipage}[b]{0.5\linewidth} |
| 1172 |
\begin{verbatim} |
| 1173 |
\algblock{If}{EndIf} |
| 1174 |
\algcblock[If]{If}{ElsIf}{EndIf} |
| 1175 |
\algcblock{If}{Else}{EndIf} |
| 1176 |
\algcblockdefx[Strange]{If}{Eeee}{Oooo} |
| 1177 |
[1]{\textbf{Eeee} "#1"} |
| 1178 |
{\textbf{Wuuuups\dots}} |
| 1179 |
\begin{algorithmic}[1] |
| 1180 |
\If |
| 1181 |
\If |
| 1182 |
\ElsIf |
| 1183 |
\ElsIf |
| 1184 |
\If |
| 1185 |
\ElsIf |
| 1186 |
\Else |
| 1187 |
\EndIf |
| 1188 |
\EndIf |
| 1189 |
\If |
| 1190 |
\EndIf |
| 1191 |
\Eeee{Creep} |
| 1192 |
\Oooo |
| 1193 |
\end{algorithmic} |
| 1194 |
\end{verbatim} |
| 1195 |
\end{minipage} |
| 1196 |
\begin{minipage}[b]{0.5\linewidth} |
| 1197 |
{ |
| 1198 |
\algblock{If}{EndIf} |
| 1199 |
\algcblock[If]{If}{ElsIf}{EndIf} |
| 1200 |
\algcblock{If}{Else}{EndIf} |
| 1201 |
\algcblockdefx[Strange]{If}{Eeee}{Oooo} |
| 1202 |
[1]{\textbf{Eeee} "#1"} |
| 1203 |
{\textbf{Wuuuups\dots}} |
| 1204 |
\begin{algorithmic}[1] |
| 1205 |
\If |
| 1206 |
\If |
| 1207 |
\ElsIf |
| 1208 |
\ElsIf |
| 1209 |
\If |
| 1210 |
\ElsIf |
| 1211 |
\Else |
| 1212 |
\EndIf |
| 1213 |
\EndIf |
| 1214 |
\If |
| 1215 |
\EndIf |
| 1216 |
\Eeee{Creep} |
| 1217 |
\Oooo |
| 1218 |
\Statex |
| 1219 |
\end{algorithmic} |
| 1220 |
} |
| 1221 |
\end{minipage}\bigskip |
| 1222 |
|
| 1223 |
\bigskip\noindent\begin{minipage}[b]{0.5\linewidth} |
| 1224 |
\begin{verbatim} |
| 1225 |
\algloop{If} |
| 1226 |
\algcloop{If}{Else} |
| 1227 |
\algblock{Begin}{End} |
| 1228 |
\begin{algorithmic}[1] |
| 1229 |
\If |
| 1230 |
\Begin |
| 1231 |
\End |
| 1232 |
\Else |
| 1233 |
\If |
| 1234 |
\Begin |
| 1235 |
\End |
| 1236 |
\end{algorithmic} |
| 1237 |
\end{verbatim} |
| 1238 |
\end{minipage} |
| 1239 |
\begin{minipage}[b]{0.5\linewidth} |
| 1240 |
{ |
| 1241 |
\algloop{If} |
| 1242 |
\algcloop{If}{Else} |
| 1243 |
\algblock{Begin}{End} |
| 1244 |
\begin{algorithmic}[1] |
| 1245 |
\If |
| 1246 |
\Begin |
| 1247 |
\End |
| 1248 |
\Else |
| 1249 |
\If |
| 1250 |
\Begin |
| 1251 |
\End |
| 1252 |
\Statex |
| 1253 |
\end{algorithmic} |
| 1254 |
} |
| 1255 |
\end{minipage}\bigskip |
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
\subsection{Even more customisation}\label{setblock} |
| 1260 |
|
| 1261 |
With the following macros you can give the indentation used by the new block (or loop), |
| 1262 |
and the number of stataments after that the "block" is automatically closed. This value is $\infty$ |
| 1263 |
for blocks, 1 for loops, and 0 for stataments. There is a special value, 65535, meaning that the |
| 1264 |
defined "block" does not end automatically, but if it is enclosed in a block, then the ending |
| 1265 |
command of the block closes this "block" as well. |
| 1266 |
|
| 1267 |
\begin{verbatim} |
| 1268 |
\algsetblock[<block>]{<start>}{<end>} |
| 1269 |
{<lifetime>}{<indent>} |
| 1270 |
|
| 1271 |
\algsetblockdefx[<block>]{<start>}{<end>} |
| 1272 |
{<lifetime>}{<indent>} |
| 1273 |
[<startparamcount>][<default value>]{<start text>} |
| 1274 |
[<endparamcount>][<default value>]{<end text>} |
| 1275 |
|
| 1276 |
\algsetblockx[<block>]{<start>}{<end>} |
| 1277 |
{<lifetime>}{<indent>} |
| 1278 |
[<startparamcount>][<default value>]{<start text>} |
| 1279 |
[<endparamcount>][<default value>]{<end text>} |
| 1280 |
|
| 1281 |
\algcsetblock[<new block>]{<old block>}{<continue>}{<end>} |
| 1282 |
{<lifetime>}{<indent>} |
| 1283 |
|
| 1284 |
\algcsetblockdefx[<new block>]{<old block>}{<continue>}{<stop>} |
| 1285 |
{<lifetime>}{<indent>} |
| 1286 |
[<continueparamcount>][<default value>]{<continue text>} |
| 1287 |
[<endparamcount>][<default value>]{<end text>} |
| 1288 |
|
| 1289 |
\algcsetblockx[<new block>]{<old block>}{<continue>}{<stop>} |
| 1290 |
{<lifetime>}{<indent>} |
| 1291 |
[<continueparamcount>][<default value>]{<continue text>} |
| 1292 |
[<endparamcount>][<default value>]{<end text>} |
| 1293 |
\end{verbatim} |
| 1294 |
|
| 1295 |
The \verb:<lifetime>: is the number of stataments after that the block is closed. An empty |
| 1296 |
\verb:<lifetime>: field means $\infty$. The \verb:<indent>: gives the indentation of the block. |
| 1297 |
Leave this field empty for the default indentation. The rest of the parameters has the same |
| 1298 |
function as for the previous macros. |
| 1299 |
|
| 1300 |
\bigskip\noindent\begin{minipage}[b]{0.5\linewidth} |
| 1301 |
\begin{verbatim} |
| 1302 |
\algsetblock[Name]{Start}{Stop}{3}{1cm} |
| 1303 |
\algsetcblock[CName]{Name}{CStart}{CStop}{2}{2cm} |
| 1304 |
\begin{algorithmic}[1] |
| 1305 |
\Start |
| 1306 |
\State 1 |
| 1307 |
\State 2 |
| 1308 |
\State 3 |
| 1309 |
\State 4 |
| 1310 |
\Start |
| 1311 |
\State 1 |
| 1312 |
\Stop |
| 1313 |
\State 2 |
| 1314 |
\Start |
| 1315 |
\State 1 |
| 1316 |
\CStart |
| 1317 |
\State 1 |
| 1318 |
\State 2 |
| 1319 |
\State 3 |
| 1320 |
\Start |
| 1321 |
\State 1 |
| 1322 |
\CStart |
| 1323 |
\State 1 |
| 1324 |
\CStop |
| 1325 |
\end{algorithmic} |
| 1326 |
\end{verbatim} |
| 1327 |
\end{minipage} |
| 1328 |
\begin{minipage}[b]{0.5\linewidth} |
| 1329 |
{ |
| 1330 |
\algsetblock[Name]{Start}{Stop}{3}{1cm} |
| 1331 |
\algsetcblock[CName]{Name}{CStart}{CStop}{2}{2cm} |
| 1332 |
\begin{algorithmic}[1] |
| 1333 |
\Start |
| 1334 |
\State 1 |
| 1335 |
\State 2 |
| 1336 |
\State 3 |
| 1337 |
\State 4 |
| 1338 |
\Start |
| 1339 |
\State 1 |
| 1340 |
\Stop |
| 1341 |
\State 2 |
| 1342 |
\Start |
| 1343 |
\State 1 |
| 1344 |
\CStart |
| 1345 |
\State 1 |
| 1346 |
\State 2 |
| 1347 |
\State 3 |
| 1348 |
\Start |
| 1349 |
\State 1 |
| 1350 |
\CStart |
| 1351 |
\State 1 |
| 1352 |
\CStop |
| 1353 |
\Statex |
| 1354 |
\end{algorithmic} |
| 1355 |
} |
| 1356 |
\end{minipage}\bigskip |
| 1357 |
|
| 1358 |
The created environments behave as follows: |
| 1359 |
|
| 1360 |
\begin{itemize} |
| 1361 |
\item It starts with \verb:\Start:. The nested environments are |
| 1362 |
indented by 1 cm. |
| 1363 |
\item If it is followed by at least 3 environments (stataments), then it closes |
| 1364 |
automatically after the third one. |
| 1365 |
\item If you put a \verb:\Stop: before the automatic closure, then this |
| 1366 |
\verb:\Stop: closes the environment. \verb:CStart: closes a block called \verb:Name: |
| 1367 |
and opens a new one called \verb:CName: and having an indentaion of 2 cm. |
| 1368 |
\item \verb:CName: can be closed with \verb:CStop: or it is closed automatically after |
| 1369 |
2 environments. |
| 1370 |
\end{itemize} |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
\subsection{Parameters, custom text}\label{custom text} |
| 1375 |
|
| 1376 |
With \verb:\algrenewtext: you can change the number of parameters, and the text displayed by the |
| 1377 |
commands. With \verb:algnotext: you can makes the vole output line disappear, but |
| 1378 |
it works only for ending commands, for beginning commands you will get an incorrect output. |
| 1379 |
|
| 1380 |
\begin{verbatim} |
| 1381 |
\algrenewcommand[<block>]{<command>} |
| 1382 |
[<paramcount>][<default value>]{<text>} |
| 1383 |
|
| 1384 |
\algnotext[<block>]{<ending command>} |
| 1385 |
\end{verbatim} |
| 1386 |
|
| 1387 |
If \verb:<block>: is missing, then the default text is changed, and if \verb:<block>: is given, |
| 1388 |
then the text displayed at the end of \verb:<block>: is changed. |
| 1389 |
|
| 1390 |
To make a command output the default text at the end of a block (say, you have changed the text |
| 1391 |
for this block), use \verb:\algdefaulttext:. |
| 1392 |
|
| 1393 |
\begin{verbatim} |
| 1394 |
\algdefaulttext[<block>]{<command>} |
| 1395 |
\end{verbatim} |
| 1396 |
|
| 1397 |
If the \verb:<block>: is missing, than the default text itself will be set to the default value |
| 1398 |
(this is \verb:\textbf{<command>}:). |
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
\subsection{The ONE defining macro} |
| 1403 |
|
| 1404 |
All block and loop defining macros call the same macro. You may use this macro to gain a |
| 1405 |
better acces to what will be defined. This macro is \verb:\algdef:. |
| 1406 |
|
| 1407 |
\begin{verbatim} |
| 1408 |
\algdef{<flags>}... |
| 1409 |
\end{verbatim} |
| 1410 |
|
| 1411 |
Depending on the flags the macro can have many forms. |
| 1412 |
|
| 1413 |
\begin{center} |
| 1414 |
\begin{tabular}{|c|l|} |
| 1415 |
\hline |
| 1416 |
\textbf{Flag}&\textbf{Meaning}\\ |
| 1417 |
\hline |
| 1418 |
s&starting command, without text\\ |
| 1419 |
S&starting command with text\\ |
| 1420 |
c&continuing command, without text\\ |
| 1421 |
C&continuing command, with default text\\ |
| 1422 |
xC&continuing command, with block specific text\\ |
| 1423 |
\hline |
| 1424 |
e&ending command, without text\\ |
| 1425 |
E&continuing command, with default text\\ |
| 1426 |
xE&continuing command, with block specific text\\ |
| 1427 |
N&ending command, with default "no text"\\ |
| 1428 |
xN&ending command, with no text for this block\\ |
| 1429 |
\hline |
| 1430 |
b&block(default)\\ |
| 1431 |
l&loop\\ |
| 1432 |
L&loop closes after the given number of stataments\\ |
| 1433 |
\hline |
| 1434 |
i&indentation specified\\ |
| 1435 |
\hline |
| 1436 |
\end{tabular} |
| 1437 |
\end{center} |
| 1438 |
|
| 1439 |
The \verb:<new block>: may be given for any combination of flags, and it is not allowed to have |
| 1440 |
\verb:<new block>: missing and \verb:<start>: missing/empty. |
| 1441 |
For c, C, xC an old block is expected. For s, S, c, C, xC the \verb:<start>: must be given. |
| 1442 |
For e, E, xE, N, xN the \verb:<end>: must be given. For L the \verb:<lifetime>: must be given. |
| 1443 |
For i the \verb:<indent>: must be given. |
| 1444 |
For S, C, xC the starting text and related infos must be given. For E, xE the ending text must be given. |
| 1445 |
For each combination of flags give only the needed parameters, in the following order: |
| 1446 |
|
| 1447 |
\begin{verbatim} |
| 1448 |
\algdef{<flags>}[<new block>]{<old block>}{<start>}{<end>} |
| 1449 |
{<lifetime>}{<indent>} |
| 1450 |
[<startparamcount>][<default value>]{<start text>} |
| 1451 |
[<endparamcount>][<default value>]{<end text>} |
| 1452 |
\end{verbatim} |
| 1453 |
|
| 1454 |
The block and loop defining macros call \verb:\algdef: with the following flags: |
| 1455 |
\begin{center} |
| 1456 |
\begin{tabular}{|l|l|} |
| 1457 |
\hline |
| 1458 |
\textbf{Macro}&\textbf{Meaning}\\ |
| 1459 |
\hline |
| 1460 |
\verb:\algblock:&\verb:\algdef{se}:\\ |
| 1461 |
\hline |
| 1462 |
\verb:\algcblock:&\verb:\algdef{ce}:\\ |
| 1463 |
\hline |
| 1464 |
\verb:\algloop:&\verb:\algdef{sl}:\\ |
| 1465 |
\hline |
| 1466 |
\verb:\algcloop:&\verb:\algdef{cl}:\\ |
| 1467 |
\hline |
| 1468 |
\verb:\algsetblock:&\verb:\algdef{seLi}:\\ |
| 1469 |
\hline |
| 1470 |
\verb:\algsetcblock:&\verb:\algdef{ceLi}:\\ |
| 1471 |
\hline |
| 1472 |
\verb:\algblockx:&\verb:\algdef{SxE}:\\ |
| 1473 |
\hline |
| 1474 |
\verb:\algblockdefx:&\verb:\algdef{SE}:\\ |
| 1475 |
\hline |
| 1476 |
\verb:\algcblockx:&\verb:\algdef{CxE}:\\ |
| 1477 |
\hline |
| 1478 |
\verb:\algcblockdefx:&\verb:\algdef{CE}:\\ |
| 1479 |
\hline |
| 1480 |
\verb:\algsetblockx:&\verb:\algdef{SxELi}:\\ |
| 1481 |
\hline |
| 1482 |
\verb:\algsetblockdefx:&\verb:\algdef{SELi}:\\ |
| 1483 |
\hline |
| 1484 |
\verb:\algsetcblockx:&\verb:\algdef{CxELi}:\\ |
| 1485 |
\hline |
| 1486 |
\verb:\algsetcblockdefx:&\verb:\algdef{CELi}:\\ |
| 1487 |
\hline |
| 1488 |
\verb:\algloopdefx:&\verb:\algdef{Sl}:\\ |
| 1489 |
\hline |
| 1490 |
\verb:\algcloopx:&\verb:\algdef{Cxl}:\\ |
| 1491 |
\hline |
| 1492 |
\verb:\algcloopdefx:&\verb:\algdef{Cl}:\\ |
| 1493 |
\hline |
| 1494 |
\end{tabular} |
| 1495 |
\end{center} |
| 1496 |
|
| 1497 |
|
| 1498 |
\vfill |
| 1499 |
\section{Examples} |
| 1500 |
\subsection{A full example using \textbf{algpseudocode}} |
| 1501 |
|
| 1502 |
|
| 1503 |
\begin{verbatim} |
| 1504 |
\documentclass{article} |
| 1505 |
\usepackage{algorithm} |
| 1506 |
\usepackage{algpseudocode} |
| 1507 |
\begin{document} |
| 1508 |
\begin{algorithm} |
| 1509 |
\caption{The Bellman-Kalaba algorithm} |
| 1510 |
\begin{algorithmic}[1] |
| 1511 |
\Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$} |
| 1512 |
\ForAll {$v \in V(G)$} |
| 1513 |
\State $l(v) \leftarrow \infty$ |
| 1514 |
\EndFor |
| 1515 |
\State $l(u) \leftarrow 0$ |
| 1516 |
\Repeat |
| 1517 |
\For {$i \leftarrow 1, n$} |
| 1518 |
\State $min \leftarrow l(v_i)$ |
| 1519 |
\For {$j \leftarrow 1, n$} |
| 1520 |
\If {$min > e(v_i, v_j) + l(v_j)$} |
| 1521 |
\State $min \leftarrow e(v_i, v_j) + l(v_j)$ |
| 1522 |
\State $p(i) \leftarrow v_j$ |
| 1523 |
\EndIf |
| 1524 |
\EndFor |
| 1525 |
\State $l'(i) \leftarrow min$ |
| 1526 |
\EndFor |
| 1527 |
\State $changed \leftarrow l \not= l'$ |
| 1528 |
\State $l \leftarrow l'$ |
| 1529 |
\Until{$\neg changed$} |
| 1530 |
\EndProcedure |
| 1531 |
\Statex |
| 1532 |
\Procedure {FindPathBK}{$v$, $u$, $p$} |
| 1533 |
\If {$v = u$} |
| 1534 |
\State \textbf{Write} $v$ |
| 1535 |
\Else |
| 1536 |
\State $w \leftarrow v$ |
| 1537 |
\While {$w \not= u$} |
| 1538 |
\State \textbf{Write} $w$ |
| 1539 |
\State $w \leftarrow p(w)$ |
| 1540 |
\EndWhile |
| 1541 |
\EndIf |
| 1542 |
\EndProcedure |
| 1543 |
\end{algorithmic} |
| 1544 |
\end{algorithm} |
| 1545 |
\end{document} |
| 1546 |
\end{verbatim} |
| 1547 |
\eject |
| 1548 |
|
| 1549 |
|
| 1550 |
\alglanguage{pseudocode} |
| 1551 |
\begin{algorithm}[h] |
| 1552 |
\caption{The Bellman-Kalaba algorithm} |
| 1553 |
\begin{algorithmic}[1] |
| 1554 |
\Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$} |
| 1555 |
\ForAll {$v \in V(G)$} |
| 1556 |
\State $l(v) \leftarrow \infty$ |
| 1557 |
\EndFor |
| 1558 |
\State $l(u) \leftarrow 0$ |
| 1559 |
\Repeat |
| 1560 |
\For {$i \leftarrow 1, n$} |
| 1561 |
\State $min \leftarrow l(v_i)$ |
| 1562 |
\For {$j \leftarrow 1, n$} |
| 1563 |
\If {$min > e(v_i, v_j) + l(v_j)$} |
| 1564 |
\State $min \leftarrow e(v_i, v_j) + l(v_j)$ |
| 1565 |
\State $p(i) \leftarrow v_j$ |
| 1566 |
\EndIf |
| 1567 |
\EndFor |
| 1568 |
\State $l'(i) \leftarrow min$ |
| 1569 |
\EndFor |
| 1570 |
\State $changed \leftarrow l \not= l'$ |
| 1571 |
\State $l \leftarrow l'$ |
| 1572 |
\Until{$\neg changed$} |
| 1573 |
\EndProcedure |
| 1574 |
\Statex |
| 1575 |
\Procedure {FindPathBK}{$v$, $u$, $p$} |
| 1576 |
\If {$v = u$} |
| 1577 |
\State \textbf{Write} $v$ |
| 1578 |
\Else |
| 1579 |
\State $w \leftarrow v$ |
| 1580 |
\While {$w \not= u$} |
| 1581 |
\State \textbf{Write} $w$ |
| 1582 |
\State $w \leftarrow p(w)$ |
| 1583 |
\EndWhile |
| 1584 |
\EndIf |
| 1585 |
\EndProcedure |
| 1586 |
\end{algorithmic} |
| 1587 |
\end{algorithm} |
| 1588 |
\eject |
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
\subsection{Breaking up an algorithm} |
| 1594 |
|
| 1595 |
|
| 1596 |
\begin{verbatim} |
| 1597 |
\documentclass{article} |
| 1598 |
\usepackage{algorithm} |
| 1599 |
\usepackage{algpseudocode} |
| 1600 |
\begin{document} |
| 1601 |
\begin{algorithm} |
| 1602 |
\caption{Part 1} |
| 1603 |
\begin{algorithmic}[1] |
| 1604 |
\Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$} |
| 1605 |
\ForAll {$v \in V(G)$} |
| 1606 |
\State $l(v) \leftarrow \infty$ |
| 1607 |
\EndFor |
| 1608 |
\State $l(u) \leftarrow 0$ |
| 1609 |
\Repeat |
| 1610 |
\For {$i \leftarrow 1, n$} |
| 1611 |
\State $min \leftarrow l(v_i)$ |
| 1612 |
\For {$j \leftarrow 1, n$} |
| 1613 |
\If {$min > e(v_i, v_j) + l(v_j)$} |
| 1614 |
\State $min \leftarrow e(v_i, v_j) + l(v_j)$ |
| 1615 |
\State \Comment For some reason we need to break here! |
| 1616 |
\algstore{bkbreak} |
| 1617 |
\end{algorithmic} |
| 1618 |
\end{algorithm} |
| 1619 |
|
| 1620 |
And we need to put some additional text between\dots |
| 1621 |
|
| 1622 |
\begin{algorithm}[h] |
| 1623 |
\caption{Part 2} |
| 1624 |
\begin{algorithmic}[1] |
| 1625 |
\algrestore{bkbreak} |
| 1626 |
\State $p(i) \leftarrow v_j$ |
| 1627 |
\EndIf |
| 1628 |
\EndFor |
| 1629 |
\State $l'(i) \leftarrow min$ |
| 1630 |
\EndFor |
| 1631 |
\State $changed \leftarrow l \not= l'$ |
| 1632 |
\State $l \leftarrow l'$ |
| 1633 |
\Until{$\neg changed$} |
| 1634 |
\EndProcedure |
| 1635 |
\end{algorithmic} |
| 1636 |
\end{algorithm} |
| 1637 |
\end{document} |
| 1638 |
\end{verbatim} |
| 1639 |
\eject |
| 1640 |
|
| 1641 |
|
| 1642 |
\alglanguage{pseudocode} |
| 1643 |
\begin{algorithm}[h] |
| 1644 |
\caption{Part 1} |
| 1645 |
\begin{algorithmic}[1] |
| 1646 |
\Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$} |
| 1647 |
\ForAll {$v \in V(G)$} |
| 1648 |
\State $l(v) \leftarrow \infty$ |
| 1649 |
\EndFor |
| 1650 |
\State $l(u) \leftarrow 0$ |
| 1651 |
\Repeat |
| 1652 |
\For {$i \leftarrow 1, n$} |
| 1653 |
\State $min \leftarrow l(v_i)$ |
| 1654 |
\For {$j \leftarrow 1, n$} |
| 1655 |
\If {$min > e(v_i, v_j) + l(v_j)$} |
| 1656 |
\State $min \leftarrow e(v_i, v_j) + l(v_j)$ |
| 1657 |
\State \Comment For some reason we need to break here! |
| 1658 |
\algstore{bkbreak} |
| 1659 |
\end{algorithmic} |
| 1660 |
\end{algorithm} |
| 1661 |
|
| 1662 |
And we need to put some additional text between\dots |
| 1663 |
|
| 1664 |
\begin{algorithm}[h] |
| 1665 |
\caption{Part 2} |
| 1666 |
\begin{algorithmic}[1] |
| 1667 |
\algrestore{bkbreak} |
| 1668 |
\State $p(i) \leftarrow v_j$ |
| 1669 |
\EndIf |
| 1670 |
\EndFor |
| 1671 |
\State $l'(i) \leftarrow min$ |
| 1672 |
\EndFor |
| 1673 |
\State $changed \leftarrow l \not= l'$ |
| 1674 |
\State $l \leftarrow l'$ |
| 1675 |
\Until{$\neg changed$} |
| 1676 |
\EndProcedure |
| 1677 |
\end{algorithmic} |
| 1678 |
\end{algorithm} |
| 1679 |
\eject |
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
\subsection{Using multiple layouts} |
| 1685 |
|
| 1686 |
|
| 1687 |
\begin{verbatim} |
| 1688 |
\documentclass{article} |
| 1689 |
\usepackage{algorithm} |
| 1690 |
\usepackage{algpseudocode} |
| 1691 |
\usepackage{algpascal} |
| 1692 |
\begin{document} |
| 1693 |
|
| 1694 |
\alglanguage{pseudocode} |
| 1695 |
\begin{algorithm} |
| 1696 |
\caption{A small pseudocode} |
| 1697 |
\begin{algorithmic}[1] |
| 1698 |
\State $s \gets 0$ |
| 1699 |
\State $p \gets 0$ |
| 1700 |
\For{$i \gets 1,\, 10$} |
| 1701 |
\State $s \gets s + i$ |
| 1702 |
\State $p \gets p + s$ |
| 1703 |
\EndFor |
| 1704 |
\end{algorithmic} |
| 1705 |
\end{algorithm} |
| 1706 |
|
| 1707 |
\alglanguage{pascal} |
| 1708 |
\begin{algorithm} |
| 1709 |
\caption{The pascal version} |
| 1710 |
\begin{algorithmic}[1] |
| 1711 |
\State $s := 0$ |
| 1712 |
\State $p := 0$ |
| 1713 |
\For{i = 1}{10} |
| 1714 |
\Begin |
| 1715 |
\State $s := s + i$ |
| 1716 |
\State $p := p + s$ |
| 1717 |
\End |
| 1718 |
\end{algorithmic} |
| 1719 |
\end{algorithm} |
| 1720 |
|
| 1721 |
\end{document} |
| 1722 |
\end{verbatim} |
| 1723 |
\eject |
| 1724 |
|
| 1725 |
|
| 1726 |
\alglanguage{pseudocode} |
| 1727 |
\begin{algorithm} |
| 1728 |
\caption{A small pseudocode} |
| 1729 |
\begin{algorithmic}[1] |
| 1730 |
\State $s \gets 0$ |
| 1731 |
\State $p \gets 0$ |
| 1732 |
\For{$i \gets 1,\, 10$} |
| 1733 |
\State $s \gets s + i$ |
| 1734 |
\State $p \gets p + s$ |
| 1735 |
\EndFor |
| 1736 |
\end{algorithmic} |
| 1737 |
\end{algorithm} |
| 1738 |
|
| 1739 |
\alglanguage{pascal} |
| 1740 |
\begin{algorithm} |
| 1741 |
\caption{The pascal version} |
| 1742 |
\begin{algorithmic}[1] |
| 1743 |
\State $s := 0$ |
| 1744 |
\State $p := 0$ |
| 1745 |
\For{i = 1}{10} |
| 1746 |
\Begin |
| 1747 |
\State $s := s + i$ |
| 1748 |
\State $p := p + s$ |
| 1749 |
\End |
| 1750 |
\end{algorithmic} |
| 1751 |
\end{algorithm} |
| 1752 |
\eject |
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
\section{Bugs} |
| 1757 |
|
| 1758 |
If you have a question or find a bug you can contact me on: |
| 1759 |
|
| 1760 |
\medskip |
| 1761 |
\textbf{[email protected]} |
| 1762 |
|
| 1763 |
\medskip |
| 1764 |
\noindent If possible, please create a small \LaTeX{} example related to your problem. |
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
\end{document} |