LaTex Algorithm2e 编写论文中的伪代码

写论文要写一个伪代码,伪代码中有一部分是一个递归函数,但是找了很久都没找到LaTex中Function怎么定义,各种依赖报错,折腾了很久终于可以用了,代码和效果如下:

\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{amsmath}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}

\begin{algorithm}
    \caption{Mobility Tree Construction}
    \label{alg:1}
    \begin{algorithmic}[1] %每行显示行号
        \Require One of the user's trajectory $traj$; The number of branches in the tree $nary$;
        \Ensure The Mobility Tree $MT$ corresponding to the input trajectory
        \Function {AddChild}{$MT, traj, idx, map, flags$}
            \State $node\gets traj[idx]$
            \State $map[idx]\gets$ The number of nodes in $MT$
            \State Add $node$ to $MT$
            \If {$idx > 0$ and $flag[idx] != 0$}
                \State $flag[idx]\gets 0$
                \State $i\gets nary$
                \While{$i > 0$}
                    \State $child\gets$ \Call{AddChild}{$MT, traj, idx-i, map, flags$}
                    \State $edge\gets$ $child$ to $node$  // $src \rightarrow dst$
                    \State Add $edge$ to $MT$
                \EndWhile
            \EndIf
            \State \Return{$node$}
        \EndFunction
        \State
        \Function{ConstructTree}{$traj, nary$}
            \State Initialize an empty tree $MT$ with no nodes and edges
            \State Initialize an empty dictionary variable $map$
            \State $len\gets$ The length of $traj$
            \State Initialize the dictionary variable $flags$ of length $len$
            \State \Call{AddChild}{$MT, traj, len-1, map, flags$}
            \State \Return{$MT$}
        \EndFunction
    \end{algorithmic}
\end{algorithm}

在这里插入图片描述

但是导师说不太好看,建议我用Algorithm2e再改一改,再次Google并修改之,于是变成了以下版本:

\usepackage[linesnumbered,ruled,vlined]{algorithm2e}

\begin{algorithm}
    \SetAlgoLined %显示end
    \caption{Mobility Tree Construction}
    \label{alg:1}
    \KwIn{One of the user's trajectory $traj$; The number of branches in the tree $nary$;}
    \KwOut{The Mobility Tree $MT$ corresponding to the input trajectory}
    \SetKwProg{Fn}{Function}{:}{end}
    \Fn{AddChild(MT, traj, idx, map, flags)}{
        $node\gets traj_{idx}$\;
        $map_{idx}\gets$ The number of nodes in $MT$\;
        Add $node$ to $MT$\;
        \If{$idx > 0$ and $flag_{idx} != 0$}{
            $flag_{idx}\gets 0$\;
            $i\gets nary$\;
            \While{$i > 0$}{
                $child\gets$ \textit{AddChild(MT, traj, idx-i, map, flags)}\;
                $edge\gets$ $child$ to $node$  // $src \rightarrow dst$\;
                Add $edge$ to $MT$\;
            }
        }
        \Return{$node$}
    }

    \Fn{ConstructTree(traj, nary)}{
        Initialize an empty tree $MT$ with no nodes and edges\;
        Initialize an empty dictionary variable $map$\;
        $len\gets$ The length of $traj$\;
        Initialize the dictionary variable $flags$ of length $len$\;
        \textit{AddChild(MT, traj, len-1, map, flags)}\;
        \Return{$MT$}
    }
\end{algorithm}

在这里插入图片描述

Algorithm2e官方手册

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值