a算法和a*算法的区别_Latex 算法怎么写?(一):algorithm, algorithmic算法包到底什么区别?

原文: https:// tex.stackexchange.com/q uestions/229355/algorithm-algorithmic-algorithmicx-algorithm2e-algpseudocode-confused

概括版本

  • algorithm - float wrapper for algorithms.
  • algorithmic - first algorithm typesetting environment.
  • algorithmicx - second algorithm typesetting environment.
  • algpseudocode - layout for algorithmicx.
  • algorithm2e - third algorithm typesetting environment.

我结合algpseudocode使用algorithmicx,因为他们比algorithmic高级。另外algorithmicx提供了和algorithm2e相同的功能,但是它的语法更加简洁易懂。

絮絮叨叨版本

1. algorithm

algorithm是 算法的float warpper,类似于table, figure这样的们命令,你可以在你的表格/图形上加一个数字,防止它被分成两页。官方文档说明如下:

When placed within the text without being encapsulated in a floating environment algorithmic environments may be split over a page boundary, greatly detracting from their appearance. In addition, it is useful to have algorithms numbered for reference and for lists of algorithms to be appended to the list of contents. The algorithm environment is meant to address these concerns by providing a floating environment for algorithms.

例子

begin{algorithm}
    caption{Algorithm caption}
    label{alg:algorithm-label}
    begin{algorithmic}
        ... Your pseudocode ...
    end{algorithmic}
end{algorithm}

2. algorithmic

事先已经定义好一些常用的命令语句,有如IF,WHILE等。需要注意的是所有命令语句必须大写。官方文档说明如下:

The algorithmic environment provides an environment for describing algorithms and the algorithm environment provides a “float” wrapper for algorithms (implemented using algorithmic or some other method at the users’s option). The reason for two environments being provided is to allow the user maximum flexibility.

3. algorithmicx

这个可以理解成algorithmic的升级版,他可以让我们自定义一些命令,这个是algorithmic没有的功能。如果你不需要自定义命令,使用algorithmic就好了。另外因为二者的语法很相似,只有部分语法和细节有略微差异,可见下面的例子。官方文档说明如下:

The package algorithmicx itself doesn’t define any algorithmic commands, but gives a set of macros to define such a command set. You may use only algorithmicx, and define the commands yourself, or you may use one of the predefined command sets
begin{algorithm}
    caption{Euclid’s algorithm}
    label{euclid}
    begin{algorithmic}[1] % The number tells where the line numbering should start
        Procedure{Euclid}{$a,b$} Comment{The g.c.d. of a and b}
            State $rgets a bmod b$
            While{$rnot=0$} Comment{We have the answer if r is 0}
                State $a gets b$
                State $b gets r$
                State $r gets a bmod b$
            EndWhilelabel{euclidendwhile}
            State textbf{return} $b$Comment{The gcd is b}
        EndProcedure
    end{algorithmic}
end{algorithm}

4. algpseudocode

这个其实就是algorithmicxlayout(不知道咋翻译。。), 它试图尽可能类似于algorithmic。 还有其他layout,例如:

  • algcompatible(与algorithmic包完全兼容),
  • algpascal(目标是创建一个格式化的Pascal程序,可以用一些基本的替换规则将Pascal程序转换成algpascal算法描述)。
  • algc(就像algpascal一样,但是为c语言设计的。)

官方文档说明如下:

If you are familiar with the algorithmic package, then you’ll find it easy to switch. You can use the old algorithms with the algcompatible layout, but please use the algpseudocode layout for new algorithms. To use algpseudocode, simply use usepackage{algpseudocode}. You don’t need to manually load the algorithmicx package, as this is done by algpseudocode.

总的来说就是如果你要是用这个包,直接usepackage{algpseudocode}就可以了,因为它会自动导入algorithmicx,然后语法直接用algorithmicx的语法就行了。

5. algorithm2e

这是另外一个和algorithmicalgorithmicx类似的算法包,官方文档说明如下:

Algorithm2e is an environment for writing algorithms in LaTeX2e. An algorithm is defined as floating object like figures. It provides macros that allow you to create different sorts of key words, thus a set of predefined key words is given. You can also change the typography of the keywords.

例子

begin{algorithm}[H]
    SetAlgoLined
    KwData{this text}
    KwResult{how to write algorithm with LaTeX2e }
    initialization;
    While{not at end of this document}{
        read current;
        eIf{understand}{
            go to next section;
            current section becomes this one;
            }{
            go back to the beginning of current section;
        }
    }
caption{How to write algorithms}
end{algorithm}

微信公众号:AutoML机器学习

bb782448a91d22cd49ed1c194a805160.png

MARSGGBO♥原创如有意合作或学术讨论欢迎私戳联系~邮箱:marsggbo@foxmail.com 2020-06-01 23:21:53

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值