summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/clrscode/clrscode.tex
blob: e2bb05724742dff46f087a44d6c674ca0730fd19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
% $Id$
% $Log$

\documentclass[11pt,mtbold]{article}
\usepackage{mathtime}
\usepackage{clrscode}
\input{page}

\newcommand{\subheading}[1]{\subsubsection*{#1}}
\newcommand{\clrscode}{\texttt{clrscode}}
\newcommand{\Codebox}{\texttt{codebox}}

\begin{document}

\title{Using the \clrscode{} Package in \LaTeXe}
\author{Thomas H. Cormen \\ thc@cs.dartmouth.edu}
\date{June 11, 2003}

\maketitle

\section{Introduction}
\label{sec:intro}

This document describes how to use the \clrscode{} package in
\LaTeXe{} to typeset pseudocode in the style of \textit{Introduction
to Algorithms}, Second edition, by Cormen, Leiserson, Rivest, and
Stein (CLRS) \cite{CLRS01}.  You use the commands in the same way we
did in writing CLRS, and your output will look just like the
pseudocode in the text.

\section{Setup}
\label{sec:setup}

To get the \clrscode{} package, download
http://www.cs.dartmouth.edu/\~{ }thc/clrscode/clrscode.sty .
To use the package, include the following line in your source file:

{\small
\begin{verbatim}
\usepackage{clrscode}
\end{verbatim}
}

\noindent The \clrscode{} package itself includes the line

{\small
\begin{verbatim}
\usepackage{latexsym}
\end{verbatim}
}

\noindent This line is necessary in order to get the character $\rhd$
for comments in pseudocode.  Therefore, you will need to have the
\texttt{latexsym} package installed and available on your system.

\section{Typesetting names}
\label{sec:names}

Pseudocode in CLRS uses four types of names: identifiers, procedures,
constants, and fixed functions.  We provide commands \verb`\id`,
\verb`\proc`, \verb`\const`, and \verb`\func` for these names.  Each
of these commands takes one argument, which is the name being typeset.
These commands work both in and out of math mode.  When used in math
mode, and when the name given as an argument contains a dash, the dash
is typeset as a hyphen rather than as a minus sign.

\begin{description}

\item[Identifiers:] Identifiers are used for variable and attribute
names.  When a variable name is just a single letter, e.g., the
identifier $j$ in line~2 of \proc{Insertion-Sort} on page~17, we just
typeset it in math mode: \verb`$j$`.

Identifiers consisting of two or more letters, e.g., the attribute
\id{length} in the same line of \proc{Insertion-Sort}, should not be
typeset in this way.  (See page~51 of Lamport \cite{Lamport93}.)
Although \LaTeXe{} provides the \verb`\mathit` command for typesetting
multiletter identifiers, we use our \verb`\id` command instead:
\verb`\id{length}`.  We recommend that you use it, too.  Since the
\verb`\id` command may be used both in and out of math mode, the source
text

{\small
\begin{verbatim}
We use the \id{length} attribute to denote the length of an array,
e.g., $\id{length}[A]$.
\end{verbatim}
}

\noindent will produce
\begin{quote}
We use the \id{length} attribute to denote the length of an array,
e.g., $\id{length}[A]$.
\end{quote}

\sloppy

To see how a dash turns into a hyphen, consider line~3
of \proc{Max-Heapify} on page~130.  Its source contains the text
\verb`$l \leq \id{heap-size}[A]$`, which typesets as $l \leq
\id{heap-size}[A]$.  Using \verb`$l \leq \mathit{heap-size}[A]$` would
produce $l \leq \mathit{heap-size}[A]$, with a minus sign rather than
a hyphen in the identifier.

\fussy

\item[Procedures:] For procedure names, use the \verb`\proc` command.
It typesets procedure names in small caps, and dashes (which occur
frequently in our procedure names) are typeset as hyphens.  Thus, the
source \verb`\proc{Insertion-Sort}` produces \proc{Insertion-Sort}.
Since you can use the \verb`\proc` command both in and out of math mode,
the source text

{\small
\begin{verbatim}
We call \proc{Insertion-Sort} with an array $A$, so that the
call is $\proc{Insertion-Sort}(A)$.
\end{verbatim}
}

\noindent will produce
\begin{quote}
We call \proc{Insertion-Sort} with an array $A$, so that the
call is $\proc{Insertion-Sort}(A)$.
\end{quote}

\item[Constants:] We typeset constants like \const{nil}, \const{true},
and \const{red} in small caps with the \verb`\const` command, e.g.,
\verb`\const{nil}`, \verb`\const{true}`, and \verb`\const{red}`.  I
don't think that any of our constants have dashes in them, but the
\verb`\const` command would typeset a dash within a constant name as a
hyphen, so that \verb`$\const{red-and-black}$` will produce
$\const{red-and-black}$.

\item[Fixed functions:] We typeset the names of fixed functions in
plain old roman with the \verb`\func` command, e.g., \func{level} and
\func{out-degree}.  By a ``fixed function,'' we mean a function that
is a specific, given function.  For example, the $\sin$ function is
typically typeset in roman; $\sin x$ looks right, but wouldn't
$\id{sin} x$ look strange?  Yet, on page~42, $\Theta(g(n))$ looks
right, but $\Theta(\textrm{g}(n))$ would look wrong, since $g$ is a
variable that stands for any one of a number of functions.

As with the other commands for names, a dash within a function name will
typeset as a hyphen, so that \verb`$\func{out-degree}$` will produce
$\func{out-degree}$ rather than $\textrm{out}-\textrm{degree}$.  Note
that \LaTeXe{} provides commands for many fixed functions, such as
$\sin$ and $\log$; Table 3.9 on page~44 of~\cite{Lamport93} lists
these ``log-like'' functions.

\end{description}

There is one other command that doesn't really fit anywhere else, so
I'll mention it here.  We denote subarrays with the ``$\twodots$''
notation, which is produced by the \verb`\twodots` command.  Thus, the
source text \verb`$A[1 \twodots j-1]$` will produce $A[1 \twodots
j-1]$.  The \verb`\twodots` command must be used in math mode.

\section{The \Codebox{} environment}
\label{sec:codebox}

We typeset pseudocode by putting it in a \Codebox{} environment.  A
\Codebox{} is a section of code that will not break across pages (I
hope).

\subheading{Contents of a \Codebox{}}

Each procedure should go in a separate \Codebox{}, even if you have
multiple procedures appearing consecutively.  The only possible reason
I can think of to put more than one procedure in a single \Codebox{}
is to ensure that the procedures appear on the same page.  If you
really need your procedures to appear on the same page, there are
other means in \LaTeXe, such as the \texttt{minipage} environment,
that you can use.  Moreover, if you have written your procedures so
that they have to appear on the same page, you should probably be
asking yourself whether they are too interdependent.

The typical structure within a \Codebox{} is as follows.  Usually, the
first line is the name of a procedure, along with a list of
parameters.  (Not all \Codebox{}es include procedure names; for
example, see the pseudocode near the bottom of page~306 of CLRS\@.)
After the line containing the procedure name comes one or more lines
of code, usually numbered.  Some of the lines may be unnumbered, being
continuations of previous lines.  In rare cases, when there is just
one line of code per procedure, we don't bother numbering the line;
see the \proc{Parent}, \proc{Left}, and \proc{Right} procedures on
page~128 for example.  Lines are usually numbered starting from~1, but
again there are exceptions, such as the pseudocode near the bottom of
page~306.

\subheading{Using \texttt{$\backslash$Procname} to name the procedure}

The \verb`\Procname` command specifies the name of the procedure.  It
takes as a parameter the procedure name and parameters, typically all
in math mode.  \verb`\Procname` makes its argument flush left against
the margin, and it leaves a little bit of extra space below the line.
For example, here is how we typeset the \proc{Insertion-Sort}
procedure on page~17:

{\small
\begin{verbatim}
\begin{codebox}
\Procname{$\proc{Insertion-Sort}(A)$}
\li \For $j \gets 2$ \To $\id{length}[A]$
\li     \Do
            $\id{key} \gets A[j]$
\li         \Comment Insert $A[j]$ into the sorted sequence
                $A[1 \twodots j-1]$.
\li         $i \gets j-1$
\li         \While $i > 0$ and $A[i] > \id{key}$
\li             \Do
                    $A[i+1] \gets A[i]$
\li                 $i \gets i-1$
                \End
\li         $A[i+1] \gets \id{key}$
        \End
\end{codebox}
\end{verbatim}
}

\subheading{Using \texttt{$\backslash$li} and \texttt{$\backslash$zi}
to start new lines}

To start a new, numbered line, use the \verb`\li` command.  To start a
new, \emph{un}numbered line, use the \verb`\zi` command.  Note that
since a \Codebox{} is not like the \verb`verbatim` environment, the
line breaks within the source text do not correspond to the line
breaks in the typeset output.

\subheading{Tabs}

I find that it is best to set the tab stops to every 4 characters when
typing in and displaying pseudocode source with the \clrscode{}
package.  I use emacs, and to get the tabs set up the way I want them,
my \mbox{\texttt{tex-mode.el}} file includes the line
\verb`(setq tab-width 4)`.  For reasons I do not understand, sometimes
emacs ``forgets'' this setting, and I have to do a little friendly
persuasion.

A \Codebox{} environment has a \verb`tabbing` environment within it.
The tab stops come in pairs, in that each pair of tab stops gives one
level of indentation.  Tab stops are paired up so that when we typeset
the keywords \kw{then} and \kw{else}, they have the correct vertical
alignment.  In other words, within each pair of tab stops, the first
stop is where \kw{then} and \kw{else} begin, and the second stop
completes a full level of indentation.  For the most part, you won't
need to be concerned with tabs.  The primary exception is when you
want to include a comment at the end of a line of pseudocode, and
especially when you want to include comments after several lines and
you want the comments to vertically align.  Note that the
\verb`tabbing` environment within a codebox has nothing to do with
tabs that you enter in your source code; when you press the TAB key,
that's the same as pressing the space bar in the eyes of \LaTeXe{}.

\subheading{Commands for keywords}

As you can see from the source for \proc{Insertion-Sort}, there are
commands \verb`\For`, \verb`\Do`, and \verb`\While` that produce the
keywords \kw{for}, \kw{do}, and \kw{while}.  \verb`\Do` and some other
commands also affect indentation.

Sometimes you want to include a keyword in the main text, as I have
done in several places in this document.  Use the \verb`\kw` command
to do so.  For example, to produce a sentence that appeared two
paragraphs ago, I typed in the following:

{\small
\begin{verbatim}
Tab stops are paired up so that when we typeset the keywords \kw{then}
and \kw{else}, they have the correct vertical alignment.
\end{verbatim}
}

The following commands simply produce their corresponding keywords,
typeset in boldface: \verb`\For`, \verb`\To`, \verb`\Downto`,
\verb`\By`, \verb`\While`, \verb`\If`, \verb`\Return`, \verb`\Goto`,
and \verb`\Error`.  Although you could achieve the same effect with
the \verb`\kw` command (e.g., \verb`\kw{for}` instead of \verb`\For`),
you will find it easier and more readable to use the above commands.
The \verb`\Comment` command simply produces the comment symbol~$\rhd$.
None of the above commands affects indentation.

In \kw{for} loops and \kw{while} loops, the important commands are
\verb`\Do` and \verb`\End`.  \verb`\Do` produces the keyword \kw{do},
and it also increments the indentation level.  \verb`\End` simply
decrements the indentation level, and it is the way to end any
\kw{for} or \kw{while} loop or otherwise decrement the indentation
level.

As you can see from the above example, I like to place each \verb`\Do`
and \verb`\End` on its own line.  You can of course format your source
text as you like, but I find that the way I format pseudocode makes it
easy to match up \verb`\Do`-\verb`\End` pairs.

We also use \verb`\End` to terminate an \kw{if}-\kw{then} or
\kw{if}-\kw{then}-\kw{else} construct.  For an example of
\kw{if}-\kw{then}, here's the \proc{Merge-Sort} procedure on page~32:

\newpage

{\small
\begin{verbatim}
\begin{codebox}
\Procname{$\proc{Merge-Sort}(A, p, r)$}
\li \If $p < r$
\li     \Then
            $q \gets \floor{(p + r) / 2}$
\li         $\proc{Merge-Sort}(A, p, q)$
\li         $\proc{Merge-Sort}(A, q+1, r)$
\li         $\proc{Merge}(A, p, q, r)$
        \End
\end{codebox}
\end{verbatim}
}

\noindent For a more complicated example, using
\kw{if}-\kw{then}-\kw{else}, here's the \proc{Tree-Insert} procedure
on page~261:

{\footnotesize
\begin{verbatim}
\begin{codebox}
\Procname{$\proc{Tree-Insert}(T,z)$}
\li $y \gets \const{nil}$
\li $x \gets \id{root}[T]$
\li \While $x \neq \const{nil}$
\li     \Do
            $y \gets x$
\li         \If $\id{key}[z] < \id{key}[x]$
\li             \Then $x \gets \id{left}[x]$
\li             \Else $x \gets \id{right}[x]$
                \End
        \End
\li $p[z] \gets y$
\li \If $y = \const{nil}$
\li     \Then
            $\id{root}[T] \gets z$\>\>\>\>\>\>\>\>\Comment Tree $T$ was empty
\li     \Else
            \If $\id{key}[z] <\ id{key}[y]$
\li             \Then $\id{left}[y]\ gets z$
\li             \Else $\id{right}[y] \gets z$
                \End
        \End
\end{codebox}
\end{verbatim}
}

\noindent As you can see, I like to line up the \verb`\End` commands
under the \verb`\Then` and \verb`\Else` commands.  I could just as
easily have chosen to line up \verb`\End` under the \verb`\If` command
instead.  I also sometimes elect to put the ``then'' or ``else'' code
on the same source line as the \verb`\Then` or \verb`\Else` command,
especially when that code is short.

The \proc{Tree-Insert} example also shows how we put a comment on the
same line as code.  Via the tab command \verb`\>`, we explicitly tab
to where we want the comment to begin and then use the \verb`\Comment`
command to produce the comment symbol.  When there are several lines
with comments, you probably want them to align vertically.  I just add
tab characters, using a trial-and-error approach, until I am pleased
with the result.  For example, here's how we produced the
\proc{KMP-Matcher} procedure on page~926:

\newpage

{\footnotesize
\begin{verbatim}
\begin{codebox}
\Procname{$\proc{KMP-Matcher}(T,P)$}
\li $n \gets \id{length}[T]$
\li $m \gets \id{length}[P]$
\li $\pi \gets \proc{Compute-Prefix-Function}(P)$
\li $q \gets 0$\>\>\>\>\>\>\>\>\>\>\Comment Number of characters matched.
\li \For $i \gets 1$ \To $n$\>\>\>\>\>\>\>\>\>\>\Comment
                Scan the text from left to right.
\li     \Do
            \While $q > 0$ and $\Px{q+1} \neq \Tx{i}$
\li             \Do $q \gets \pi[q]$\>\>\>\>\>\>\Comment Next character does not match.
                \End
\li         \If $\Px{q+1} = \Tx{i}$ 
\li             \Then $q \gets q+1$\>\>\>\>\>\>\Comment Next character matches.
                \End
\li         \If $q = m$\>\>\>\>\>\>\>\>\Comment Is all of $P$ matched?
\li             \Then
                    print ``Pattern occurs with shift'' $i-m$
\li                 $q \gets \pi[q]$\>\>\>\>\>\>\Comment Look for the next match.
                \End
        \End
\end{codebox}
\end{verbatim}
}

\noindent All six comments align nicely.

For a \kw{repeat} loop, use the \verb`\Repeat` and \verb`\Until`
commands, as in the \proc{Hash-Insert} procedure on page~238:

{\small
\begin{verbatim}
\begin{codebox}
\Procname{$\proc{Hash-Insert}(T,k)$}
\li $i \gets 0$
\li \Repeat
        $j \gets h(k,i)$
\li     \If $T[j] = \const{nil}$
\li         \Then
                $T[j]\gets k$
\li             \Return $j$
\li         \Else
                $i \gets i+1$
            \End
\li \Until $i = m$
\li \Error ``hash table overflow''
\end{codebox}
\end{verbatim}
}

\noindent Note that the \verb`\Until` command has an implied
\verb`\End`.

Sometimes, you need more complicated ``\kw{if}-ladders'' than you can
get from the \verb`\Then` and \verb`\Else` commands.  The
\proc{Randomized-Select} procedure on page~186 provides an example,
and it uses the \verb`\ElseIf` and \verb`\ElseNoIf` commands:

\newpage

{\small
\begin{verbatim}
\begin{codebox}
\Procname{$\proc{Randomized-Select}(A, p, r, i)$}
\li \If $p = r$
\li     \Then \Return $A[p]$
        \End
\li $q \gets \proc{Randomized-Partition}(A, p, r)$
\li $k \gets q - p +1$
\li \If $i = k$\>\>\>\>\Comment the pivot value is the answer
\li     \Then \Return $A[q]$
\li \ElseIf $i < k$
\li     \Then \Return $\proc{Randomized-Select}(A, p, q-1, i)$ 
\li \ElseNoIf \Return $\proc{Randomized-Select}(A, q+1, r, i-k)$
        \End
\end{codebox}
\end{verbatim}
}

\noindent For an \kw{if}-ladder, use \verb`\Then` for the first case,
\verb`\ElseNoIf` for the last case, and \verb`\ElseIf` for all
intermediate cases.  An \kw{if}-ladder is terminated by \verb`\End`.
As another example, here is the \proc{Segments-Intersect} procedure on
page~937:

{\small
\begin{verbatim}
\begin{codebox}
\Procname{$\proc{Segments-Intersect}(p_1, p_2, p_3, p_4)$}
\li $d_1 \gets \proc{Direction}(p_3, p_4, p_1)$
\li $d_2 \gets \proc{Direction}(p_3, p_4, p_2)$
\li $d_3 \gets \proc{Direction}(p_1, p_2, p_3)$
\li $d_4 \gets \proc{Direction}(p_1, p_2, p_4)$
\li \If $((d_1 > 0 \mbox{ and } d_2 < 0) \mbox{ or }
        (d_1 < 0 \mbox{ and } d_2 > 0))$ and
        \Indentmore
\zi     $((d_3 > 0 \mbox{ and } d_4 < 0) \mbox{ or }
        (d_3 < 0 \mbox{ and } d_4 > 0))$
        \End
\li     \Then \Return \const{true}
\li \ElseIf $d_1 = 0$ and $\proc{On-Segment}(p_3, p_4, p_1)$
\li     \Then \Return \const{true}
\li \ElseIf $d_2 = 0$ and $\proc{On-Segment}(p_3, p_4, p_2)$
\li     \Then \Return \const{true}
\li \ElseIf $d_3 = 0$ and $\proc{On-Segment}(p_1, p_2, p_3)$
\li     \Then \Return \const{true}
\li \ElseIf $d_4 = 0$ and $\proc{On-Segment}(p_1, p_2, p_4)$
\li     \Then \Return \const{true}
\li \ElseNoIf \Return \const{false}
        \End
\end{codebox}
\end{verbatim}
}

This example also shows our first use of an unnumbered line: the
second half of the tests on line~5.  We use \verb`\zi` to indicate
that we're starting an unnumbered line.

\subheading{Indentation}

We also wish to indent the unnumbered line after line~5 by one level
more than the line above it.  We do so with the \verb`\Indentmore`
command.  The \verb`\End` command following the indented line
decrements the indentation level back to what it was prior to the
\verb`\Indentmore`.  If I had wanted to indent the line by two levels,
I would have used two \verb`\Indentmore` commands before the line and
two \verb`\End` commands afterward.  (Recall that \verb`\End` simply
decrements the indentation level.)

Upon seeing the \verb`\end{codebox}` command, the \Codebox{}
environment checks that the indentation level is back to where it was
when it started, namely an indentation level of~0.  If it is not, you
will get a warning message like the following:

{\small
\begin{verbatim}
Warning: Indentation ends at level 1 in codebox on page 1.
\end{verbatim}
}

\noindent This message would indicate that there is one missing
\verb`\End` command.  On the other hand, you might have one too many
\verb`\End` commands, in which case you would get

{\small
\begin{verbatim}
Warning: Indentation ends at level -1 in codebox on page 1.
\end{verbatim}
}

\subheading{Right justification}

The final two commands, \verb`\RComment` and \verb`\Flushright`, are
infrequently used and pertain to right justification.
\verb`\RComment` produces a comment that is flush against the right
margin.  For example, here is how we typeset the
\proc{Binomial-Heap-Union} procedure on page~463:

\newpage

{\footnotesize
\begin{verbatim}
\begin{codebox}
\Procname{$\proc{Binomial-Heap-Union}(H_1,H_2)$}
\li $H \gets \proc{Make-Binomial-Heap}()$
\li $\id{head}[H] \gets \proc{Binomial-Heap-Merge}(H_1,H_2)$
\li free the objects $H_1$ and $H_2$ but not the lists they point to
\li \If $\id{head}[H] = \const{nil}$
\li     \Then \Return $H$
        \End
\li $\id{prev-x} \gets \const{nil}$
\li $x \gets \id{head}[H]$
\li $\id{next-x} \gets \id{sibling}[x]$
\li \While $\id{next-x} \neq \const{nil}$
\li     \Do
            \If $(\id{degree}[x] \neq \id{degree}[\id{next-x}])$ or
\zi             \Flushright $(\id{sibling}[\id{next-x}] \neq \const{nil}$
                and $\id{degree}[\id{sibling}[\id{next-x}]] = \id{degree}[x])$
\li             \Then
                    $\id{prev-x} \gets x$
                        \RComment Cases 1 and 2
\li                 $x \gets \id{next-x}$
                        \RComment Cases 1 and 2
\li             \Else
                    \If $\id{key}[x] \leq \id{key}[\id{next-x}]$
\li                     \Then
                            $\id{sibling}[x] \gets \id{sibling}[\id{next-x}]$
                                \RComment Case 3
\li                         $\proc{Binomial-Link}(\id{next-x},x)$
                                \RComment Case 3
\li                     \Else
                            \If $\id{prev-x} = \const{nil}$
                                \RComment Case 4
\li                             \Then $\id{head}[H] \gets \id{next-x}$
                                    \RComment Case 4
\li                             \Else $\id{sibling}[\id{prev-x}] \gets
                                    \id{next-x}$
                                    \RComment Case 4
                                \End
\li                         $\proc{Binomial-Link}(x,\id{next-x})$
                                    \RComment Case 4
\li                         $x \gets \id{next-x}$
                                    \RComment Case 4
                        \End
                \End
\li         $\id{next-x} \gets \id{sibling}[x]$
        \End
\li \Return $H$
\end{codebox}
\end{verbatim}
}

\noindent This procedure is also the only place in CLRS in which we
used the \verb`\Flushright` command.  It puts the unnumbered line
following line~10 flush against the right margin.

\subheading{Referencing line numbers}

In the source files for CLRS, there are no absolute references to line
numbers.  We use \emph{only} symbolic references.  The \Codebox{}
environment is set up to allow you to place \verb`\label` commands on
lines of pseudocode and then reference these labels.  The references
will resolve to the line numbers.  Our convention is that any label
for a line number begins with \verb`\li:`, but you can name the labels
any way that you like.

For example, here's how we \emph{really} wrote the
\proc{Insertion-Sort} procedure on page~17:

{\small
\begin{verbatim}
\begin{codebox}
\Procname{$\proc{Insertion-Sort}(A)$}
\li \For $j \gets 2$ \To $\id{length}[A]$
                                            \label{li:ins-sort-for}
\li     \Do $\id{key} \gets A[j]$           \label{li:ins-sort-pick}
                                            \label{li:ins-sort-for-body-begin}
\li         \Comment Insert $A[j]$ into the sorted sequence
                $A[1 \twodots j-1]$.
\li         $i \gets j-1$                   \label{li:ins-sort-find-begin}
\li         \While $i > 0$ and $A[i] > \id{key}$
                                            \label{li:ins-sort-while}
\li             \Do
                    $A[i+1] \gets A[i]$     \label{li:ins-sort-while-begin}
\li                 $i \gets i-1$           \label{li:ins-sort-find-end}
                                            \label{li:ins-sort-while-end}
                \End
\li         $A[i+1] \gets \id{key}$         \label{li:ins-sort-ins}
                                            \label{li:ins-sort-for-body-end}
        \End
\end{codebox}
\end{verbatim}
}

\noindent Note that any line may have multiple labels.  As an example
of referencing these labels, here's the beginning of the first item
under ``Pseudocode conventions'' on page~19:

{\small
\begin{verbatim}
\item For example, the body of the \kw{for} loop that begins on
line~\ref{li:ins-sort-for} consists of lines
\ref{li:ins-sort-for-body-begin}--\ref{li:ins-sort-for-body-end},
and the body of the \kw{while} loop that begins on
line~\ref{li:ins-sort-while} contains lines
\ref{li:ins-sort-while-begin}--\ref{li:ins-sort-while-end} but
not line~\ref{li:ins-sort-for-body-end}.
\end{verbatim}
}

\subheading{Setting line numbers}

On rare occasions, we needed to start line numbers somewhere other
than~1.  Use the \verb`setlinenumber` command to set the next line
number.  For example, in Exercise~24.2-2 on page~594, we want the line
number to be the same as a line number within the
\proc{Dag-Shortest-Paths} procedure on page~592.  Here's the source
for the exercise:

{\small
\begin{verbatim}
Suppose we change line~\ref{li:dag-sp-loop-begin} of
\proc{Dag-Shortest-Paths} to read

\begin{codebox}
\setlinenumber{li:dag-sp-loop-begin}
\li \For the first $\card{V}-1$ vertices, taken in topologically sorted order
\end{codebox}
Show that the procedure would remain correct.
\end{verbatim}
}

\noindent The \proc{Dag-Shortest-Paths} procedure is

{\small
\begin{verbatim}
\begin{codebox}
\Procname{$\proc{Dag-Shortest-Paths}(G,w,s)$}
\li topologically sort the vertices of $G$      \label{li:dag-sp-topo-sort}
\li $\proc{Initialize-Single-Source}(G,s)$      \label{li:dag-sp-init}
\li \For each vertex $u$, taken in topologically sorted order
                                                \label{li:dag-sp-loop-begin}
\li     \Do
            \For each vertex $v \in \id{Adj}[u]$
                                                \label{li:dag-sp-inner-begin}
\li             \Do $\proc{Relax}(u,v,w)$       \label{li:dag-sp-loop-end}
                \End
        \End
\end{codebox}
\end{verbatim}
}

Even more rarely (just once, in fact), we needed to set a line number
to be some other line number plus an offset.  That was in the two
lines of pseudocode near the bottom of page~306, where the first line
number had to be one greater than the number of the last line of
\proc{Left-Rotate} on page~278.  Use the \verb`setlinenumberplus`
command:

{\footnotesize
\begin{verbatim}
\begin{codebox}
\setlinenumberplus{li:left-rot-parent}{1}
\li $\id{size}[y] \gets \id{size}[x]$
\li $\id{size}[x] \gets \id{size}[\id{left}[x]] + \id{size}[\id{right}[x]] + 1$
\end{codebox}
\end{verbatim}
}

\noindent Here, the last line of \proc{Left-Rotate} has
\verb`\label{li:left-rot-parent}`.

\section{Reporting bugs}
\label{sec:bugs}

If you find errors in the \clrscode{} package, please send me email
(thc@cs.dartmouth.edu).  It would be best if your message included
everything I would require to elicit the error myself.

The \texttt{clrscode.sty} file contains the following disclaimer:

{\small
\begin{verbatim}
% Written for general distribution by Thomas H. Cormen, June 2003.

% The author grants permission for anyone to use this macro package and
% to distribute it unchanged without further restriction.  If you choose
% to modify this package, you must indicate that you have modified it
% prior to your distributing it.  I don't want to get bug reports about
% changes that *you* have made!
\end{verbatim}
}

\noindent I have enough trouble keeping up with my own bugs; I don't
want to hear about bugs that others have introduced in the package!

\section{Revision history}

\begin{itemize}

\item 11 June 2003.  Initial revision of document and code.

\end{itemize}

\begin{thebibliography}{9}

\bibitem{CLRS01} Thomas H. Cormen, Charles E. Leiserson, Ronald L.
Rivest, and Clifford Stein.  \textit{Introduction to Algorithms},
second edition.  The MIT Press and McGraw-Hill, 2001.

\bibitem{Lamport93} Leslie Lamport.  \textit{\LaTeX: A Document
Preparation System User's Guide and Reference Manual}.
Addison-Wesley, 1993.

\end{thebibliography}

\end{document}