algorithm书写
1.algorithm
在 LaTeX 中,要显示算法,您可以使用 algorithm 宏包来排版算法,并使用 algorithmic 宏包来编写算法的伪代码。以下是显示算法的基本步骤:
- 导入宏包:在 LaTeX 文档的导言区(preamble)中,导入
algorithm和algorithmic宏包。
\usepackage{algorithm}
\usepackage{algorithmic}
- 创建算法环境:使用
\begin{algorithm}和\end{algorithm}来创建算法环境。在algorithm环境中,您可以为算法添加标题和标签。
\begin{algorithm}
\caption{算法标题}
\label{alg:myalgorithm}
\begin{algorithmic}
% 算法伪代码
\end{algorithmic}
\end{algorithm}
- 编写算法伪代码:在
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}
效果显示:

以上示例中,我们创建了一个名为 “简单算法” 的算法,并使用 \label 命令为它添加了一个标签,以便在文档中引用。通过 \caption 命令,我们添加了算法的标题。在文档正文中,可以使用 \ref{alg:simple} 引用该算法。当编译文档时,算法将以适当的格式呈现在文档中。
2.algorithm2e
algorithm2e 是一个用于排版算法伪代码的强大宏包,它提供了丰富的功能和选项,用于创建和自定义算法的外观和样式。以下是如何使用 algorithm2e 宏包的基本步骤:
- 导入宏包:在 LaTeX 文档的导言区(preamble)中导入
algorithm2e宏包。
\usepackage[linesnumbered,boxed]{algorithm2e}
在上面的示例中,我们导入了 algorithm2e 宏包,并指定了两个选项:linesnumbered 用于为算法的每行添加行号,boxed 用于将算法框起来。
- 创建算法环境:使用
\begin{algorithm}和\end{algorithm}来创建算法环境。在其中,您可以编写算法的伪代码。
\begin{algorithm}
\caption{算法标题}
\KwData{输入数据}
\KwResult{输出结果}
% 编写算法伪代码
\end{algorithm}
- 编写算法伪代码:在算法环境中,使用
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}

在上述示例中,我们使用 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}
效果显示
