| 1 |
\documentclass{article} |
| 2 |
\textwidth=4.5in |
| 3 |
\textheight=7.125in |
| 4 |
\usepackage{pseudocode} |
| 5 |
% |
| 6 |
% |
| 7 |
% September 2, 1999 |
| 8 |
% |
| 9 |
% |
| 10 |
\title{Pseudocode: A \LaTeX\ Style File for Displaying Algorithms} |
| 11 |
\author{ |
| 12 |
D.L.\ Kreher\\ |
| 13 |
Department of Mathematical Sciences\\ |
| 14 |
Michigan Technological University \\ |
| 15 |
Houghton, MI 49931\\ |
| 16 |
{\tt [email protected]}\\[1ex] |
| 17 |
and \\[1ex] |
| 18 |
D.R.\ Stinson\\ |
| 19 |
Department of Combinatorics and Optimization\\ |
| 20 |
University of Waterloo\\ |
| 21 |
Waterloo ON, N2L 3G1\\ |
| 22 |
{\tt [email protected]} |
| 23 |
} |
| 24 |
|
| 25 |
\newcommand{\keyword}[1]{\texttt{#1}} |
| 26 |
\begin{document} |
| 27 |
%\makeatletter |
| 28 |
%\setlength{\pcode@width}{2.75in} |
| 29 |
%\addtolength{\pcode@width}{-40pt} |
| 30 |
%\makeatother |
| 31 |
|
| 32 |
\maketitle |
| 33 |
|
| 34 |
\section{Introduction} |
| 35 |
|
| 36 |
This paper describes a \LaTeX\ environment named \keyword{pseudocode} that can be used |
| 37 |
for describing algorithms in pseudocode form. This is the style used |
| 38 |
in our %recently published |
| 39 |
textbook {\em Combinatorial Algorithms: Generation, Enumeration and Search} |
| 40 |
\cite{cages}. The style file \keyword{pseudocode.sty} is available for free |
| 41 |
downloading from the web page |
| 42 |
\verb+http://www.math.mtu.edu/~kreher/cages.html+ |
| 43 |
|
| 44 |
This package is quite easy to use, and allows algorithms to be described |
| 45 |
in a \LaTeX\ document using a natural Pascal-like syntax. In the remaining sections |
| 46 |
of this note, we describe how to use the \keyword{pseudocode} environment |
| 47 |
and we present some examples. Readers familiar with \LaTeX\ |
| 48 |
(see \cite{lamport}) should be |
| 49 |
able to easily customize the style file to include additional desired features. |
| 50 |
|
| 51 |
The \keyword{pseudocode} environment requires the \keyword{fancybox} |
| 52 |
package by Timothy Van Zandt. This package is described in |
| 53 |
Section 10.1.3 of \cite{LC}. |
| 54 |
Other environments for describing algorithms include |
| 55 |
\keyword{alg}, \keyword{algorithmic}, \keyword{newalg} and \keyword{program}. |
| 56 |
These style files, as well as \keyword{fancybox}, are all available from the CTAN web site |
| 57 |
\verb+http://www.ctan.org/+ |
| 58 |
|
| 59 |
\section{The \keyword{pseudocode} Environment} |
| 60 |
|
| 61 |
Within the \keyword{pseudocode} environment, a number of commands for |
| 62 |
popular algorithmic constructs are available. |
| 63 |
In general, the commands provided can be nested to |
| 64 |
describe quite complex algorithms. |
| 65 |
|
| 66 |
The \keyword{pseudocode} environment is invoked |
| 67 |
as follows: |
| 68 |
\begin{verbatim} |
| 69 |
\begin{pseudocode}{<Name>}{<Parameters>} |
| 70 |
pseudocode constructs |
| 71 |
\end{pseudocode} |
| 72 |
\end{verbatim} |
| 73 |
|
| 74 |
\noindent{}The argument \verb+<Name>+ is the name of the algorithm, and |
| 75 |
\verb+<Parameters>+ is a list of parameters for the algorithm. |
| 76 |
For example, the commands |
| 77 |
|
| 78 |
\begin{verbatim} |
| 79 |
\begin{pseudocode}{CelsiusToFahrenheit}{c} |
| 80 |
f \GETS {9c/5} + 32\\ |
| 81 |
\RETURN{f} |
| 82 |
\end{pseudocode} |
| 83 |
\end{verbatim} |
| 84 |
|
| 85 |
\noindent{}produce the following output when included in |
| 86 |
a \LaTeX\ document: |
| 87 |
|
| 88 |
\begin{pseudocode}{CelsiusToFahrenheit}{c} |
| 89 |
f \GETS {9c/5} + 32\\ |
| 90 |
\RETURN{f} |
| 91 |
\end{pseudocode} |
| 92 |
|
| 93 |
Notice that the command \verb+\GETS+ produces a |
| 94 |
left arrow, which we use to indicate an assignment of |
| 95 |
a variable. The user could use instead some other symbol, if desired. |
| 96 |
For example, \verb+\GETS+ could be replaced by \verb+=+, |
| 97 |
as is done in the ``C'' programming language. |
| 98 |
|
| 99 |
\subsection{ The {\em begin-end} Construct} |
| 100 |
|
| 101 |
To form compound statements from simple statements, the {\em begin-end} |
| 102 |
construct is used as follows: |
| 103 |
\begin{verbatim} |
| 104 |
\BEGIN |
| 105 |
some statement\\ |
| 106 |
another statement\\ |
| 107 |
yet another statement |
| 108 |
\END |
| 109 |
\end{verbatim} |
| 110 |
This generates the following: |
| 111 |
|
| 112 |
\medskip |
| 113 |
\begin{pseudocode}[display]{}{} |
| 114 |
\BEGIN |
| 115 |
\mbox{some statement}\\ |
| 116 |
\mbox{another statement}\\ |
| 117 |
\mbox{yet another statement} |
| 118 |
\END |
| 119 |
\end{pseudocode} |
| 120 |
|
| 121 |
\noindent{}The effect of this construct is to group |
| 122 |
a collection of statements |
| 123 |
using a left brace bracket of the appropriate size. |
| 124 |
|
| 125 |
In the sections that follow we will use the notation |
| 126 |
\verb+<stmt>+ |
| 127 |
to indicate a simple statement or a compound statement. |
| 128 |
Note that the contents of statements are typeset in math mode. |
| 129 |
Therefore, non-math mode text must be enclosed in an |
| 130 |
\verb+\mbox{}+. |
| 131 |
|
| 132 |
Observe that the double backslash \verb+\\+ plays the same |
| 133 |
role as the semicolon in Pascal, i.e., it is used to |
| 134 |
separate statements, and should never appear before |
| 135 |
\verb+\END+. |
| 136 |
|
| 137 |
\subsection{The {\em if-then-else} Construct} |
| 138 |
|
| 139 |
The {\em if-then-else} construct takes various forms, |
| 140 |
such as the following: |
| 141 |
\begin{verbatim} |
| 142 |
\IF <condition> \THEN <stmt> |
| 143 |
\IF <condition> \THEN <stmt> \ELSE <stmt> |
| 144 |
\IF <condition> \THEN <stmt> \ELSEIF <stmt> \THEN <stmt> |
| 145 |
\end{verbatim} |
| 146 |
Note that there is no limit placed on the number |
| 147 |
of \verb+\ELSEIF+s that may be used in an {\em if-then-else} construct. |
| 148 |
For example, the commands: |
| 149 |
\begin{verbatim} |
| 150 |
\IF some condition is true |
| 151 |
\THEN |
| 152 |
\BEGIN |
| 153 |
some statement\\ |
| 154 |
another statement\\ |
| 155 |
yet another statement |
| 156 |
\END |
| 157 |
\ELSEIF some other condition is true |
| 158 |
\THEN |
| 159 |
\BEGIN |
| 160 |
some statement\\ |
| 161 |
another statement\\ |
| 162 |
yet another statement |
| 163 |
\END |
| 164 |
\ELSEIF some even more bizarre condition is met |
| 165 |
\THEN |
| 166 |
do something else |
| 167 |
\ELSE |
| 168 |
do the default actions |
| 169 |
\end{verbatim} |
| 170 |
|
| 171 |
\noindent{}would produce the following output: |
| 172 |
|
| 173 |
\medskip |
| 174 |
\begin{pseudocode}[display]{}{} |
| 175 |
\IF \mbox{some condition is true} |
| 176 |
\THEN |
| 177 |
\BEGIN |
| 178 |
\mbox{some statement}\\ |
| 179 |
\mbox{another statement}\\ |
| 180 |
\mbox{yet another statement} |
| 181 |
\END |
| 182 |
\ELSEIF \mbox{some other condition is true} |
| 183 |
\THEN |
| 184 |
\BEGIN |
| 185 |
\mbox{some statement}\\ |
| 186 |
\mbox{another statement}\\ |
| 187 |
\mbox{yet another statement} |
| 188 |
\END |
| 189 |
\ELSEIF\mbox{some even more bizarre condition is met} |
| 190 |
\THEN \mbox{do something else} |
| 191 |
\ELSE |
| 192 |
\mbox{do the default actions} |
| 193 |
\end{pseudocode} |
| 194 |
|
| 195 |
\subsection{The {\em for} Loop} |
| 196 |
|
| 197 |
The {\em for} loop takes the following forms: |
| 198 |
\begin{verbatim} |
| 199 |
\FOR <var> \GETS <lower> \TO <upper> \DO <stmt> |
| 200 |
\FOR <var> \GETS <upper> \DOWNTO <lower> \DO <stmt> |
| 201 |
\FOREACH <condition> \DO <stmt> |
| 202 |
\end{verbatim} |
| 203 |
For example, |
| 204 |
\begin{verbatim} |
| 205 |
\FOR i \GETS 0 \TO 10 \DO |
| 206 |
some processing |
| 207 |
\end{verbatim} |
| 208 |
produces |
| 209 |
|
| 210 |
\medskip |
| 211 |
\begin{pseudocode}[display]{}{} |
| 212 |
\FOR i \GETS 0 \TO 10 \DO |
| 213 |
\mbox{some processing} |
| 214 |
\end{pseudocode} |
| 215 |
|
| 216 |
\noindent{}and |
| 217 |
|
| 218 |
\begin{verbatim} |
| 219 |
\FOREACH x \in \mathcal{S} \DO |
| 220 |
some processing |
| 221 |
\end{verbatim} |
| 222 |
|
| 223 |
\noindent{produces} |
| 224 |
|
| 225 |
\medskip |
| 226 |
\begin{pseudocode}[display]{}{} |
| 227 |
\FOREACH x \in \mathcal{S} \DO |
| 228 |
\mbox{some processing} |
| 229 |
\end{pseudocode} |
| 230 |
|
| 231 |
\subsection{The {\em while} Loop} |
| 232 |
|
| 233 |
The {\em while} loop takes the following form: |
| 234 |
|
| 235 |
\begin{verbatim} |
| 236 |
\WHILE <condition> \DO <stmt> |
| 237 |
\end{verbatim} |
| 238 |
|
| 239 |
\noindent{}For example, |
| 240 |
|
| 241 |
\begin{verbatim} |
| 242 |
\WHILE some condition holds \DO |
| 243 |
some processing |
| 244 |
\end{verbatim} |
| 245 |
|
| 246 |
\noindent{}produces |
| 247 |
|
| 248 |
\medskip |
| 249 |
\begin{pseudocode}[display]{}{} |
| 250 |
\WHILE \mbox{some condition holds} \DO |
| 251 |
\mbox{some processing } |
| 252 |
\end{pseudocode} |
| 253 |
|
| 254 |
\subsection{The {\em repeat-until} Loop} |
| 255 |
|
| 256 |
The {\em repeat-until} loop takes the following form: |
| 257 |
|
| 258 |
\begin{verbatim} |
| 259 |
\REPEAT <stmt> \UNTIL <condition> |
| 260 |
\end{verbatim} |
| 261 |
|
| 262 |
\noindent{}For example, |
| 263 |
|
| 264 |
\begin{verbatim} |
| 265 |
\REPEAT |
| 266 |
some processing |
| 267 |
\UNTIL some condition is met |
| 268 |
\end{verbatim} |
| 269 |
|
| 270 |
\noindent{}produces |
| 271 |
|
| 272 |
\medskip |
| 273 |
\begin{pseudocode}[display]{}{} |
| 274 |
\REPEAT |
| 275 |
\mbox{some processing} |
| 276 |
\UNTIL \mbox{some condition is met} |
| 277 |
\end{pseudocode} |
| 278 |
|
| 279 |
\subsection{Main Programs and Procedures} |
| 280 |
|
| 281 |
We can describe a main program that calls one (or more) procedures as follows: |
| 282 |
|
| 283 |
\begin{verbatim} |
| 284 |
\begin{pseudocode}{<Name>}{<Parameters>} |
| 285 |
\PROCEDURE{<ProcedureName>}{<ProcedureParameters>} |
| 286 |
some stuff |
| 287 |
\ENDPROCEDURE |
| 288 |
\MAIN |
| 289 |
some stuff\\ |
| 290 |
\CALL{<ProcedureName>}{<ActualParameters}>\\ |
| 291 |
more stuff |
| 292 |
\ENDMAIN |
| 293 |
\end{pseudocode} |
| 294 |
\end{verbatim} |
| 295 |
|
| 296 |
Here is a simple example to illustrate the |
| 297 |
use of a main program calling a procedure. |
| 298 |
The commands |
| 299 |
|
| 300 |
\begin{verbatim} |
| 301 |
\begin{pseudocode}{TemperatureTable}{lower, upper} |
| 302 |
\PROCEDURE{CelsiusToFahrenheit}{c} |
| 303 |
f \GETS {9c/5} + 32\\ |
| 304 |
\RETURN{f} |
| 305 |
\ENDPROCEDURE |
| 306 |
\MAIN |
| 307 |
x \GETS lower \\ |
| 308 |
\WHILE x \leq upper \DO |
| 309 |
\BEGIN |
| 310 |
\OUTPUT{x, \CALL{CelsiusToFahrenheit}{x}}\\ |
| 311 |
x \GETS x+1 |
| 312 |
\END |
| 313 |
\ENDMAIN |
| 314 |
\end{pseudocode} |
| 315 |
\end{verbatim} |
| 316 |
|
| 317 |
\noindent{}produce the following output: |
| 318 |
|
| 319 |
\begin{pseudocode}{TemperatureTable}{lower, upper} |
| 320 |
\PROCEDURE{CelsiusToFahrenheit}{c} |
| 321 |
f \GETS {9c/5} + 32\\ |
| 322 |
\RETURN{f} |
| 323 |
\ENDPROCEDURE |
| 324 |
|
| 325 |
\MAIN |
| 326 |
x \GETS lower \\ |
| 327 |
\WHILE x \leq upper \DO |
| 328 |
\BEGIN |
| 329 |
\OUTPUT{x, \CALL{CelsiusToFahrenheit}{x}}\\ |
| 330 |
x \GETS x+1 |
| 331 |
\END |
| 332 |
\ENDMAIN |
| 333 |
\end{pseudocode} |
| 334 |
|
| 335 |
|
| 336 |
\subsection{Comments} |
| 337 |
|
| 338 |
A comment statement may be inserted in an algorithm using the |
| 339 |
following command: |
| 340 |
\begin{verbatim} |
| 341 |
\COMMENT{<stmt>} |
| 342 |
\end{verbatim} |
| 343 |
For example, the commands |
| 344 |
\begin{verbatim} |
| 345 |
A \GETS B\\ |
| 346 |
\COMMENT{Now increment the value of $A$}\\ |
| 347 |
A \GETS A+1 |
| 348 |
\end{verbatim} |
| 349 |
|
| 350 |
\noindent{}produce the output |
| 351 |
|
| 352 |
\medskip |
| 353 |
\begin{pseudocode}[display]{}{} |
| 354 |
A \GETS B\\ |
| 355 |
\COMMENT{Now increment the value of $A$}\\ |
| 356 |
A \GETS A+1 |
| 357 |
\end{pseudocode} |
| 358 |
|
| 359 |
\medskip |
| 360 |
|
| 361 |
Note that comments are assumed to be text. Thus, in order to include |
| 362 |
mathematical expressions in a comment, math mode must be used explicitly. |
| 363 |
|
| 364 |
\subsection{Other Predefined Keywords} |
| 365 |
|
| 366 |
Several other predefined keywords are available. |
| 367 |
We summarize their usage in Table \ref{other predefined keywords}. |
| 368 |
|
| 369 |
\begin{table}[h] |
| 370 |
\caption{Other Predefined Keywords} |
| 371 |
\label{other predefined keywords} |
| 372 |
\begin{center} |
| 373 |
\begin{tabular}{@{}ll@{}} |
| 374 |
\hline |
| 375 |
command & output\\ |
| 376 |
\hline |
| 377 |
\verb+\LOCAL{list of variables}+& |
| 378 |
$\LOCAL{\mbox{list of variables}}$\\ |
| 379 |
\verb+\GLOBAL{list of variables}+& |
| 380 |
$\GLOBAL{\mbox{list of variables}}$\\ |
| 381 |
\verb+\EXTERNAL{list of procedures}+& |
| 382 |
$\EXTERNAL{\mbox{list of procedures}}$\\ |
| 383 |
\verb+\RETURN{list of values}+& |
| 384 |
$\RETURN{\mbox{list of values}}$\\ |
| 385 |
\verb+\OUTPUT{list of values}+& |
| 386 |
$\OUTPUT{\mbox{list of values}}$\\ |
| 387 |
\verb+\EXIT+& |
| 388 |
$\EXIT$\\ |
| 389 |
\verb+\AND+& |
| 390 |
$\AND$\\ |
| 391 |
\verb+\OR+& |
| 392 |
$\OR$\\ |
| 393 |
\verb+\NOT+& |
| 394 |
$\NOT$\\ |
| 395 |
\verb+\TRUE+& |
| 396 |
$\TRUE$\\ |
| 397 |
\verb+\FALSE+& |
| 398 |
$\FALSE$\\ |
| 399 |
\verb+\GETS+& |
| 400 |
$\GETS$\\ |
| 401 |
\hline |
| 402 |
\end{tabular} |
| 403 |
\end{center} |
| 404 |
\end{table} |
| 405 |
|
| 406 |
Also note |
| 407 |
that all of the keywords |
| 408 |
\verb+\IF+, \verb+\WHILE+, \verb+\CALL{}{}+, \verb+\NOT+, etc.\ |
| 409 |
are available for |
| 410 |
use outside of the \keyword{pseudocode} environment, but they must |
| 411 |
be input in math mode. For example, |
| 412 |
\begin{center} |
| 413 |
\verb+The $\WHILE$ loop is our friend.+ |
| 414 |
\end{center} |
| 415 |
generates |
| 416 |
\begin{center} |
| 417 |
The $\WHILE$ loop is our friend. |
| 418 |
\end{center} |
| 419 |
|
| 420 |
|
| 421 |
\subsection{Statement Numbering} |
| 422 |
|
| 423 |
Statements can be numbered and given a reference key so that |
| 424 |
they can be referenced in a \LaTeX\ document using a \verb+\ref{}+ command |
| 425 |
(see section 4.2 of \cite{lamport}). |
| 426 |
This is done as follows: |
| 427 |
|
| 428 |
\begin{verbatim} |
| 429 |
\STMTNUM{<space>}{<key>} |
| 430 |
\end{verbatim} |
| 431 |
|
| 432 |
\noindent{}The argument \verb+<space>+ is the amount of space to be left between |
| 433 |
the text and the statement number. This is a length that is specified by the |
| 434 |
user, and generally will require some experimentation in order for it to look nice. |
| 435 |
The argument \verb+<key>+ is the reference key |
| 436 |
used in the LaTeX\ \verb+\ref{}+ command to refer to the given |
| 437 |
statement. |
| 438 |
|
| 439 |
The default numbering for statements is arabic. However, it can be changed |
| 440 |
by a suitable \verb+\renewcommand{}+. |
| 441 |
An example is provided in the next section. |
| 442 |
|
| 443 |
\section{An example} |
| 444 |
|
| 445 |
The following example demonstrates the use of the |
| 446 |
\keyword{pseudocode} environment to describe a complete |
| 447 |
algorithm, the familiar ``mergesort'' algorithm. The \LaTeX\ input |
| 448 |
|
| 449 |
\begin{verbatim} |
| 450 |
\renewcommand{\thepseudonum}{\roman{pseudonum}} |
| 451 |
\begin{pseudocode}{MergeSort}{n,X} |
| 452 |
\label{MergeSort} |
| 453 |
\COMMENT{Sort the array $X$ of length $n$}\\ |
| 454 |
\IF n=2 \THEN |
| 455 |
\BEGIN |
| 456 |
\IF X[0]>X[1] \THEN |
| 457 |
\BEGIN |
| 458 |
T \GETS X[0]\\ |
| 459 |
X[0]\GETS X[1]\\ |
| 460 |
X[1]\GETS T |
| 461 |
\END |
| 462 |
\END |
| 463 |
\ELSEIF n>2 \THEN |
| 464 |
\BEGIN |
| 465 |
m\GETS \lfloor n/2 \rfloor\\ |
| 466 |
\FOR i\GETS 0 \TO m-1 \DO A[i] \GETS X[i]\\ |
| 467 |
\FOR i\GETS m \TO n-1 \DO B[i] \GETS X[i]\\ |
| 468 |
\COMMENT{Now sort the subarrays $A$ and $B$}\\ |
| 469 |
\CALL{MergeSort}{m,A}\\ |
| 470 |
\CALL{MergeSort}{n-m,B}\\ |
| 471 |
i\GETS 0\\ |
| 472 |
j\GETS 0\\ |
| 473 |
\FOR k \GETS 0 \TO n-1 \DO |
| 474 |
\BEGIN |
| 475 |
\IF A[i] \leq B[j] \THEN |
| 476 |
\BEGIN |
| 477 |
X[k]\GETS A[i] \STMTNUM{1in}{st.1}\\ |
| 478 |
i\GETS i+1 |
| 479 |
\END |
| 480 |
\ELSE |
| 481 |
\BEGIN |
| 482 |
X[k]\GETS B[j] \STMTNUM{1.03in}{st.2}\\ |
| 483 |
j\GETS j+1 |
| 484 |
\END |
| 485 |
\END |
| 486 |
\END |
| 487 |
\end{pseudocode} |
| 488 |
\end{verbatim} |
| 489 |
|
| 490 |
\noindent{}produces the following output: |
| 491 |
|
| 492 |
\renewcommand{\thepseudonum}{\roman{pseudonum}} |
| 493 |
\begin{pseudocode}{MergeSort}{n,X} |
| 494 |
\label{MergeSort} |
| 495 |
\COMMENT{Sort the array $X$ of length $n$}\\ |
| 496 |
\IF n=2 \THEN |
| 497 |
\BEGIN |
| 498 |
\IF X[0]>X[1] \THEN |
| 499 |
\BEGIN |
| 500 |
T \GETS X[0]\\ |
| 501 |
X[0]\GETS X[1]\\ |
| 502 |
X[1]\GETS T |
| 503 |
\END |
| 504 |
\END |
| 505 |
\ELSEIF n>2 \THEN |
| 506 |
\BEGIN |
| 507 |
m\GETS \lfloor n/2 \rfloor\\ |
| 508 |
\FOR i\GETS 0 \TO m-1 \DO A[i] \GETS X[i]\\ |
| 509 |
\FOR i\GETS m \TO n-1 \DO B[i] \GETS X[i]\\ |
| 510 |
\COMMENT{Now sort the subarrays $A$ and $B$}\\ |
| 511 |
\CALL{MergeSort}{m,A}\\ |
| 512 |
\CALL{MergeSort}{n-m,B}\\ |
| 513 |
i\GETS 0\\ |
| 514 |
j\GETS 0\\ |
| 515 |
\FOR k \GETS 0 \TO n-1 \DO |
| 516 |
\BEGIN |
| 517 |
\IF A[i] \leq B[j] \THEN |
| 518 |
\BEGIN |
| 519 |
X[k]\GETS A[i] \STMTNUM{.9in}{st.1}\\ |
| 520 |
i\GETS i+1 |
| 521 |
\END |
| 522 |
\ELSE |
| 523 |
\BEGIN |
| 524 |
X[k]\GETS B[j] \STMTNUM{.93in}{st.2}\\ |
| 525 |
j\GETS j+1 |
| 526 |
\END |
| 527 |
\END |
| 528 |
\END |
| 529 |
\end{pseudocode} |
| 530 |
|
| 531 |
The counter \verb+pseudonum+ keeps track of the statement numbers. |
| 532 |
The style of the counter values can be changed using the method |
| 533 |
described in Section 6.3 of \cite{lamport}. For example, |
| 534 |
we used the command |
| 535 |
\begin{center} |
| 536 |
\verb+ \renewcommand{\thepseudonum}{\roman{pseudonum}} + |
| 537 |
\end{center} |
| 538 |
in our example so that statements were numbered with |
| 539 |
lowercase Roman numerals. |
| 540 |
We also assigned a label to the algorithm using the |
| 541 |
\verb+\label{}+ command that is described in Section 4.2 of \cite{lamport}. |
| 542 |
Finally, by trial and error, we determined |
| 543 |
spacing so that the statement numbers would be vertically aligned. |
| 544 |
|
| 545 |
We now give an example of how the numbered statements in the above |
| 546 |
algorithm can be referenced in a \LaTeX document. The commands |
| 547 |
|
| 548 |
\begin{verbatim} |
| 549 |
On lines (\ref{st.1}) and (\ref{st.2}) of Algorithm |
| 550 |
\ref{MergeSort}, we determine the $k$th element of the |
| 551 |
sorted array. |
| 552 |
\end{verbatim} |
| 553 |
|
| 554 |
\noindent{}produce the following output: |
| 555 |
|
| 556 |
\medskip |
| 557 |
|
| 558 |
\noindent{}On lines (\ref{st.1}) and (\ref{st.2}) of Algorithm \ref{MergeSort}, |
| 559 |
we determine the $k$th element of the |
| 560 |
sorted array. |
| 561 |
|
| 562 |
%\medskip |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
\section{Framing} |
| 567 |
The \keyword{pseudocode} environment also has an optional parameter, |
| 568 |
\verb+<frame>+. The complete form of the \keyword{pseudocode} environment is |
| 569 |
|
| 570 |
\begin{verbatim} |
| 571 |
\begin{pseudocode}[<frame>]{<Name>}{<Parameters>} |
| 572 |
pseudocode constructs |
| 573 |
\end{pseudocode} |
| 574 |
\end{verbatim} |
| 575 |
|
| 576 |
The possible values of \verb+<frame>+ are: |
| 577 |
\begin{center} |
| 578 |
\begin{tabular}{llll} |
| 579 |
\verb+shadowbox+ & |
| 580 |
\verb+doublebox+ & |
| 581 |
\verb+ovalbox+ & |
| 582 |
\verb+Ovalbox+ \\ |
| 583 |
\verb+framebox+ & |
| 584 |
\verb+plain+ & |
| 585 |
\verb+ruled+ & |
| 586 |
\verb+display+ |
| 587 |
\end{tabular} |
| 588 |
\end{center} |
| 589 |
The values ending with ``box'' draw various types of frames around the algorithm. |
| 590 |
The value \verb+plain+ is the default and adds no frame to the algorithm. |
| 591 |
The value \verb+display+ is used for displaying sections of code |
| 592 |
without the algorithm name or parameters. Here are some examples with |
| 593 |
input: |
| 594 |
|
| 595 |
\begin{verbatim} |
| 596 |
\begin{pseudocode}[<frame>]{SquareAndMultiply}{x,b,n} |
| 597 |
\COMMENT{ Compute $x^b \pmod{n}$}\\ |
| 598 |
z\GETS 1\\ |
| 599 |
\WHILE b > 0 \DO |
| 600 |
\BEGIN |
| 601 |
z \GETS z^2 \pmod{n} \\ |
| 602 |
\IF b\mbox{ is odd} |
| 603 |
\THEN z \GETS z \cdot x \pmod{n} \\ |
| 604 |
b \GETS \CALL{ShiftRight}{b} |
| 605 |
\END\\ |
| 606 |
\RETURN{z} |
| 607 |
\end{pseudocode} |
| 608 |
\end{verbatim} |
| 609 |
|
| 610 |
\noindent{}where we give \verb+<frame>+ each of the values described above. |
| 611 |
|
| 612 |
\medskip |
| 613 |
|
| 614 |
\noindent When \verb+<frame>+ is \verb+shadowbox+ we obtain: |
| 615 |
|
| 616 |
\begin{pseudocode}[shadowbox]{SquareAndMultiply}{x,b,n} |
| 617 |
\COMMENT{ Compute $x^b \pmod{n}$}\\ |
| 618 |
z\GETS 1\\ |
| 619 |
\WHILE b > 0 \DO |
| 620 |
\BEGIN |
| 621 |
z \GETS z^2 \pmod{n} \\ |
| 622 |
\IF b\mbox{ is odd} |
| 623 |
\THEN z \GETS z \cdot x \pmod{n} \\ |
| 624 |
b \GETS \CALL{ShiftRight}{b} |
| 625 |
\END\\ |
| 626 |
\RETURN{z} |
| 627 |
\end{pseudocode} |
| 628 |
|
| 629 |
\noindent When \verb+<frame>+ is \verb+doublebox+ we obtain: |
| 630 |
|
| 631 |
\begin{pseudocode}[doublebox]{SquareAndMultiply}{x,b,n} |
| 632 |
\COMMENT{ Compute $x^b \pmod{n}$}\\ |
| 633 |
z\GETS 1\\ |
| 634 |
\WHILE b > 0 \DO |
| 635 |
\BEGIN |
| 636 |
z \GETS z^2 \pmod{n} \\ |
| 637 |
\IF b\mbox{ is odd} |
| 638 |
\THEN z \GETS z \cdot x \pmod{n} \\ |
| 639 |
b \GETS \CALL{ShiftRight}{b} |
| 640 |
\END\\ |
| 641 |
\RETURN{z} |
| 642 |
\end{pseudocode} |
| 643 |
|
| 644 |
\noindent When \verb+<frame>+ is \verb+ovalbox+ we obtain: |
| 645 |
|
| 646 |
\begin{pseudocode}[ovalbox]{SquareAndMultiply}{x,b,n} |
| 647 |
\COMMENT{ Compute $x^b \pmod{n}$}\\ |
| 648 |
z\GETS 1\\ |
| 649 |
\WHILE b > 0 \DO |
| 650 |
\BEGIN |
| 651 |
z \GETS z^2 \pmod{n} \\ |
| 652 |
\IF b\mbox{ is odd} |
| 653 |
\THEN z \GETS z \cdot x \pmod{n} \\ |
| 654 |
b \GETS \CALL{ShiftRight}{b} |
| 655 |
\END\\ |
| 656 |
\RETURN{z} |
| 657 |
\end{pseudocode} |
| 658 |
|
| 659 |
\noindent When \verb+<frame>+ is \verb+Ovalbox+ we obtain: |
| 660 |
|
| 661 |
\begin{pseudocode}[Ovalbox]{SquareAndMultiply}{x,b,n} |
| 662 |
\COMMENT{ Compute $x^b \pmod{n}$}\\ |
| 663 |
z\GETS 1\\ |
| 664 |
\WHILE b > 0 \DO |
| 665 |
\BEGIN |
| 666 |
z \GETS z^2 \pmod{n} \\ |
| 667 |
\IF b\mbox{ is odd} |
| 668 |
\THEN z \GETS z \cdot x \pmod{n} \\ |
| 669 |
b \GETS \CALL{ShiftRight}{b} |
| 670 |
\END\\ |
| 671 |
\RETURN{z} |
| 672 |
\end{pseudocode} |
| 673 |
|
| 674 |
\noindent When \verb+<frame>+ is \verb+framebox+ we obtain: |
| 675 |
|
| 676 |
\begin{pseudocode}[framebox]{SquareAndMultiply}{x,b,n} |
| 677 |
\COMMENT{ Compute $x^b \pmod{n}$}\\ |
| 678 |
z\GETS 1\\ |
| 679 |
\WHILE b > 0 \DO |
| 680 |
\BEGIN |
| 681 |
z \GETS z^2 \pmod{n} \\ |
| 682 |
\IF b\mbox{ is odd} |
| 683 |
\THEN z \GETS z \cdot x \pmod{n} \\ |
| 684 |
b \GETS \CALL{ShiftRight}{b} |
| 685 |
\END\\ |
| 686 |
\RETURN{z} |
| 687 |
\end{pseudocode} |
| 688 |
|
| 689 |
\noindent When \verb+<frame>+ is \verb+plain+ or if \verb+[<frame>]+ is omitted we obtain: |
| 690 |
|
| 691 |
\begin{pseudocode}[plain]{SquareAndMultiply}{x,b,n} |
| 692 |
\COMMENT{ Compute $x^b \pmod{n}$}\\ |
| 693 |
z\GETS 1\\ |
| 694 |
\WHILE b > 0 \DO |
| 695 |
\BEGIN |
| 696 |
z \GETS z^2 \pmod{n} \\ |
| 697 |
\IF b\mbox{ is odd} |
| 698 |
\THEN z \GETS z \cdot x \pmod{n} \\ |
| 699 |
b \GETS \CALL{ShiftRight}{b} |
| 700 |
\END\\ |
| 701 |
\RETURN{z} |
| 702 |
\end{pseudocode} |
| 703 |
|
| 704 |
\noindent When \verb+<frame>+ is \verb+ruled+ we obtain: |
| 705 |
|
| 706 |
\begin{pseudocode}[ruled]{SquareAndMultiply}{x,b,n} |
| 707 |
\COMMENT{ Compute $x^b \pmod{n}$}\\ |
| 708 |
z\GETS 1\\ |
| 709 |
\WHILE b > 0 \DO |
| 710 |
\BEGIN |
| 711 |
z \GETS z^2 \pmod{n} \\ |
| 712 |
\IF b\mbox{ is odd} |
| 713 |
\THEN z \GETS z \cdot x \pmod{n} \\ |
| 714 |
b \GETS \CALL{ShiftRight}{b} |
| 715 |
\END\\ |
| 716 |
\RETURN{z} |
| 717 |
\end{pseudocode} |
| 718 |
|
| 719 |
|
| 720 |
The purpose of the value \verb+display+ is to allow portions of |
| 721 |
algorithms to be displayed with out the algorithm header. Thus |
| 722 |
for example to display the section of code in the $\WHILE$ loop |
| 723 |
of the $\CALL{SquareAndMultiply}{}$ algorithm one could write |
| 724 |
|
| 725 |
\begin{verbatim} |
| 726 |
\begin{center} |
| 727 |
\begin{minipage}{2in} |
| 728 |
\begin{pseudocode}[display]{}{} |
| 729 |
z \GETS z^2 \pmod{n} \\ |
| 730 |
\IF b\mbox{ is odd} |
| 731 |
\THEN z \GETS z \cdot x \pmod{n}\\ |
| 732 |
b \GETS \CALL{ShiftRight}{b} |
| 733 |
\end{pseudocode} |
| 734 |
\end{minipage} |
| 735 |
\end{center} |
| 736 |
\end{verbatim} |
| 737 |
|
| 738 |
\noindent which would produce the following output: |
| 739 |
|
| 740 |
\begin{center} |
| 741 |
\begin{minipage}{2in} |
| 742 |
\begin{pseudocode}[display]{}{} |
| 743 |
z \GETS z^2 \pmod{n} \\ |
| 744 |
\IF b\mbox{ is odd} |
| 745 |
\THEN z \GETS z \cdot x \pmod{n}\\ |
| 746 |
b \GETS \CALL{ShiftRight}{b} |
| 747 |
\end{pseudocode} |
| 748 |
\end{minipage} |
| 749 |
\end{center} |
| 750 |
|
| 751 |
\begin{thebibliography}{99} |
| 752 |
|
| 753 |
\bibitem{LC} |
| 754 |
M.\ Goossens, F.\ Mittelbach and A.\ Samarin, {\it The \LaTeX Companion}, |
| 755 |
Addison-Wesley, 1994. |
| 756 |
|
| 757 |
\bibitem{cages} |
| 758 |
D.L.\ Kreher and D.R.\ Stinson, |
| 759 |
{\em Combinatorial Algorithms: Generation, Enumeration and Search}, |
| 760 |
CRC Press, 1999. |
| 761 |
|
| 762 |
\bibitem{lamport} |
| 763 |
L.\ Lamport, \LaTeX, |
| 764 |
{\em A Document Preparation System}, |
| 765 |
Addison-Wesley, 1994. |
| 766 |
\end{thebibliography} |
| 767 |
\end{document} |
| 768 |
|