Several templates for algorithms in latex

LaTeX: algorithm templates

Reference one

\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}

result:

insert image description here
insert image description here
insert image description here
insert image description here

Reference two

\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}

result:
insert image description here
insert image description here

references

[1]  latex algorithm flow chart_The little donkey who flies the plane-CSDN blog_latex algorithm flow chart

[2]  LaTeX algorithm code typesetting--latex2e example summary- Tsingke - 博客园 

[3] For more LaTeX knowledge, refer to: LaTeX common links and materials

Reprint:  http://www.cnblogs.com/kailugaji/

Guess you like

Origin blog.csdn.net/weixin_50514171/article/details/125136121