http://www.math-linux.com/spip.php?article129 
We must use the following packages

\usepackage{algorithm}
\usepackage{algorithmic}


见我gmail Sep 19, 2012和gmail Oct 22, 2013 的例子:\STATE本身就具有换行功能,不必要加\\。要粗体,{\bfseries Solve}
希望每一行加一个序号1,2等,用\begin{algorithmic} [1]代替\begin{algorithmic};

例1:gmail Oct 22, 2013 的例子的简略版

\renewcommand{\algorithmicrequire}{\textbf{Initialization:}} %\renewcommand见Latex教材P18,修改已有命令;\textbf见Latex教材P99,粗体的意思;
\renewcommand{\algorithmicensure}{\textbf{Iteration:}}
\begin{algorithm}[htbp]
\caption{Label}
\begin{algorithmic}[1]
\REQUIRE ~~\\ %与上面的\renewcommand{\algorithmicrequire}{\textbf{Initialization:}}对应
The initial optimization 
\ENSURE ~~\\
\FOR {each node }
  \STATE Get the value
\ENDFOR
\renewcommand{\algorithmicrequire}{\textbf{Output:}}
\REQUIRE ~~\\
 \STATE Return label
\end{algorithmic}
\end{algorithm}

例2:Here is an exemple:
\begin{algorithm}
\caption{Calculate $y = x^n$}
\begin{algorithmic} 
\REQUIRE $n \geq 0 \vee x \neq 0$
\ENSURE $y = x^n$
\STATE $y \leftarrow 1$
\IF{$n < 0$}
\STATE $X \leftarrow 1 / x$
\STATE $N \leftarrow -n$
\ELSE
\STATE $X \leftarrow x$
\STATE $N \leftarrow n$
\ENDIF
\WHILE{$N \neq 0$}
\IF{$N$ is even}
\STATE $X \leftarrow X \times X$
\STATE $N \leftarrow N / 2$
\ELSE[$N$ is odd]
\STATE $y \leftarrow y \times X$
\STATE $N \leftarrow N - 1$
\ENDIF
\ENDWHILE
\end{algorithmic}
\end{algorithm}


问题:The floating object algorithm doesn't behave well with beamer (which onviously disables floating objects). To prevent problems you can 1) use the H placement specifier for algorithm. This works well for my talk: "talk\2017\I\1\Jie Gui_3\Multi-view".

Reference:
https://tex.stackexchange.com/questions/26539/beamer-and-pseudocode

---------------------------------------------------以下暂不看-------------------------------------------------------
选自:
http://nckunoname.pixnet.net/blog

http://blog.sina.com.cn/s/blog_5e16f1770102ezhv.html

 

網路上查了很多資料關於"用LaTeX寫algorithm"

自己稍微整理一下

algorithm排版可能需要的套件

\documentclass[journal]{IEEEtran}
\usepackage{algorithm}
%\usepackage{algorithmic}
\usepackage{algpseudocode}
\usepackage{amsmath}
\usepackage{graphics}
\usepackage{epsfig}

其中algorithmic在compile時會出現

! LaTex Error: Command \algorithm already defined.

Or name \end... illegal, see p.192 of the manual

原因不是很清楚,所以只好先mark掉

\renewcommand{\algorithmicrequire}{\textbf{Input:}} % Use Input in the format of Algorithm
\renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm

網路上範例一:
\begin{algorithm}[htb]
\caption{ Framework of ensemble learning for our system.}
\label{alg:Framwork}
\begin{algorithmic}[1]
\Require
The set of positive samples for current batch, $P_n$;
The set of unlabelled samples for current batch, $U_n$;
Ensemble of classifiers on former batches, $E_{n-1}$;
\Ensure
Ensemble of classifiers on the current batch, $E_n$;
\State Extracting the set of reliable negative and/or positive samples $T_n$ from $U_n$ with help of $P_n$;
\label{code:fram:extract}
\State Training ensemble of classifiers $E$ on $T_n \cup P_n$, with help of data in former batches;
\label{code:fram:trainbase}
\State $E_n=E_{n-1}cup E$;
\label{code:fram:add}
\State Classifying samples in $U_n-T_n$ by $E_n$;
\label{code:fram:classify}
\State Deleting some weak classifiers in $E_n$ so as to keep the capacity of $E_n$;
\label{code:fram:select} \\
\Return $E_n$;
\end{algorithmic}
\end{algorithm}

排版效果圖:
clip_image001

網路上範例二:

\begin{algorithm}[h]
\caption{An example for format For \& While Loop in Algorithm}
\begin{algorithmic}[1]
\For{each $i\in [1,9]$}
\State initialize a tree $T_{i}$ with only a leaf (the root);
\State $T=T\cup T_{i};$
\EndFor
\ForAll {$c$ such that $c\in RecentMBatch(E_{n-1})$}
\label{code:TrainBase:getc}
\State $T=T\cup PosSample(c)$;
\label{code:TrainBase:pos}
\EndFor;
\For{$i=1$; $i<n$; $i++$ }
\State $//$ Your source here;
\EndFor
\For{$i=1$ to $n$}
\State $//$ Your source here;
\EndFor
\State $//$ Reusing recent base classifiers.
\label{code:recentStart}
\While {$(|E_n| \leq L_1 )and( D \neq \phi)$}
\State Selecting the most recent classifier $c_i$ from $D$;
\State $D=D-c_i$;
\State $E_n=E_n+c_i$;
\EndWhile
\label{code:recentEnd}
\end{algorithmic}
\end{algorithm}

排版效果圖:
clip_image002

個人範例:

\begin{algorithm}[h]
\caption{Conjugate Gradient Algorithm with Dynamic Step-Size Control}
\label{alg::conjugateGradient}
\begin{algorithmic}[1]
\Require
$f(x)$: objective funtion;
$x_0$: initial solution;
$s$: step size;
\Ensure
optimal $x^{*}$
\State initial $g_0=0$ and $d_0=0$;
\Repeat
\State compute gradient directions $g_k=\bigtriangledown f(x_k)$;
\State compute Polak-Ribiere parameter $\beta_k=\frac{g_k^{T}(g_k-g_{k-1})}{\parallel g_{k-1} \parallel^{2}}$;
\State compute the conjugate directions $d_k=-g_k+\beta_k d_{k-1}$;
\State compute the step size $\alpha_k=s/\parallel d_k \parallel_{2}$;
\Until{($f(x_k)>f(x_{k-1})$)}
\end{algorithmic}
\end{algorithm}

排版效果圖:
clip_image003

先前所使用的套件為algorithm或algorithmic

接下來介紹另一個寫algorithm的套件alogrithm2e

首先使用\usepackage指令
\usepackage[linesnumbered,boxed]{algorithm2e}
接下來是網路範例:
\begin{algorithm}
\caption{identifyRowContext}
\KwIn{$r_i$, $Backgrd(T_i)$=${T_1,T_2,\ldots ,T_n}$ and similarity threshold $\theta_r$}
\KwOut{$con(r_i)$}
$con(r_i)= \Phi$\;
\For{$j=1;j \le n;j \ne i$}
{
float $maxSim=0$\;
$r^{maxSim}=null$\;
\While{not end of $T_j$}
{
compute Jaro($r_i,r_m$)($r_m\in T_j$)\;
\If{$(Jaro(r_i,r_m) \ge \theta_r)\wedge (Jaro(r_i,r_m)\ge r^{maxSim})$}
{
replace $r^{maxSim}$ with $r_m$\;
}
}
$con(r_i)=con(r_i)\cup {r^{maxSim}}$\;
}
return $con(r_i)$\;
\end{algorithm}
排版效果圖:
clip_image004

延伸幾個問題:

一、如何修改Algorithm的標題為中文的"演算法”?

在\begin{document}之前加入\renewcommand{\algorithmcfname}{算法} 即可(註:需先安裝中文字形)

二、如何去掉演算法中的豎線?

加入 \SetAlgoNoLine 指令在\begin{algorithm}之後

排版效果圖:

clip_image005

三、還可以使用其他標題樣式?

也可以使用\usepackage[ruled,vlined]{algorithm2e}
排版效果圖:

clip_image006

其他樣式可以參考:http://mirror.bjtu.edu.cn/CTAN/macros/latex/contrib/algorithm2e/algorithm2e.pdf

關於algorithm2e還有以下一些information
The algorithm2e LaTeX package conflicts with several others over the use of the algorithm identifier.

A common indicator is something like this message: Too many }'s.l.1616 }

To resolve the issues, simply put the following just before the inclusion of the algorithm2e package:

\makeatletter
\newif\if@restonecol
\makeatother
\let\algorithm\relax
\let\endalgorithm\relax