3
\$\begingroup\$

(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{TracebackPath}$(u, \pi)$}
  \end{algorithm}

\end{document}

The result looks like this:

A*

Any critique is much appreciated.

\$\endgroup\$
  • 1
    \$\begingroup\$ I really like the visualization of the blocks \$\endgroup\$ – MrSmith42 Apr 27 '16 at 10:18
  • \$\begingroup\$ @MrSmith42 Yes, they are fairly convenient. \$\endgroup\$ – coderodde Apr 27 '16 at 10:26
3
\$\begingroup\$

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}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.