Debian Codegraph @

texmf-dist/doc/latex/clrscode/clrscode.tex

raw
1 % $Id$
2 % $Log$
3
4 \documentclass[11pt,mtbold]{article}
5 \usepackage{mathtime}
6 \usepackage{clrscode}
7 \input{page}
8
9 \newcommand{\subheading}[1]{\subsubsection*{#1}}
10 \newcommand{\clrscode}{\texttt{clrscode}}
11 \newcommand{\Codebox}{\texttt{codebox}}
12
13 \begin{document}
14
15 \title{Using the \clrscode{} Package in \LaTeXe}
16 \author{Thomas H. Cormen \\ [email protected]}
17 \date{June 11, 2003}
18
19 \maketitle
20
21 \section{Introduction}
22 \label{sec:intro}
23
24 This document describes how to use the \clrscode{} package in
25 \LaTeXe{} to typeset pseudocode in the style of \textit{Introduction
26 to Algorithms}, Second edition, by Cormen, Leiserson, Rivest, and
27 Stein (CLRS) \cite{CLRS01}. You use the commands in the same way we
28 did in writing CLRS, and your output will look just like the
29 pseudocode in the text.
30
31 \section{Setup}
32 \label{sec:setup}
33
34 To get the \clrscode{} package, download
35 http://www.cs.dartmouth.edu/\~{ }thc/clrscode/clrscode.sty .
36 To use the package, include the following line in your source file:
37
38 {\small
39 \begin{verbatim}
40 \usepackage{clrscode}
41 \end{verbatim}
42 }
43
44 \noindent The \clrscode{} package itself includes the line
45
46 {\small
47 \begin{verbatim}
48 \usepackage{latexsym}
49 \end{verbatim}
50 }
51
52 \noindent This line is necessary in order to get the character $\rhd$
53 for comments in pseudocode. Therefore, you will need to have the
54 \texttt{latexsym} package installed and available on your system.
55
56 \section{Typesetting names}
57 \label{sec:names}
58
59 Pseudocode in CLRS uses four types of names: identifiers, procedures,
60 constants, and fixed functions. We provide commands \verb`\id`,
61 \verb`\proc`, \verb`\const`, and \verb`\func` for these names. Each
62 of these commands takes one argument, which is the name being typeset.
63 These commands work both in and out of math mode. When used in math
64 mode, and when the name given as an argument contains a dash, the dash
65 is typeset as a hyphen rather than as a minus sign.
66
67 \begin{description}
68
69 \item[Identifiers:] Identifiers are used for variable and attribute
70 names. When a variable name is just a single letter, e.g., the
71 identifier $j$ in line~2 of \proc{Insertion-Sort} on page~17, we just
72 typeset it in math mode: \verb`$j$`.
73
74 Identifiers consisting of two or more letters, e.g., the attribute
75 \id{length} in the same line of \proc{Insertion-Sort}, should not be
76 typeset in this way. (See page~51 of Lamport \cite{Lamport93}.)
77 Although \LaTeXe{} provides the \verb`\mathit` command for typesetting
78 multiletter identifiers, we use our \verb`\id` command instead:
79 \verb`\id{length}`. We recommend that you use it, too. Since the
80 \verb`\id` command may be used both in and out of math mode, the source
81 text
82
83 {\small
84 \begin{verbatim}
85 We use the \id{length} attribute to denote the length of an array,
86 e.g., $\id{length}[A]$.
87 \end{verbatim}
88 }
89
90 \noindent will produce
91 \begin{quote}
92 We use the \id{length} attribute to denote the length of an array,
93 e.g., $\id{length}[A]$.
94 \end{quote}
95
96 \sloppy
97
98 To see how a dash turns into a hyphen, consider line~3
99 of \proc{Max-Heapify} on page~130. Its source contains the text
100 \verb`$l \leq \id{heap-size}[A]$`, which typesets as $l \leq
101 \id{heap-size}[A]$. Using \verb`$l \leq \mathit{heap-size}[A]$` would
102 produce $l \leq \mathit{heap-size}[A]$, with a minus sign rather than
103 a hyphen in the identifier.
104
105 \fussy
106
107 \item[Procedures:] For procedure names, use the \verb`\proc` command.
108 It typesets procedure names in small caps, and dashes (which occur
109 frequently in our procedure names) are typeset as hyphens. Thus, the
110 source \verb`\proc{Insertion-Sort}` produces \proc{Insertion-Sort}.
111 Since you can use the \verb`\proc` command both in and out of math mode,
112 the source text
113
114 {\small
115 \begin{verbatim}
116 We call \proc{Insertion-Sort} with an array $A$, so that the
117 call is $\proc{Insertion-Sort}(A)$.
118 \end{verbatim}
119 }
120
121 \noindent will produce
122 \begin{quote}
123 We call \proc{Insertion-Sort} with an array $A$, so that the
124 call is $\proc{Insertion-Sort}(A)$.
125 \end{quote}
126
127 \item[Constants:] We typeset constants like \const{nil}, \const{true},
128 and \const{red} in small caps with the \verb`\const` command, e.g.,
129 \verb`\const{nil}`, \verb`\const{true}`, and \verb`\const{red}`. I
130 don't think that any of our constants have dashes in them, but the
131 \verb`\const` command would typeset a dash within a constant name as a
132 hyphen, so that \verb`$\const{red-and-black}$` will produce
133 $\const{red-and-black}$.
134
135 \item[Fixed functions:] We typeset the names of fixed functions in
136 plain old roman with the \verb`\func` command, e.g., \func{level} and
137 \func{out-degree}. By a ``fixed function,'' we mean a function that
138 is a specific, given function. For example, the $\sin$ function is
139 typically typeset in roman; $\sin x$ looks right, but wouldn't
140 $\id{sin} x$ look strange? Yet, on page~42, $\Theta(g(n))$ looks
141 right, but $\Theta(\textrm{g}(n))$ would look wrong, since $g$ is a
142 variable that stands for any one of a number of functions.
143
144 As with the other commands for names, a dash within a function name will
145 typeset as a hyphen, so that \verb`$\func{out-degree}$` will produce
146 $\func{out-degree}$ rather than $\textrm{out}-\textrm{degree}$. Note
147 that \LaTeXe{} provides commands for many fixed functions, such as
148 $\sin$ and $\log$; Table 3.9 on page~44 of~\cite{Lamport93} lists
149 these ``log-like'' functions.
150
151 \end{description}
152
153 There is one other command that doesn't really fit anywhere else, so
154 I'll mention it here. We denote subarrays with the ``$\twodots$''
155 notation, which is produced by the \verb`\twodots` command. Thus, the
156 source text \verb`$A[1 \twodots j-1]$` will produce $A[1 \twodots
157 j-1]$. The \verb`\twodots` command must be used in math mode.
158
159 \section{The \Codebox{} environment}
160 \label{sec:codebox}
161
162 We typeset pseudocode by putting it in a \Codebox{} environment. A
163 \Codebox{} is a section of code that will not break across pages (I
164 hope).
165
166 \subheading{Contents of a \Codebox{}}
167
168 Each procedure should go in a separate \Codebox{}, even if you have
169 multiple procedures appearing consecutively. The only possible reason
170 I can think of to put more than one procedure in a single \Codebox{}
171 is to ensure that the procedures appear on the same page. If you
172 really need your procedures to appear on the same page, there are
173 other means in \LaTeXe, such as the \texttt{minipage} environment,
174 that you can use. Moreover, if you have written your procedures so
175 that they have to appear on the same page, you should probably be
176 asking yourself whether they are too interdependent.
177
178 The typical structure within a \Codebox{} is as follows. Usually, the
179 first line is the name of a procedure, along with a list of
180 parameters. (Not all \Codebox{}es include procedure names; for
181 example, see the pseudocode near the bottom of page~306 of CLRS\@.)
182 After the line containing the procedure name comes one or more lines
183 of code, usually numbered. Some of the lines may be unnumbered, being
184 continuations of previous lines. In rare cases, when there is just
185 one line of code per procedure, we don't bother numbering the line;
186 see the \proc{Parent}, \proc{Left}, and \proc{Right} procedures on
187 page~128 for example. Lines are usually numbered starting from~1, but
188 again there are exceptions, such as the pseudocode near the bottom of
189 page~306.
190
191 \subheading{Using \texttt{$\backslash$Procname} to name the procedure}
192
193 The \verb`\Procname` command specifies the name of the procedure. It
194 takes as a parameter the procedure name and parameters, typically all
195 in math mode. \verb`\Procname` makes its argument flush left against
196 the margin, and it leaves a little bit of extra space below the line.
197 For example, here is how we typeset the \proc{Insertion-Sort}
198 procedure on page~17:
199
200 {\small
201 \begin{verbatim}
202 \begin{codebox}
203 \Procname{$\proc{Insertion-Sort}(A)$}
204 \li \For $j \gets 2$ \To $\id{length}[A]$
205 \li \Do
206 $\id{key} \gets A[j]$
207 \li \Comment Insert $A[j]$ into the sorted sequence
208 $A[1 \twodots j-1]$.
209 \li $i \gets j-1$
210 \li \While $i > 0$ and $A[i] > \id{key}$
211 \li \Do
212 $A[i+1] \gets A[i]$
213 \li $i \gets i-1$
214 \End
215 \li $A[i+1] \gets \id{key}$
216 \End
217 \end{codebox}
218 \end{verbatim}
219 }
220
221 \subheading{Using \texttt{$\backslash$li} and \texttt{$\backslash$zi}
222 to start new lines}
223
224 To start a new, numbered line, use the \verb`\li` command. To start a
225 new, \emph{un}numbered line, use the \verb`\zi` command. Note that
226 since a \Codebox{} is not like the \verb`verbatim` environment, the
227 line breaks within the source text do not correspond to the line
228 breaks in the typeset output.
229
230 \subheading{Tabs}
231
232 I find that it is best to set the tab stops to every 4 characters when
233 typing in and displaying pseudocode source with the \clrscode{}
234 package. I use emacs, and to get the tabs set up the way I want them,
235 my \mbox{\texttt{tex-mode.el}} file includes the line
236 \verb`(setq tab-width 4)`. For reasons I do not understand, sometimes
237 emacs ``forgets'' this setting, and I have to do a little friendly
238 persuasion.
239
240 A \Codebox{} environment has a \verb`tabbing` environment within it.
241 The tab stops come in pairs, in that each pair of tab stops gives one
242 level of indentation. Tab stops are paired up so that when we typeset
243 the keywords \kw{then} and \kw{else}, they have the correct vertical
244 alignment. In other words, within each pair of tab stops, the first
245 stop is where \kw{then} and \kw{else} begin, and the second stop
246 completes a full level of indentation. For the most part, you won't
247 need to be concerned with tabs. The primary exception is when you
248 want to include a comment at the end of a line of pseudocode, and
249 especially when you want to include comments after several lines and
250 you want the comments to vertically align. Note that the
251 \verb`tabbing` environment within a codebox has nothing to do with
252 tabs that you enter in your source code; when you press the TAB key,
253 that's the same as pressing the space bar in the eyes of \LaTeXe{}.
254
255 \subheading{Commands for keywords}
256
257 As you can see from the source for \proc{Insertion-Sort}, there are
258 commands \verb`\For`, \verb`\Do`, and \verb`\While` that produce the
259 keywords \kw{for}, \kw{do}, and \kw{while}. \verb`\Do` and some other
260 commands also affect indentation.
261
262 Sometimes you want to include a keyword in the main text, as I have
263 done in several places in this document. Use the \verb`\kw` command
264 to do so. For example, to produce a sentence that appeared two
265 paragraphs ago, I typed in the following:
266
267 {\small
268 \begin{verbatim}
269 Tab stops are paired up so that when we typeset the keywords \kw{then}
270 and \kw{else}, they have the correct vertical alignment.
271 \end{verbatim}
272 }
273
274 The following commands simply produce their corresponding keywords,
275 typeset in boldface: \verb`\For`, \verb`\To`, \verb`\Downto`,
276 \verb`\By`, \verb`\While`, \verb`\If`, \verb`\Return`, \verb`\Goto`,
277 and \verb`\Error`. Although you could achieve the same effect with
278 the \verb`\kw` command (e.g., \verb`\kw{for}` instead of \verb`\For`),
279 you will find it easier and more readable to use the above commands.
280 The \verb`\Comment` command simply produces the comment symbol~$\rhd$.
281 None of the above commands affects indentation.
282
283 In \kw{for} loops and \kw{while} loops, the important commands are
284 \verb`\Do` and \verb`\End`. \verb`\Do` produces the keyword \kw{do},
285 and it also increments the indentation level. \verb`\End` simply
286 decrements the indentation level, and it is the way to end any
287 \kw{for} or \kw{while} loop or otherwise decrement the indentation
288 level.
289
290 As you can see from the above example, I like to place each \verb`\Do`
291 and \verb`\End` on its own line. You can of course format your source
292 text as you like, but I find that the way I format pseudocode makes it
293 easy to match up \verb`\Do`-\verb`\End` pairs.
294
295 We also use \verb`\End` to terminate an \kw{if}-\kw{then} or
296 \kw{if}-\kw{then}-\kw{else} construct. For an example of
297 \kw{if}-\kw{then}, here's the \proc{Merge-Sort} procedure on page~32:
298
299 \newpage
300
301 {\small
302 \begin{verbatim}
303 \begin{codebox}
304 \Procname{$\proc{Merge-Sort}(A, p, r)$}
305 \li \If $p < r$
306 \li \Then
307 $q \gets \floor{(p + r) / 2}$
308 \li $\proc{Merge-Sort}(A, p, q)$
309 \li $\proc{Merge-Sort}(A, q+1, r)$
310 \li $\proc{Merge}(A, p, q, r)$
311 \End
312 \end{codebox}
313 \end{verbatim}
314 }
315
316 \noindent For a more complicated example, using
317 \kw{if}-\kw{then}-\kw{else}, here's the \proc{Tree-Insert} procedure
318 on page~261:
319
320 {\footnotesize
321 \begin{verbatim}
322 \begin{codebox}
323 \Procname{$\proc{Tree-Insert}(T,z)$}
324 \li $y \gets \const{nil}$
325 \li $x \gets \id{root}[T]$
326 \li \While $x \neq \const{nil}$
327 \li \Do
328 $y \gets x$
329 \li \If $\id{key}[z] < \id{key}[x]$
330 \li \Then $x \gets \id{left}[x]$
331 \li \Else $x \gets \id{right}[x]$
332 \End
333 \End
334 \li $p[z] \gets y$
335 \li \If $y = \const{nil}$
336 \li \Then
337 $\id{root}[T] \gets z$\>\>\>\>\>\>\>\>\Comment Tree $T$ was empty
338 \li \Else
339 \If $\id{key}[z] <\ id{key}[y]$
340 \li \Then $\id{left}[y]\ gets z$
341 \li \Else $\id{right}[y] \gets z$
342 \End
343 \End
344 \end{codebox}
345 \end{verbatim}
346 }
347
348 \noindent As you can see, I like to line up the \verb`\End` commands
349 under the \verb`\Then` and \verb`\Else` commands. I could just as
350 easily have chosen to line up \verb`\End` under the \verb`\If` command
351 instead. I also sometimes elect to put the ``then'' or ``else'' code
352 on the same source line as the \verb`\Then` or \verb`\Else` command,
353 especially when that code is short.
354
355 The \proc{Tree-Insert} example also shows how we put a comment on the
356 same line as code. Via the tab command \verb`\>`, we explicitly tab
357 to where we want the comment to begin and then use the \verb`\Comment`
358 command to produce the comment symbol. When there are several lines
359 with comments, you probably want them to align vertically. I just add
360 tab characters, using a trial-and-error approach, until I am pleased
361 with the result. For example, here's how we produced the
362 \proc{KMP-Matcher} procedure on page~926:
363
364 \newpage
365
366 {\footnotesize
367 \begin{verbatim}
368 \begin{codebox}
369 \Procname{$\proc{KMP-Matcher}(T,P)$}
370 \li $n \gets \id{length}[T]$
371 \li $m \gets \id{length}[P]$
372 \li $\pi \gets \proc{Compute-Prefix-Function}(P)$
373 \li $q \gets 0$\>\>\>\>\>\>\>\>\>\>\Comment Number of characters matched.
374 \li \For $i \gets 1$ \To $n$\>\>\>\>\>\>\>\>\>\>\Comment
375 Scan the text from left to right.
376 \li \Do
377 \While $q > 0$ and $\Px{q+1} \neq \Tx{i}$
378 \li \Do $q \gets \pi[q]$\>\>\>\>\>\>\Comment Next character does not match.
379 \End
380 \li \If $\Px{q+1} = \Tx{i}$
381 \li \Then $q \gets q+1$\>\>\>\>\>\>\Comment Next character matches.
382 \End
383 \li \If $q = m$\>\>\>\>\>\>\>\>\Comment Is all of $P$ matched?
384 \li \Then
385 print ``Pattern occurs with shift'' $i-m$
386 \li $q \gets \pi[q]$\>\>\>\>\>\>\Comment Look for the next match.
387 \End
388 \End
389 \end{codebox}
390 \end{verbatim}
391 }
392
393 \noindent All six comments align nicely.
394
395 For a \kw{repeat} loop, use the \verb`\Repeat` and \verb`\Until`
396 commands, as in the \proc{Hash-Insert} procedure on page~238:
397
398 {\small
399 \begin{verbatim}
400 \begin{codebox}
401 \Procname{$\proc{Hash-Insert}(T,k)$}
402 \li $i \gets 0$
403 \li \Repeat
404 $j \gets h(k,i)$
405 \li \If $T[j] = \const{nil}$
406 \li \Then
407 $T[j]\gets k$
408 \li \Return $j$
409 \li \Else
410 $i \gets i+1$
411 \End
412 \li \Until $i = m$
413 \li \Error ``hash table overflow''
414 \end{codebox}
415 \end{verbatim}
416 }
417
418 \noindent Note that the \verb`\Until` command has an implied
419 \verb`\End`.
420
421 Sometimes, you need more complicated ``\kw{if}-ladders'' than you can
422 get from the \verb`\Then` and \verb`\Else` commands. The
423 \proc{Randomized-Select} procedure on page~186 provides an example,
424 and it uses the \verb`\ElseIf` and \verb`\ElseNoIf` commands:
425
426 \newpage
427
428 {\small
429 \begin{verbatim}
430 \begin{codebox}
431 \Procname{$\proc{Randomized-Select}(A, p, r, i)$}
432 \li \If $p = r$
433 \li \Then \Return $A[p]$
434 \End
435 \li $q \gets \proc{Randomized-Partition}(A, p, r)$
436 \li $k \gets q - p +1$
437 \li \If $i = k$\>\>\>\>\Comment the pivot value is the answer
438 \li \Then \Return $A[q]$
439 \li \ElseIf $i < k$
440 \li \Then \Return $\proc{Randomized-Select}(A, p, q-1, i)$
441 \li \ElseNoIf \Return $\proc{Randomized-Select}(A, q+1, r, i-k)$
442 \End
443 \end{codebox}
444 \end{verbatim}
445 }
446
447 \noindent For an \kw{if}-ladder, use \verb`\Then` for the first case,
448 \verb`\ElseNoIf` for the last case, and \verb`\ElseIf` for all
449 intermediate cases. An \kw{if}-ladder is terminated by \verb`\End`.
450 As another example, here is the \proc{Segments-Intersect} procedure on
451 page~937:
452
453 {\small
454 \begin{verbatim}
455 \begin{codebox}
456 \Procname{$\proc{Segments-Intersect}(p_1, p_2, p_3, p_4)$}
457 \li $d_1 \gets \proc{Direction}(p_3, p_4, p_1)$
458 \li $d_2 \gets \proc{Direction}(p_3, p_4, p_2)$
459 \li $d_3 \gets \proc{Direction}(p_1, p_2, p_3)$
460 \li $d_4 \gets \proc{Direction}(p_1, p_2, p_4)$
461 \li \If $((d_1 > 0 \mbox{ and } d_2 < 0) \mbox{ or }
462 (d_1 < 0 \mbox{ and } d_2 > 0))$ and
463 \Indentmore
464 \zi $((d_3 > 0 \mbox{ and } d_4 < 0) \mbox{ or }
465 (d_3 < 0 \mbox{ and } d_4 > 0))$
466 \End
467 \li \Then \Return \const{true}
468 \li \ElseIf $d_1 = 0$ and $\proc{On-Segment}(p_3, p_4, p_1)$
469 \li \Then \Return \const{true}
470 \li \ElseIf $d_2 = 0$ and $\proc{On-Segment}(p_3, p_4, p_2)$
471 \li \Then \Return \const{true}
472 \li \ElseIf $d_3 = 0$ and $\proc{On-Segment}(p_1, p_2, p_3)$
473 \li \Then \Return \const{true}
474 \li \ElseIf $d_4 = 0$ and $\proc{On-Segment}(p_1, p_2, p_4)$
475 \li \Then \Return \const{true}
476 \li \ElseNoIf \Return \const{false}
477 \End
478 \end{codebox}
479 \end{verbatim}
480 }
481
482 This example also shows our first use of an unnumbered line: the
483 second half of the tests on line~5. We use \verb`\zi` to indicate
484 that we're starting an unnumbered line.
485
486 \subheading{Indentation}
487
488 We also wish to indent the unnumbered line after line~5 by one level
489 more than the line above it. We do so with the \verb`\Indentmore`
490 command. The \verb`\End` command following the indented line
491 decrements the indentation level back to what it was prior to the
492 \verb`\Indentmore`. If I had wanted to indent the line by two levels,
493 I would have used two \verb`\Indentmore` commands before the line and
494 two \verb`\End` commands afterward. (Recall that \verb`\End` simply
495 decrements the indentation level.)
496
497 Upon seeing the \verb`\end{codebox}` command, the \Codebox{}
498 environment checks that the indentation level is back to where it was
499 when it started, namely an indentation level of~0. If it is not, you
500 will get a warning message like the following:
501
502 {\small
503 \begin{verbatim}
504 Warning: Indentation ends at level 1 in codebox on page 1.
505 \end{verbatim}
506 }
507
508 \noindent This message would indicate that there is one missing
509 \verb`\End` command. On the other hand, you might have one too many
510 \verb`\End` commands, in which case you would get
511
512 {\small
513 \begin{verbatim}
514 Warning: Indentation ends at level -1 in codebox on page 1.
515 \end{verbatim}
516 }
517
518 \subheading{Right justification}
519
520 The final two commands, \verb`\RComment` and \verb`\Flushright`, are
521 infrequently used and pertain to right justification.
522 \verb`\RComment` produces a comment that is flush against the right
523 margin. For example, here is how we typeset the
524 \proc{Binomial-Heap-Union} procedure on page~463:
525
526 \newpage
527
528 {\footnotesize
529 \begin{verbatim}
530 \begin{codebox}
531 \Procname{$\proc{Binomial-Heap-Union}(H_1,H_2)$}
532 \li $H \gets \proc{Make-Binomial-Heap}()$
533 \li $\id{head}[H] \gets \proc{Binomial-Heap-Merge}(H_1,H_2)$
534 \li free the objects $H_1$ and $H_2$ but not the lists they point to
535 \li \If $\id{head}[H] = \const{nil}$
536 \li \Then \Return $H$
537 \End
538 \li $\id{prev-x} \gets \const{nil}$
539 \li $x \gets \id{head}[H]$
540 \li $\id{next-x} \gets \id{sibling}[x]$
541 \li \While $\id{next-x} \neq \const{nil}$
542 \li \Do
543 \If $(\id{degree}[x] \neq \id{degree}[\id{next-x}])$ or
544 \zi \Flushright $(\id{sibling}[\id{next-x}] \neq \const{nil}$
545 and $\id{degree}[\id{sibling}[\id{next-x}]] = \id{degree}[x])$
546 \li \Then
547 $\id{prev-x} \gets x$
548 \RComment Cases 1 and 2
549 \li $x \gets \id{next-x}$
550 \RComment Cases 1 and 2
551 \li \Else
552 \If $\id{key}[x] \leq \id{key}[\id{next-x}]$
553 \li \Then
554 $\id{sibling}[x] \gets \id{sibling}[\id{next-x}]$
555 \RComment Case 3
556 \li $\proc{Binomial-Link}(\id{next-x},x)$
557 \RComment Case 3
558 \li \Else
559 \If $\id{prev-x} = \const{nil}$
560 \RComment Case 4
561 \li \Then $\id{head}[H] \gets \id{next-x}$
562 \RComment Case 4
563 \li \Else $\id{sibling}[\id{prev-x}] \gets
564 \id{next-x}$
565 \RComment Case 4
566 \End
567 \li $\proc{Binomial-Link}(x,\id{next-x})$
568 \RComment Case 4
569 \li $x \gets \id{next-x}$
570 \RComment Case 4
571 \End
572 \End
573 \li $\id{next-x} \gets \id{sibling}[x]$
574 \End
575 \li \Return $H$
576 \end{codebox}
577 \end{verbatim}
578 }
579
580 \noindent This procedure is also the only place in CLRS in which we
581 used the \verb`\Flushright` command. It puts the unnumbered line
582 following line~10 flush against the right margin.
583
584 \subheading{Referencing line numbers}
585
586 In the source files for CLRS, there are no absolute references to line
587 numbers. We use \emph{only} symbolic references. The \Codebox{}
588 environment is set up to allow you to place \verb`\label` commands on
589 lines of pseudocode and then reference these labels. The references
590 will resolve to the line numbers. Our convention is that any label
591 for a line number begins with \verb`\li:`, but you can name the labels
592 any way that you like.
593
594 For example, here's how we \emph{really} wrote the
595 \proc{Insertion-Sort} procedure on page~17:
596
597 {\small
598 \begin{verbatim}
599 \begin{codebox}
600 \Procname{$\proc{Insertion-Sort}(A)$}
601 \li \For $j \gets 2$ \To $\id{length}[A]$
602 \label{li:ins-sort-for}
603 \li \Do $\id{key} \gets A[j]$ \label{li:ins-sort-pick}
604 \label{li:ins-sort-for-body-begin}
605 \li \Comment Insert $A[j]$ into the sorted sequence
606 $A[1 \twodots j-1]$.
607 \li $i \gets j-1$ \label{li:ins-sort-find-begin}
608 \li \While $i > 0$ and $A[i] > \id{key}$
609 \label{li:ins-sort-while}
610 \li \Do
611 $A[i+1] \gets A[i]$ \label{li:ins-sort-while-begin}
612 \li $i \gets i-1$ \label{li:ins-sort-find-end}
613 \label{li:ins-sort-while-end}
614 \End
615 \li $A[i+1] \gets \id{key}$ \label{li:ins-sort-ins}
616 \label{li:ins-sort-for-body-end}
617 \End
618 \end{codebox}
619 \end{verbatim}
620 }
621
622 \noindent Note that any line may have multiple labels. As an example
623 of referencing these labels, here's the beginning of the first item
624 under ``Pseudocode conventions'' on page~19:
625
626 {\small
627 \begin{verbatim}
628 \item For example, the body of the \kw{for} loop that begins on
629 line~\ref{li:ins-sort-for} consists of lines
630 \ref{li:ins-sort-for-body-begin}--\ref{li:ins-sort-for-body-end},
631 and the body of the \kw{while} loop that begins on
632 line~\ref{li:ins-sort-while} contains lines
633 \ref{li:ins-sort-while-begin}--\ref{li:ins-sort-while-end} but
634 not line~\ref{li:ins-sort-for-body-end}.
635 \end{verbatim}
636 }
637
638 \subheading{Setting line numbers}
639
640 On rare occasions, we needed to start line numbers somewhere other
641 than~1. Use the \verb`setlinenumber` command to set the next line
642 number. For example, in Exercise~24.2-2 on page~594, we want the line
643 number to be the same as a line number within the
644 \proc{Dag-Shortest-Paths} procedure on page~592. Here's the source
645 for the exercise:
646
647 {\small
648 \begin{verbatim}
649 Suppose we change line~\ref{li:dag-sp-loop-begin} of
650 \proc{Dag-Shortest-Paths} to read
651
652 \begin{codebox}
653 \setlinenumber{li:dag-sp-loop-begin}
654 \li \For the first $\card{V}-1$ vertices, taken in topologically sorted order
655 \end{codebox}
656 Show that the procedure would remain correct.
657 \end{verbatim}
658 }
659
660 \noindent The \proc{Dag-Shortest-Paths} procedure is
661
662 {\small
663 \begin{verbatim}
664 \begin{codebox}
665 \Procname{$\proc{Dag-Shortest-Paths}(G,w,s)$}
666 \li topologically sort the vertices of $G$ \label{li:dag-sp-topo-sort}
667 \li $\proc{Initialize-Single-Source}(G,s)$ \label{li:dag-sp-init}
668 \li \For each vertex $u$, taken in topologically sorted order
669 \label{li:dag-sp-loop-begin}
670 \li \Do
671 \For each vertex $v \in \id{Adj}[u]$
672 \label{li:dag-sp-inner-begin}
673 \li \Do $\proc{Relax}(u,v,w)$ \label{li:dag-sp-loop-end}
674 \End
675 \End
676 \end{codebox}
677 \end{verbatim}
678 }
679
680 Even more rarely (just once, in fact), we needed to set a line number
681 to be some other line number plus an offset. That was in the two
682 lines of pseudocode near the bottom of page~306, where the first line
683 number had to be one greater than the number of the last line of
684 \proc{Left-Rotate} on page~278. Use the \verb`setlinenumberplus`
685 command:
686
687 {\footnotesize
688 \begin{verbatim}
689 \begin{codebox}
690 \setlinenumberplus{li:left-rot-parent}{1}
691 \li $\id{size}[y] \gets \id{size}[x]$
692 \li $\id{size}[x] \gets \id{size}[\id{left}[x]] + \id{size}[\id{right}[x]] + 1$
693 \end{codebox}
694 \end{verbatim}
695 }
696
697 \noindent Here, the last line of \proc{Left-Rotate} has
698 \verb`\label{li:left-rot-parent}`.
699
700 \section{Reporting bugs}
701 \label{sec:bugs}
702
703 If you find errors in the \clrscode{} package, please send me email
704 ([email protected]). It would be best if your message included
705 everything I would require to elicit the error myself.
706
707 The \texttt{clrscode.sty} file contains the following disclaimer:
708
709 {\small
710 \begin{verbatim}
711 % Written for general distribution by Thomas H. Cormen, June 2003.
712
713 % The author grants permission for anyone to use this macro package and
714 % to distribute it unchanged without further restriction. If you choose
715 % to modify this package, you must indicate that you have modified it
716 % prior to your distributing it. I don't want to get bug reports about
717 % changes that *you* have made!
718 \end{verbatim}
719 }
720
721 \noindent I have enough trouble keeping up with my own bugs; I don't
722 want to hear about bugs that others have introduced in the package!
723
724 \section{Revision history}
725
726 \begin{itemize}
727
728 \item 11 June 2003. Initial revision of document and code.
729
730 \end{itemize}
731
732 \begin{thebibliography}{9}
733
734 \bibitem{CLRS01} Thomas H. Cormen, Charles E. Leiserson, Ronald L.
735 Rivest, and Clifford Stein. \textit{Introduction to Algorithms},
736 second edition. The MIT Press and McGraw-Hill, 2001.
737
738 \bibitem{Lamport93} Leslie Lamport. \textit{\LaTeX: A Document
739 Preparation System User's Guide and Reference Manual}.
740 Addison-Wesley, 1993.
741
742 \end{thebibliography}
743
744 \end{document}