2008/03/06

latex中的算法描述

Latex排版算法使用algorithm2e.sty 相较 algorithm+algorithmic 宏包为优.
它提供了各种控制流命令,可以加行号,显示对齐线,定义输入输出,而且作为浮动体,还能排版算法标题。 详情参见 algorithm2e.pdf

一个完整的algorithm2e例子
\documentclass{article}
\usepackage[linesnumbered,boxed]{algorithm2e}
\begin{document}
One example.
\begin{algorithm}
%% \SetLine
\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)$\;
\caption{identifyRowContext}
\label{algo:1}
\end{algorithm}
\end{document}

-----------------------------------------------------
一个algorithmic 的例子
\usepackage{algorithmic,algorithm}

\begin{algorithm}
\caption{MED approximation. } \label{algo_minball}
\begin{algorithmic}[1]
\STATE choose two points $ x,y $ randomly from $P$, and
construct a disk $B(c,r)$ whose center $c = \frac{x+y}{2}$
and radius $r = \frac{\|x-y \|}{2} $.
\STATE $ P = P - \{x,y\}$;
\WHILE {$P$ is not empty}
\STATE choose a point $w \in P$.
\IF { $B(c,r)$ does not cover $w$ }
\STATE $ r = \frac{r + \| w-c \| }{2} $,
\STATE $ c = c + \frac{w-c}{ \|w-c\| } r $, where $r$ is the updated.
\ENDIF
\STATE $ P = P - \{ w \} $.
\ENDWHILE
\STATE return $B(c,r)$
\end{algorithmic}
\end{algorithm}

没有评论: