凯鲁嘎吉
用书写铭记日常,最迷人的不在远方

LaTeX:算法模板

作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/

参考一

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

结果:

补充:常用到的分条目

\begin{itemize}
\item[1. ] xxx
\item[2. ] xxx
\end{itemize}

参考文献

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

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

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

posted on 2020-07-20 20:04  凯鲁嘎吉  阅读(22711)  评论(0编辑  收藏  举报