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}

结果如下
在这里插入图片描述

第二个: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}  

结果如下
在这里插入图片描述
关于 algorithm2e的补充

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

展示效果如下:

在这里插入图片描述

另外补充:

代码含义
\;在行末添加分号,并自动换行
\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

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

  • 10
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值