伪代码的标准?

发布于 2024-08-22 06:04:06 字数 1431 浏览 7 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

北笙凉宸 2024-08-29 06:04:06

我建议您看一下《算法导论》一书(Cormen、Leiserson 和 Rivest 着)。我一直发现它的算法伪代码描述非常清晰和一致。

一个例子:

DIJKSTRA(G, w, s)
1  INITIALIZE-SINGLE-SOURCE(G, s)
2  S ← Ø
3  Q ← V[G]
4  while Q ≠ Ø
5      do u ← EXTRACT-MIN(Q)
6         S ← S ∪{u}
7         for each vertex v ∈ Adj[u]
8             do RELAX(u, v, w)

I recommend looking at the "Introduction to Algorithms" book (by Cormen, Leiserson and Rivest). I've always found its pseudo-code description of algorithms very clear and consistent.

An example:

DIJKSTRA(G, w, s)
1  INITIALIZE-SINGLE-SOURCE(G, s)
2  S ← Ø
3  Q ← V[G]
4  while Q ≠ Ø
5      do u ← EXTRACT-MIN(Q)
6         S ← S ∪{u}
7         for each vertex v ∈ Adj[u]
8             do RELAX(u, v, w)
清醇 2024-08-29 06:04:06

回答我自己的问题,我只是想提请注意 TeX FAQ 条目 在 LaTeX 中排版伪代码。它描述了许多不同的风格,列出了优点和缺点。顺便说一句,正如上面推荐的那样,恰好存在两种用于以 Cormen 的“算法简介”中使用的方式编写伪代码的样式表:newalgclrscode。后者是科门本人撰写的。

Answering my own question, I just wanted to draw attention to the TeX FAQ entry Typesetting pseudocode in LaTeX. It describes a number of different styles, listing advantages and drawbacks. Incidentally, there happen to exist two stylesheets for writing pseudo code in the manner used in "Introductin to Algorithms" by Cormen, as recommended above: newalg and clrscode. The latter was written by Cormen himself.

相权↑美人 2024-08-29 06:04:06

我建议您看一下 Fortress 编程语言

这是一种实际的编程语言,而不是伪代码,但它被设计为尽可能接近可执行伪代码。特别是,为了设计语法,他们阅读并分析了数百篇计算机科学和数学论文、课程、书籍和期刊,以找到伪代码和其他计算/数学符号的常见使用模式。

您只需查看 Fortress 源代码并抽象出您不需要的东西即可利用所有这些研究,因为您的目标受众是人类,而 Fortress 是一个编译器。

以下是从 运行 Fortress 代码的实际示例NAS(NASA 高级超级计算)共轭梯度并行基准。为了获得有趣的体验,请将基准测试的规范与 Fortress 中的实现进行比较,并注意几乎是 1:1 的对应关系。还要比较其他几种语言(例如 C 或 Fortran)的实现,并注意它们与规范完全无关(并且通常比规范长一个数量级)。

我必须强调:这不是伪代码,这是实际工作的 Fortress 代码!来自 https://umbilicus.wordpress.com/2009/ 10/16/fortress-parallel-by-default/

共轭梯度的Fortress代码示例

注意Fortress是用ASCII字符书写的;特殊字符用格式化程序呈现。

I suggest you take a look at the Fortress Programming Language.

This is an actual programming language, and not pseudocode, but it was designed to be as close to executable pseudocode as possible. In particular, for designing the syntax, they read and analyzed hundreds of CS and math papers, courses, books and journals to find common usage patterns for pseudocode and other computational/mathematical notations.

You can leverage all that research by just looking at Fortress source code and abstracting out the things you don't need, since your target audience is human, whereas Fortress's is a compiler.

Here is an actual example of running Fortress code from the NAS (NASA Advanced Supercomputing) Conjugate Gradient Parallel Benchmark. For a fun experience, compare the specification of the benchmark with the implementation in Fortress and notice how there is almost a 1:1 correspondence. Also compare the implementation in a couple of other languages, like C or Fortran, and notice how they have absolutely nothing to do with the specification (and are also often an order of magnitude longer than the spec).

I must stress: this is not pseudocode, this is actual working Fortress code! From https://umbilicus.wordpress.com/2009/10/16/fortress-parallel-by-default/

Fortress code example of conjugate gradient

Note that Fortress is written in ASCII characters; the special characters are rendered with a formatter.

冷情 2024-08-29 06:04:06

如果代码是程序性的,那么普通的伪代码可能很容易(维基百科有一些例子)。

面向对象的伪代码可能更困难。考虑:

  • 使用 UML 类图来描述类/继承,
  • 使用 UML 序列图来描述代码序列

If the code is procedural, normal pseudo-code is probably easy (Wikipedia has some examples).

Object-oriented pseudo-code might be more difficult. Consider:

  • using UML class diagrams to depict the classes/inheritence
  • using UML sequence diagrams to depict the sequence of code
一梦等七年七年为一梦 2024-08-29 06:04:06

我不明白你的要求“不要太接近某种具体的编程语言”。

Python 通常被认为是编写伪代码的良好候选者。也许稍微简化的 python 版本适合您。

I don't understand your requirement of "not too close to some concrete programming language".

Python is generally considered as a good candidate for writing pseudo-code. Perhaps a slightly simplified version of python would work for you.

梅倚清风 2024-08-29 06:04:06

当涉及到数学和技术领域时,Pascal 传统上一直是与伪代码最相似的。我不知道为什么,总是这样。

我有一些(哦,我不知道,书架上可能有 10 本书,这具体说明了这个理论)。

正如所建议的那样,Python 可以是很好的代码,但它也可能很难读,这本身就是一个奇迹。较旧的语言更难变得难以阅读——它们比今天的语言“更简单”(谨慎对待)。它们可能更难理解正在发生的事情,但更容易阅读(理解程序的作用需要更少的语法/语言功能)。

Pascal has always been traditionally the most similar to pseudocode, when it comes to mathematical and technical fields. I don't know why, it was just always so.

I have some (oh, I don't know, 10 maybe books on a shelf, which concrete this theory).

Python as suggested, can be nice code, but it can be so unreadable as well, that it's a wonder by itself. Older languages are harder to make unreadable - them being "simpler" (take with caution) than today's ones. They'll maybe be harder to understand what's going on, but easier to read (less syntax/language features is needed for to understand what the program does).

莫多说 2024-08-29 06:04:06

这篇文章很旧,但希望这对其他人有帮助。

《算法导论》一书(Cormen、Leiserson 和 Rivest 着)是一本关于算法的好书,但是“伪代码”很糟糕。当人们需要理解 Q[1...n] 的含义时,像 Q[1...n] 这样的东西就是无意义的。在“伪代码”之外必须注意这一点。而且,像《算法导论》这样的书喜欢使用数学语法,这违背了伪代码的一个目的。

伪代码应该做两件事。远离语法的抽象并且易于阅读。如果实际代码比伪代码更具描述性,并且实际代码更具描述性,那么它就不是伪代码。

假设您正在编写一个简单的程序。

屏幕设计:

Welcome to the Consumer Discount Program!
Please enter the customers subtotal: 9999.99
The customer receives a 10 percent discount
The customer receives a 20 percent discount
The customer does not receive a discount
The customer's total is: 9999.99

变量列表:

TOTAL:         double
SUB_TOTAL:     double
DISCOUNT:      double

伪代码:

DISCOUNT_PROGRAM

    Print "Welcome to the Consumer Discount Program!"
    Print "Please enter the customers subtotal:"
    Input SUB_TOTAL

    Select the case for SUB_TOTAL
        SUB_TOTAL > 10000 AND SUB_TOTAL <= 50000
            DISCOUNT = 0.1
            Print "The customer receives a 10 percent discount"
        SUB_TOTAL > 50000
            DISCOUNT = 0.2
            Print "The customer receives a 20 percent discount"
        Otherwise
            DISCOUNT = 0
            Print "The customer does not a receive a discount"

    TOTAL = SUB_TOTAL - (SUB_TOTAL * DISCOUNT)
    Print "The customer's total is:", TOTAL

请注意,这非常容易阅读并且不引用任何语法。这支持玻姆和雅科皮尼的所有三个控制结构。

序列:

Print "Some stuff"
VALUE = 2 + 1
SOME_FUNCTION(SOME_VARIABLE)

选择:

if condition
    Do one extra thing

if condition
    do one extra thing
else
    do one extra thing

if condition
    do one extra thing
else if condition
    do one extra thing
else
    do one extra thing

Select the case for SYSTEM_NAME
    condition 1
        statement 1
    condition 2
        statement 2
    condition 3
        statement 3
    otherwise
        statement 4

重复:

while condition
    do stuff

for SOME_VALUE TO ANOTHER_VALUE
    do stuff

将其与 N 皇后“伪代码”进行比较 (https://en .wikipedia.org/wiki/Eight_queens_puzzle):

PlaceQueens(Q[1 .. n],r)

    if r = n + 1
        print Q
    else
        for j ← 1 to n
            legal ← True
            for i ← 1 to r − 1
                if (Q[i] = j) or (Q[i] = j + r − i) or (Q[i] = j − r + i)
                    legal ← False
        if legal
            Q[r] ← j
            PlaceQueens(Q[1 .. n],r + 1) 

如果你不能简单地解释它,那么你还没有理解它。
——阿尔伯特·爱因斯坦

This post is old, but hopefully this will help others.

"Introduction to Algorithms" book (by Cormen, Leiserson and Rivest) is a good book to read about algorithms, but the "pseudo-code" is terrible. Things like Q[1...n] is nonsense when one needs to understand what Q[1...n] is suppose to mean. Which will have to be noted outside of the "pseudo-code." Moreover, books like "Introduction to Algorithms" like to use a mathematical syntax, which is violating one purpose of pseudo-code.

Pseudo-code should do two things. Abstract away from syntax and be easy to read. If actual code is more descriptive than the pseudo-code, and actual code is more descriptive, then it is not pseudo-code.

Say you were writing a simple program.

Screen design:

Welcome to the Consumer Discount Program!
Please enter the customers subtotal: 9999.99
The customer receives a 10 percent discount
The customer receives a 20 percent discount
The customer does not receive a discount
The customer's total is: 9999.99

Variable List:

TOTAL:         double
SUB_TOTAL:     double
DISCOUNT:      double

Pseudo-code:

DISCOUNT_PROGRAM

    Print "Welcome to the Consumer Discount Program!"
    Print "Please enter the customers subtotal:"
    Input SUB_TOTAL

    Select the case for SUB_TOTAL
        SUB_TOTAL > 10000 AND SUB_TOTAL <= 50000
            DISCOUNT = 0.1
            Print "The customer receives a 10 percent discount"
        SUB_TOTAL > 50000
            DISCOUNT = 0.2
            Print "The customer receives a 20 percent discount"
        Otherwise
            DISCOUNT = 0
            Print "The customer does not a receive a discount"

    TOTAL = SUB_TOTAL - (SUB_TOTAL * DISCOUNT)
    Print "The customer's total is:", TOTAL

Notice that this is very easy to read and does not reference any syntax. This supports all three of Bohm and Jacopini's control structures.

Sequence:

Print "Some stuff"
VALUE = 2 + 1
SOME_FUNCTION(SOME_VARIABLE)

Selection:

if condition
    Do one extra thing

if condition
    do one extra thing
else
    do one extra thing

if condition
    do one extra thing
else if condition
    do one extra thing
else
    do one extra thing

Select the case for SYSTEM_NAME
    condition 1
        statement 1
    condition 2
        statement 2
    condition 3
        statement 3
    otherwise
        statement 4

Repetition:

while condition
    do stuff

for SOME_VALUE TO ANOTHER_VALUE
    do stuff

compare that to this N-Queens "pseudo-code" (https://en.wikipedia.org/wiki/Eight_queens_puzzle):

PlaceQueens(Q[1 .. n],r)

    if r = n + 1
        print Q
    else
        for j ← 1 to n
            legal ← True
            for i ← 1 to r − 1
                if (Q[i] = j) or (Q[i] = j + r − i) or (Q[i] = j − r + i)
                    legal ← False
        if legal
            Q[r] ← j
            PlaceQueens(Q[1 .. n],r + 1) 

If you can't explain it simply, you don't understand it well enough.
- Albert Einstein

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文