如何像 CLRS(算法简介)中那样排版带有列的算法?

如何像 CLRS(算法简介)中那样排版带有列的算法?

算法导论,作者是 Thomas Cormen,有一些像图中这样的算法。一列是代码,一列是成本,一列是时间。

我搜索了很多次,但没有找到如何去做。

在此处输入图片描述

答案1

如果我不使用的话,这就是我会做的clrscode

\documentclass[11pt]{article}

\usepackage[noend]{algorithmic}

\newcommand{\TITLE}[1]{\item[#1]}
\renewcommand{\algorithmiccomment}[1]{$/\!/$ \parbox[t]{4.5cm}{\raggedright #1}}
% ugly hack for for/while
\newbox\fixbox
\renewcommand{\algorithmicdo}{\setbox\fixbox\hbox{\ {} }\hskip-\wd\fixbox}
% end of hack
\newcommand{\algcost}[2]{\strut\hfill\makebox[1.5cm][l]{#1}\makebox[4cm][l]{#2}}

\begin{document}
\begin{algorithmic}[1]
  \TITLE{\textsc{Insertion-Sort}$(A)$}
    \algcost{\textit{cost}}{\textit{times}}
  \FOR{$j=2$ \TO $A.\mathit{length}$
    \algcost{$c_1$}{$n$}}
  \STATE $\mathit{key} = A[j]$
    \algcost{$c_2$}{$n-1$}
  \STATE \COMMENT{Insert $A[j]$ to the sorted sequence $A[1..j-1]$}
    \algcost{$0$}{$n-1$}
  \STATE $i = j-1$
    \algcost{$c_4$}{$n-1$}
  \WHILE{$i>0$ \AND $A[i]>key$
    \algcost{$c_5$}{$\sum_{j=2}^{n} t_j$}}
  \STATE $A[i+1]= A[i]$
    \algcost{$c_6$}{$\sum_{j=2}^{n} (t_j-1)$}
  \STATE $i = i-1$
    \algcost{$c_7$}{$\sum_{j=2}^{n} (t_j-1)$}
  \ENDWHILE
  \STATE $A[i+1] = \mathit{key}$
    \algcost{$c_8$}{$n-1$}
  \ENDFOR
\end{algorithmic}
\end{document}

请注意以下几点:

  1. 有三个宽度(以厘米为单位)可能需要调整。

  2. for和语句的成本while在括号内,并且它们和结束括号之间不能有任何空格。这有点脆弱。if和所有其他块语句一样。

  3. 第 3 行的费用显示在两条线中的第一条线旁边,而不是第二条线(与原版一样)。这样比较简单,但我想我也更喜欢它。

这个答案与 Jubobs 的答案相反,后者使用包clrscode。如果您更喜欢使用clrscode,我相信您会找到正确使用的方法,|>用于制表额外的列,尽管它没有很好的文档记录。此外,如果您希望您的算法看起来与书中的算法完全一样,那么出于其他原因,它可能也是更好的选择。


结果

答案2

(仅部分答案)

科门利用他的clrscode包裹CLRS 第二版,但有一个“加强版”,称为clrscode3e,第三版,其中插入排序屏幕截图中的算法已采用。有关更多详细信息,请参阅

用于排版算法的代码(但没有请参阅第 6 页的“成本”和“时间”栏。clrscode3e文档。后者没有提到如何排版列。此外,该包的源代码似乎没有提供任何(未记录的)机制。

然而,Cormen 的codebox环境是基于tabbing环境的;也许那里有可以做的事情,但我不太熟悉tabbing......

在此处输入图片描述

\documentclass{article}

\usepackage{clrscode3e}

\begin{document}

\begin{codebox}
\Procname{$\proc{Insertion-Sort}(A)$}
\li \For $j \gets 2$ \To $\attrib{A}{length}$
\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] = A[i]$
\li                 $i \gets i-1$
                \End
\li         $A[i+1] \gets \id{key}$
        \End
\end{codebox}

\end{document}

答案3

有一个 CLRS 包,代号为“clrscode3e”,该包的描述以及文件.sty可以在网站上找到:http://www.cs.dartmouth.edu/~thc/clrscode/,您可以轻松下载.sty文件并阅读说明。

\documentclass{article}

\usepackage{clrscode3e}

\begin{document}

\begin{codebox}
\Procname{$\proc{Insertion-Sort}(A)$}
\li \For $j \gets 2$ \To $\attrib{A}{length}$
\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] = A[i]$
\li                 $i \gets i-1$
                \End
\li         $A[i+1] \gets \id{key}$
        \End
\end{codebox}
\end{document}

相关内容