1. 程式人生 > 實用技巧 >LaTeX:演算法模板

LaTeX:演算法模板

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}

結果:

參考文獻

[1]latex演算法流程圖_開飛機的小毛驢兒-CSDN部落格_latex 演算法流程圖

[2]LaTeX 演算法程式碼排版 --latex2e範例總結 - Tsingke - 部落格園