LaTeX:算法模板

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

参考一

documentclass{article} 
usepackage{algorithm}
usepackage{algorithmic} 
enewcommand{algorithmicrequire}{ 	extbf{Input:}} %Use Input in the format of Algorithm
enewcommand{algorithmicensure}{ 	extbf{Output:}} %UseOutput in the format of Algorithm
% 参考:https://blog.csdn.net/jzwong/article/details/52399112
egin{document}
% 例1
egin{algorithm}[htb] 
caption{ Framework of ensemble learning for our system.} 
label{alg:Framwork} 
egin{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
egin{algorithm}
	caption{An example}
	label{alg:2}
	egin{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
egin{algorithm}
	caption{Calculate $y = x^n$} 
	label{alg:3}
	egin{algorithmic}
		REQUIRE $n geq 0 vee x 
eq 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 
eq 0$} 
		IF{$N$ is even} 
			STATE $X Leftarrow X 	imes X$ 
			STATE $N Leftarrow N / 2$ 
		ELSE[$N$ is odd] 
			STATE $y Leftarrow y 	imes X$ 
			STATE $N Leftarrow N - 1$ 
		ENDIF 
	ENDWHILE
	end{algorithmic}
end{algorithm}
% 例4
egin{algorithm}[h]
	caption{An example for format For & While Loop in Algorithm}
	label{alg:4}
	egin{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 
eq 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}

结果:

LaTeX:算法模板-冯金伟博客园

LaTeX:算法模板-冯金伟博客园

LaTeX:算法模板-冯金伟博客园

LaTeX:算法模板-冯金伟博客园

参考二

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

结果:

LaTeX:算法模板-冯金伟博客园

LaTeX:算法模板-冯金伟博客园

参考文献

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

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