Bootstrap

LeTeX 写算法流程【algorithm,algorithmic】【algorithm, algorithmicx, algpseudocode】


记录自己用到的两个方法

方法一:algorithm,algorithmic

\usepackage{algorithm,algorithmic}

\begin{algorithm}[!ht]
    \renewcommand{\algorithmicrequire}{\textbf{Input:}}
	\renewcommand{\algorithmicensure}{\textbf{Output:}}
	\caption{Power method}
    \label{power}
    \begin{algorithmic}[1] % 控制是否有序号
        \REQUIRE  $b$ and $Iter$; % input 的内容
	    \ENSURE $\mathbf{Q}$; % output 的内容
        
        \STATE $a = b-1;$
        \STATE $c = a+b;$
        
         % for loop
        \FORALL {$i=1,2,\cdots,Iter$}
            \STATE $\mathbf{Q}_{i} = QR(\mathbf{Z}(\mathbf{Z}^{T}\mathbf{Q}_{i-1}))$;
        \ENDFOR
        
        % if ... else
        \IF {$a < 0$}
            \STATE $y = 2$;
        \ELSE
            \STATE $y = 4$;
        \ENDIF
        
        % while 
        \WHILE {a < 0}
            \STATE a--;
        \ENDWHILE
        
        % do ... while
        \REPEAT
            \STATE a--
        \UNTIL {a < 0}
        
        \STATE \textbf{return} $\mathbf{Q}_{Iter}$.
    \end{algorithmic}
\end{algorithm}

效果如下:
在这里插入图片描述
如不想要前面的序号,比如想自己添加step 1...,将方括号[1]去掉即可

\begin{algorithm}[!ht]
	\caption{Power method}
    \label{power1}
    \begin{algorithmic} % 控制是否有序号
        \STATE step 1: $a = b-1;$
        \STATE step 2: $c = a+b;$
    \end{algorithmic}
\end{algorithm}

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

方法二:algorithm, algorithmicx, algpseudocode

\usepackage{algorithm, algorithmicx, algpseudocode}

\begin{algorithm}[!ht]
    \renewcommand{\algorithmicrequire}{\textbf{Input:}}
    \renewcommand{\algorithmicensure}{\textbf{Output:}}
    \caption{algorithm for problem}
    \label{ADMM}
    \begin{algorithmic}[1]
        \Require $b$ and $Iter$; % input
        \Ensure $c$; % output
 
        \State $a = b-1$;
        \State $c = 1 + b$;
        \Statex /* restart rule */ % 用 \Statex 前面没有序号
    
        % if ... else
        \If {$a < 0$}
            \State $y = 2;$
        \Else
            \State $y = 4;$
        \EndIf
    
        % while
        \While {$a > 0$}
            \State $a--;$
        \EndWhile
    
        % for loop
        \For {$i=1,2,\cdots,Iter$}
            \State $a--;$
        \EndFor
  
        % return
        \State \Return $c$.
 \end{algorithmic}
\end{algorithm}

效果如下:
在这里插入图片描述
如果不希望从Algorithm 1 开始,而是Algorithm 5,6,…,可以在\caption{算法标题}前面加\setcounter{algorithm}{4},则会从5开始:(参考自这篇博文嘻嘻呵呵哈哈嘿嘿嗯嗯的回复)
在这里插入图片描述

参考资料:

  • https://blog.csdn.net/huang_shao1/article/details/82978294
  • https://blog.csdn.net/lwb102063/article/details/53046265?locationNum=7&fps=1

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;