- 模版1
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}\begin{document}\begin{algorithm}
\caption{AST Pruning Algorithm}
\begin{algorithmic}[1]\State $F_k \gets \text{false}$, $F_f \gets \text{false}$, $T \gets R$ \Comment{Initial states and tree root}\Function{ast\_pruning}{$T$}\If{$F_f = \text{true}$} \Comment{If pruning flag is true, stop recursion}\State \Return\EndIf\If{$\text{node} = K$} \Comment{If node is key node, set flag}\State $F_k \gets \text{true}$\State \Return\EndIf\For{$v \in T.\text{childs}$} \Comment{Traverse children nodes}\State $\text{ast\_pruning}(v)$\EndFor\If{$\text{node} = \text{function\_definition} \And F_k = \text{true}$} \Comment{Check function definition}\State $R_c \gets \text{node}$\State $F_f \gets \text{true}$\State \Return\EndIf
\EndFunction\end{algorithmic}
\end{algorithm}\end{document}
- 模版2
\documentclass{article}
\usepackage{amsmath}\begin{document}\textbf{Algorithm 1:} algorithm \texttt{DFG2CDFG}\textbf{Input:} input \(root_node, , \text{key\_info}, \text{Code}\)\textbf{Output:} output \(V', E'\)\begin{enumerate}\item \(V' = \emptyset\);\item \(E' = \emptyset\);\item \textbf{for} \(v\) \textbf{in} \(V\) \textbf{do}\item \quad \textbf{if} \(v\) \textbf{is in the same line with} \(\text{key\_info}\) \textbf{then}\item \quad \quad add \(v\) into \(V'\);\item \textbf{while} \(V'\) \textbf{becomes larger do}\item \quad \textbf{for} \(v\) \textbf{in} \(V\) \textbf{do}\item \quad \quad \textbf{for} \(v'\) \textbf{in} \(V'\) \textbf{do}\item \quad \quad \quad \textbf{if} \((v, v')\) \textbf{in} \(E\) \textbf{or} \((v', v)\) \textbf{in} \(E\) \textbf{then}\item \quad \quad \quad \quad add \(v\) into \(V'\);\item \textbf{for} \(e = (v_i, v_j)\) \textbf{in} \(E\) \textbf{do}\item \quad \textbf{if} \(v_i\) \textbf{in} \(V'\) \textbf{or} \(v_j\) \textbf{in} \(V'\) \textbf{then}\item \quad \quad add \(e\) in to \(E'\)\item \textbf{for} \(v_s\) \textbf{in} \(V\) \textbf{do}\item \quad \textbf{if none of the edges in} \(E'\) \textbf{pass through the} \(v_s\) \textbf{then}\item \quad \quad remove \(v_s\) from \(V'\)\item \textbf{Return} \(V', E'\)
\end{enumerate}\end{document}
- 模版3
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}\begin{document}\begin{algorithm}
\caption{待写}
\begin{algorithmic}[1] % 1 ensures that lines are numbered
\State \textbf{Input:} 根节点 $root$, 关键节点 $key\_node$, 终止节点 $stop\_node$
\State \textbf{Output:} 终止节点对象或None
\Function{GetSubTree}{$root, key\_node, stop\_node$}\State $\text{min\_subtree} \gets \Call{FindMinTree}{root, key\_node}$\If{$\text{min\_subtree} = \text{None}$}\State \Return None \EndIf\While{$\text{min\_subtree} \neq \text{None} \land \text{min\_subtree} \neq stop\_node$}\State $\text{min\_subtree} \gets \text{min\_subtree}.\text{parent}$ \EndWhile\State \Return $\text{min\_subtree}$
\EndFunction\Function{FindMinTree}{$node, key\_node$}\If{$node = \text{None}$}\State \Return None\ElsIf{$node = key\_node$}\State \Return $node$\EndIf\For{$child \in node.\text{children}$}\State $result \gets \Call{FindMinTree}{child, key\_node}$\If{$result \neq \text{None}$}\State \Return $result$\EndIf\EndFor\State \Return None
\EndFunction
\end{algorithmic}
\end{algorithm}\end{document}
在线Latex网站:https://www.overleaf.com/