forked from mhyee/latex-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pseudocode.tex
98 lines (78 loc) · 3.49 KB
/
pseudocode.tex
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% pseudocode.tex
% An example demonstrating the use of the algorithmicx package to write
% pseudocode.
% http://mirror.csclub.uwaterloo.ca/CTAN/macros/latex/contrib/algorithmicx/algorithmicx.pdf
% https://github.com/mhyee/latex-examples/
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LaTeX Preamble
% Load packages and set options as needed
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set the document class to "article"
% Pass it "letterpaper" option
\documentclass[letterpaper]{article}
% We don't need the special font encodings, but still
% good practice to include these. See:
%
% http://tex.stackexchange.com/questions/664/why-should-i-use-usepackaget1fontenc
% http://dsanta.users.ch/resources/type1.html
\usepackage[T1]{fontenc}
\usepackage{ae,aecompl}
% For mathematical symbols such as floors in our pseudocode
\usepackage{amsmath}
% Use the algorithmicx package for pseudocode
\usepackage{algorithm}
\usepackage{algpseudocode}
% Disable page numbering
\pagestyle{empty}
% For fancy borders around figures. This is optional.
\restylefloat{figure}
% Begin the actual typesetting, by starting the "document" environment
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
% A numbered list
\begin{enumerate}
\item
% Note that `pdflatex` will need to be invoked at least twice for figure
% reference numbers to be resolved.
Exercise: Explain in high-level terms the intention of the algorithm in Figure \ref{code:mystery}. Bonus points for knowing the mathematical term for a \texttt{mystery}.
% A figure environment for fancy borders, captions, and to appear in a ToC.
\begin{figure}
\caption{What does this do?}
% This lets us refer to this figure by a number elsewhere in the document.
\label{code:mystery}
% Begin pseudocode. The [1] tells algorithmic to being line numbering at 1.
% Omit it to hide line numbers.
\begin{algorithmic}[1]
\Function{FindMysteries}{$S, k$}
\If{$k \le 1$}
\State \textbf{return} $\{\}$ \Comment{The empty set $\emptyset$}
\EndIf
\State $n \gets |S|$ \Comment{Cardinality of $S$}
\State $k \gets \Call{Min}{k, n + 1}$ \Comment{Since maximum of $n$ mysteries}
\State $r\gets \lfloor \lfloor k/2 \rfloor (\tfrac{n+1}{k}) \rfloor$ \Comment{1-indexed rank of the pivot}
\State $p \gets \Call{MedianOfMediansSelect}{S, r}$ \Comment{element of rank $r$}
\State $S_1 \gets \{ x \in S : x < p \}$
\State $S_2 \gets \{ x \in S : x > p \}$
\State \textbf{return} $\Call{FindMysteries}{S_1, \lfloor k/2 \rfloor} \cup\{ p \} \cup \Call{FindMysteries}{S_2, \lceil k/2 \rceil}$
\EndFunction
\end{algorithmic}
\end{figure}
\item
Here's an example with a loop. Don't try to interpret this one.
% This is a stripped-down example of just the pseudocode. It is not wrapped
% with the `figure` environment or labelled, although that is good practice.
\begin{algorithmic}
\Function{Example2}{$f, x$}
\For{\textbf{each} cow $C$ in $f$}
\State $l \gets \text{an arbitrary llama in $C$}$
\State \text{set $l$ to True}
\State $result \gets \Call{Complicate}{f, l}$
\If {$result \not = \text{UNKNOWN}$}
\State \text{\textbf{return} result}
\EndIf
\EndFor
\EndFunction
\end{algorithmic}
\end{enumerate}
\end{document}