latex中算法的几种模板

LaTeX:算法模板

参考一

\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}
\renewcommand{\algorithmicrequire}{ \textbf{Input:}} %Use Input in the format of Algorithm
\renewcommand{\algorithmicensure}{ \textbf{Output:}} %UseOutput in the format of Algorithm
% 参考:https://blog.csdn.net/jzwong/article/details/52399112
\begin{document}
% 例1
\begin{algorithm}[htb]
\caption{ Framework of ensemble learning for our system.}
\label{alg:Framwork}
\begin{algorithmic}[1] %这个1 表示每一行都显示数字
\REQUIRE ~~\\ %算法的输入参数:Input
    The set of positive samples for current batch, $P_n$;\\
    The set of unlabelled samples for current batch, $U_n$;\\
    Ensemble of classifiers on former batches, $E_{n-1}$;
\ENSURE ~~\\ %算法的输出:Output
    Ensemble of classifiers on the current batch, $E_n$;
    \STATE Extracting the set of reliable negative and/or positive samples $T_n$ from $U_n$ with help of $P_n$;
    \STATE Training ensemble of classifiers $E$ on $T_n \cup P_n$, with help of data in former batches;
    \STATE $E_n=E_{n-1}\cup E$;
    \STATE Classifying samples in $U_n-T_n$ by $E_n$;
    \STATE Deleting some weak classifiers in $E_n$ so as to keep the capacity of $E_n$;
\RETURN $E_n$; %算法的返回值
\end{algorithmic}
\end{algorithm}
 
% 例2
\begin{algorithm}
    \caption{An example}
    \label{alg:2}
    \begin{algorithmic}
        \STATE {set $r(t)=x(t)$}
        \REPEAT
        \STATE set $h(t)=r(t)$
        \REPEAT
        \STATE set $h(t)=r(t)$
        \UNTIL{B}
        \UNTIL{B}
    \end{algorithmic}
\end{algorithm}
% 例3
\begin{algorithm}
    \caption{Calculate $y = x^n$}
    \label{alg:3}
    \begin{algorithmic}
        \REQUIRE $n \geq 0 \vee x \neq 0$
        \ENSURE $y = x^n$
        \STATE $y \Leftarrow 1$
    \IF{$n < 0$}
        \STATE $X \Leftarrow 1 / x$
        \STATE $N \Leftarrow -n$
    \ELSE
        \STATE $X \Leftarrow x$
        \STATE $N \Leftarrow n$
    \ENDIF
    \WHILE{$N \neq 0$}
        \IF{$N$ is even}
            \STATE $X \Leftarrow X \times X$
            \STATE $N \Leftarrow N / 2$
        \ELSE[$N$ is odd]
            \STATE $y \Leftarrow y \times X$
            \STATE $N \Leftarrow N - 1$
        \ENDIF
    \ENDWHILE
    \end{algorithmic}
\end{algorithm}
% 例4
\begin{algorithm}[h]
    \caption{An example for format For \& While Loop in Algorithm}
    \label{alg:4}
    \begin{algorithmic}[1]
        \FOR{each $i \in [1,9]$}
            \STATE initialize a tree $T_{i}$ with only a leaf (the root);\
            \STATE $T=T \cup T_{i};$\
        \ENDFOR
        \FORALL {$c$ such that $c \in RecentMBatch(E_{n-1})$}
            \STATE $T=T \cup PosSample(c)$;
        \ENDFOR
        \FOR{$i=1$; $i<n$; $i++$ }
            \STATE $//$ Your source here;
        \ENDFOR
        \FOR{$i=1$ to $n$}
            \STATE $//$ Your source here;
        \ENDFOR
            \STATE $//$ Reusing recent base classifiers.
        \WHILE {$(|E_n| \leq L_1 )and( D \neq \phi)$}
            \STATE Selecting the most recent classifier $c_i$ from $D$;
            \STATE $D=D-c_i$;
            \STATE $E_n=E_n+c_i$;
        \ENDWHILE
    \end{algorithmic}
\end{algorithm}
\end{document}

结果:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

参考二

\documentclass{article}
 \usepackage[ruled]{algorithm2e}                 %算法排版样式1
%\usepackage[ruled,vlined]{algorithm2e}          %算法排版样式2
%\usepackage[linesnumbered,boxed]{algorithm2e}   %算法排版样式3
% 参考:https://www.cnblogs.com/tsingke/p/5833221.html
\begin{document}
% 例1
\begin{algorithm}[H]
% \SetAlgoNoLine  %去掉之前的竖线
        \caption{How to write algorithms}
        \KwIn{this text}
        \KwOut{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; \\
            }
    }
\end{algorithm}
     
% 例2
\begin{algorithm}
 \SetAlgoNoLine  %去掉之前的竖线
 \caption{identifyRowContext}
  \KwIn{$r_i$, $Backgrd(T_i)$=${T_1,T_2,\ldots ,T_n}$ and similarity threshold $\theta_r$}
  \KwOut{$con(r_i)$}
    $con(r_i)= \Phi$; \\
    \For{$j=1;j \le n;j \ne i$}
    {
        float $maxSim=0$; \\
        $r^{maxSim}=null$; \\
        \While{not end of $T_j$}
        {
            compute Jaro($r_i,r_m$)($r_m\in T_j$); \\
            \If{$(Jaro(r_i,r_m) \ge \theta_r)\wedge (Jaro(r_i,r_m)\ge r^{maxSim})$}
            {
                replace $r^{maxSim}$ with $r_m$; \\
            }
        }
        $con(r_i)=con(r_i)\cup {r^{maxSim}}$; \\
    }
    return $con(r_i)$; \\
\end{algorithm}
 
\end{document}

结果:
在这里插入图片描述
在这里插入图片描述

参考文献

[1] latex算法流程图_开飞机的小毛驴儿-CSDN博客_latex 算法流程图

[2] LaTeX 算法代码排版 --latex2e范例总结 - Tsingke - 博客园 

[3] 更多LaTeX知识,参考:LaTeX常用链接与资料

转载: http://www.cnblogs.com/kailugaji/

  • 5
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LaTeX 是一种功能强大的排版系统,可以用于撰写科技论文、书籍、报告等各种文档。当我们需要在文档插入算法流程图时,可以使用算法流程模板来快速生成。以下是一个简单的 LaTeX 算法流程模板示例: ``` \usepackage{algorithm} \usepackage{algpseudocode} \begin{document} \begin{algorithm} \caption{算法名称} \label{algorithm_label} \begin{algorithmic}[1] % 控制行号从1开始 \Require 输入参数 \Ensure 输出结果 \State 初始化变量 \While{循环条件} \State 执行操作1 \If{条件1} \State 执行操作2 \Else \State 执行操作3 \EndIf \EndWhile \State 返回结果 \end{algorithmic} \end{algorithm} \end{document} ``` 在上述模板,我们首先需要引入 `algorithm` 和 `algpseudocode` 宏包。然后,通过 `algorithm` 环境来创建一个算法块。在 `algorithm` 环境,我们可以使用 `\caption` 命令来设置算法名称,并通过 `\label` 命令来为算法添加引用标签。 接下来,在 `algorithmic` 环境编写算法的具体内容。我们可以使用诸如 `\Require`、`\Ensure`、`\State`、`\While`、`\If` 等命令来描述算法的输入、输出、变量初始化、循环和条件判断等步骤。通过正确嵌套这些命令,我们可以构建出具有一定逻辑结构的算法流程图。 最后,使用 `\end{algorithmic}` 和 `\end{algorithm}` 来闭合算法算法流程图的环境。 以上就是一个简单的 LaTeX 算法流程模板示例,可以根据实际需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值