LaTeX 伪代码

一、algorithm包

导入包

\usepackage{multirow}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{amsmath}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\usepackage{algorithmicx}
\usepackage{algpseudocode}

\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}

代码:

\begin{algorithm}[htp!]    %htp! 禁止浮动
  \caption{CPU load detection.}
  \label{al:cld}
  \begin{algorithmic}[1]
    \Require
      $L{r_{n,[{t_s},{t_e}]}}$, $v_{i,k}$, ${L_{{v_{i,k}}}}$, $r(n_i)$, $B_n$;
    \Ensure
      $minv_{i,k} \rightarrow  tn$;
    \State Declare migration target nodes $tn$
    \State $minv_{i,k}$ denotes the minimum communication load with other vertex instances on node $n_i$
    \For{ $l_i$ being the CPU load ratio for nodes in set $Gn$ }
        \State Determine whether the CPU of each node is overloaded or not
        \If{${l_i} > {B_n}$}
            \State Fetch the $minv_{i,k}$ downstream nodes $D(v_{i,k})$ and upstream nodes $U(v_{i,k})$  and assign them to $M(n)$
            \For{$n_i \in M(n)$}
                \If{$DL{r_{{n_i}}} + {L_{\min {v_{i,j}}}} > {B_{{n_i}}}$}
                    \State $Remove(M(n_i)) $
                \EndIf
            \EndFor
            \If{$M(n_i)$ is empty}
                \State $tn=max(r(n_i),n_i \in Dn(n_i))$ 
            \EndIf
            \State $tn=max(r(n_i),n_i \in M(n_i))$
        \EndIf
    \EndFor \\
    \Return $minv_{i,j} \rightarrow tn$
    
  \end{algorithmic}
\end{algorithm}

二、algorithm2e包

1、导入包

\usepackage[ruled,linesnumbered]{algorithm2e}
  • ruled 是让标题显示在上面,否则算法的标题则在下面。
  • linesnumbered 让算法中显示行号。
  • 还可以添加 boxed, 让算法排版时插入在一个盒子里。

2、基本语法

代码含义
\;在行末添加分号,并自动换行
\caption{}插入标题
\KwData{输入信息}效果为: Data输入信息
\KwIn{输入信息}效果为:In:输入信息
\KwOut{输出信息}效果为:Out:输出信息
\KwResult{输出信息效果为:Result:输出信息
\For{循环条件}{循环语句}for 条件 do 循环语句 endo
\If{条件}{肯定语句}if 条件 then 肯定语句 end
\While{条件}{语句}while 条件 then 语句 end
\tcc{注释}/注释/
\tcp{注释}//注释
\eIf{条件}{肯定语句}{否定语句}if 条件 then 肯定语句 else 否定语句 end
  1. 如果你不想让你的伪代码叫做 ‘Algorithm 编号’, 可以使用 \renewcommand{\algorithmcfname}{算法名} 命令来修改。
  2. 除了\If, \Else, \ElseIf之外,还有\uIf, \lIf, \uElse, \lElse, \uElseIf, \lElseIf等命令,他们的区别在于
  • \If, \Else, \ElseIf都是会以end结尾
  • \uIf, \uElse, \uElseIf, 是不以end结尾的块级元素
  • \lIf, \lElse, \lElseIf 是不以end为结尾的行内元素
  • 在If-else结构中,\eIf 自带else(即 if 和 else 共用一个 end),而只是用 \If 和 \Else 的话则会多出一个end给Else。

example

\begin{algorithm}[h]
     \SetAlgoLined
	 \caption{Data Buffer Pool When Runtime} 
	 \KwData{Signal from Snapshot $S$, Tuple Upstream $T_u$, Control Tuple $T_c$}

     \uIf{$S$ hasn't received and $T_c$ hasn't received}{
     \tcp{meaning $T_u$ has received}
        Store $T_u$\;
        output $T_u$\;
        }
     \uElseIf{$S$ hasn't received and $T_c$ has received}{
        Mark the set of $T_u$ before $T_c$\;
        Output $T_c$\;
        }
    \Else{
    \tcp{meaning $S$ and $T_u$ has received}
        Deleted the marked tuple\;
        Output $T_u$\;
        }
\end{algorithm}

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值