• 4. algorithm


    algorithm书写

    1.algorithm

    LaTeX 中,要显示算法,您可以使用 algorithm 宏包来排版算法,并使用 algorithmic 宏包来编写算法的伪代码。以下是显示算法的基本步骤:

    1. 导入宏包:在 LaTeX 文档的导言区(preamble)中,导入 algorithmalgorithmic 宏包。
    \usepackage{algorithm}
    \usepackage{algorithmic}
    
    • 1
    • 2
    1. 创建算法环境:使用 \begin{algorithm}\end{algorithm} 来创建算法环境。在 algorithm 环境中,您可以为算法添加标题和标签。
    \begin{algorithm}
    \caption{算法标题}
    \label{alg:myalgorithm}
    \begin{algorithmic}
    % 算法伪代码
    \end{algorithmic}
    \end{algorithm}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1. 编写算法伪代码:在 algorithmic 环境中,使用一组特定的命令来编写算法的伪代码。以下是一些常用的命令:
    • \STATE:用于表示算法的每一步。
    • \FOR\ENDFOR:用于编写循环结构。
    • \IF\ELSIF\ELSE\ENDIF:用于编写条件语句。
    • \WHILE\ENDWHILE:用于编写循环语句。
    • \RETURN:用于表示算法的返回值。
    • \REQUIRE\ENSURE:用于指定算法的输入和输出。

    以下是一个示例,演示了如何创建一个简单的算法并在文档中引用它:

    \documentclass{article}
    \usepackage{algorithm}
    \usepackage{algorithmic}
    
    \begin{document}
        
        \begin{algorithm}
            \caption{simple algorithm}
            \label{alg:simple}
            \begin{algorithmic}
        
                \STATE inital $sum$ = 0
                \FOR{$i$ form 1 to $n$}
                \STATE set $i$ add $sum$
                \ENDFOR
                \RETURN $sum$
                
            \end{algorithmic}
        \end{algorithm}
        
        %在算法 \ref{alg:simple} 中,我们计算了从 1 到 $n$ 的所有整数的和。
        
    \end{document}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    效果显示:

    在这里插入图片描述

    以上示例中,我们创建了一个名为 “简单算法” 的算法,并使用 \label 命令为它添加了一个标签,以便在文档中引用。通过 \caption 命令,我们添加了算法的标题。在文档正文中,可以使用 \ref{alg:simple} 引用该算法。当编译文档时,算法将以适当的格式呈现在文档中。

    2.algorithm2e

    algorithm2e 是一个用于排版算法伪代码的强大宏包,它提供了丰富的功能和选项,用于创建和自定义算法的外观和样式。以下是如何使用 algorithm2e 宏包的基本步骤:

    1. 导入宏包:在 LaTeX 文档的导言区(preamble)中导入 algorithm2e 宏包。
    \usepackage[linesnumbered,boxed]{algorithm2e}
    
    • 1

    在上面的示例中,我们导入了 algorithm2e 宏包,并指定了两个选项:linesnumbered 用于为算法的每行添加行号,boxed 用于将算法框起来。

    1. 创建算法环境:使用 \begin{algorithm}\end{algorithm} 来创建算法环境。在其中,您可以编写算法的伪代码。
    \begin{algorithm}
    \caption{算法标题}
    \KwData{输入数据}
    \KwResult{输出结果}
    % 编写算法伪代码
    \end{algorithm}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 编写算法伪代码:在算法环境中,使用 algorithm2e 提供的一组命令来编写算法伪代码。以下是一些常用的命令:
    • \KwData\KwResult:用于指定算法的输入数据和输出结果。
    • \While\EndWhile:用于编写循环结构。
    • \If\ElseIf\Else\EndIf:用于编写条件语句。
    • \For\EndFor:用于编写循环语句。
    • \Return:用于表示算法的返回值。

    以下是一个示例,演示了如何使用 algorithm2e 创建一个简单的算法:

    \documentclass{article}
    \usepackage[ruled,linesnumbered,vlined,boxruled]{algorithm2e}
    
    \begin{document}
        
        \begin{algorithm}
            \caption{example}
            \KwData{int $n$}
            \KwResult{int $sum$}
            $sum \leftarrow 0$\;
            \For{$i \leftarrow 1$ \KwTo $n$}{
                $sum \leftarrow sum + i$\;
            }
            \Return{$sum$}\;
        \end{algorithm}
        
    \end{document}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在这里插入图片描述

    在上述示例中,我们使用 algorithm2e 创建了一个名为 “简单算法示例” 的算法环境,然后编写了算法伪代码。算法的输入是整数 $n$,输出是整数 $sum$。通过 \caption\KwData\KwResult,我们为算法添加了标题和描述。

    以上只是 algorithm2e 宏包的基本用法。您可以根据需要使用更多选项和命令来自定义算法的样式和外观。此宏包的文档提供了更多详细信息,可帮助您深入了解其功能和选项。

    自己的实例

    \documentclass{article}
    \usepackage{amsmath}
    \usepackage[ruled,vlined,boxed,linesnumbered]{algorithm2e}
    
    \begin{document}
        
    \begin{algorithm}
        \caption{Quality Adaption}
        \label{algo:Quality Adaptive Allocation Algorithm}
        \SetAlgoLined
        $\boldsymbol{Initialize}: Q_n = [1,1,...,1]$, quality min firstly,$Q_{max}=[6,6,...,6]$ , $U_n =[1,2,...,n]$ \;
        
        %Confirmation $q^*$ \;
        \For{$ each ~ n \in U_n$}
        {
            Compute quality improvement ($\hat{f}^R_c(q)$) from $j$ to $j+1$: $\hat{f}^R_c(q)[n] = f^R_c(q_j + 1) - f^R_c(q_j)$ \;
            Compute $QoE$ increment: $\hat{v}_{n,j} = h_n(q_j + 1) - h_n(q_j)$, where $h_n$ computed by Eq. (\ref{hn}), $d_n$ and $c_n$ are parameters of Eq. (\ref{hn}), among them  $d_n = T_t$, $c_n = T_d$\;
            Compute the density:
            : $\eta_{n,j} =\frac{\hat{v}_{n,j}}{\hat{f}^R_c(q)} $ \;
            Obtain the max density $ D^* = \arg max(\eta_n) $\;
            %Obtain quality $q_{t}$ under $D^*$\;
            Calculate bandwidth $\boldsymbol{B_w[n]}$ in terms of $q_{t}$ \;
            Calculate total bandwidth usage $\boldsymbol{B_t}$ \;
            \eIf{$(f^R_c(q_n)>\boldsymbol{B_w}[n])\parallel(\sum_{1}^{n}f^R_c(q_n)>\boldsymbol{B_t})$}
            {
                $q_{t} = q_{t} - 1 $
            }
            {
                $q_{t} = q_{t} + 1 $
            }
            
            
            \If{$ (D^* < 0)  \parallel (q_{t} == Q_{max}) $}
            {
                $q^*[n] = q_{t}$
            }
        }
        $Q_n[n] = q^*[n]$
        
    \end{algorithm}
    
        
    \end{document}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    效果显示

    在这里插入图片描述

  • 相关阅读:
    [vscode]使用cmake时将命令行参数传递给调试目标
    Linux——Linux用户管理、组管理和文件管理常用命令总结
    齐岳:FITC/Rhodamine/Cy7荧光标记半乳糖Galactose/松醇D-Pinitol/甘露糖Mannos
    Unity游戏客户端进阶路线(只针对本人)
    Python遇上SQL,于是一个好用的Python第三方库出现了
    沉睡者IT - 关于知乎好物赚钱方法都在这里了
    ActiveMQ、RabbitMQ、Kafka、RocketMQ在事务性消息、性能、高可用和容错、定时消息、负载均衡、刷盘策略的区别
    【Java】学生管理系统-登录、注册、CRUD(附完整代码)
    【论文笔记】LLM-Augmenter
    OptiCoupe 6:光学切割面板和型材切割优化[OptiCut]
  • 原文地址:https://blog.csdn.net/u014217137/article/details/133126593