Latex——伪代码算法

一般在论文的写作中,需要有算法流程图,现讲述两种伪代码算法流程图的实现:algorithm,algorithme2

第一个:algorithm

%导包
\usepackage{algorithm}
\usepackage{algorithmic}

完整代码:

\documentclass{
     article}
\usepackage{
     algorithm}
\usepackage{
     algorithmic}

\begin{
     document}
	
\begin{
     algorithm}
	\renewcommand{
     \algorithmicrequire}{
     \textbf{
     Input:}}
	\renewcommand{
     \algorithmicensure}{
     \textbf{
     Output:}}
	\caption{
     Bayesian Personalized Ranking Based Latent Feature Embedding Model}
	\label{
     alg:1}
	\begin{
     algorithmic}[1]
		\REQUIRE latent dimension $K$, $G$, target predicate $p$
		\ENSURE $U^{
     p}$, $V^{
     p}$, $b^{
     p}$
		\STATE Given target predicate $p$ and entire knowledge graph $G$, construct its bipartite subgraph, $G_{
     p}$ 
		\STATE $m$ = number of subject entities in $G_{
     p}$
		\STATE $n$ = number of object entities in $G_{
     p}$ 
		\STATE Generate a set of training samples $D_{
     p} = \{
     (s_p, o^{
     +}_{
     p}, o^{
     -}_{
     p})\}$ using uniform sampling technique
		\STATE Initialize $U^{
     p}$ as size $m \times K$ matrix with $0$ mean and standard deviation $0.1$
		\STATE Initialize $V^{
     p}$ as size $n \times K$ matrix with $0$ mean and stardard deviation $0.1$
		\STATE Initialize $b^{
     p}$ as size $n \times 1$ column vector with $0$ mean and stardard deviation $0.1$
		\FORALL{
     $(s_p, o^{
     +}_{
     p}, o^{
     -}_{
     p}) \in D_{
     p}$}
		\STATE Update $U_{
     s}^{
     p}$ based on Equation~\ref{
     eq:sgd1}
		\STATE Update $V_{
     o^{
     +}}^{
     p}$ based on Equation~\ref{
     eq:sgd2}
		\STATE Update $V_{
     o^{
     -}}^{
     p}$ based on Equation~\ref{
     eq:sgd3}
		\STATE Update $b_{
     o^{
     +}}^{
     p}$ based on Equation~\ref{
     eq:sgd4}
		\STATE Update $b_{
     o^{
     -}}^{
     p}$ based on Equation~\ref{
     eq:sgd5}
		\ENDFOR
		\STATE \textbf{
     return} $U^{
     p}$, $V^{
     p}$, $b^{
     p}$
	\end{
     algorithmic}  
\end{
     algorithm}

\end{
     document}

结果如下
Latex——伪代码算法_第1张图片

第二个:algorith2me

%导包
\usepackage[ruled]{algorithm2e}

完整代码

\documentclass{
     article}
\usepackage[ruled]{
     algorithm2e}

\begin{
     document}

\begin{
     algorithm}[h]
	
	\caption{
     The process of location change of fish}  
	\label{
     alg:1}
	\KwIn{
     $\rho(x,y,0),\beta,v_{
     max},u_{
     min},u_{
     max}$}
	\KwOut{
     $\rho(x,y,50)$}  
	
	\For{
     t=1 to 50}{
     
		The random distractor $\epsilon_t$ can be get in the process of identification of variance \newline
		The dispersed $u(x, y, t)$ can be predicted based on the model ARIMA(1,1,0) and the $u(x, y, t 1)$ \newline
		The continuous $u(x, y, t)$ can be get based on the linear interposition of value of the dispersed $u(x, y, t)$\newline
		The continuous $\nabla u$ can be identified based on the equation (11) \newline
		The continuous $v(x, y, t)$ can be calculated based on the equation (12) \newline
		The location change of each fish can be calculated based on equation (7) \newline
		The $\rho_t(x, y)$ of each fish can be refreshed based on the equation (8) \newline
	}
\end{
     algorithm}  

\end{
     document}  

结果如下
Latex——伪代码算法_第2张图片
关于 algorithm2e的补充

\usepackage[ruled,linesnumbered]{algorithm2e}
  • ruled 让标题显示在上方,否则将显示在下方
  • linesnumbered 在算法流程图内显示行号
  • 可添加 boxed,让算法排版时插入在一个盒子里

展示效果如下:

Latex——伪代码算法_第3张图片

另外补充:

代码 含义
\; 在行末添加分号,并自动换行
\caption{} 插入标题
\KwIn{输入消息} 效果为:“input:输入消息”
KwOut{输出消息} 效果为:“Out:输出消息”
KwData{输入消息} 效果为:“Data:输入消息”
KwResult{输出消息} 效果为:“Result:输出消息”
\For{条件}{循环语句} for 条件 do 循环语句 end
\If{条件}{肯定语句} if 条件 then 肯定语句 end
While{条件}{循环语句} while 条件 then 循环语句 end
\tcc{注释} /*注释*/
\tcp{注释} //注释
\elf{条件}{肯定语句}{否定语句} if 条件 then 肯定语句 else 否定语句 end

如果有问题,欢迎给我留言

你可能感兴趣的:(Latex,latex)