HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Typesetting A* in LaTeX using algorithm2e - follow-up

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
latexusingfollowalgorithm2etypesetting

Problem

(See the previous and initial iteration.)

I have this second version of my LaTeX code. I made it less dry by removing the duplicate keyword definitions shared by the two algorithms being typeset. Also, in the previous version, in the argument of main While loop, OPEN was typeset instead of desired OPEN.

See what I have now:

`\documentclass[10pt]{article}

\usepackage{amsmath}
\usepackage[ruled,vlined,linesnumbered]{algorithm2e}

\SetArgSty{textnormal} % Make the While argument non-italic

% Define special keywords.
\SetKw{Nil}{nil}
\SetKw{Is}{is}
\SetKw{Not}{not}
\SetKw{Mapped}{mapped}
\SetKw{In}{in}
\SetKw{ChildNode}{child node}
\SetKw{Of}{of}
\SetKw{Continue}{continue}

\begin{document}

% A*.
\begin{algorithm}
$\text{OPEN} = \{ s \}$ \\
$\text{CLOSED} = \emptyset$ \\
$\pi = \{ (s \mapsto$ \Nil $)\}$ \\
$g = \{ (s \mapsto 0) \}$ \\
\While{$|\text{OPEN}| > 0$}{
$u = \textsc{ExtractMinimum}(\text{OPEN})$ \\
\If{$u$ \Is $t$}{
\KwRet \textsc{TracebackPath}$(u, \pi)$ \\
}
$\text{CLOSED} = \text{CLOSED} \cup \{ u \}$ \\
\ForEach{\ChildNode $v$ \Of $u$}{
\If{$v \in \textsc{CLOSED}$}{
\Continue \\
}
$c = g(u) + w(u, v)$ \\
\If{$v$ \Is \Not \Mapped \In $g$}{
$g(v) = c$ \\
$\pi(v) = u$ \\
\textsc{Insert}$(\text{OPEN}, v, c + h(v))$ \\
}
\ElseIf{$g(v) > c$}{
$g(v) = c$ \\
$\pi(v) = u$ \\
\textsc{DecreaseKey}$(\text{OPEN}, v, c + h(v))$ \\
}
}
}
\KwRet $\langle \rangle$
\caption{\textsc{AStarPathFinder}$(s, t, w, h)$}
\end{algorithm}

% Traceback path.
\begin{algorithm}
$p = \langle \rangle$ \\
\While{$u$ \Is \Not \Nil}{
$p = u \circ p$ \\
$u = \pi(u)$ \\
}
\KwRet $p$
\caption{\textsc{Tra

Solution

Nice. I mean if you're going for perfection I'm going to write
something but otherwise that looks really good.

  • The


documentation for algorithm2e
mentions that every line should be terminated with \; - and then has
a flag to disable showing that semicolon, \DontPrintSemicolon - take
that how you will. At least Emacs highlights that less garishly than
the forced newline.

  • The variables could be marked via \SetKwData. If you want to keep


the same style, modify it via \SetDataSty{textnormal}. Similarly
for the functions via \SetKwFunction.

  • The style for the captions could also just be set to smallcaps


globally via \let\AlCapNameSty\textsc (works and should be correct,
but I'm not a \$\LaTeX\$ expert by any means.

  • Also there's the procedure environment (instead of algorithm) that


has some more shortcuts. Note that I added procnumbered and
\SetAlgoProcName to set the names and numbering back to what is the
default algorithm settings.

Looks like this:

\documentclass[10pt]{article}

\usepackage{amsmath}
\usepackage[ruled,vlined,linesnumbered,procnumbered]{algorithm2e}

\SetArgSty{textnormal} % Make the While argument non-italic
\SetDataSty{textnormal}
\SetFuncSty{textsc}
\let\AlCapNameSty\textsc
\DontPrintSemicolon
\SetAlgoProcName{Algorithm}

% Define special keywords.
\SetKw{Nil}{nil}
\SetKw{Is}{is}
\SetKw{Not}{not}
\SetKw{Mapped}{mapped}
\SetKw{In}{in}
\SetKw{ChildNode}{child node}
\SetKw{Of}{of}
\SetKw{Continue}{continue}

\SetKwData{Open}{OPEN}
\SetKwData{Closed}{CLOSED}

\SetKwFunction{ExtractMinimum}{ExtractMinimum}
\SetKwFunction{TracebackPath}{TracebackPath}
\SetKwFunction{Insert}{Insert}
\SetKwFunction{DecreaseKey}{DecreaseKey}

\begin{document}

% A*.
\begin{procedure}
$\Open = \{ s \}$ \;
$\Closed = \emptyset$ \;
$\pi = \{ (s \mapsto$ \Nil $)\}$ \;
$g = \{ (s \mapsto 0) \}$ \;
\While{$|\Open| > 0$}{
$u = \ExtractMinimum(\Open)$ \;
\If{$u$ \Is $t$}{
\KwRet \TracebackPath{$u$, $\pi$} \;
}
$\Closed = \Closed \cup \{ u \}$ \;
\ForEach{\ChildNode $v$ \Of $u$}{
\If{$v \in \Closed$}{
\Continue \;
}
$c = g(u) + w(u, v)$ \;
\If{$v$ \Is \Not \Mapped \In $g$}{
$g(v) = c$ \;
$\pi(v) = u$ \;
\Insert{\Open, $v$, $c + h(v)$} \;
}
\ElseIf{$g(v) > c$}{
$g(v) = c$ \;
$\pi(v) = u$ \;
\DecreaseKey{\Open, $v$, $c + h(v)$} \;
}
}
}
\KwRet $\langle \rangle$
\caption{AStarPathFinder($s$, $t$, $w$, $h$)}
\end{procedure}

% Traceback path.
\begin{procedure}
$p = \langle \rangle$ \;
\While{$u$ \Is \Not \Nil}{
$p = u \circ p$ \;
$u = \pi(u)$ \;
}
\KwRet $p$
\caption{TracebackPath($u$, $\pi$)}
\end{procedure}

\end{document}

Context

StackExchange Code Review Q#126827, answer score: 3

Revisions (0)

No revisions yet.